Merge lp:~charlesk/indicator-location/location-service-proxy into lp:indicator-location/15.04

Proposed by Charles Kerr
Status: Merged
Approved by: Antti Kaijanmäki
Approved revision: 161
Merged at revision: 145
Proposed branch: lp:~charlesk/indicator-location/location-service-proxy
Merge into: lp:indicator-location/15.04
Prerequisite: lp:~charlesk/indicator-location/lp-1435141-fix-false-state
Diff against target: 1041 lines (+408/-536)
9 files modified
CMakeLists.txt (+0/-1)
debian/control (+0/-1)
src/CMakeLists.txt (+1/-1)
src/location-service-controller.cc (+385/-106)
src/location-service-controller.h (+20/-28)
src/main.cc (+2/-2)
tests/CMakeLists.txt (+0/-10)
tests/ualc-mock.c (+0/-152)
tests/ualc-test.cc (+0/-235)
To merge this branch: bzr merge lp:~charlesk/indicator-location/location-service-proxy
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Antti Kaijanmäki (community) Approve
Review via email: mp+256678@code.launchpad.net

Commit message

Create a dbus proxy that talks to location-service to get/set the two location-service-enabled and gps-enabled boolean properties.

Description of the change

Create a dbus proxy that talks to location-service to get/set the two location-service-enabled and gps-enabled boolean properties.

To post a comment you must log in.
159. By Charles Kerr

fix a couple of bugs in the new proxy code

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

in LocationServiceController, remove an unnecessary temporary

161. By Charles Kerr

in LocationServiceController, delete the copy ctor and assignment operator

Revision history for this message
Antti Kaijanmäki (kaijanmaki) wrote :

LGTM.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

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 2014-09-02 16:56:38 +0000
3+++ CMakeLists.txt 2015-04-17 16:51:36 +0000
4@@ -32,7 +32,6 @@
5
6 include (FindPkgConfig)
7 pkg_check_modules (SERVICE_DEPS REQUIRED
8- ubuntu-platform-api>=1.0.0
9 ubuntu-app-launch-2
10 url-dispatcher-1
11 gio-unix-2.0>=2.36
12
13=== modified file 'debian/control'
14--- debian/control 2015-04-17 16:51:36 +0000
15+++ debian/control 2015-04-17 16:51:36 +0000
16@@ -13,7 +13,6 @@
17 libgtest-dev,
18 libubuntu-app-launch2-dev,
19 liburl-dispatcher1-dev,
20- libubuntu-application-api-dev,
21 python,
22 libproperties-cpp-dev,
23 Standards-Version: 3.9.4
24
25=== modified file 'src/CMakeLists.txt'
26--- src/CMakeLists.txt 2014-09-02 16:34:36 +0000
27+++ src/CMakeLists.txt 2015-04-17 16:51:36 +0000
28@@ -17,7 +17,7 @@
29 controller.cc
30 phone.cc
31 service.cc
32- controller-ualc.cc
33+ location-service-controller.cc
34 accounts-service-license-controller.cc
35 license-controller.cc
36 )
37
38=== renamed file 'src/controller-ualc.cc' => 'src/location-service-controller.cc'
39--- src/controller-ualc.cc 2015-04-17 16:51:36 +0000
40+++ src/location-service-controller.cc 2015-04-17 16:51:36 +0000
41@@ -20,109 +20,388 @@
42
43 #include <glib.h>
44
45-#include "controller-ualc.h"
46-
47-UbuntuAppLocController :: UbuntuAppLocController ():
48- ualc (ua_location_service_create_controller ())
49-{
50- g_return_if_fail (ualc != nullptr);
51-
52- // update our status when the ualc changes
53- ua_location_service_controller_set_status_changed_handler (ualc,
54- on_ualc_status_changed,
55- this);
56-
57- update_status();
58-}
59-
60-UbuntuAppLocController :: ~UbuntuAppLocController ()
61-{
62- if (m_update_status_tag)
63- g_source_remove(m_update_status_tag);
64-
65- if (ualc != nullptr)
66- ua_location_service_controller_unref (ualc);
67-}
68-
69-void
70-UbuntuAppLocController :: on_ualc_status_changed (UALocationServiceStatusFlags /*flags*/, void *vself)
71-{
72- static_cast<UbuntuAppLocController*>(vself)->update_status();
73-}
74-
75-void
76-UbuntuAppLocController :: update_status_soon()
77-{
78- if (m_update_status_tag == 0)
79- {
80- constexpr int retry_interval_seconds = 5;
81-
82- auto callback = [](gpointer gself){
83- g_debug("%s periodic check", G_STRLOC);
84- auto self = static_cast<UbuntuAppLocController*>(gself);
85- self->m_update_status_tag = 0;
86- self->update_status();
87- return G_SOURCE_REMOVE;
88- };
89-
90- m_update_status_tag = g_timeout_add_seconds(retry_interval_seconds,
91- callback,
92- this);
93- }
94-}
95-
96-void
97-UbuntuAppLocController :: update_status ()
98-{
99- const bool loc_was_enabled = is_location_service_enabled();
100- const bool gps_was_enabled = is_gps_enabled();
101-
102- // update this.current_status with a fresh ualc status
103- UALocationServiceStatusFlags flags {};
104- auto status = ua_location_service_controller_query_status(ualc, &flags);
105- if (status == U_STATUS_SUCCESS)
106- {
107- g_debug("%s %s updating ualc controller status with flags: %d", G_STRLOC, G_STRFUNC, (int)flags);
108- current_status = flags;
109- m_is_valid.set(true);
110- }
111- else
112- {
113- g_warning("%s %s ualc_query_status returned %d", G_STRLOC, G_STRFUNC, status);
114- m_is_valid.set(false);
115- update_status_soon();
116- }
117-
118- const bool loc_is_enabled = is_location_service_enabled();
119- const bool gps_is_enabled = is_gps_enabled();
120-
121- if (loc_was_enabled != loc_is_enabled)
122- notify_location_service_enabled (loc_is_enabled);
123-
124- if (gps_was_enabled != gps_is_enabled)
125- notify_gps_enabled (gps_is_enabled);
126-}
127-
128-/***
129-****
130-***/
131-
132-void
133-UbuntuAppLocController :: set_gps_enabled (bool enabled)
134-{
135- auto status = enabled ? ua_location_service_controller_enable_gps (ualc)
136- : ua_location_service_controller_disable_gps (ualc);
137-
138- if (status != U_STATUS_SUCCESS)
139- g_warning ("Error turning GPS %s", (enabled?"on":"off"));
140-}
141-
142-void
143-UbuntuAppLocController :: set_location_service_enabled (bool enabled)
144-{
145- auto status = enabled ? ua_location_service_controller_enable_service (ualc)
146- : ua_location_service_controller_disable_service (ualc);
147-
148- if (status != U_STATUS_SUCCESS)
149- g_warning ("Error turning Location Service %s", (enabled?"on":"off"));
150-}
151+#include "location-service-controller.h"
152+#include "utils.h"
153+
154+/***
155+****
156+***/
157+
158+class LocationServiceController::Impl
159+{
160+public:
161+
162+ Impl(LocationServiceController& owner)
163+ {
164+ m_cancellable.reset(g_cancellable_new(), [](GCancellable* c) {
165+ g_cancellable_cancel(c);
166+ g_object_unref(c);
167+ });
168+
169+ m_gps_enabled.changed().connect([&owner](bool b){
170+ owner.notify_gps_enabled(b);
171+ });
172+
173+ m_loc_enabled.changed().connect([&owner](bool b){
174+ owner.notify_location_service_enabled(b);
175+ });
176+
177+ g_bus_get(G_BUS_TYPE_SYSTEM,
178+ m_cancellable.get(),
179+ on_system_bus_ready,
180+ this);
181+ }
182+
183+ ~Impl() =default;
184+
185+ const core::Property<bool>& is_valid() const
186+ {
187+ return m_is_valid;
188+ }
189+
190+ bool is_gps_enabled() const
191+ {
192+ return m_gps_enabled.get();
193+ }
194+
195+ bool is_location_service_enabled() const
196+ {
197+ return m_loc_enabled.get();
198+ }
199+
200+ void set_gps_enabled(bool enabled)
201+ {
202+ set_bool_property(PROP_KEY_GPS_ENABLED, enabled);
203+ }
204+
205+ void set_location_service_enabled(bool enabled)
206+ {
207+ set_bool_property(PROP_KEY_LOC_ENABLED, enabled);
208+ }
209+
210+private:
211+
212+ /***
213+ **** bus bootstrapping & name watching
214+ ***/
215+
216+ static void on_system_bus_ready(GObject*, GAsyncResult* res, gpointer gself)
217+ {
218+ GError* error;
219+ GDBusConnection* system_bus;
220+
221+ error = nullptr;
222+ system_bus = g_bus_get_finish(res, &error);
223+ if (system_bus != nullptr)
224+ {
225+ auto self = static_cast<Impl*>(gself);
226+
227+ self->m_system_bus.reset(system_bus, GObjectDeleter());
228+
229+ auto name_tag = g_bus_watch_name_on_connection(
230+ system_bus,
231+ BUS_NAME,
232+ G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
233+ on_name_appeared,
234+ on_name_vanished,
235+ gself,
236+ nullptr);
237+
238+ // manage the name_tag's lifespan
239+ self->m_name_tag.reset(new guint{name_tag}, [](guint* name_tag){
240+ g_bus_unwatch_name(*name_tag);
241+ delete name_tag;
242+ });
243+ }
244+ else if (error != nullptr)
245+ {
246+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
247+ g_warning("Couldn't get system bus: %s", error->message);
248+ g_error_free(error);
249+ }
250+ }
251+
252+ static void on_name_appeared(GDBusConnection * system_bus,
253+ const gchar * bus_name,
254+ const gchar * name_owner,
255+ gpointer gself)
256+ {
257+ auto self = static_cast<Impl*>(gself);
258+
259+ // Why do we use PropertiesChanged, Get, and Set by hand instead
260+ // of letting gdbus-codegen or g_dbus_proxy_new() do the dirty work?
261+ // ubuntu-location-service's GetAll method is broken; proxies aren't
262+ // able to bootstrap themselves and cache the object properties. Ugh.
263+
264+ // subscribe to PropertiesChanged signals from the service
265+ auto signal_tag = g_dbus_connection_signal_subscribe(
266+ system_bus,
267+ name_owner,
268+ PROP_IFACE_NAME,
269+ "PropertiesChanged",
270+ OBJECT_PATH,
271+ nullptr, // arg0
272+ G_DBUS_SIGNAL_FLAGS_NONE,
273+ on_properties_changed,
274+ gself,
275+ nullptr);
276+
277+ // manage the signal_tag lifespan
278+ g_object_ref(system_bus);
279+ self->m_signal_tag.reset(new guint{signal_tag},
280+ [system_bus](guint* tag){
281+ g_dbus_connection_signal_unsubscribe(system_bus, *tag);
282+ g_object_unref(system_bus);
283+ delete tag;
284+ });
285+
286+ // GetAll is borked so call Get on each property we care about
287+ struct {
288+ const char * name;
289+ GAsyncReadyCallback callback;
290+ } props[] = {
291+ { PROP_KEY_LOC_ENABLED, on_loc_enabled_reply },
292+ { PROP_KEY_GPS_ENABLED, on_gps_enabled_reply }
293+ };
294+ for (const auto& prop : props)
295+ {
296+ g_dbus_connection_call(
297+ system_bus,
298+ BUS_NAME,
299+ OBJECT_PATH,
300+ PROP_IFACE_NAME,
301+ "Get",
302+ g_variant_new("(ss)", LOC_IFACE_NAME, prop.name), // args
303+ G_VARIANT_TYPE("(v)"), // return type
304+ G_DBUS_CALL_FLAGS_NONE,
305+ -1, // use default timeout
306+ self->m_cancellable.get(),
307+ prop.callback,
308+ gself);
309+ }
310+
311+ g_debug("setting is_valid to true: location-service appeared");
312+ self->m_is_valid.set(true);
313+ }
314+
315+ static void on_name_vanished(GDBusConnection*, const gchar*, gpointer gself)
316+ {
317+ auto self = static_cast<Impl*>(gself);
318+
319+ g_debug("setting is_valid to false: location-service vanished");
320+ self->m_is_valid.set(false);
321+ self->m_signal_tag.reset();
322+ }
323+
324+ /***
325+ **** org.freedesktop.dbus.properties.PropertiesChanged handling
326+ ***/
327+
328+ static void on_properties_changed(GDBusConnection * /*connection*/,
329+ const gchar * /*sender_name*/,
330+ const gchar * /*object_path*/,
331+ const gchar * /*interface_name*/,
332+ const gchar * /*signal_name*/,
333+ GVariant * parameters,
334+ gpointer gself)
335+ {
336+ auto self = static_cast<Impl*>(gself);
337+ const gchar *interface_name;
338+ GVariant *changed_properties;
339+ const gchar **invalidated_properties;
340+ GVariantIter property_iter;
341+ const gchar *key;
342+ GVariant* val;
343+
344+ g_variant_get(parameters,
345+ "(&s@a{sv}^a&s)",
346+ &interface_name,
347+ &changed_properties,
348+ &invalidated_properties);
349+
350+ g_variant_iter_init(&property_iter, changed_properties);
351+ while (g_variant_iter_next(&property_iter, "{&sv}", &key, &val))
352+ {
353+ if (!g_strcmp0(key, PROP_KEY_LOC_ENABLED))
354+ self->m_loc_enabled.set(g_variant_get_boolean(val));
355+ else if (!g_strcmp0(key, PROP_KEY_GPS_ENABLED))
356+ self->m_gps_enabled.set(g_variant_get_boolean(val));
357+
358+ g_variant_unref(val);
359+ }
360+
361+ g_variant_unref(changed_properties);
362+ g_free(invalidated_properties);
363+ }
364+
365+ /***
366+ **** org.freedesktop.dbus.properties.Get handling
367+ ***/
368+
369+ static std::tuple<bool,bool> get_bool_reply_from_call(GObject * source,
370+ GAsyncResult * res)
371+ {
372+ GError * error;
373+ GVariant * v;
374+ bool success {false};
375+ bool result {false};
376+
377+ error = nullptr;
378+ GDBusConnection * conn = G_DBUS_CONNECTION(source);
379+ v = g_dbus_connection_call_finish(conn, res, &error);
380+ if (v != nullptr)
381+ {
382+ GVariant* inner {};
383+ g_variant_get(v, "(v)", &inner);
384+ success = true;
385+ result = g_variant_get_boolean(inner);
386+ g_variant_unref(inner);
387+ g_variant_unref(v);
388+ }
389+ else if (error != nullptr)
390+ {
391+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
392+ g_warning("Error calling dbus method: %s", error->message);
393+ g_error_free(error);
394+ success = false;
395+ }
396+
397+ return std::make_tuple(success, result);
398+ }
399+
400+ static void on_loc_enabled_reply(GObject * source_object,
401+ GAsyncResult * res,
402+ gpointer gself)
403+ {
404+ bool success, value;
405+ std::tie(success, value) = get_bool_reply_from_call(source_object, res);
406+ g_debug("service loc reply: success %d value %d", (int)success, (int)value);
407+ if (success)
408+ static_cast<Impl*>(gself)->m_loc_enabled.set(value);
409+ }
410+
411+ static void on_gps_enabled_reply(GObject * source_object,
412+ GAsyncResult * res,
413+ gpointer gself)
414+ {
415+ bool success, value;
416+ std::tie(success, value) = get_bool_reply_from_call(source_object, res);
417+ g_debug("service gps reply: success %d value %d", (int)success, (int)value);
418+ if (success)
419+ static_cast<Impl*>(gself)->m_gps_enabled.set(value);
420+ }
421+
422+ /***
423+ **** org.freedesktop.dbus.properties.Set handling
424+ ***/
425+
426+ void set_bool_property(const char* property_name, bool b)
427+ {
428+ g_return_if_fail(m_system_bus);
429+
430+ auto args = g_variant_new("(ssv)",
431+ LOC_IFACE_NAME,
432+ property_name,
433+ g_variant_new_boolean(b));
434+ g_dbus_connection_call(m_system_bus.get(),
435+ BUS_NAME,
436+ OBJECT_PATH,
437+ PROP_IFACE_NAME,
438+ "Set", // method name,
439+ args,
440+ nullptr, // reply type
441+ G_DBUS_CALL_FLAGS_NONE,
442+ -1, // timeout msec
443+ m_cancellable.get(),
444+ check_method_call_reply,
445+ this);
446+ }
447+
448+ static void check_method_call_reply(GObject *connection,
449+ GAsyncResult *res,
450+ gpointer gself)
451+ {
452+ GError * error;
453+ GVariant * v;
454+
455+ error = nullptr;
456+ v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(connection), res, &error);
457+ if (v != nullptr)
458+ {
459+ auto vs = g_variant_print(v, true);
460+ g_debug("method call returned '%s'", vs);
461+ g_free(vs);
462+ g_variant_unref(v);
463+ }
464+ else if (error != nullptr)
465+ {
466+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
467+ g_warning("dbus method returned an error : %s", error->message);
468+
469+ g_error_free(error);
470+ }
471+ }
472+
473+ /***
474+ ****
475+ ***/
476+
477+ static constexpr const char* BUS_NAME {"com.ubuntu.location.Service"};
478+ static constexpr const char* OBJECT_PATH {"/com/ubuntu/location/Service"};
479+ static constexpr const char* LOC_IFACE_NAME {"com.ubuntu.location.Service"};
480+ static constexpr const char* PROP_IFACE_NAME {"org.freedesktop.DBus.Properties"};
481+ static constexpr const char* PROP_KEY_LOC_ENABLED {"IsOnline"};
482+ static constexpr const char* PROP_KEY_GPS_ENABLED {"DoesSatelliteBasedPositioning"};
483+
484+ core::Property<bool> m_gps_enabled {false};
485+ core::Property<bool> m_loc_enabled {false};
486+ core::Property<bool> m_is_valid {false};
487+
488+ std::shared_ptr<GCancellable> m_cancellable {};
489+ std::shared_ptr<GDBusConnection> m_system_bus {};
490+ std::shared_ptr<guint> m_name_tag {};
491+ std::shared_ptr<guint> m_signal_tag {};
492+};
493+
494+/***
495+****
496+***/
497+
498+LocationServiceController::LocationServiceController():
499+ impl{new Impl{*this}}
500+{
501+}
502+
503+LocationServiceController::~LocationServiceController()
504+{
505+}
506+
507+const core::Property<bool>&
508+LocationServiceController::is_valid() const
509+{
510+ return impl->is_valid();
511+}
512+
513+bool
514+LocationServiceController::is_gps_enabled() const
515+{
516+ return impl->is_gps_enabled();
517+}
518+
519+bool
520+LocationServiceController::is_location_service_enabled() const
521+{
522+ return impl->is_location_service_enabled();
523+}
524+void
525+LocationServiceController::set_gps_enabled(bool enabled)
526+{
527+ impl->set_gps_enabled(enabled);
528+}
529+
530+void
531+LocationServiceController::set_location_service_enabled(bool enabled)
532+{
533+ impl->set_location_service_enabled(enabled);
534+}
535+
536
537=== renamed file 'src/controller-ualc.h' => 'src/location-service-controller.h'
538--- src/controller-ualc.h 2015-04-17 16:51:36 +0000
539+++ src/location-service-controller.h 2015-04-17 16:51:36 +0000
540@@ -17,41 +17,33 @@
541 * Charles Kerr <charles.kerr@canonical.com>
542 */
543
544-#ifndef __INDICATOR_LOCATION_CONTROLLER_UALC__H__
545-#define __INDICATOR_LOCATION_CONTROLLER_UALC__H__
546-
547-#include <ubuntu/status.h>
548-#include <ubuntu/application/location/controller.h>
549-#include <ubuntu/application/location/service.h>
550+#ifndef INDICATOR_LOCATION_CONTROLLER_LOCATION_SERVICE
551+#define INDICATOR_LOCATION_CONTROLLER_LOCATION_SERVICE
552
553 #include "controller.h"
554
555-class UbuntuAppLocController: public Controller
556+#include <memory> // std::unique_ptr
557+
558+class LocationServiceController: public Controller
559 {
560 public:
561-
562- UbuntuAppLocController ();
563- virtual ~UbuntuAppLocController();
564-
565- virtual const core::Property<bool>& is_valid() const override { return m_is_valid; }
566- bool is_gps_enabled () const { return (current_status & UA_LOCATION_SERVICE_GPS_ENABLED) != 0; }
567- bool is_location_service_enabled () const { return (current_status & UA_LOCATION_SERVICE_ENABLED) != 0; }
568-
569- void set_gps_enabled (bool enabled);
570- void set_location_service_enabled (bool enabled);
571+ LocationServiceController();
572+ virtual ~LocationServiceController();
573+
574+ virtual const core::Property<bool>& is_valid() const override;
575+ bool is_gps_enabled () const override;
576+ bool is_location_service_enabled () const override;
577+ void set_gps_enabled (bool enabled) override;
578+ void set_location_service_enabled (bool enabled) override;
579+
580+ LocationServiceController(const LocationServiceController&) =delete;
581+ LocationServiceController& operator=(const LocationServiceController&) =delete;
582
583 private:
584-
585- UALocationServiceStatusFlags current_status {};
586- UbuntuApplicationLocationServiceController* ualc {};
587- core::Property<bool> m_is_valid {false};
588- unsigned int m_update_status_tag {};
589-
590- static void on_ualc_status_changed (UALocationServiceStatusFlags, void *vself);
591- void update_status();
592- void update_status_soon();
593+ friend class Impl;
594+ class Impl;
595+ std::unique_ptr<Impl> impl;
596 };
597
598-#endif // __INDICATOR_LOCATION_CONTROLLER_UALC__H__
599-
600+#endif // INDICATOR_LOCATION_CONTROLLER_LOCATION_SERVICE
601
602
603=== modified file 'src/main.cc'
604--- src/main.cc 2014-09-02 16:34:36 +0000
605+++ src/main.cc 2015-04-17 16:51:36 +0000
606@@ -22,7 +22,7 @@
607 #include <glib.h>
608
609 #include "accounts-service-license-controller.h"
610-#include "controller-ualc.h"
611+#include "location-service-controller.h"
612 #include "service.h"
613
614 static void
615@@ -43,7 +43,7 @@
616
617 /* set up the service */
618 loop = g_main_loop_new (nullptr, false);
619- std::shared_ptr<Controller> controller (new UbuntuAppLocController ());
620+ auto controller = std::make_shared<LocationServiceController>();
621 auto license_controller = std::make_shared<AccountsServiceLicenseController>();
622 Service service (controller, license_controller);
623 service.set_name_lost_callback (on_name_lost, loop);
624
625=== modified file 'tests/CMakeLists.txt'
626--- tests/CMakeLists.txt 2014-05-27 15:34:07 +0000
627+++ tests/CMakeLists.txt 2015-04-17 16:51:36 +0000
628@@ -30,16 +30,6 @@
629 target_link_libraries (${TEST_NAME} ${SERVICE_LIB} gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
630
631 ###
632-### ualc-test
633-###
634-
635-set (TEST_NAME ualc-test)
636-add_executable (${TEST_NAME} ${TEST_NAME}.cc ualc-mock.c)
637-add_test (${TEST_NAME} ${TEST_NAME})
638-add_dependencies (${TEST_NAME} ${SERVICE_LIB})
639-target_link_libraries (${TEST_NAME} ${SERVICE_LIB} gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
640-
641-###
642 ### globals
643 ###
644
645
646=== removed file 'tests/ualc-mock.c'
647--- tests/ualc-mock.c 2014-01-27 00:03:50 +0000
648+++ tests/ualc-mock.c 1970-01-01 00:00:00 +0000
649@@ -1,152 +0,0 @@
650-/*
651- * Copyright 2014 Canonical Ltd.
652- *
653- * This program is free software; you can redistribute it and/or modify
654- * it under the terms of the GNU General Public License as published by
655- * the Free Software Foundation; version 3.
656- *
657- * This program is distributed in the hope that it will be useful,
658- * but WITHOUT ANY WARRANTY; without even the implied warranty of
659- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
660- * GNU Lesser General Public License for more details.
661- *
662- * You should have received a copy of the GNU Lesser General Public License
663- * along with this program. If not, see <http://www.gnu.org/licenses/>.
664- *
665- * Authors:
666- * Charles Kerr <charles.kerr@canonical.com>
667- */
668-
669-#include <ubuntu/status.h>
670-#include <ubuntu/application/location/controller.h>
671-#include <ubuntu/application/location/service.h>
672-
673-#include <glib.h>
674-
675-static UALocationServiceController * singleton = NULL;
676-
677-struct UbuntuApplicationLocationServiceController
678-{
679- int ref_count;
680- gboolean loc_enabled;
681- gboolean gps_enabled;
682- UALocationServiceStatusChangedHandler changed_handler;
683- void * changed_handler_context;
684-};
685-
686-UALocationServiceController*
687-ua_location_service_create_controller(void)
688-{
689- if (singleton == NULL)
690- {
691- UALocationServiceController * c = g_new0 (UALocationServiceController, 1);
692- c->loc_enabled = FALSE;
693- c->gps_enabled = FALSE;
694- singleton = c;
695- }
696-
697- singleton->ref_count++;
698-
699- return singleton;
700-}
701-
702-void
703-ua_location_service_controller_set_status_changed_handler (UALocationServiceController *self,
704- UALocationServiceStatusChangedHandler changed_handler,
705- void *changed_handler_context)
706-{
707- self->changed_handler = changed_handler;
708- self->changed_handler_context = changed_handler_context;
709-}
710-
711-static void
712-notify_status_changed (UALocationServiceController *self)
713-{
714- if (self->changed_handler != NULL)
715- {
716- UALocationServiceStatusFlags flags = 0;
717- ua_location_service_controller_query_status(self, &flags);
718- self->changed_handler (flags, self->changed_handler_context);
719- }
720-}
721-
722-UStatus
723-ua_location_service_controller_query_status(UALocationServiceController *self,
724- UALocationServiceStatusFlags *out_flags)
725-{
726- UALocationServiceStatusFlags out = 0;
727-
728- g_return_val_if_fail (self != NULL, U_STATUS_ERROR);
729- g_return_val_if_fail (self->ref_count >= 1, U_STATUS_ERROR);
730- g_return_val_if_fail (out_flags != NULL, U_STATUS_ERROR);
731-
732- if (self->loc_enabled)
733- out |= UA_LOCATION_SERVICE_ENABLED;
734-
735- if (self->gps_enabled)
736- out |= UA_LOCATION_SERVICE_GPS_ENABLED;
737-
738- *out_flags = out;
739- return U_STATUS_SUCCESS;
740-}
741-
742-void
743-ua_location_service_controller_unref (UALocationServiceController *self)
744-{
745- g_return_if_fail (self != NULL);
746- g_return_if_fail (self->ref_count >= 1);
747-
748- if (!--self->ref_count)
749- {
750- g_free (self);
751- singleton = NULL;
752- }
753-}
754-
755-static UStatus
756-set_gps_enabled (UALocationServiceController *self, gboolean enabled)
757-{
758- g_return_if_fail (self != NULL);
759- g_return_if_fail (self->ref_count >= 1);
760-
761- self->gps_enabled = enabled;
762- notify_status_changed (self);
763-
764- return U_STATUS_SUCCESS;
765-}
766-
767-UStatus
768-ua_location_service_controller_enable_gps (UALocationServiceController *self)
769-{
770- return set_gps_enabled (self, TRUE);
771-}
772-
773-UStatus
774-ua_location_service_controller_disable_gps (UALocationServiceController *self)
775-{
776- return set_gps_enabled (self, FALSE);
777-}
778-
779-static UStatus
780-set_service_enabled (UALocationServiceController *self, gboolean enabled)
781-{
782- g_return_if_fail (self != NULL);
783- g_return_if_fail (self->ref_count >= 1);
784-
785- self->loc_enabled = enabled;
786- notify_status_changed (self);
787-
788- return U_STATUS_SUCCESS;
789-}
790-
791-UStatus
792-ua_location_service_controller_enable_service (UALocationServiceController *self)
793-{
794- return set_service_enabled (self, TRUE);
795-}
796-
797-UStatus
798-ua_location_service_controller_disable_service (UALocationServiceController *self)
799-{
800- return set_service_enabled (self, FALSE);
801-}
802
803=== removed file 'tests/ualc-test.cc'
804--- tests/ualc-test.cc 2014-01-27 00:15:30 +0000
805+++ tests/ualc-test.cc 1970-01-01 00:00:00 +0000
806@@ -1,235 +0,0 @@
807-/*
808- * Copyright 2014 Canonical Ltd.
809- *
810- * This program is free software; you can redistribute it and/or modify
811- * it under the terms of the GNU General Public License as published by
812- * the Free Software Foundation; version 3.
813- *
814- * This program is distributed in the hope that it will be useful,
815- * but WITHOUT ANY WARRANTY; without even the implied warranty of
816- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
817- * GNU Lesser General Public License for more details.
818- *
819- * You should have received a copy of the GNU Lesser General Public License
820- * along with this program. If not, see <http://www.gnu.org/licenses/>.
821- *
822- * Authors:
823- * Charles Kerr <charles.kerr@canonical.com>
824- */
825-
826-#include "src/controller-ualc.h"
827-
828-#include <ubuntu/status.h>
829-#include <ubuntu/application/location/controller.h>
830-#include <ubuntu/application/location/service.h>
831-
832-#include <gtest/gtest.h>
833-
834-#include <glib.h>
835-
836-/***
837-****
838-***/
839-
840-class UalcFixture: public ::testing::Test
841-{
842- typedef ::testing::Test super;
843-
844- protected:
845-
846- GMainLoop * loop = nullptr;
847- UALocationServiceController * ualc = nullptr;
848-
849- virtual void SetUp()
850- {
851- super::SetUp();
852-
853- loop = g_main_loop_new(nullptr, FALSE);
854- ualc = ua_location_service_create_controller();
855- }
856-
857- virtual void TearDown()
858- {
859- g_clear_pointer(&ualc, ua_location_service_controller_unref);
860- g_clear_pointer(&loop, g_main_loop_unref);
861-
862- super::TearDown();
863- }
864-};
865-
866-
867-/***
868-**** Sanity check for build + fixture
869-***/
870-
871-TEST_F(UalcFixture, HelloWorld)
872-{
873- EXPECT_TRUE(true);
874- EXPECT_FALSE(false);
875-}
876-
877-
878-/***
879-**** Simple test to make sure our mock UALC is behaving sanely
880-***/
881-
882-namespace
883-{
884- UALocationServiceStatusFlags simple_mock_test_flags = 0;
885-
886- void * simple_mock_test_context = (void*)(0xDEADBEEF);
887-
888- void simple_mock_test_changed_handler(UALocationServiceStatusFlags flags, void * c)
889- {
890- g_assert(c == simple_mock_test_context);
891- simple_mock_test_flags = flags;
892- }
893-}
894-
895-TEST_F(UalcFixture, SimpleMockTest)
896-{
897- ua_location_service_controller_set_status_changed_handler(
898- ualc, simple_mock_test_changed_handler, simple_mock_test_context);
899-
900- UALocationServiceStatusFlags flags = 0;
901- EXPECT_EQ(0, simple_mock_test_flags);
902- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
903- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_service(ualc));
904- EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, simple_mock_test_flags);
905- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
906- EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, flags);
907- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_gps(ualc));
908- EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, simple_mock_test_flags);
909- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
910- EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, flags);
911-}
912-
913-/***
914-**** Now, actual controller tests
915-***/
916-
917-TEST_F(UalcFixture, ControllerAccessors)
918-{
919- UbuntuAppLocController c;
920- EXPECT_FALSE(c.is_location_service_enabled());
921- EXPECT_FALSE(c.is_gps_enabled());
922-
923- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_service(ualc));
924- EXPECT_TRUE(c.is_location_service_enabled());
925- EXPECT_FALSE(c.is_gps_enabled());
926-
927- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_enable_gps(ualc));
928- EXPECT_TRUE(c.is_location_service_enabled());
929- EXPECT_TRUE(c.is_gps_enabled());
930-
931- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_disable_service(ualc));
932- EXPECT_FALSE(c.is_location_service_enabled());
933- EXPECT_TRUE(c.is_gps_enabled());
934-
935- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_disable_gps(ualc));
936- EXPECT_FALSE(c.is_location_service_enabled());
937- EXPECT_FALSE(c.is_gps_enabled());
938-}
939-
940-TEST_F(UalcFixture, ControllerMutators)
941-{
942- UbuntuAppLocController c;
943- UALocationServiceStatusFlags flags;
944-
945- flags = 0;
946- EXPECT_FALSE(c.is_location_service_enabled());
947- EXPECT_FALSE(c.is_gps_enabled());
948- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
949- EXPECT_EQ(0, flags);
950-
951- // enable gps
952- c.set_gps_enabled(true);
953- EXPECT_FALSE(c.is_location_service_enabled());
954- EXPECT_TRUE(c.is_gps_enabled());
955- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
956- EXPECT_EQ(UA_LOCATION_SERVICE_GPS_ENABLED, flags);
957-
958- // disable gps
959- c.set_gps_enabled(false);
960- EXPECT_FALSE(c.is_location_service_enabled());
961- EXPECT_FALSE(c.is_gps_enabled());
962- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
963- EXPECT_EQ(0, flags);
964-
965- // enable service
966- c.set_location_service_enabled(true);
967- EXPECT_TRUE(c.is_location_service_enabled());
968- EXPECT_FALSE(c.is_gps_enabled());
969- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
970- EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED, flags);
971-
972- // disable service
973- c.set_location_service_enabled(false);
974- EXPECT_FALSE(c.is_location_service_enabled());
975- EXPECT_FALSE(c.is_gps_enabled());
976- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
977- EXPECT_EQ(0, flags);
978-
979- // enable both
980- c.set_gps_enabled(true);
981- c.set_location_service_enabled(true);
982- EXPECT_TRUE(c.is_location_service_enabled());
983- EXPECT_TRUE(c.is_gps_enabled());
984- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
985- EXPECT_EQ(UA_LOCATION_SERVICE_ENABLED | UA_LOCATION_SERVICE_GPS_ENABLED, flags);
986-
987- // disable both
988- c.set_gps_enabled(false);
989- c.set_location_service_enabled(false);
990- EXPECT_FALSE(c.is_location_service_enabled());
991- EXPECT_FALSE(c.is_gps_enabled());
992- EXPECT_EQ(U_STATUS_SUCCESS, ua_location_service_controller_query_status(ualc, &flags));
993- EXPECT_EQ(0, flags);
994-}
995-
996-/***
997-****
998-***/
999-
1000-namespace
1001-{
1002- struct MockListener: public ControllerListener {
1003- bool last_gps = false;
1004- bool last_service = false;
1005- bool gps_tickled = false;
1006- bool service_tickled = false;
1007- void reset() { gps_tickled = service_tickled = last_gps = last_service = false; }
1008- void on_gps_enabled_changed (bool b) { gps_tickled = true; last_gps = b; }
1009- void on_location_service_enabled_changed (bool b) { service_tickled = true; last_service = b; }
1010- };
1011-
1012-} // unnamed namespace
1013-
1014-TEST_F(UalcFixture, ControllerListeners)
1015-{
1016- UbuntuAppLocController c;
1017- MockListener l;
1018- c.add_listener(&l);
1019- EXPECT_FALSE(l.service_tickled);
1020- EXPECT_FALSE(l.gps_tickled);
1021-
1022- // turn on the gps...
1023- c.set_gps_enabled(true);
1024- EXPECT_FALSE(l.service_tickled);
1025- EXPECT_TRUE(l.gps_tickled);
1026- EXPECT_TRUE(l.last_gps);
1027- l.reset();
1028-
1029- // turn on the location service...
1030- c.set_location_service_enabled(true);
1031- EXPECT_FALSE(l.gps_tickled);
1032- EXPECT_TRUE(l.service_tickled);
1033- EXPECT_TRUE(l.last_service);
1034- l.reset();
1035-
1036- // service is /already/ enabled so turning
1037- // it on again shouldn't trigger any changes
1038- c.set_location_service_enabled(true);
1039- EXPECT_FALSE(l.service_tickled);
1040- EXPECT_FALSE(l.gps_tickled);
1041-}

Subscribers

People subscribed via source and target branches