Merge lp:~charlesk/indicator-location/sync-with-platform-api-changes-to-accuracy-and-service into lp:indicator-location/14.04

Proposed by Ted Gould
Status: Superseded
Proposed branch: lp:~charlesk/indicator-location/sync-with-platform-api-changes-to-accuracy-and-service
Merge into: lp:indicator-location/14.04
Diff against target: 770 lines (+503/-112)
8 files modified
CMakeLists.txt (+2/-1)
src/CMakeLists.txt (+16/-25)
src/controller-ualc.cc (+37/-52)
src/controller-ualc.h (+5/-4)
tests/CMakeLists.txt (+33/-14)
tests/gtest-dbus-fixture.h (+23/-16)
tests/ualc-mock.c (+152/-0)
tests/ualc-test.cc (+235/-0)
To merge this branch: bzr merge lp:~charlesk/indicator-location/sync-with-platform-api-changes-to-accuracy-and-service
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Pending
Ted Gould Pending
Review via email: mp+221043@code.launchpad.net

This proposal supersedes a proposal from 2014-01-26.

This proposal has been superseded by a proposal from 2014-05-27.

Description of the change

Syncs indicator-location to the changed platform location service's API that's coming in lp:~thomas-voss/platform-api/expose_accuracy_and_service_status_to_platform

It's possible that this will pass CI before tvoss' branch lands because we're not relying on any new API there, only synchronizing with ones that have been removed. However it would probably be better if this went through at the same time as the other MPs in bug #1248973

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : Posted in a previous version of this proposal
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : Posted in a previous version of this proposal
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote : Posted in a previous version of this proposal

Doing a code review as I can't do a full review as I don't have HW to test anything on.

113 + // query the ualc to bootstrap our initial status
114 + UALocationServiceStatusFlags initial_status = 0;
115 + if (ua_location_service_controller_query_status (ualc, &initial_status) == U_STATUS_SUCCESS)
116 + set_status (initial_status);

230 + UALocationServiceStatusFlags current_status = 0;

Seems like the default initial status should be service disabled and GPS disabled, not zero, which I think has no meaning. Not sure which block would be the best to put that, perhaps both.

review: Needs Fixing
Revision history for this message
Charles Kerr (charlesk) wrote : Posted in a previous version of this proposal

I'm not sure that code block needs fixing?

The "initial_status = 0;" call just zeroes out the field before ua_location_service_controller_query_status() populates it with a real value. It might make sense to remove that line as it's a dead store; still, it's harmless in the context you're talking about.

UALocationServiceStatusFlags is an enum with the fields "UA_LOCATION_SERVICE_GPS_ENABLED" and "UA_LOCATION_SERVICE_ENABLED", so 0 means neither are enabled. So we're in agreement about what the Controller's initial status should be (and is) before ua_location_service_controller_query_status() is called.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote : Posted in a previous version of this proposal
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote : Posted in a previous version of this proposal

On Wed, 2014-01-29 at 00:20 +0000, Charles Kerr wrote:

> UALocationServiceStatusFlags is an enum with the fields
> "UA_LOCATION_SERVICE_GPS_ENABLED" and "UA_LOCATION_SERVICE_ENABLED",
> so 0 means neither are enabled. So we're in agreement about what the
> Controller's initial status should be (and is) before
> ua_location_service_controller_query_status() is called.

I think that it would make total sense if that is what it was, but it's
not :-( It seems to be a field with both enable and disabled flags that
are independent of each other.

http://bazaar.launchpad.net/~phablet-team/platform-api/trunk/view/head:/include/ubuntu/application/location/controller.h#L30

Revision history for this message
Charles Kerr (charlesk) wrote : Posted in a previous version of this proposal

> I think that it would make total sense if that is what it was, but it's
> not :-( It seems to be a field with both enable and disabled flags that
> are independent of each other.
>
> http://bazaar.launchpad.net/~phablet-team/platform-api/trunk/view/head:/include/ubuntu/application/location/controller.h#L30

Ah, OK I understand your confusion now. That's the API change that this branch is addressing: http://bazaar.launchpad.net/~thomas-voss/platform-api/expose_accuracy_and_service_status_to_platform/view/head:/include/ubuntu/application/location/controller.h#L34

Revision history for this message
Ted Gould (ted) wrote : Posted in a previous version of this proposal

Ah, okay. I see.

review: Approve
80. By Charles Kerr

fix copy-paste bug introduced by copying cmake files from indicator-datetime

Unmerged revisions

80. By Charles Kerr

fix copy-paste bug introduced by copying cmake files from indicator-datetime

79. By Charles Kerr

remove a dead store assignment in controller-ualc's constructor

78. By Charles Kerr

add unit tests to confirm that Controller notifies with the correct status when the ualc's status changes.

77. By Charles Kerr

add unit tests to confirm that our mock ualc works, and that the production Controller keeps in sync with it

76. By Charles Kerr

add a mock implementation of the platform location API for our unit tests

75. By Charles Kerr

prepare for adding unit tests for ualc: fix unnecessary g_warning() console message in our test harness

74. By Charles Kerr

copyediting: more understandable variable name

73. By Charles Kerr

consistency in UbuntuAppLocController::set_state()

72. By Charles Kerr

minor copyediting: use consistent terminology 'status' instead of 'state'

71. By Charles Kerr

failing to get a ualc in the UbuntuAppLocController constructor should be impossible, so instead of just safeguarding by doing a nullptr test before using, use g_return_if_fail() so a console message will get logged too.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2013-10-01 19:48:43 +0000
+++ CMakeLists.txt 2014-05-27 09:31:36 +0000
@@ -28,11 +28,12 @@
28find_package (PkgConfig REQUIRED)28find_package (PkgConfig REQUIRED)
2929
30include (FindPkgConfig)30include (FindPkgConfig)
31pkg_check_modules (SERVICE REQUIRED31pkg_check_modules (SERVICE_DEPS REQUIRED
32 ubuntu-platform-api>=1.0.032 ubuntu-platform-api>=1.0.0
33 url-dispatcher-133 url-dispatcher-1
34 gio-unix-2.0>=2.3634 gio-unix-2.0>=2.36
35 glib-2.0>=2.36)35 glib-2.0>=2.36)
36include_directories (SYSTEM ${SERVICE_DEPS_INCLUDE_DIRS})
3637
37##38##
38##39##
3940
=== modified file 'src/CMakeLists.txt'
--- src/CMakeLists.txt 2013-08-29 17:48:37 +0000
+++ src/CMakeLists.txt 2014-05-27 09:31:36 +0000
@@ -1,9 +1,12 @@
11set (SERVICE_LIB "indicatordatetimeservice")
2set_property (DIRECTORY2set (SERVICE_EXEC "indicator-datetime-service")
3 APPEND PROPERTY INCLUDE_DIRECTORIES ${SERVICE_INCLUDE_DIRS})3SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g ${CXX_WARNING_ARGS} ${GCOV_FLAGS}")
44
5set_property (DIRECTORY5#set_property (DIRECTORY
6 APPEND PROPERTY LINK_DIRECTORIES ${SERVICE_INCLUDE_DIRS})6# APPEND PROPERTY INCLUDE_DIRECTORIES ${SERVICE_INCLUDE_DIRS})
7#
8#set_property (DIRECTORY
9# APPEND PROPERTY LINK_DIRECTORIES ${SERVICE_INCLUDE_DIRS})
710
811
9###12###
@@ -12,34 +15,22 @@
12### it's built apart from main() as a convenience for tests/15### it's built apart from main() as a convenience for tests/
13###16###
1417
15set (INDICATOR_LIB "libindicator-location-service")18add_library (${SERVICE_LIB} STATIC controller.cc phone.cc service.cc controller-ualc.cc)
1619include_directories (${CMAKE_SOURCE_DIR})
17add_library (${INDICATOR_LIB} STATIC controller.cc phone.cc service.cc)20link_directories (${SERVICE_DEPS_LIBRARY_DIRS})
18
19set_property (TARGET ${INDICATOR_LIB}
20 APPEND PROPERTY LINK_LIBRARIES ${INDICATOR_LIB} ${SERVICE_LIBRARIES} ${GCOV_LIBS})
2121
2222
23###23###
24### The service execuable.24### The service execuable.
25###25###
2626
27set (INDICATOR_EXEC "indicator-location-service")27add_executable (${SERVICE_EXEC} main.cc)
2828target_link_libraries (${SERVICE_EXEC} ${SERVICE_LIB} ${SERVICE_DEPS_LIBRARIES})
29add_executable (${INDICATOR_EXEC} main.cc controller-ualc.cc)29install (TARGETS ${SERVICE_EXEC}
30 RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_PKGLIBEXECDIR})
3031
31set_property (SOURCE main.cc32set_property (SOURCE main.cc
32 APPEND PROPERTY COMPILE_DEFINITIONS33 APPEND PROPERTY COMPILE_DEFINITIONS
33 GETTEXT_PACKAGE="${GETTEXT_PACKAGE}"34 GETTEXT_PACKAGE="${GETTEXT_PACKAGE}"
34 GNOMELOCALEDIR="@CMAKE_INSTALL_FULL_LOCALEDIR@")35 GNOMELOCALEDIR="@CMAKE_INSTALL_FULL_LOCALEDIR@")
3536
36target_link_libraries (${INDICATOR_EXEC} libindicator-location-service)
37
38install (TARGETS ${INDICATOR_EXEC}
39 RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_PKGLIBEXECDIR})
40
41
42
43set_property (TARGET ${INDICATOR_LIB} ${INDICATOR_EXEC}
44 APPEND_STRING PROPERTY COMPILE_FLAGS " ${CC_WARNING_ARGS} ${GCOV_FLAGS} --std=c++11")
45
4637
=== modified file 'src/controller-ualc.cc'
--- src/controller-ualc.cc 2013-08-29 17:49:40 +0000
+++ src/controller-ualc.cc 2014-05-27 09:31:36 +0000
@@ -18,20 +18,24 @@
18 * Charles Kerr <charles.kerr@canonical.com>18 * Charles Kerr <charles.kerr@canonical.com>
19 */19 */
2020
21#include <iostream>21#include <glib.h>
2222
23#include "controller-ualc.h"23#include "controller-ualc.h"
2424
25UbuntuAppLocController :: UbuntuAppLocController ():25UbuntuAppLocController :: UbuntuAppLocController ():
26 ualc (ua_location_service_create_controller ())26 ualc (ua_location_service_create_controller ())
27{27{
28 if (ualc != nullptr)28 g_return_if_fail (ualc != nullptr);
29 {29
30 ua_location_service_controller_set_status_changed_handler (30 // update our status when the ualc changes
31 ualc,31 ua_location_service_controller_set_status_changed_handler (ualc,
32 on_location_service_controller_status_changed,32 on_ualc_status_changed,
33 this);33 this);
34 }34
35 // query the ualc to bootstrap our initial status
36 UALocationServiceStatusFlags initial_status;
37 if (ua_location_service_controller_query_status (ualc, &initial_status) == U_STATUS_SUCCESS)
38 set_status (initial_status);
35}39}
3640
37UbuntuAppLocController :: ~UbuntuAppLocController ()41UbuntuAppLocController :: ~UbuntuAppLocController ()
@@ -41,66 +45,47 @@
41}45}
4246
43void47void
44UbuntuAppLocController :: on_location_service_controller_status_changed (48UbuntuAppLocController :: on_ualc_status_changed (UALocationServiceStatusFlags flags, void *vself)
45 UALocationServiceStatusFlags f,49{
46 void * vself)50 static_cast<UbuntuAppLocController*>(vself)->set_status (flags);
47{51}
48 auto self = static_cast<UbuntuAppLocController*>(vself);52
4953void
50 if (f & (UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_DISABLED))54UbuntuAppLocController :: set_status (UALocationServiceStatusFlags new_status)
51 self->notify_gps_enabled (self->is_location_service_enabled ());55{
5256 const bool loc_was_enabled = is_location_service_enabled();
53 if (f & (UA_LOCATION_SERVICE_GPS_ENABLED | UA_LOCATION_SERVICE_GPS_DISABLED))57 const bool gps_was_enabled = is_gps_enabled();
54 self->notify_gps_enabled (self->is_gps_enabled());58 current_status = new_status;
59 const bool loc_is_enabled = is_location_service_enabled();
60 const bool gps_is_enabled = is_gps_enabled();
61
62 if (loc_was_enabled != loc_is_enabled)
63 notify_location_service_enabled (loc_is_enabled);
64
65 if (gps_was_enabled != gps_is_enabled)
66 notify_gps_enabled (gps_is_enabled);
55}67}
5668
57/***69/***
58****70****
59***/71***/
6072
61bool
62UbuntuAppLocController :: is_gps_enabled () const
63{
64 UALocationServiceStatusFlags flags = 0;
65
66 return (ualc != nullptr)
67 && (!ua_location_service_controller_query_status (ualc, &flags))
68 && (flags & UA_LOCATION_SERVICE_GPS_ENABLED);
69}
70
71void73void
72UbuntuAppLocController :: set_gps_enabled (bool enabled)74UbuntuAppLocController :: set_gps_enabled (bool enabled)
73{75{
74 UStatus status = enabled76 auto status = enabled ? ua_location_service_controller_enable_gps (ualc)
75 ? ua_location_service_controller_enable_gps (ualc)77 : ua_location_service_controller_disable_gps (ualc);
76 : ua_location_service_controller_disable_gps (ualc);
7778
78 if (status != U_STATUS_SUCCESS)79 if (status != U_STATUS_SUCCESS)
79 std::cerr << "Error turning GPS " << (enabled?"on":"off") << std::endl;80 g_warning ("Error turning GPS %s", (enabled?"on":"off"));
80}
81
82/***
83****
84***/
85
86bool
87UbuntuAppLocController :: is_location_service_enabled () const
88{
89 UALocationServiceStatusFlags flags = 0;
90
91 return (ualc != nullptr)
92 && (!ua_location_service_controller_query_status (ualc, &flags))
93 && (flags & UA_LOCATION_SERVICE_ENABLED);
94}81}
9582
96void83void
97UbuntuAppLocController :: set_location_service_enabled (bool enabled)84UbuntuAppLocController :: set_location_service_enabled (bool enabled)
98{85{
99 UStatus status = enabled86 auto status = enabled ? ua_location_service_controller_enable_service (ualc)
100 ? ua_location_service_controller_enable_service (ualc)87 : ua_location_service_controller_disable_service (ualc);
101 : ua_location_service_controller_disable_service (ualc);
10288
103 if (status != U_STATUS_SUCCESS)89 if (status != U_STATUS_SUCCESS)
104 std::cerr << "Error turning Location Service " << (enabled?"on":"off")90 g_warning ("Error turning Location Service %s", (enabled?"on":"off"));
105 << std::endl;
106}91}
10792
=== modified file 'src/controller-ualc.h'
--- src/controller-ualc.h 2013-08-25 21:44:50 +0000
+++ src/controller-ualc.h 2014-05-27 09:31:36 +0000
@@ -34,18 +34,19 @@
34 virtual ~UbuntuAppLocController();34 virtual ~UbuntuAppLocController();
3535
36 bool is_valid () const { return ualc != nullptr; }36 bool is_valid () const { return ualc != nullptr; }
37 bool is_gps_enabled () const;37 bool is_gps_enabled () const { return (current_status & UA_LOCATION_SERVICE_GPS_ENABLED) != 0; }
38 bool is_location_service_enabled () const;38 bool is_location_service_enabled () const { return (current_status & UA_LOCATION_SERVICE_ENABLED) != 0; }
3939
40 void set_gps_enabled (bool enabled);40 void set_gps_enabled (bool enabled);
41 void set_location_service_enabled (bool enabled);41 void set_location_service_enabled (bool enabled);
4242
43 private:43 private:
4444
45 UALocationServiceStatusFlags current_status = 0;
45 UbuntuApplicationLocationServiceController * ualc;46 UbuntuApplicationLocationServiceController * ualc;
4647
47 static void on_location_service_controller_status_changed (48 static void on_ualc_status_changed (UALocationServiceStatusFlags, void *vself);
48 UALocationServiceStatusFlags, void *);49 void set_status (UALocationServiceStatusFlags);
49};50};
5051
51#endif // __INDICATOR_LOCATION_CONTROLLER_UALC__H__52#endif // __INDICATOR_LOCATION_CONTROLLER_UALC__H__
5253
=== modified file 'tests/CMakeLists.txt'
--- tests/CMakeLists.txt 2013-08-25 20:31:26 +0000
+++ tests/CMakeLists.txt 2014-05-27 09:31:36 +0000
@@ -1,3 +1,4 @@
1
1###2###
2### Google Test Framework3### Google Test Framework
3###4###
@@ -5,28 +6,46 @@
5add_library (gtest STATIC 6add_library (gtest STATIC
6 ${GTEST_SOURCE_DIR}/gtest-all.cc 7 ${GTEST_SOURCE_DIR}/gtest-all.cc
7 ${GTEST_SOURCE_DIR}/gtest_main.cc)8 ${GTEST_SOURCE_DIR}/gtest_main.cc)
9set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES ${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR})
10set_target_properties (gtest PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS} -w)
11
12###
13### General setup
14###
15
16include_directories (${CMAKE_SOURCE_DIR})
17#include_directories (${CMAKE_CURRENT_BINARY_DIR})
18
19set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g ${CC_WARNING_ARGS}")
20set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g ${CC_WARNING_ARGS} -std=c++11")
821
9###22###
10### phone-test23### phone-test
11###24###
1225
13set (PHONE_TEST "phone-test")26set (TEST_NAME phone-test)
14add_executable (${PHONE_TEST} phone-test.cc)27add_executable (${TEST_NAME} ${TEST_NAME}.cc)
15add_test (${PHONE_TEST} ${PHONE_TEST})28add_test (${TEST_NAME} ${TEST_NAME})
1629add_dependencies (${TEST_NAME} libindicatorlocationservice)
30target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
31
32###
33### ualc-test
34###
35
36set (TEST_NAME ualc-test)
37add_executable (${TEST_NAME} ${TEST_NAME}.cc ualc-mock.c)
38add_test (${TEST_NAME} ${TEST_NAME})
39add_dependencies (${TEST_NAME} libindicatorlocationservice)
40target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
1741
18###42###
19### globals43### globals
20###44###
2145
22set (ALL_TESTS ${PHONE_TEST})46#set_property (TARGET ${ALL_TESTS}
2347# APPEND PROPERTY LINK_LIBRARIES libindicator-location-service gtest ${GTEST_LIBS} ${GCOV_LIBS})
24set_property (TARGET ${ALL_TESTS}48#
25 APPEND_STRING PROPERTY COMPILE_FLAGS "${CC_WARNING_ARGS} -g --std=c++11")49#set_property (TARGET ${ALL_TESTS} gtest
2650# APPEND PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR} ${SERVICE_INCLUDE_DIRS} ${GTEST_INCLUDE_DIR})
27set_property (TARGET ${ALL_TESTS}
28 APPEND PROPERTY LINK_LIBRARIES libindicator-location-service gtest ${GTEST_LIBS} ${GCOV_LIBS})
29
30set_property (TARGET ${ALL_TESTS} gtest
31 APPEND PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR} ${SERVICE_INCLUDE_DIRS} ${GTEST_INCLUDE_DIR})
3251
3352
=== modified file 'tests/gtest-dbus-fixture.h'
--- tests/gtest-dbus-fixture.h 2013-08-25 21:44:50 +0000
+++ tests/gtest-dbus-fixture.h 2014-05-27 09:31:36 +0000
@@ -61,6 +61,13 @@
61 return G_SOURCE_REMOVE;61 return G_SOURCE_REMOVE;
62 }62 }
6363
64 static gboolean
65 wait_msec__timeout(gpointer loop)
66 {
67 g_main_loop_quit(static_cast<GMainLoop*>(loop));
68 return G_SOURCE_CONTINUE;
69 }
70
64 protected:71 protected:
6572
66 virtual void SetUp ()73 virtual void SetUp ()
@@ -103,29 +110,29 @@
103 g_clear_pointer (&loop, g_main_loop_unref);110 g_clear_pointer (&loop, g_main_loop_unref);
104 }111 }
105112
113 protected:
114
106 /* convenience func to loop while waiting for a GObject's signal */115 /* convenience func to loop while waiting for a GObject's signal */
107 void wait_for_signal (gpointer o, const gchar * signal)116 void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5)
108 {117 {
109 const int timeout_seconds = 5; // arbitrary
110
111 // wait for the signal or for timeout, whichever comes first118 // wait for the signal or for timeout, whichever comes first
112 guint handler_id = g_signal_connect_swapped (o, signal,119 const auto handler_id = g_signal_connect_swapped(o, signal,
113 G_CALLBACK(g_main_loop_quit),120 G_CALLBACK(g_main_loop_quit),
114 loop);121 loop);
115 gulong timeout_id = g_timeout_add_seconds (timeout_seconds,122 const auto timeout_id = g_timeout_add_seconds(timeout_seconds,
116 wait_for_signal__timeout,123 wait_for_signal__timeout,
117 const_cast<gchar*>(signal));124 loop);
118 g_main_loop_run (loop);125 g_main_loop_run(loop);
119 g_source_remove (timeout_id);126 g_source_remove(timeout_id);
120 g_signal_handler_disconnect (o, handler_id);127 g_signal_handler_disconnect(o, handler_id);
121 }128 }
122129
123 /* convenience func to loop for N msec */130 /* convenience func to loop for N msec */
124 void wait_msec (int msec)131 void wait_msec(int msec=50)
125 {132 {
126 guint id = g_timeout_add (msec, (GSourceFunc)g_main_loop_quit, loop);133 const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
127 g_main_loop_run (loop);134 g_main_loop_run(loop);
128 g_source_remove (id);135 g_source_remove(id);
129 }136 }
130137
131 GMainLoop * loop;138 GMainLoop * loop;
132139
=== added file 'tests/ualc-mock.c'
--- tests/ualc-mock.c 1970-01-01 00:00:00 +0000
+++ tests/ualc-mock.c 2014-05-27 09:31:36 +0000
@@ -0,0 +1,152 @@
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Charles Kerr <charles.kerr@canonical.com>
18 */
19
20#include <ubuntu/status.h>
21#include <ubuntu/application/location/controller.h>
22#include <ubuntu/application/location/service.h>
23
24#include <glib.h>
25
26static UALocationServiceController * singleton = NULL;
27
28struct UbuntuApplicationLocationServiceController
29{
30 int ref_count;
31 gboolean loc_enabled;
32 gboolean gps_enabled;
33 UALocationServiceStatusChangedHandler changed_handler;
34 void * changed_handler_context;
35};
36
37UALocationServiceController*
38ua_location_service_create_controller(void)
39{
40 if (singleton == NULL)
41 {
42 UALocationServiceController * c = g_new0 (UALocationServiceController, 1);
43 c->loc_enabled = FALSE;
44 c->gps_enabled = FALSE;
45 singleton = c;
46 }
47
48 singleton->ref_count++;
49
50 return singleton;
51}
52
53void
54ua_location_service_controller_set_status_changed_handler (UALocationServiceController *self,
55 UALocationServiceStatusChangedHandler changed_handler,
56 void *changed_handler_context)
57{
58 self->changed_handler = changed_handler;
59 self->changed_handler_context = changed_handler_context;
60}
61
62static void
63notify_status_changed (UALocationServiceController *self)
64{
65 if (self->changed_handler != NULL)
66 {
67 UALocationServiceStatusFlags flags = 0;
68 ua_location_service_controller_query_status(self, &flags);
69 self->changed_handler (flags, self->changed_handler_context);
70 }
71}
72
73UStatus
74ua_location_service_controller_query_status(UALocationServiceController *self,
75 UALocationServiceStatusFlags *out_flags)
76{
77 UALocationServiceStatusFlags out = 0;
78
79 g_return_val_if_fail (self != NULL, U_STATUS_ERROR);
80 g_return_val_if_fail (self->ref_count >= 1, U_STATUS_ERROR);
81 g_return_val_if_fail (out_flags != NULL, U_STATUS_ERROR);
82
83 if (self->loc_enabled)
84 out |= UA_LOCATION_SERVICE_ENABLED;
85
86 if (self->gps_enabled)
87 out |= UA_LOCATION_SERVICE_GPS_ENABLED;
88
89 *out_flags = out;
90 return U_STATUS_SUCCESS;
91}
92
93void
94ua_location_service_controller_unref (UALocationServiceController *self)
95{
96 g_return_if_fail (self != NULL);
97 g_return_if_fail (self->ref_count >= 1);
98
99 if (!--self->ref_count)
100 {
101 g_free (self);
102 singleton = NULL;
103 }
104}
105
106static UStatus
107set_gps_enabled (UALocationServiceController *self, gboolean enabled)
108{
109 g_return_if_fail (self != NULL);
110 g_return_if_fail (self->ref_count >= 1);
111
112 self->gps_enabled = enabled;
113 notify_status_changed (self);
114
115 return U_STATUS_SUCCESS;
116}
117
118UStatus
119ua_location_service_controller_enable_gps (UALocationServiceController *self)
120{
121 return set_gps_enabled (self, TRUE);
122}
123
124UStatus
125ua_location_service_controller_disable_gps (UALocationServiceController *self)
126{
127 return set_gps_enabled (self, FALSE);
128}
129
130static UStatus
131set_service_enabled (UALocationServiceController *self, gboolean enabled)
132{
133 g_return_if_fail (self != NULL);
134 g_return_if_fail (self->ref_count >= 1);
135
136 self->loc_enabled = enabled;
137 notify_status_changed (self);
138
139 return U_STATUS_SUCCESS;
140}
141
142UStatus
143ua_location_service_controller_enable_service (UALocationServiceController *self)
144{
145 return set_service_enabled (self, TRUE);
146}
147
148UStatus
149ua_location_service_controller_disable_service (UALocationServiceController *self)
150{
151 return set_service_enabled (self, FALSE);
152}
0153
=== added file 'tests/ualc-test.cc'
--- tests/ualc-test.cc 1970-01-01 00:00:00 +0000
+++ tests/ualc-test.cc 2014-05-27 09:31:36 +0000
@@ -0,0 +1,235 @@
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Charles Kerr <charles.kerr@canonical.com>
18 */
19
20#include "src/controller-ualc.h"
21
22#include <ubuntu/status.h>
23#include <ubuntu/application/location/controller.h>
24#include <ubuntu/application/location/service.h>
25
26#include <gtest/gtest.h>
27
28#include <glib.h>
29
30/***
31****
32***/
33
34class UalcFixture: public ::testing::Test
35{
36 typedef ::testing::Test super;
37
38 protected:
39
40 GMainLoop * loop = nullptr;
41 UALocationServiceController * ualc = nullptr;
42
43 virtual void SetUp()
44 {
45 super::SetUp();
46
47 loop = g_main_loop_new(nullptr, FALSE);
48 ualc = ua_location_service_create_controller();
49 }
50
51 virtual void TearDown()
52 {
53 g_clear_pointer(&ualc, ua_location_service_controller_unref);
54 g_clear_pointer(&loop, g_main_loop_unref);
55
56 super::TearDown();
57 }
58};
59
60
61/***
62**** Sanity check for build + fixture
63***/
64
65TEST_F(UalcFixture, HelloWorld)
66{
67 EXPECT_TRUE(true);
68 EXPECT_FALSE(false);
69}
70
71
72/***
73**** Simple test to make sure our mock UALC is behaving sanely
74***/
75
76namespace
77{
78 UALocationServiceStatusFlags simple_mock_test_flags = 0;
79
80 void * simple_mock_test_context = (void*)(0xDEADBEEF);
81
82 void simple_mock_test_changed_handler(UALocationServiceStatusFlags flags, void * c)
83 {
84 g_assert(c == simple_mock_test_context);
85 simple_mock_test_flags = flags;
86 }
87}
88
89TEST_F(UalcFixture, SimpleMockTest)
90{
91 ua_location_service_controller_set_status_changed_handler(
92 ualc, simple_mock_test_changed_handler, simple_mock_test_context);
93
94 UALocationServiceStatusFlags flags = 0;
95 EXPECT_EQ(0, simple_mock_test_flags);
96 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
97 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_service(ualc));
98 EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, simple_mock_test_flags);
99 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
100 EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, flags);
101 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_gps(ualc));
102 EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, simple_mock_test_flags);
103 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
104 EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, flags);
105}
106
107/***
108**** Now, actual controller tests
109***/
110
111TEST_F(UalcFixture, ControllerAccessors)
112{
113 UbuntuAppLocController c;
114 EXPECT_FALSE(c.is_location_service_enabled());
115 EXPECT_FALSE(c.is_gps_enabled());
116
117 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_service(ualc));
118 EXPECT_TRUE(c.is_location_service_enabled());
119 EXPECT_FALSE(c.is_gps_enabled());
120
121 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_gps(ualc));
122 EXPECT_TRUE(c.is_location_service_enabled());
123 EXPECT_TRUE(c.is_gps_enabled());
124
125 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_disable_service(ualc));
126 EXPECT_FALSE(c.is_location_service_enabled());
127 EXPECT_TRUE(c.is_gps_enabled());
128
129 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_disable_gps(ualc));
130 EXPECT_FALSE(c.is_location_service_enabled());
131 EXPECT_FALSE(c.is_gps_enabled());
132}
133
134TEST_F(UalcFixture, ControllerMutators)
135{
136 UbuntuAppLocController c;
137 UALocationServiceStatusFlags flags;
138
139 flags = 0;
140 EXPECT_FALSE(c.is_location_service_enabled());
141 EXPECT_FALSE(c.is_gps_enabled());
142 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
143 EXPECT_EQ(0, flags);
144
145 // enable gps
146 c.set_gps_enabled(true);
147 EXPECT_FALSE(c.is_location_service_enabled());
148 EXPECT_TRUE(c.is_gps_enabled());
149 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
150 EXPECT_EQ(UA_LOCATION_SERVICE_GPS_ENABLED, flags);
151
152 // disable gps
153 c.set_gps_enabled(false);
154 EXPECT_FALSE(c.is_location_service_enabled());
155 EXPECT_FALSE(c.is_gps_enabled());
156 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
157 EXPECT_EQ(0, flags);
158
159 // enable service
160 c.set_location_service_enabled(true);
161 EXPECT_TRUE(c.is_location_service_enabled());
162 EXPECT_FALSE(c.is_gps_enabled());
163 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
164 EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, flags);
165
166 // disable service
167 c.set_location_service_enabled(false);
168 EXPECT_FALSE(c.is_location_service_enabled());
169 EXPECT_FALSE(c.is_gps_enabled());
170 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
171 EXPECT_EQ(0, flags);
172
173 // enable both
174 c.set_gps_enabled(true);
175 c.set_location_service_enabled(true);
176 EXPECT_TRUE(c.is_location_service_enabled());
177 EXPECT_TRUE(c.is_gps_enabled());
178 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
179 EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, flags);
180
181 // disable both
182 c.set_gps_enabled(false);
183 c.set_location_service_enabled(false);
184 EXPECT_FALSE(c.is_location_service_enabled());
185 EXPECT_FALSE(c.is_gps_enabled());
186 EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
187 EXPECT_EQ(0, flags);
188}
189
190/***
191****
192***/
193
194namespace
195{
196 struct MockListener: public ControllerListener {
197 bool last_gps = false;
198 bool last_service = false;
199 bool gps_tickled = false;
200 bool service_tickled = false;
201 void reset() { gps_tickled = service_tickled = last_gps = last_service = false; }
202 void on_gps_enabled_changed (bool b) { gps_tickled = true; last_gps = b; }
203 void on_location_service_enabled_changed (bool b) { service_tickled = true; last_service = b; }
204 };
205
206} // unnamed namespace
207
208TEST_F(UalcFixture, ControllerListeners)
209{
210 UbuntuAppLocController c;
211 MockListener l;
212 c.add_listener(&l);
213 EXPECT_FALSE(l.service_tickled);
214 EXPECT_FALSE(l.gps_tickled);
215
216 // turn on the gps...
217 c.set_gps_enabled(true);
218 EXPECT_FALSE(l.service_tickled);
219 EXPECT_TRUE(l.gps_tickled);
220 EXPECT_TRUE(l.last_gps);
221 l.reset();
222
223 // turn on the location service...
224 c.set_location_service_enabled(true);
225 EXPECT_FALSE(l.gps_tickled);
226 EXPECT_TRUE(l.service_tickled);
227 EXPECT_TRUE(l.last_service);
228 l.reset();
229
230 // service is /already/ enabled so turning
231 // it on again shouldn't trigger any changes
232 c.set_location_service_enabled(true);
233 EXPECT_FALSE(l.service_tickled);
234 EXPECT_FALSE(l.gps_tickled);
235}

Subscribers

People subscribed via source and target branches