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

Proposed by Charles Kerr
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
Ted Gould (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+203264@code.launchpad.net

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.
73. By Charles Kerr

consistency in UbuntuAppLocController::set_state()

74. By Charles Kerr

copyediting: more understandable variable name

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
75. By Charles Kerr

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

76. By Charles Kerr

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

77. By Charles Kerr

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

78. By Charles Kerr

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

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote :

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 :

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.

79. By Charles Kerr

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

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote :

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 :

> 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 :

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
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2013-10-01 19:48:43 +0000
3+++ CMakeLists.txt 2014-01-29 00:21:35 +0000
4@@ -28,11 +28,12 @@
5 find_package (PkgConfig REQUIRED)
6
7 include (FindPkgConfig)
8-pkg_check_modules (SERVICE REQUIRED
9+pkg_check_modules (SERVICE_DEPS REQUIRED
10 ubuntu-platform-api>=1.0.0
11 url-dispatcher-1
12 gio-unix-2.0>=2.36
13 glib-2.0>=2.36)
14+include_directories (SYSTEM ${SERVICE_DEPS_INCLUDE_DIRS})
15
16 ##
17 ##
18
19=== modified file 'src/CMakeLists.txt'
20--- src/CMakeLists.txt 2013-08-29 17:48:37 +0000
21+++ src/CMakeLists.txt 2014-01-29 00:21:35 +0000
22@@ -1,9 +1,12 @@
23-
24-set_property (DIRECTORY
25- APPEND PROPERTY INCLUDE_DIRECTORIES ${SERVICE_INCLUDE_DIRS})
26-
27-set_property (DIRECTORY
28- APPEND PROPERTY LINK_DIRECTORIES ${SERVICE_INCLUDE_DIRS})
29+set (SERVICE_LIB "indicatordatetimeservice")
30+set (SERVICE_EXEC "indicator-datetime-service")
31+SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g ${CXX_WARNING_ARGS} ${GCOV_FLAGS}")
32+
33+#set_property (DIRECTORY
34+# APPEND PROPERTY INCLUDE_DIRECTORIES ${SERVICE_INCLUDE_DIRS})
35+#
36+#set_property (DIRECTORY
37+# APPEND PROPERTY LINK_DIRECTORIES ${SERVICE_INCLUDE_DIRS})
38
39
40 ###
41@@ -12,34 +15,22 @@
42 ### it's built apart from main() as a convenience for tests/
43 ###
44
45-set (INDICATOR_LIB "libindicator-location-service")
46-
47-add_library (${INDICATOR_LIB} STATIC controller.cc phone.cc service.cc)
48-
49-set_property (TARGET ${INDICATOR_LIB}
50- APPEND PROPERTY LINK_LIBRARIES ${INDICATOR_LIB} ${SERVICE_LIBRARIES} ${GCOV_LIBS})
51+add_library (${SERVICE_LIB} STATIC controller.cc phone.cc service.cc controller-ualc.cc)
52+include_directories (${CMAKE_SOURCE_DIR})
53+link_directories (${SERVICE_DEPS_LIBRARY_DIRS})
54
55
56 ###
57 ### The service execuable.
58 ###
59
60-set (INDICATOR_EXEC "indicator-location-service")
61-
62-add_executable (${INDICATOR_EXEC} main.cc controller-ualc.cc)
63+add_executable (${SERVICE_EXEC} main.cc)
64+target_link_libraries (${SERVICE_EXEC} ${SERVICE_LIB} ${SERVICE_DEPS_LIBRARIES})
65+install (TARGETS ${SERVICE_EXEC}
66+ RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_PKGLIBEXECDIR})
67
68 set_property (SOURCE main.cc
69 APPEND PROPERTY COMPILE_DEFINITIONS
70 GETTEXT_PACKAGE="${GETTEXT_PACKAGE}"
71 GNOMELOCALEDIR="@CMAKE_INSTALL_FULL_LOCALEDIR@")
72
73-target_link_libraries (${INDICATOR_EXEC} libindicator-location-service)
74-
75-install (TARGETS ${INDICATOR_EXEC}
76- RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_PKGLIBEXECDIR})
77-
78-
79-
80-set_property (TARGET ${INDICATOR_LIB} ${INDICATOR_EXEC}
81- APPEND_STRING PROPERTY COMPILE_FLAGS " ${CC_WARNING_ARGS} ${GCOV_FLAGS} --std=c++11")
82-
83
84=== modified file 'src/controller-ualc.cc'
85--- src/controller-ualc.cc 2013-08-29 17:49:40 +0000
86+++ src/controller-ualc.cc 2014-01-29 00:21:35 +0000
87@@ -18,20 +18,24 @@
88 * Charles Kerr <charles.kerr@canonical.com>
89 */
90
91-#include <iostream>
92+#include <glib.h>
93
94 #include "controller-ualc.h"
95
96 UbuntuAppLocController :: UbuntuAppLocController ():
97 ualc (ua_location_service_create_controller ())
98 {
99- if (ualc != nullptr)
100- {
101- ua_location_service_controller_set_status_changed_handler (
102- ualc,
103- on_location_service_controller_status_changed,
104- this);
105- }
106+ g_return_if_fail (ualc != nullptr);
107+
108+ // update our status when the ualc changes
109+ ua_location_service_controller_set_status_changed_handler (ualc,
110+ on_ualc_status_changed,
111+ this);
112+
113+ // query the ualc to bootstrap our initial status
114+ UALocationServiceStatusFlags initial_status;
115+ if (ua_location_service_controller_query_status (ualc, &initial_status) == U_STATUS_SUCCESS)
116+ set_status (initial_status);
117 }
118
119 UbuntuAppLocController :: ~UbuntuAppLocController ()
120@@ -41,66 +45,47 @@
121 }
122
123 void
124-UbuntuAppLocController :: on_location_service_controller_status_changed (
125- UALocationServiceStatusFlags f,
126- void * vself)
127-{
128- auto self = static_cast<UbuntuAppLocController*>(vself);
129-
130- if (f & (UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_DISABLED))
131- self->notify_gps_enabled (self->is_location_service_enabled ());
132-
133- if (f & (UA_LOCATION_SERVICE_GPS_ENABLED | UA_LOCATION_SERVICE_GPS_DISABLED))
134- self->notify_gps_enabled (self->is_gps_enabled());
135+UbuntuAppLocController :: on_ualc_status_changed (UALocationServiceStatusFlags flags, void *vself)
136+{
137+ static_cast<UbuntuAppLocController*>(vself)->set_status (flags);
138+}
139+
140+void
141+UbuntuAppLocController :: set_status (UALocationServiceStatusFlags new_status)
142+{
143+ const bool loc_was_enabled = is_location_service_enabled();
144+ const bool gps_was_enabled = is_gps_enabled();
145+ current_status = new_status;
146+ const bool loc_is_enabled = is_location_service_enabled();
147+ const bool gps_is_enabled = is_gps_enabled();
148+
149+ if (loc_was_enabled != loc_is_enabled)
150+ notify_location_service_enabled (loc_is_enabled);
151+
152+ if (gps_was_enabled != gps_is_enabled)
153+ notify_gps_enabled (gps_is_enabled);
154 }
155
156 /***
157 ****
158 ***/
159
160-bool
161-UbuntuAppLocController :: is_gps_enabled () const
162-{
163- UALocationServiceStatusFlags flags = 0;
164-
165- return (ualc != nullptr)
166- && (!ua_location_service_controller_query_status (ualc, &flags))
167- && (flags & UA_LOCATION_SERVICE_GPS_ENABLED);
168-}
169-
170 void
171 UbuntuAppLocController :: set_gps_enabled (bool enabled)
172 {
173- UStatus status = enabled
174- ? ua_location_service_controller_enable_gps (ualc)
175- : ua_location_service_controller_disable_gps (ualc);
176+ auto status = enabled ? ua_location_service_controller_enable_gps (ualc)
177+ : ua_location_service_controller_disable_gps (ualc);
178
179 if (status != U_STATUS_SUCCESS)
180- std::cerr << "Error turning GPS " << (enabled?"on":"off") << std::endl;
181-}
182-
183-/***
184-****
185-***/
186-
187-bool
188-UbuntuAppLocController :: is_location_service_enabled () const
189-{
190- UALocationServiceStatusFlags flags = 0;
191-
192- return (ualc != nullptr)
193- && (!ua_location_service_controller_query_status (ualc, &flags))
194- && (flags & UA_LOCATION_SERVICE_ENABLED);
195+ g_warning ("Error turning GPS %s", (enabled?"on":"off"));
196 }
197
198 void
199 UbuntuAppLocController :: set_location_service_enabled (bool enabled)
200 {
201- UStatus status = enabled
202- ? ua_location_service_controller_enable_service (ualc)
203- : ua_location_service_controller_disable_service (ualc);
204+ auto status = enabled ? ua_location_service_controller_enable_service (ualc)
205+ : ua_location_service_controller_disable_service (ualc);
206
207 if (status != U_STATUS_SUCCESS)
208- std::cerr << "Error turning Location Service " << (enabled?"on":"off")
209- << std::endl;
210+ g_warning ("Error turning Location Service %s", (enabled?"on":"off"));
211 }
212
213=== modified file 'src/controller-ualc.h'
214--- src/controller-ualc.h 2013-08-25 21:44:50 +0000
215+++ src/controller-ualc.h 2014-01-29 00:21:35 +0000
216@@ -34,18 +34,19 @@
217 virtual ~UbuntuAppLocController();
218
219 bool is_valid () const { return ualc != nullptr; }
220- bool is_gps_enabled () const;
221- bool is_location_service_enabled () const;
222+ bool is_gps_enabled () const { return (current_status & UA_LOCATION_SERVICE_GPS_ENABLED) != 0; }
223+ bool is_location_service_enabled () const { return (current_status & UA_LOCATION_SERVICE_ENABLED) != 0; }
224
225 void set_gps_enabled (bool enabled);
226 void set_location_service_enabled (bool enabled);
227
228 private:
229
230+ UALocationServiceStatusFlags current_status = 0;
231 UbuntuApplicationLocationServiceController * ualc;
232
233- static void on_location_service_controller_status_changed (
234- UALocationServiceStatusFlags, void *);
235+ static void on_ualc_status_changed (UALocationServiceStatusFlags, void *vself);
236+ void set_status (UALocationServiceStatusFlags);
237 };
238
239 #endif // __INDICATOR_LOCATION_CONTROLLER_UALC__H__
240
241=== modified file 'tests/CMakeLists.txt'
242--- tests/CMakeLists.txt 2013-08-25 20:31:26 +0000
243+++ tests/CMakeLists.txt 2014-01-29 00:21:35 +0000
244@@ -1,3 +1,4 @@
245+
246 ###
247 ### Google Test Framework
248 ###
249@@ -5,28 +6,46 @@
250 add_library (gtest STATIC
251 ${GTEST_SOURCE_DIR}/gtest-all.cc
252 ${GTEST_SOURCE_DIR}/gtest_main.cc)
253+set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES ${INCLUDE_DIRECTORIES} ${GTEST_INCLUDE_DIR})
254+set_target_properties (gtest PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS} -w)
255+
256+###
257+### General setup
258+###
259+
260+include_directories (${CMAKE_SOURCE_DIR})
261+#include_directories (${CMAKE_CURRENT_BINARY_DIR})
262+
263+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g ${CC_WARNING_ARGS}")
264+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g ${CC_WARNING_ARGS} -std=c++11")
265
266 ###
267 ### phone-test
268 ###
269
270-set (PHONE_TEST "phone-test")
271-add_executable (${PHONE_TEST} phone-test.cc)
272-add_test (${PHONE_TEST} ${PHONE_TEST})
273-
274+set (TEST_NAME phone-test)
275+add_executable (${TEST_NAME} ${TEST_NAME}.cc)
276+add_test (${TEST_NAME} ${TEST_NAME})
277+add_dependencies (${TEST_NAME} libindicatorlocationservice)
278+target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
279+
280+###
281+### ualc-test
282+###
283+
284+set (TEST_NAME ualc-test)
285+add_executable (${TEST_NAME} ${TEST_NAME}.cc ualc-mock.c)
286+add_test (${TEST_NAME} ${TEST_NAME})
287+add_dependencies (${TEST_NAME} libindicatorlocationservice)
288+target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
289
290 ###
291 ### globals
292 ###
293
294-set (ALL_TESTS ${PHONE_TEST})
295-
296-set_property (TARGET ${ALL_TESTS}
297- APPEND_STRING PROPERTY COMPILE_FLAGS "${CC_WARNING_ARGS} -g --std=c++11")
298-
299-set_property (TARGET ${ALL_TESTS}
300- APPEND PROPERTY LINK_LIBRARIES libindicator-location-service gtest ${GTEST_LIBS} ${GCOV_LIBS})
301-
302-set_property (TARGET ${ALL_TESTS} gtest
303- APPEND PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR} ${SERVICE_INCLUDE_DIRS} ${GTEST_INCLUDE_DIR})
304+#set_property (TARGET ${ALL_TESTS}
305+# APPEND PROPERTY LINK_LIBRARIES libindicator-location-service gtest ${GTEST_LIBS} ${GCOV_LIBS})
306+#
307+#set_property (TARGET ${ALL_TESTS} gtest
308+# APPEND PROPERTY INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR} ${SERVICE_INCLUDE_DIRS} ${GTEST_INCLUDE_DIR})
309
310
311=== modified file 'tests/gtest-dbus-fixture.h'
312--- tests/gtest-dbus-fixture.h 2013-08-25 21:44:50 +0000
313+++ tests/gtest-dbus-fixture.h 2014-01-29 00:21:35 +0000
314@@ -61,6 +61,13 @@
315 return G_SOURCE_REMOVE;
316 }
317
318+ static gboolean
319+ wait_msec__timeout(gpointer loop)
320+ {
321+ g_main_loop_quit(static_cast<GMainLoop*>(loop));
322+ return G_SOURCE_CONTINUE;
323+ }
324+
325 protected:
326
327 virtual void SetUp ()
328@@ -103,29 +110,29 @@
329 g_clear_pointer (&loop, g_main_loop_unref);
330 }
331
332+ protected:
333+
334 /* convenience func to loop while waiting for a GObject's signal */
335- void wait_for_signal (gpointer o, const gchar * signal)
336+ void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5)
337 {
338- const int timeout_seconds = 5; // arbitrary
339-
340 // wait for the signal or for timeout, whichever comes first
341- guint handler_id = g_signal_connect_swapped (o, signal,
342- G_CALLBACK(g_main_loop_quit),
343- loop);
344- gulong timeout_id = g_timeout_add_seconds (timeout_seconds,
345- wait_for_signal__timeout,
346- const_cast<gchar*>(signal));
347- g_main_loop_run (loop);
348- g_source_remove (timeout_id);
349- g_signal_handler_disconnect (o, handler_id);
350+ const auto handler_id = g_signal_connect_swapped(o, signal,
351+ G_CALLBACK(g_main_loop_quit),
352+ loop);
353+ const auto timeout_id = g_timeout_add_seconds(timeout_seconds,
354+ wait_for_signal__timeout,
355+ loop);
356+ g_main_loop_run(loop);
357+ g_source_remove(timeout_id);
358+ g_signal_handler_disconnect(o, handler_id);
359 }
360
361 /* convenience func to loop for N msec */
362- void wait_msec (int msec)
363+ void wait_msec(int msec=50)
364 {
365- guint id = g_timeout_add (msec, (GSourceFunc)g_main_loop_quit, loop);
366- g_main_loop_run (loop);
367- g_source_remove (id);
368+ const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
369+ g_main_loop_run(loop);
370+ g_source_remove(id);
371 }
372
373 GMainLoop * loop;
374
375=== added file 'tests/ualc-mock.c'
376--- tests/ualc-mock.c 1970-01-01 00:00:00 +0000
377+++ tests/ualc-mock.c 2014-01-29 00:21:35 +0000
378@@ -0,0 +1,152 @@
379+/*
380+ * Copyright 2014 Canonical Ltd.
381+ *
382+ * This program is free software; you can redistribute it and/or modify
383+ * it under the terms of the GNU General Public License as published by
384+ * the Free Software Foundation; version 3.
385+ *
386+ * This program is distributed in the hope that it will be useful,
387+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
388+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
389+ * GNU Lesser General Public License for more details.
390+ *
391+ * You should have received a copy of the GNU Lesser General Public License
392+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
393+ *
394+ * Authors:
395+ * Charles Kerr <charles.kerr@canonical.com>
396+ */
397+
398+#include <ubuntu/status.h>
399+#include <ubuntu/application/location/controller.h>
400+#include <ubuntu/application/location/service.h>
401+
402+#include <glib.h>
403+
404+static UALocationServiceController * singleton = NULL;
405+
406+struct UbuntuApplicationLocationServiceController
407+{
408+ int ref_count;
409+ gboolean loc_enabled;
410+ gboolean gps_enabled;
411+ UALocationServiceStatusChangedHandler changed_handler;
412+ void * changed_handler_context;
413+};
414+
415+UALocationServiceController*
416+ua_location_service_create_controller(void)
417+{
418+ if (singleton == NULL)
419+ {
420+ UALocationServiceController * c = g_new0 (UALocationServiceController, 1);
421+ c->loc_enabled = FALSE;
422+ c->gps_enabled = FALSE;
423+ singleton = c;
424+ }
425+
426+ singleton->ref_count++;
427+
428+ return singleton;
429+}
430+
431+void
432+ua_location_service_controller_set_status_changed_handler (UALocationServiceController *self,
433+ UALocationServiceStatusChangedHandler changed_handler,
434+ void *changed_handler_context)
435+{
436+ self->changed_handler = changed_handler;
437+ self->changed_handler_context = changed_handler_context;
438+}
439+
440+static void
441+notify_status_changed (UALocationServiceController *self)
442+{
443+ if (self->changed_handler != NULL)
444+ {
445+ UALocationServiceStatusFlags flags = 0;
446+ ua_location_service_controller_query_status(self, &flags);
447+ self->changed_handler (flags, self->changed_handler_context);
448+ }
449+}
450+
451+UStatus
452+ua_location_service_controller_query_status(UALocationServiceController *self,
453+ UALocationServiceStatusFlags *out_flags)
454+{
455+ UALocationServiceStatusFlags out = 0;
456+
457+ g_return_val_if_fail (self != NULL, U_STATUS_ERROR);
458+ g_return_val_if_fail (self->ref_count >= 1, U_STATUS_ERROR);
459+ g_return_val_if_fail (out_flags != NULL, U_STATUS_ERROR);
460+
461+ if (self->loc_enabled)
462+ out |= UA_LOCATION_SERVICE_ENABLED;
463+
464+ if (self->gps_enabled)
465+ out |= UA_LOCATION_SERVICE_GPS_ENABLED;
466+
467+ *out_flags = out;
468+ return U_STATUS_SUCCESS;
469+}
470+
471+void
472+ua_location_service_controller_unref (UALocationServiceController *self)
473+{
474+ g_return_if_fail (self != NULL);
475+ g_return_if_fail (self->ref_count >= 1);
476+
477+ if (!--self->ref_count)
478+ {
479+ g_free (self);
480+ singleton = NULL;
481+ }
482+}
483+
484+static UStatus
485+set_gps_enabled (UALocationServiceController *self, gboolean enabled)
486+{
487+ g_return_if_fail (self != NULL);
488+ g_return_if_fail (self->ref_count >= 1);
489+
490+ self->gps_enabled = enabled;
491+ notify_status_changed (self);
492+
493+ return U_STATUS_SUCCESS;
494+}
495+
496+UStatus
497+ua_location_service_controller_enable_gps (UALocationServiceController *self)
498+{
499+ return set_gps_enabled (self, TRUE);
500+}
501+
502+UStatus
503+ua_location_service_controller_disable_gps (UALocationServiceController *self)
504+{
505+ return set_gps_enabled (self, FALSE);
506+}
507+
508+static UStatus
509+set_service_enabled (UALocationServiceController *self, gboolean enabled)
510+{
511+ g_return_if_fail (self != NULL);
512+ g_return_if_fail (self->ref_count >= 1);
513+
514+ self->loc_enabled = enabled;
515+ notify_status_changed (self);
516+
517+ return U_STATUS_SUCCESS;
518+}
519+
520+UStatus
521+ua_location_service_controller_enable_service (UALocationServiceController *self)
522+{
523+ return set_service_enabled (self, TRUE);
524+}
525+
526+UStatus
527+ua_location_service_controller_disable_service (UALocationServiceController *self)
528+{
529+ return set_service_enabled (self, FALSE);
530+}
531
532=== added file 'tests/ualc-test.cc'
533--- tests/ualc-test.cc 1970-01-01 00:00:00 +0000
534+++ tests/ualc-test.cc 2014-01-29 00:21:35 +0000
535@@ -0,0 +1,235 @@
536+/*
537+ * Copyright 2014 Canonical Ltd.
538+ *
539+ * This program is free software; you can redistribute it and/or modify
540+ * it under the terms of the GNU General Public License as published by
541+ * the Free Software Foundation; version 3.
542+ *
543+ * This program is distributed in the hope that it will be useful,
544+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
545+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
546+ * GNU Lesser General Public License for more details.
547+ *
548+ * You should have received a copy of the GNU Lesser General Public License
549+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
550+ *
551+ * Authors:
552+ * Charles Kerr <charles.kerr@canonical.com>
553+ */
554+
555+#include "src/controller-ualc.h"
556+
557+#include <ubuntu/status.h>
558+#include <ubuntu/application/location/controller.h>
559+#include <ubuntu/application/location/service.h>
560+
561+#include <gtest/gtest.h>
562+
563+#include <glib.h>
564+
565+/***
566+****
567+***/
568+
569+class UalcFixture: public ::testing::Test
570+{
571+ typedef ::testing::Test super;
572+
573+ protected:
574+
575+ GMainLoop * loop = nullptr;
576+ UALocationServiceController * ualc = nullptr;
577+
578+ virtual void SetUp()
579+ {
580+ super::SetUp();
581+
582+ loop = g_main_loop_new(nullptr, FALSE);
583+ ualc = ua_location_service_create_controller();
584+ }
585+
586+ virtual void TearDown()
587+ {
588+ g_clear_pointer(&ualc, ua_location_service_controller_unref);
589+ g_clear_pointer(&loop, g_main_loop_unref);
590+
591+ super::TearDown();
592+ }
593+};
594+
595+
596+/***
597+**** Sanity check for build + fixture
598+***/
599+
600+TEST_F(UalcFixture, HelloWorld)
601+{
602+ EXPECT_TRUE(true);
603+ EXPECT_FALSE(false);
604+}
605+
606+
607+/***
608+**** Simple test to make sure our mock UALC is behaving sanely
609+***/
610+
611+namespace
612+{
613+ UALocationServiceStatusFlags simple_mock_test_flags = 0;
614+
615+ void * simple_mock_test_context = (void*)(0xDEADBEEF);
616+
617+ void simple_mock_test_changed_handler(UALocationServiceStatusFlags flags, void * c)
618+ {
619+ g_assert(c == simple_mock_test_context);
620+ simple_mock_test_flags = flags;
621+ }
622+}
623+
624+TEST_F(UalcFixture, SimpleMockTest)
625+{
626+ ua_location_service_controller_set_status_changed_handler(
627+ ualc, simple_mock_test_changed_handler, simple_mock_test_context);
628+
629+ UALocationServiceStatusFlags flags = 0;
630+ EXPECT_EQ(0, simple_mock_test_flags);
631+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
632+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_service(ualc));
633+ EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, simple_mock_test_flags);
634+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
635+ EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, flags);
636+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_gps(ualc));
637+ EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, simple_mock_test_flags);
638+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
639+ EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, flags);
640+}
641+
642+/***
643+**** Now, actual controller tests
644+***/
645+
646+TEST_F(UalcFixture, ControllerAccessors)
647+{
648+ UbuntuAppLocController c;
649+ EXPECT_FALSE(c.is_location_service_enabled());
650+ EXPECT_FALSE(c.is_gps_enabled());
651+
652+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_service(ualc));
653+ EXPECT_TRUE(c.is_location_service_enabled());
654+ EXPECT_FALSE(c.is_gps_enabled());
655+
656+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_gps(ualc));
657+ EXPECT_TRUE(c.is_location_service_enabled());
658+ EXPECT_TRUE(c.is_gps_enabled());
659+
660+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_disable_service(ualc));
661+ EXPECT_FALSE(c.is_location_service_enabled());
662+ EXPECT_TRUE(c.is_gps_enabled());
663+
664+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_disable_gps(ualc));
665+ EXPECT_FALSE(c.is_location_service_enabled());
666+ EXPECT_FALSE(c.is_gps_enabled());
667+}
668+
669+TEST_F(UalcFixture, ControllerMutators)
670+{
671+ UbuntuAppLocController c;
672+ UALocationServiceStatusFlags flags;
673+
674+ flags = 0;
675+ EXPECT_FALSE(c.is_location_service_enabled());
676+ EXPECT_FALSE(c.is_gps_enabled());
677+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
678+ EXPECT_EQ(0, flags);
679+
680+ // enable gps
681+ c.set_gps_enabled(true);
682+ EXPECT_FALSE(c.is_location_service_enabled());
683+ EXPECT_TRUE(c.is_gps_enabled());
684+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
685+ EXPECT_EQ(UA_LOCATION_SERVICE_GPS_ENABLED, flags);
686+
687+ // disable gps
688+ c.set_gps_enabled(false);
689+ EXPECT_FALSE(c.is_location_service_enabled());
690+ EXPECT_FALSE(c.is_gps_enabled());
691+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
692+ EXPECT_EQ(0, flags);
693+
694+ // enable service
695+ c.set_location_service_enabled(true);
696+ EXPECT_TRUE(c.is_location_service_enabled());
697+ EXPECT_FALSE(c.is_gps_enabled());
698+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
699+ EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, flags);
700+
701+ // disable service
702+ c.set_location_service_enabled(false);
703+ EXPECT_FALSE(c.is_location_service_enabled());
704+ EXPECT_FALSE(c.is_gps_enabled());
705+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
706+ EXPECT_EQ(0, flags);
707+
708+ // enable both
709+ c.set_gps_enabled(true);
710+ c.set_location_service_enabled(true);
711+ EXPECT_TRUE(c.is_location_service_enabled());
712+ EXPECT_TRUE(c.is_gps_enabled());
713+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
714+ EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, flags);
715+
716+ // disable both
717+ c.set_gps_enabled(false);
718+ c.set_location_service_enabled(false);
719+ EXPECT_FALSE(c.is_location_service_enabled());
720+ EXPECT_FALSE(c.is_gps_enabled());
721+ EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
722+ EXPECT_EQ(0, flags);
723+}
724+
725+/***
726+****
727+***/
728+
729+namespace
730+{
731+ struct MockListener: public ControllerListener {
732+ bool last_gps = false;
733+ bool last_service = false;
734+ bool gps_tickled = false;
735+ bool service_tickled = false;
736+ void reset() { gps_tickled = service_tickled = last_gps = last_service = false; }
737+ void on_gps_enabled_changed (bool b) { gps_tickled = true; last_gps = b; }
738+ void on_location_service_enabled_changed (bool b) { service_tickled = true; last_service = b; }
739+ };
740+
741+} // unnamed namespace
742+
743+TEST_F(UalcFixture, ControllerListeners)
744+{
745+ UbuntuAppLocController c;
746+ MockListener l;
747+ c.add_listener(&l);
748+ EXPECT_FALSE(l.service_tickled);
749+ EXPECT_FALSE(l.gps_tickled);
750+
751+ // turn on the gps...
752+ c.set_gps_enabled(true);
753+ EXPECT_FALSE(l.service_tickled);
754+ EXPECT_TRUE(l.gps_tickled);
755+ EXPECT_TRUE(l.last_gps);
756+ l.reset();
757+
758+ // turn on the location service...
759+ c.set_location_service_enabled(true);
760+ EXPECT_FALSE(l.gps_tickled);
761+ EXPECT_TRUE(l.service_tickled);
762+ EXPECT_TRUE(l.last_service);
763+ l.reset();
764+
765+ // service is /already/ enabled so turning
766+ // it on again shouldn't trigger any changes
767+ c.set_location_service_enabled(true);
768+ EXPECT_FALSE(l.service_tickled);
769+ EXPECT_FALSE(l.gps_tickled);
770+}

Subscribers

People subscribed via source and target branches