Merge lp:~charlesk/indicator-power/use-libnotify-for-audio into lp:indicator-power

Proposed by Charles Kerr on 2016-05-16
Status: Merged
Approved by: Ted Gould on 2016-05-16
Approved revision: 300
Merged at revision: 294
Proposed branch: lp:~charlesk/indicator-power/use-libnotify-for-audio
Merge into: lp:indicator-power
Diff against target: 1409 lines (+239/-728)
16 files modified
CMakeLists.txt (+11/-5)
debian/control (+0/-1)
src/CMakeLists.txt (+1/-8)
src/main.c (+1/-5)
src/notifier.c (+18/-76)
src/notifier.h (+1/-5)
src/sound-player-gst.c (+0/-173)
src/sound-player-gst.h (+0/-70)
src/sound-player.c (+0/-45)
src/sound-player.h (+0/-72)
tests/CMakeLists.txt (+2/-9)
tests/glib-fixture.h (+145/-58)
tests/sound-player-mock.c (+0/-94)
tests/sound-player-mock.h (+0/-75)
tests/test-device.cc (+2/-2)
tests/test-notify.cc (+58/-30)
To merge this branch: bzr merge lp:~charlesk/indicator-power/use-libnotify-for-audio
Reviewer Review Type Date Requested Status
Ted Gould (community) 2016-05-16 Approve on 2016-05-16
PS Jenkins bot (community) continuous-integration Needs Fixing on 2016-05-16
Review via email: mp+294834@code.launchpad.net

Commit message

Use the fdo 'sound-file' hint to play a sound when a low battery notification is shown.

Description of the change

Use the fdo 'sound-file' hint to play a sound when a low battery notification is shown. Previously the code was doing a lot of unnecessary work by playing it directly via gst.

Minor secondary work: fixing cmake warnings, syncing glib test fixture with indicator-datetime

To post a comment you must log in.
Ted Gould (ted) wrote :

Love deleting code! Probably should check on the copyright issue, kinda silly, but might effect from where you sync'd from as well.

review: Approve
301. By Charles Kerr on 2016-05-16

fix glib-fixture.h copyright year in header comment

Charles Kerr (charlesk) wrote :

Whoops, yep looks like the version in indicator-datetime didn't get updated. Fixed r301.

This kind of error wouldn't happen if I stored this header in some common area. :P

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 2016-01-02 01:37:51 +0000
3+++ CMakeLists.txt 2016-05-16 21:28:42 +0000
4@@ -3,16 +3,23 @@
5
6 list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
7
8-set(PROJECT_VERSION "14.10.0")
9+set(PROJECT_VERSION "16.10.0")
10 set(PACKAGE ${CMAKE_PROJECT_NAME})
11 set(GETTEXT_PACKAGE "indicator-power")
12-add_definitions (-DGETTEXT_PACKAGE="${GETTEXT_PACKAGE}"
13- -DGNOMELOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}"
14- -DLOW_BATTERY_SOUND="Low battery.ogg")
15+add_definitions(
16+ -DGETTEXT_PACKAGE="${GETTEXT_PACKAGE}"
17+ -DG_LOG_DOMAIN="${GETTEXT_PACKAGE}"
18+ -DGNOMELOCALEDIR="${CMAKE_INSTALL_FULL_LOCALEDIR}"
19+ -DLOW_BATTERY_SOUND="Low battery.ogg"
20+)
21
22 option (enable_tests "Build the package's automatic tests." ON)
23 option (enable_lcov "Generate lcov code coverage reports." ON)
24
25+set(SERVICE_LIB "indicatorpowerservice")
26+set(SERVICE_EXEC "indicator-power-service")
27+add_definitions(-DSERVICE_EXEC="${SERVICE_EXEC}")
28+
29 ##
30 ## GNU standard installation directories
31 ##
32@@ -39,7 +46,6 @@
33 gio-unix-2.0>=2.36
34 gudev-1.0>=204
35 libnotify>=0.7.6
36- gstreamer-1.0>=1.2
37 url-dispatcher-1>=1)
38
39 include_directories (SYSTEM ${SERVICE_DEPS_INCLUDE_DIRS})
40
41=== modified file 'debian/control'
42--- debian/control 2016-01-04 15:38:01 +0000
43+++ debian/control 2016-05-16 21:28:42 +0000
44@@ -7,7 +7,6 @@
45 libglib2.0-dev (>= 2.36),
46 libgudev-1.0-dev,
47 liburl-dispatcher1-dev,
48- libgstreamer1.0-dev,
49 python:any,
50 # for com.ubuntu.touch.AccountsService.Sound.xml
51 accountsservice-ubuntu-schemas,
52
53=== modified file 'src/CMakeLists.txt'
54--- src/CMakeLists.txt 2016-01-04 16:19:59 +0000
55+++ src/CMakeLists.txt 2016-05-16 21:28:42 +0000
56@@ -1,8 +1,3 @@
57-set (SERVICE_LIB "indicatorpowerservice")
58-set (SERVICE_EXEC "indicator-power-service")
59-
60-add_definitions(-DG_LOG_DOMAIN="Indicator-Power")
61-
62 # handwritten sources
63 set(SERVICE_MANUAL_SOURCES
64 brightness.c
65@@ -13,9 +8,7 @@
66 device.c
67 notifier.c
68 testing.c
69- service.c
70- sound-player.c
71- sound-player-gst.c)
72+ service.c)
73
74 # generated sources
75 include(GdbusCodegen)
76
77=== modified file 'src/main.c'
78--- src/main.c 2016-01-02 00:49:13 +0000
79+++ src/main.c 2016-05-16 21:28:42 +0000
80@@ -25,7 +25,6 @@
81 #include "device.h"
82 #include "notifier.h"
83 #include "service.h"
84-#include "sound-player-gst.h"
85 #include "testing.h"
86
87 /***
88@@ -42,7 +41,6 @@
89 int
90 main (int argc G_GNUC_UNUSED, char ** argv G_GNUC_UNUSED)
91 {
92- IndicatorPowerSoundPlayer * sound_player;
93 IndicatorPowerNotifier * notifier;
94 IndicatorPowerService * service;
95 IndicatorPowerTesting * testing;
96@@ -54,8 +52,7 @@
97 textdomain (GETTEXT_PACKAGE);
98
99 /* run */
100- sound_player = indicator_power_sound_player_gst_new ();
101- notifier = indicator_power_notifier_new (sound_player);
102+ notifier = indicator_power_notifier_new();
103 service = indicator_power_service_new(NULL, notifier);
104 testing = indicator_power_testing_new (service);
105 loop = g_main_loop_new (NULL, FALSE);
106@@ -68,6 +65,5 @@
107 g_clear_object (&testing);
108 g_clear_object (&service);
109 g_clear_object (&notifier);
110- g_clear_object (&sound_player);
111 return 0;
112 }
113
114=== modified file 'src/notifier.c'
115--- src/notifier.c 2016-01-04 20:25:56 +0000
116+++ src/notifier.c 2016-05-16 21:28:42 +0000
117@@ -22,7 +22,6 @@
118 #include "dbus-battery.h"
119 #include "dbus-shared.h"
120 #include "notifier.h"
121-#include "sound-player.h"
122
123 #include <url-dispatcher.h>
124
125@@ -49,12 +48,10 @@
126 {
127 PROP_0,
128 PROP_BATTERY,
129- PROP_SOUND_PLAYER,
130 LAST_PROP
131 };
132
133 #define PROP_BATTERY_NAME "battery"
134-#define PROP_SOUND_PLAYER_NAME "sound-player"
135
136 static GParamSpec * properties[LAST_PROP];
137
138@@ -83,8 +80,6 @@
139 gboolean caps_queried;
140 gboolean actions_supported;
141
142- IndicatorPowerSoundPlayer * sound_player;
143-
144 GCancellable * cancellable;
145 DbusAccountsServiceSound * accounts_service_sound_proxy;
146 gboolean accounts_service_sound_proxy_pending;
147@@ -189,34 +184,6 @@
148 && dbus_accounts_service_sound_get_silent_mode(p->accounts_service_sound_proxy);
149 }
150
151-static void
152-play_low_battery_sound (IndicatorPowerNotifier * self)
153-{
154- const gchar * const key = LOW_BATTERY_SOUND;
155- gchar * filename;
156- priv_t * const p = get_priv(self);
157-
158- /* can't play? */
159- g_return_if_fail (p->sound_player != NULL);
160-
161- /* won't play? */
162- if (silent_mode(self))
163- return;
164-
165- filename = datafile_find(DATAFILE_TYPE_SOUND, key);
166- if (filename != NULL)
167- {
168- gchar * uri = g_filename_to_uri(filename, NULL, NULL);
169- indicator_power_sound_player_play_uri (p->sound_player, uri);
170- g_free(uri);
171- g_free(filename);
172- }
173- else
174- {
175- g_warning("Unable to find '%s' in XDG data dirs", key);
176- }
177-}
178-
179 /***
180 **** Notifications
181 ***/
182@@ -331,6 +298,21 @@
183
184 if (are_actions_supported(self))
185 {
186+ if (!silent_mode(self))
187+ {
188+ gchar* filename = datafile_find(DATAFILE_TYPE_SOUND, LOW_BATTERY_SOUND);
189+ if (filename != NULL)
190+ {
191+ gchar * uri = g_filename_to_uri(filename, NULL, NULL);
192+ notify_notification_set_hint(nn, "sound-file", g_variant_new_take_string(uri));
193+ g_clear_pointer(&filename, g_free);
194+ }
195+ else
196+ {
197+ g_warning("Unable to find '%s' in XDG data dirs", LOW_BATTERY_SOUND);
198+ }
199+ }
200+
201 notify_notification_set_hint(nn, "x-canonical-snap-decisions", g_variant_new_string("true"));
202 notify_notification_set_hint(nn, "x-canonical-non-shaped-icon", g_variant_new_string("true"));
203 notify_notification_set_hint(nn, "x-canonical-private-affirmative-tint", g_variant_new_string("true"));
204@@ -387,7 +369,6 @@
205 ((new_power_level != POWER_LEVEL_OK) && new_discharging && !old_discharging))
206 {
207 notification_show (self);
208- play_low_battery_sound (self);
209 }
210 else if (!new_discharging || (new_power_level == POWER_LEVEL_OK))
211 {
212@@ -418,10 +399,6 @@
213 g_value_set_object (value, p->battery);
214 break;
215
216- case PROP_SOUND_PLAYER:
217- g_value_set_object (value, p->sound_player);
218- break;
219-
220 default:
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
222 }
223@@ -441,10 +418,6 @@
224 indicator_power_notifier_set_battery (self, g_value_get_object(value));
225 break;
226
227- case PROP_SOUND_PLAYER:
228- indicator_power_notifier_set_sound_player (self, g_value_get_object(value));
229- break;
230-
231 default:
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (o, property_id, pspec);
233 }
234@@ -463,7 +436,6 @@
235 }
236
237 indicator_power_notifier_set_bus (self, NULL);
238- indicator_power_notifier_set_sound_player (self, NULL);
239 notification_clear (self);
240 indicator_power_notifier_set_battery (self, NULL);
241 g_clear_object (&p->dbus_battery);
242@@ -499,7 +471,7 @@
243
244 p->cancellable = g_cancellable_new();
245
246- if (!instance_count++ && !notify_init("indicator-power-service"))
247+ if (!instance_count++ && !notify_init(SERVICE_EXEC))
248 g_critical("Unable to initialize libnotify! Notifications might not be shown.");
249
250 p->accounts_service_sound_proxy_pending = TRUE;
251@@ -532,13 +504,6 @@
252 G_TYPE_OBJECT,
253 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
254
255- properties[PROP_SOUND_PLAYER] = g_param_spec_object (
256- PROP_SOUND_PLAYER_NAME,
257- "Sound Player",
258- "The current sound player",
259- G_TYPE_OBJECT,
260- G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
261-
262 g_object_class_install_properties (object_class, LAST_PROP, properties);
263 }
264
265@@ -547,12 +512,9 @@
266 ***/
267
268 IndicatorPowerNotifier *
269-indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player)
270+indicator_power_notifier_new (void)
271 {
272- GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER,
273- PROP_SOUND_PLAYER_NAME, sound_player,
274- NULL);
275-
276+ GObject * o = g_object_new (INDICATOR_TYPE_POWER_NOTIFIER, NULL);
277 return INDICATOR_POWER_NOTIFIER (o);
278 }
279
280@@ -591,26 +553,6 @@
281 }
282
283 void
284-indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self,
285- IndicatorPowerSoundPlayer * sound_player)
286-{
287- priv_t * p;
288-
289- g_return_if_fail(INDICATOR_IS_POWER_NOTIFIER(self));
290- g_return_if_fail((sound_player == NULL) || INDICATOR_IS_POWER_SOUND_PLAYER(sound_player));
291-
292- p = get_priv (self);
293-
294- if (p->sound_player == sound_player)
295- return;
296-
297- g_clear_object(&p->sound_player);
298-
299- if (sound_player != NULL)
300- p->sound_player = g_object_ref(sound_player);
301-}
302-
303-void
304 indicator_power_notifier_set_bus (IndicatorPowerNotifier * self,
305 GDBusConnection * bus)
306 {
307
308=== modified file 'src/notifier.h'
309--- src/notifier.h 2016-01-01 22:12:16 +0000
310+++ src/notifier.h 2016-05-16 21:28:42 +0000
311@@ -23,7 +23,6 @@
312 #include <gio/gio.h>
313
314 #include "device.h"
315-#include "sound-player.h"
316
317 G_BEGIN_DECLS
318
319@@ -56,7 +55,7 @@
320
321 GType indicator_power_notifier_get_type (void);
322
323-IndicatorPowerNotifier * indicator_power_notifier_new (IndicatorPowerSoundPlayer * sound_player);
324+IndicatorPowerNotifier * indicator_power_notifier_new (void);
325
326 void indicator_power_notifier_set_bus (IndicatorPowerNotifier * self,
327 GDBusConnection * connection);
328@@ -64,9 +63,6 @@
329 void indicator_power_notifier_set_battery (IndicatorPowerNotifier * self,
330 IndicatorPowerDevice * battery);
331
332-void indicator_power_notifier_set_sound_player (IndicatorPowerNotifier * self,
333- IndicatorPowerSoundPlayer * battery);
334-
335 #define POWER_LEVEL_STR_OK "ok"
336 #define POWER_LEVEL_STR_LOW "low"
337 #define POWER_LEVEL_STR_VERY_LOW "very_low"
338
339=== removed file 'src/sound-player-gst.c'
340--- src/sound-player-gst.c 2016-01-01 22:27:06 +0000
341+++ src/sound-player-gst.c 1970-01-01 00:00:00 +0000
342@@ -1,173 +0,0 @@
343-/*
344- * Copyright 2016 Canonical Ltd.
345- *
346- * This program is free software: you can redistribute it and/or modify it
347- * under the terms of the GNU General Public License version 3, as published
348- * by the Free Software Foundation.
349- *
350- * This program is distributed in the hope that it will be useful, but
351- * WITHOUT ANY WARRANTY; without even the implied warranties of
352- * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
353- * PURPOSE. See the GNU General Public License for more details.
354- *
355- * You should have received a copy of the GNU General Public License along
356- * with this program. If not, see <http://www.gnu.org/licenses/>.
357- *
358- * Authors:
359- * Charles Kerr <charles.kerr@canonical.com>
360- */
361-
362-#include "sound-player.h"
363-#include "sound-player-gst.h"
364-
365-#include <gst/gst.h>
366-
367-
368-/***
369-**** private struct
370-***/
371-
372-typedef struct
373-{
374- int unused;
375-}
376-IndicatorPowerSoundPlayerGSTPrivate;
377-
378-typedef IndicatorPowerSoundPlayerGSTPrivate priv_t;
379-
380-#define get_priv(o) ((priv_t*)indicator_power_sound_player_gst_get_instance_private(o))
381-
382-
383-/***
384-**** GObject boilerplate
385-***/
386-
387-static void indicator_power_device_provider_interface_init (
388- IndicatorPowerSoundPlayerInterface * iface);
389-
390-G_DEFINE_TYPE_WITH_CODE (
391- IndicatorPowerSoundPlayerGST,
392- indicator_power_sound_player_gst,
393- G_TYPE_OBJECT,
394- G_ADD_PRIVATE(IndicatorPowerSoundPlayerGST)
395- G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_SOUND_PLAYER,
396- indicator_power_device_provider_interface_init))
397-
398-/***
399-**** GSTREAMER
400-***/
401-
402-static void
403-gst_init_once(void)
404-{
405- static gboolean gst_init_checked = FALSE;
406-
407- if (G_UNLIKELY(!gst_init_checked))
408- {
409- GError* error = NULL;
410- if (!gst_init_check(NULL, NULL, &error))
411- {
412- g_critical("Unable to play alarm sound: %s", error->message);
413- g_error_free(error);
414- }
415- gst_init_checked = TRUE;
416- }
417-}
418-
419-static gboolean bus_callback(GstBus* bus G_GNUC_UNUSED, GstMessage* msg, gpointer gelement)
420-{
421- const GstMessageType message_type = GST_MESSAGE_TYPE(msg);
422-
423- if (GST_MESSAGE_SRC(msg) != gelement)
424- return G_SOURCE_CONTINUE;
425-
426- /* on eos, cleanup the element and cancel our gst bus subscription */
427- if (message_type == GST_MESSAGE_EOS)
428- {
429- g_debug("got GST_MESSAGE_EOS on sound play");
430- gst_element_set_state(GST_ELEMENT(gelement), GST_STATE_NULL);
431- gst_object_unref(gelement);
432- return G_SOURCE_REMOVE;
433- }
434-
435- /* on stream start, set the media role to 'alert' if we're using pulsesink */
436- if (message_type == GST_MESSAGE_STREAM_START)
437- {
438- GstElement* audio_sink = NULL;
439- g_debug("got GST_MESSAGE_STREAM_START on sound play");
440- g_object_get(gelement, "audio-sink", &audio_sink, NULL);
441- if (audio_sink != NULL)
442- {
443- GstPluginFeature* feature;
444- feature = GST_PLUGIN_FEATURE_CAST(GST_ELEMENT_GET_CLASS(audio_sink)->elementfactory);
445- if (feature && g_strcmp0(gst_plugin_feature_get_name(feature), "pulsesink") == 0)
446- {
447- const gchar* const props_str = "props,media.role=alert";
448- GstStructure* props = gst_structure_from_string(props_str, NULL);
449- g_debug("setting audio sink properties to '%s'", props_str);
450- g_object_set(audio_sink, "stream-properties", props, NULL);
451- g_clear_pointer(&props, gst_structure_free);
452- }
453- gst_object_unref(audio_sink);
454- }
455- }
456-
457- return G_SOURCE_CONTINUE;
458-}
459-
460-/***
461-**** IndicatorPowerSoundPlayer virtual functions
462-***/
463-
464-static void
465-my_play_uri (IndicatorPowerSoundPlayer * self G_GNUC_UNUSED, const gchar * uri)
466-{
467- GstElement * element;
468- GstBus * bus;
469-
470- /* create the element */
471- element = gst_element_factory_make("playbin", NULL);
472-
473- /* start listening for gst events */
474- bus = gst_pipeline_get_bus(GST_PIPELINE(element));
475- gst_bus_add_watch(bus, bus_callback, element);
476- gst_object_unref(bus);
477-
478- /* play the sound */
479- g_debug("Playing '%s'", uri);
480- g_object_set(element, "uri", uri, NULL);
481- gst_element_set_state(element, GST_STATE_PLAYING);
482-}
483-
484-/***
485-**** Instantiation
486-***/
487-
488-static void
489-indicator_power_sound_player_gst_class_init (IndicatorPowerSoundPlayerGSTClass * klass G_GNUC_UNUSED)
490-{
491- gst_init_once();
492-}
493-
494-static void
495-indicator_power_device_provider_interface_init (IndicatorPowerSoundPlayerInterface * iface)
496-{
497- iface->play_uri = my_play_uri;
498-}
499-
500-static void
501-indicator_power_sound_player_gst_init (IndicatorPowerSoundPlayerGST * self G_GNUC_UNUSED)
502-{
503-}
504-
505-/***
506-**** Public API
507-***/
508-
509-IndicatorPowerSoundPlayer *
510-indicator_power_sound_player_gst_new(void)
511-{
512- gpointer o = g_object_new (INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, NULL);
513-
514- return INDICATOR_POWER_SOUND_PLAYER (o);
515-}
516
517=== removed file 'src/sound-player-gst.h'
518--- src/sound-player-gst.h 2016-01-01 22:12:16 +0000
519+++ src/sound-player-gst.h 1970-01-01 00:00:00 +0000
520@@ -1,70 +0,0 @@
521-/*
522- * Copyright 2016 Canonical Ltd.
523- *
524- * This program is free software: you can redistribute it and/or modify it
525- * under the terms of the GNU General Public License version 3, as published
526- * by the Free Software Foundation.
527- *
528- * This program is distributed in the hope that it will be useful, but
529- * WITHOUT ANY WARRANTY; without even the implied warranties of
530- * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
531- * PURPOSE. See the GNU General Public License for more details.
532- *
533- * You should have received a copy of the GNU General Public License along
534- * with this program. If not, see <http://www.gnu.org/licenses/>.
535- *
536- * Authors:
537- * Charles Kerr <charles.kerr@canonical.com>
538- */
539-
540-#ifndef __INDICATOR_POWER_SOUND_PLAYER_GST__H__
541-#define __INDICATOR_POWER_SOUND_PLAYER_GST__H__
542-
543-#include <glib-object.h> /* parent class */
544-
545-#include "device-provider.h"
546-
547-G_BEGIN_DECLS
548-
549-#define INDICATOR_TYPE_POWER_SOUND_PLAYER_GST \
550- (indicator_power_sound_player_gst_get_type())
551-
552-#define INDICATOR_POWER_SOUND_PLAYER_GST(o) \
553- (G_TYPE_CHECK_INSTANCE_CAST ((o), \
554- INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, \
555- IndicatorPowerSoundPlayerGST))
556-
557-#define INDICATOR_POWER_SOUND_PLAYER_GST_GET_CLASS(o) \
558- (G_TYPE_INSTANCE_GET_CLASS ((o), \
559- INDICATOR_TYPE_POWER_SOUND_PLAYER_GST, \
560- IndicatorPowerSoundPlayerGSTClass))
561-
562-#define INDICATOR_IS_POWER_SOUND_PLAYER_GST(o) \
563- (G_TYPE_CHECK_INSTANCE_TYPE ((o), \
564- INDICATOR_TYPE_POWER_SOUND_PLAYER_GST))
565-
566-typedef struct _IndicatorPowerSoundPlayerGST
567- IndicatorPowerSoundPlayerGST;
568-typedef struct _IndicatorPowerSoundPlayerGSTClass
569- IndicatorPowerSoundPlayerGSTClass;
570-
571-/**
572- * An IndicatorPowerSoundPlayer which gets its devices from GST.
573- */
574-struct _IndicatorPowerSoundPlayerGST
575-{
576- GObject parent_instance;
577-};
578-
579-struct _IndicatorPowerSoundPlayerGSTClass
580-{
581- GObjectClass parent_class;
582-};
583-
584-GType indicator_power_sound_player_gst_get_type (void);
585-
586-IndicatorPowerSoundPlayer * indicator_power_sound_player_gst_new (void);
587-
588-G_END_DECLS
589-
590-#endif /* __INDICATOR_POWER_SOUND_PLAYER_GST__H__ */
591
592=== removed file 'src/sound-player.c'
593--- src/sound-player.c 2016-01-01 22:12:16 +0000
594+++ src/sound-player.c 1970-01-01 00:00:00 +0000
595@@ -1,45 +0,0 @@
596-/*
597- * Copyright 2016 Canonical Ltd.
598- *
599- * This program is free software: you can redistribute it and/or modify it
600- * under the terms of the GNU General Public License version 3, as published
601- * by the Free Software Foundation.
602- *
603- * This program is distributed in the hope that it will be useful, but
604- * WITHOUT ANY WARRANTY; without even the implied warranties of
605- * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
606- * PURPOSE. See the GNU General Public License for more details.
607- *
608- * You should have received a copy of the GNU General Public License along
609- * with this program. If not, see <http://www.gnu.org/licenses/>.
610- *
611- * Authors:
612- * Charles Kerr <charles.kerr@canonical.com>
613- */
614-
615-#include "sound-player.h"
616-
617-G_DEFINE_INTERFACE (IndicatorPowerSoundPlayer,
618- indicator_power_sound_player,
619- 0)
620-
621-static void
622-indicator_power_sound_player_default_init (IndicatorPowerSoundPlayerInterface * klass G_GNUC_UNUSED)
623-{
624-}
625-
626-/***
627-**** PUBLIC API
628-***/
629-
630-void
631-indicator_power_sound_player_play_uri (IndicatorPowerSoundPlayer * self,
632- const gchar * uri)
633-{
634- IndicatorPowerSoundPlayerInterface * iface;
635-
636- g_return_if_fail (INDICATOR_IS_POWER_SOUND_PLAYER (self));
637- iface = INDICATOR_POWER_SOUND_PLAYER_GET_INTERFACE (self);
638- g_return_if_fail (iface->play_uri != NULL);
639- iface->play_uri (self, uri);
640-}
641
642=== removed file 'src/sound-player.h'
643--- src/sound-player.h 2016-01-01 22:12:16 +0000
644+++ src/sound-player.h 1970-01-01 00:00:00 +0000
645@@ -1,72 +0,0 @@
646-/*
647- * Copyright 2016 Canonical Ltd.
648- *
649- * This program is free software: you can redistribute it and/or modify it
650- * under the terms of the GNU General Public License version 3, as published
651- * by the Free Software Foundation.
652- *
653- * This program is distributed in the hope that it will be useful, but
654- * WITHOUT ANY WARRANTY; without even the implied warranties of
655- * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
656- * PURPOSE. See the GNU General Public License for more details.
657- *
658- * You should have received a copy of the GNU General Public License along
659- * with this program. If not, see <http://www.gnu.org/licenses/>.
660- *
661- * Authors:
662- * Charles Kerr <charles.kerr@canonical.com>
663- */
664-
665-#ifndef __INDICATOR_POWER_SOUND_PLAYER__H__
666-#define __INDICATOR_POWER_SOUND_PLAYER__H__
667-
668-#include <glib-object.h>
669-
670-G_BEGIN_DECLS
671-
672-#define INDICATOR_TYPE_POWER_SOUND_PLAYER \
673- (indicator_power_sound_player_get_type ())
674-
675-#define INDICATOR_POWER_SOUND_PLAYER(obj) \
676- (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
677- INDICATOR_TYPE_POWER_SOUND_PLAYER, \
678- IndicatorPowerSoundPlayer))
679-
680-#define INDICATOR_IS_POWER_SOUND_PLAYER(obj) \
681- (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_TYPE_POWER_SOUND_PLAYER))
682-
683-#define INDICATOR_POWER_SOUND_PLAYER_GET_INTERFACE(inst) \
684- (G_TYPE_INSTANCE_GET_INTERFACE ((inst), \
685- INDICATOR_TYPE_POWER_SOUND_PLAYER, \
686- IndicatorPowerSoundPlayerInterface))
687-
688-typedef struct _IndicatorPowerSoundPlayer
689- IndicatorPowerSoundPlayer;
690-
691-typedef struct _IndicatorPowerSoundPlayerInterface
692- IndicatorPowerSoundPlayerInterface;
693-
694-/**
695- * An interface class for an object that plays sounds.
696- */
697-struct _IndicatorPowerSoundPlayerInterface
698-{
699- GTypeInterface parent_iface;
700-
701- /* virtual functions */
702- void (*play_uri) (IndicatorPowerSoundPlayer * self,
703- const gchar * uri);
704-};
705-
706-GType indicator_power_sound_player_get_type (void);
707-
708-/***
709-****
710-***/
711-
712-void indicator_power_sound_player_play_uri (IndicatorPowerSoundPlayer * self,
713- const gchar * uri);
714-
715-G_END_DECLS
716-
717-#endif /* __INDICATOR_POWER_SOUND_PLAYER__H__ */
718
719=== modified file 'tests/CMakeLists.txt'
720--- tests/CMakeLists.txt 2016-01-04 16:19:59 +0000
721+++ tests/CMakeLists.txt 2016-05-16 21:28:42 +0000
722@@ -14,13 +14,6 @@
723 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g ${C_WARNING_ARGS}")
724 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-weak-vtables -Wno-global-constructors") # Google Test
725
726-# build the mocks
727-set(MOCK_LIB "indicatorpowerservicemocks")
728-set(MOCK_SOURCES sound-player-mock.c)
729-set_source_files_properties(${MOCK_SOURCES}
730- PROPERTIES COMPILE_FLAGS "${C_WARNING_ARGS} -g -std=c99")
731-add_library(${MOCK_LIB} STATIC ${MOCK_SOURCES})
732-
733 # build the necessary schemas
734 set_directory_properties (PROPERTIES
735 ADDITIONAL_MAKE_CLEAN_FILES gschemas.compiled)
736@@ -56,8 +49,8 @@
737 set (TEST_NAME ${name})
738 add_executable (${TEST_NAME} ${TEST_NAME}.cc gschemas.compiled)
739 add_test (${TEST_NAME} ${TEST_NAME})
740- add_dependencies (${TEST_NAME} ${MOCK_LIB} libindicatorpowerservice)
741- target_link_libraries (${TEST_NAME} ${MOCK_LIB} indicatorpowerservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
742+ add_dependencies (${TEST_NAME} ${SERVICE_LIB})
743+ target_link_libraries (${TEST_NAME} ${SERVICE_LIB} gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS})
744 endfunction()
745 add_test_by_name(test-notify)
746 add_test(NAME dear-reader-the-next-test-takes-80-seconds COMMAND true)
747
748=== modified file 'tests/glib-fixture.h'
749--- tests/glib-fixture.h 2016-01-04 18:52:04 +0000
750+++ tests/glib-fixture.h 2016-05-16 21:28:42 +0000
751@@ -1,5 +1,8 @@
752 /*
753- * Copyright 2014 Canonical Ltd.
754+ * Copyright 2013-2016 Canonical Ltd.
755+ *
756+ * Authors:
757+ * Charles Kerr <charles.kerr@canonical.com>
758 *
759 * This program is free software: you can redistribute it and/or modify it
760 * under the terms of the GNU General Public License version 3, as published
761@@ -12,12 +15,14 @@
762 *
763 * You should have received a copy of the GNU General Public License along
764 * with this program. If not, see <http://www.gnu.org/licenses/>.
765- *
766- * Authors:
767- * Charles Kerr <charles.kerr@canonical.com>
768 */
769
770+#pragma once
771+
772+#include <chrono>
773+#include <functional> // std::function
774 #include <map>
775+#include <memory> // std::shared_ptr
776
777 #include <glib.h>
778 #include <glib/gstdio.h>
779@@ -29,79 +34,48 @@
780
781 class GlibFixture : public ::testing::Test
782 {
783- private:
784-
785- GLogFunc realLogHandler;
786-
787- std::map<GLogLevelFlags,size_t> expected_log;
788- std::map<GLogLevelFlags,std::vector<std::string>> log;
789-
790- void test_log_counts()
791- {
792- const GLogLevelFlags levels_to_test[] = { G_LOG_LEVEL_ERROR,
793- G_LOG_LEVEL_CRITICAL,
794- G_LOG_LEVEL_MESSAGE,
795- G_LOG_LEVEL_WARNING };
796-
797- for(const auto& level : levels_to_test)
798- {
799- const auto& v = log[level];
800- const auto n = v.size();
801-
802- EXPECT_EQ(expected_log[level], n);
803-
804- if (expected_log[level] != n)
805- for (size_t i=0; i<n; ++i)
806- g_print("%d %s\n", int(n+1), v[i].c_str());
807- }
808-
809- expected_log.clear();
810- log.clear();
811- }
812-
813- static void default_log_handler(const gchar * log_domain,
814- GLogLevelFlags log_level,
815- const gchar * message,
816- gpointer self)
817- {
818- auto tmp = g_strdup_printf ("%s:%d \"%s\"", log_domain, int(log_level), message);
819- static_cast<GlibFixture*>(self)->log[log_level].push_back(tmp);
820- g_free(tmp);
821- }
822+ public:
823+
824+ virtual ~GlibFixture() =default;
825
826 protected:
827
828- void increment_expected_errors(GLogLevelFlags level, size_t n=1)
829- {
830- expected_log[level] += n;
831- }
832-
833- virtual void SetUp()
834+ virtual void SetUp() override
835 {
836 setlocale(LC_ALL, "C.UTF-8");
837
838 loop = g_main_loop_new(nullptr, false);
839
840- g_log_set_default_handler(default_log_handler, this);
841+ // only use local, temporary settings
842+ g_assert(g_setenv("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, true));
843+ g_assert(g_setenv("GSETTINGS_BACKEND", "memory", true));
844+ g_debug("SCHEMA_DIR is %s", SCHEMA_DIR);
845+
846+ // fail on unexpected messages from this domain
847+ g_log_set_fatal_mask(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING);
848
849 g_unsetenv("DISPLAY");
850+
851 }
852
853- virtual void TearDown()
854+ virtual void TearDown() override
855 {
856- test_log_counts();
857-
858- g_log_set_default_handler(realLogHandler, this);
859+ g_test_assert_expected_messages ();
860
861 g_clear_pointer(&loop, g_main_loop_unref);
862 }
863
864+ void expectLogMessage (const gchar *domain, GLogLevelFlags level, const gchar *pattern)
865+ {
866+ g_test_expect_message (domain, level, pattern);
867+ }
868+
869 private:
870
871 static gboolean
872 wait_for_signal__timeout(gpointer name)
873 {
874- g_error("%s: timed out waiting for signal '%s'", G_STRLOC, static_cast<char*>(name));
875+ g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
876 return G_SOURCE_REMOVE;
877 }
878
879@@ -115,7 +89,7 @@
880 protected:
881
882 /* convenience func to loop while waiting for a GObject's signal */
883- void wait_for_signal(gpointer o, const gchar * signal, const guint timeout_seconds=5)
884+ void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5)
885 {
886 // wait for the signal or for timeout, whichever comes first
887 const auto handler_id = g_signal_connect_swapped(o, signal,
888@@ -130,12 +104,125 @@
889 }
890
891 /* convenience func to loop for N msec */
892- void wait_msec(guint msec=50)
893+ void wait_msec(int msec=50)
894 {
895 const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
896 g_main_loop_run(loop);
897 g_source_remove(id);
898 }
899
900- GMainLoop * loop;
901+ bool wait_for(std::function<bool()> test_function, guint timeout_msec=1000)
902+ {
903+ auto timer = std::shared_ptr<GTimer>(g_timer_new(), [](GTimer* t){g_timer_destroy(t);});
904+ const auto timeout_sec = timeout_msec / 1000.0;
905+ for (;;) {
906+ if (test_function())
907+ return true;
908+ //g_message("%f ... %f", g_timer_elapsed(timer.get(), nullptr), timeout_sec);
909+ if (g_timer_elapsed(timer.get(), nullptr) >= timeout_sec)
910+ return false;
911+ wait_msec();
912+ }
913+ }
914+
915+ bool wait_for_name_owned(
916+ GDBusConnection* connection,
917+ const gchar* name,
918+ guint timeout_msec=1000,
919+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
920+ {
921+ struct Data {
922+ GMainLoop* loop = nullptr;
923+ bool owned = false;
924+ };
925+ Data data;
926+
927+ auto on_name_appeared = [](GDBusConnection* /*connection*/,
928+ const gchar* /*name_*/,
929+ const gchar* name_owner,
930+ gpointer gdata){
931+ if (name_owner == nullptr)
932+ return;
933+ auto tmp = static_cast<Data*>(gdata);
934+ tmp->owned = true;
935+ g_main_loop_quit(tmp->loop);
936+ };
937+
938+ const auto timeout_id = g_timeout_add(timeout_msec, wait_msec__timeout, loop);
939+ data.loop = loop;
940+ const auto watch_id = g_bus_watch_name_on_connection(
941+ connection,
942+ name,
943+ flags,
944+ on_name_appeared,
945+ nullptr, // name_vanished
946+ &data,
947+ nullptr // user_data_free_func
948+ );
949+
950+ g_main_loop_run(loop);
951+
952+ g_bus_unwatch_name(watch_id);
953+ g_source_remove(timeout_id);
954+
955+ return data.owned;
956+ }
957+
958+ void EXPECT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
959+ const gchar* name,
960+ guint timeout_msec=1000,
961+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
962+ {
963+ EXPECT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
964+ }
965+
966+ void EXPECT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
967+ const gchar* name,
968+ guint timeout_msec=1000,
969+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
970+ {
971+ EXPECT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
972+ }
973+
974+ void ASSERT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
975+ const gchar* name,
976+ guint timeout_msec=1000,
977+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
978+ {
979+ ASSERT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
980+ }
981+
982+ void ASSERT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
983+ const gchar* name,
984+ guint timeout_msec=1000,
985+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
986+ {
987+ ASSERT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
988+ }
989+
990+ using source_func = std::function<gboolean()>;
991+
992+ guint idle_add(source_func&& func)
993+ {
994+ return g_idle_add_full(
995+ G_PRIORITY_DEFAULT_IDLE,
996+ [](gpointer gf){return (*static_cast<source_func*>(gf))();},
997+ new std::function<gboolean()>(func),
998+ [](gpointer gf){delete static_cast<source_func*>(gf);}
999+ );
1000+ }
1001+
1002+ guint timeout_add(source_func&& func, std::chrono::milliseconds msec)
1003+ {
1004+ return g_timeout_add_full(
1005+ G_PRIORITY_DEFAULT,
1006+ msec.count(),
1007+ [](gpointer gf){return (*static_cast<source_func*>(gf))();},
1008+ new std::function<gboolean()>(func),
1009+ [](gpointer gf){delete static_cast<source_func*>(gf);}
1010+ );
1011+ }
1012+
1013+ GMainLoop* loop {};
1014 };
1015+
1016
1017=== removed file 'tests/sound-player-mock.c'
1018--- tests/sound-player-mock.c 2016-01-04 16:19:59 +0000
1019+++ tests/sound-player-mock.c 1970-01-01 00:00:00 +0000
1020@@ -1,94 +0,0 @@
1021-/*
1022- * Copyright 2016 Canonical Ltd.
1023- *
1024- * This program is free software: you can redistribute it and/or modify it
1025- * under the terms of the GNU General Public License version 3, as published
1026- * by the Free Software Foundation.
1027- *
1028- * This program is distributed in the hope that it will be useful, but
1029- * WITHOUT ANY WARRANTY; without even the implied warranties of
1030- * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
1031- * PURPOSE. See the GNU General Public License for more details.
1032- *
1033- * You should have received a copy of the GNU General Public License along
1034- * with this program. If not, see <http://www.gnu.org/licenses/>.
1035- *
1036- * Authors:
1037- * Charles Kerr <charles.kerr@canonical.com>
1038- */
1039-
1040-#include "sound-player.h"
1041-#include "sound-player-mock.h"
1042-
1043-enum
1044-{
1045- SIGNAL_URI_PLAYED,
1046- LAST_SIGNAL
1047-};
1048-
1049-static guint signals[LAST_SIGNAL] = { 0 };
1050-
1051-/***
1052-**** GObject boilerplate
1053-***/
1054-
1055-static void indicator_power_sound_player_interface_init (
1056- IndicatorPowerSoundPlayerInterface * iface);
1057-
1058-G_DEFINE_TYPE_WITH_CODE (
1059- IndicatorPowerSoundPlayerMock,
1060- indicator_power_sound_player_mock,
1061- G_TYPE_OBJECT,
1062- G_IMPLEMENT_INTERFACE (INDICATOR_TYPE_POWER_SOUND_PLAYER,
1063- indicator_power_sound_player_interface_init))
1064-
1065-/***
1066-**** IndicatorPowerSoundPlayer virtual functions
1067-***/
1068-
1069-static void
1070-my_play_uri (IndicatorPowerSoundPlayer * self, const gchar * uri)
1071-{
1072- g_signal_emit (self, signals[SIGNAL_URI_PLAYED], 0, uri, NULL);
1073-}
1074-
1075-/***
1076-**** Instantiation
1077-***/
1078-
1079-static void
1080-indicator_power_sound_player_mock_class_init (IndicatorPowerSoundPlayerMockClass * klass G_GNUC_UNUSED)
1081-{
1082- signals[SIGNAL_URI_PLAYED] = g_signal_new (
1083- "uri-played",
1084- G_TYPE_FROM_CLASS(klass),
1085- G_SIGNAL_RUN_LAST,
1086- G_STRUCT_OFFSET (IndicatorPowerSoundPlayerMockClass, uri_played),
1087- NULL, NULL,
1088- g_cclosure_marshal_VOID__STRING,
1089- G_TYPE_NONE, 1, G_TYPE_STRING);
1090-}
1091-
1092-static void
1093-indicator_power_sound_player_interface_init (IndicatorPowerSoundPlayerInterface * iface)
1094-{
1095- iface->play_uri = my_play_uri;
1096-}
1097-
1098-static void
1099-indicator_power_sound_player_mock_init (IndicatorPowerSoundPlayerMock * self G_GNUC_UNUSED)
1100-{
1101-}
1102-
1103-/***
1104-**** Public API
1105-***/
1106-
1107-IndicatorPowerSoundPlayer *
1108-indicator_power_sound_player_mock_new (void)
1109-{
1110- gpointer o = g_object_new (INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, NULL);
1111-
1112- return INDICATOR_POWER_SOUND_PLAYER (o);
1113-}
1114-
1115
1116=== removed file 'tests/sound-player-mock.h'
1117--- tests/sound-player-mock.h 2016-01-04 16:19:59 +0000
1118+++ tests/sound-player-mock.h 1970-01-01 00:00:00 +0000
1119@@ -1,75 +0,0 @@
1120-/*
1121- * Copyright 2016 Canonical Ltd.
1122- *
1123- * This program is free software: you can redistribute it and/or modify it
1124- * under the terms of the GNU General Public License version 3, as published
1125- * by the Free Software Foundation.
1126- *
1127- * This program is distributed in the hope that it will be useful, but
1128- * WITHOUT ANY WARRANTY; without even the implied warranties of
1129- * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
1130- * PURPOSE. See the GNU General Public License for more details.
1131- *
1132- * You should have received a copy of the GNU General Public License along
1133- * with this program. If not, see <http://www.gnu.org/licenses/>.
1134- *
1135- * Authors:
1136- * Charles Kerr <charles.kerr@canonical.com>
1137- */
1138-
1139-#ifndef __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__
1140-#define __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__
1141-
1142-#include <glib-object.h> /* parent class */
1143-
1144-#include "sound-player.h"
1145-
1146-G_BEGIN_DECLS
1147-
1148-#define INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK \
1149- (indicator_power_sound_player_mock_get_type())
1150-
1151-#define INDICATOR_POWER_SOUND_PLAYER_MOCK(o) \
1152- (G_TYPE_CHECK_INSTANCE_CAST ((o), \
1153- INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, \
1154- IndicatorPowerSoundPlayerMock))
1155-
1156-#define INDICATOR_POWER_SOUND_PLAYER_MOCK_GET_CLASS(o) \
1157- (G_TYPE_INSTANCE_GET_CLASS ((o), \
1158- INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK, \
1159- IndicatorPowerSoundPlayerMockClass))
1160-
1161-#define INDICATOR_IS_POWER_SOUND_PLAYER_MOCK(o) \
1162- (G_TYPE_CHECK_INSTANCE_TYPE ((o), \
1163- INDICATOR_TYPE_POWER_SOUND_PLAYER_MOCK))
1164-
1165-typedef struct _IndicatorPowerSoundPlayerMock
1166- IndicatorPowerSoundPlayerMock;
1167-typedef struct _IndicatorPowerSoundPlayerMockPriv
1168- IndicatorPowerSoundPlayerMockPriv;
1169-typedef struct _IndicatorPowerSoundPlayerMockClass
1170- IndicatorPowerSoundPlayerMockClass;
1171-
1172-/**
1173- * An IndicatorPowerSoundPlayer which gets its devices from Mock.
1174- */
1175-struct _IndicatorPowerSoundPlayerMock
1176-{
1177- GObject parent_instance;
1178-};
1179-
1180-struct _IndicatorPowerSoundPlayerMockClass
1181-{
1182- GObjectClass parent_class;
1183-
1184- /* signals */
1185- void (*uri_played) (IndicatorPowerSoundPlayer * self, const gchar* uri);
1186-};
1187-
1188-GType indicator_power_sound_player_mock_get_type (void);
1189-
1190-IndicatorPowerSoundPlayer * indicator_power_sound_player_mock_new (void);
1191-
1192-G_END_DECLS
1193-
1194-#endif /* __INDICATOR_POWER_SOUND_PLAYER_MOCK__H__ */
1195
1196=== modified file 'tests/test-device.cc'
1197--- tests/test-device.cc 2015-01-12 21:47:27 +0000
1198+++ tests/test-device.cc 2016-05-16 21:28:42 +0000
1199@@ -48,7 +48,7 @@
1200 virtual void SetUp()
1201 {
1202 const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
1203- log_handler_id = g_log_set_handler ("Indicator-Power", flags, log_count_func, this);
1204+ log_handler_id = g_log_set_handler(G_LOG_DOMAIN, flags, log_count_func, this);
1205 log_count_ipower_expected = 0;
1206 log_count_ipower_actual = 0;
1207 }
1208@@ -56,7 +56,7 @@
1209 virtual void TearDown()
1210 {
1211 ASSERT_EQ (log_count_ipower_expected, log_count_ipower_actual);
1212- g_log_remove_handler ("Indicator-Power", log_handler_id);
1213+ g_log_remove_handler (G_LOG_DOMAIN, log_handler_id);
1214 }
1215
1216 protected:
1217
1218=== modified file 'tests/test-notify.cc'
1219--- tests/test-notify.cc 2016-01-02 01:37:51 +0000
1220+++ tests/test-notify.cc 2016-05-16 21:28:42 +0000
1221@@ -23,7 +23,6 @@
1222 #include "dbus-shared.h"
1223 #include "device.h"
1224 #include "notifier.h"
1225-#include "sound-player-mock.h"
1226
1227 #include <gtest/gtest.h>
1228
1229@@ -62,8 +61,6 @@
1230 static constexpr int NOTIFICATION_CLOSED_API {3};
1231 static constexpr int NOTIFICATION_CLOSED_UNDEFINED {4};
1232
1233- static constexpr char const * APP_NAME {"indicator-power-service"};
1234-
1235 static constexpr char const * METHOD_CLOSE {"CloseNotification"};
1236 static constexpr char const * METHOD_NOTIFY {"Notify"};
1237 static constexpr char const * METHOD_GET_CAPS {"GetCapabilities"};
1238@@ -136,7 +133,7 @@
1239 g_dbus_connection_set_exit_on_close(bus, FALSE);
1240 g_object_add_weak_pointer(G_OBJECT(bus), reinterpret_cast<gpointer*>(&bus));
1241
1242- notify_init(APP_NAME);
1243+ notify_init(SERVICE_EXEC);
1244 }
1245
1246 virtual void TearDown()
1247@@ -160,6 +157,50 @@
1248
1249 super::TearDown();
1250 }
1251+
1252+ /***
1253+ ****
1254+ ***/
1255+
1256+ int get_notify_call_count() const
1257+ {
1258+ guint len {0u};
1259+ GError* error {nullptr};
1260+ dbus_test_dbus_mock_object_get_method_calls(mock, obj, METHOD_NOTIFY, &len, &error);
1261+ g_assert_no_error(error);
1262+ return len;
1263+ }
1264+
1265+ std::string get_notify_call_sound_file(int call_number)
1266+ {
1267+ std::string ret;
1268+
1269+ guint len {0u};
1270+ GError* error {nullptr};
1271+ auto calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, METHOD_NOTIFY, &len, &error);
1272+ g_return_val_if_fail(int(len) > call_number, ret);
1273+ g_assert_no_error(error);
1274+
1275+ constexpr int HINTS_PARAM_POSITION {6};
1276+ const auto& call = calls[call_number];
1277+ g_return_val_if_fail(g_variant_n_children(call.params) > HINTS_PARAM_POSITION, ret);
1278+ auto hints = g_variant_get_child_value(call.params, HINTS_PARAM_POSITION);
1279+ const gchar* sound_file = nullptr;
1280+ auto success = g_variant_lookup(hints, "sound-file", "&s", &sound_file);
1281+ g_return_val_if_fail(success, ret);
1282+ if (sound_file != nullptr)
1283+ ret = sound_file;
1284+ g_clear_pointer(&hints, g_variant_unref);
1285+
1286+ return ret;
1287+ }
1288+
1289+ void clear_method_calls()
1290+ {
1291+ GError* error{nullptr};
1292+ ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, &error));
1293+ g_assert_no_error(error);
1294+ }
1295 };
1296
1297 /***
1298@@ -276,8 +317,7 @@
1299
1300 // set up a notifier and give it the battery so changing the battery's
1301 // charge should show up on the bus.
1302- auto sound_player = indicator_power_sound_player_mock_new ();
1303- auto notifier = indicator_power_notifier_new (sound_player);
1304+ auto notifier = indicator_power_notifier_new ();
1305 indicator_power_notifier_set_battery (notifier, battery);
1306 indicator_power_notifier_set_bus (notifier, bus);
1307 wait_msec();
1308@@ -321,21 +361,12 @@
1309 g_dbus_connection_signal_unsubscribe (bus, sub_tag);
1310 g_object_unref (battery);
1311 g_object_unref (notifier);
1312- g_object_unref (sound_player);
1313 }
1314
1315 /***
1316 ****
1317 ***/
1318
1319-namespace
1320-{
1321- static void on_uri_played(IndicatorPowerSoundPlayer*, const char* uri, gpointer glast_uri)
1322- {
1323- *static_cast<std::string*>(glast_uri) = uri;
1324- }
1325-}
1326-
1327 TEST_F(NotifyFixture, EventsThatChangeNotifications)
1328 {
1329 // GetCapabilities returns an array containing 'actions', so that we'll
1330@@ -364,10 +395,7 @@
1331
1332 // set up a notifier and give it the battery so changing the battery's
1333 // charge should show up on the bus.
1334- std::string last_uri;
1335- auto sound_player = indicator_power_sound_player_mock_new ();
1336- g_signal_connect(sound_player, "uri-played", G_CALLBACK(on_uri_played), &last_uri);
1337- auto notifier = indicator_power_notifier_new (sound_player);
1338+ auto notifier = indicator_power_notifier_new ();
1339 indicator_power_notifier_set_battery (notifier, battery);
1340 indicator_power_notifier_set_bus (notifier, bus);
1341 ChangedParams changed_params;
1342@@ -385,7 +413,6 @@
1343 // test setup case
1344 wait_msec();
1345 EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str());
1346- EXPECT_TRUE(last_uri.empty());
1347
1348 // change the percent past the 'low' threshold and confirm that
1349 // a) the power level changes
1350@@ -396,48 +423,49 @@
1351 EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields);
1352 EXPECT_EQ (indicator_power_notifier_get_power_level(battery), changed_params.power_level);
1353 EXPECT_TRUE (changed_params.is_warning);
1354- EXPECT_EQ (low_power_uri, last_uri);
1355+ EXPECT_EQ (1, get_notify_call_count());
1356+ EXPECT_EQ (low_power_uri, get_notify_call_sound_file(0));
1357+ clear_method_calls();
1358
1359 // now test that the warning changes if the level goes down even lower...
1360- last_uri.clear();
1361 changed_params = ChangedParams();
1362 set_battery_percentage (battery, percent_very_low);
1363 wait_msec();
1364 EXPECT_EQ (FIELD_POWER_LEVEL, changed_params.fields);
1365 EXPECT_STREQ (POWER_LEVEL_STR_VERY_LOW, changed_params.power_level.c_str());
1366- EXPECT_EQ (low_power_uri, last_uri);
1367+ EXPECT_EQ (1, get_notify_call_count());
1368+ EXPECT_EQ (low_power_uri, get_notify_call_sound_file(0));
1369+ clear_method_calls();
1370
1371 // ...and that the warning is taken down if the battery is plugged back in...
1372- last_uri.clear();
1373 changed_params = ChangedParams();
1374 g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, nullptr);
1375 wait_msec();
1376 EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields);
1377 EXPECT_FALSE (changed_params.is_warning);
1378- EXPECT_TRUE(last_uri.empty());
1379+ EXPECT_EQ (0, get_notify_call_count());
1380
1381 // ...and that it comes back if we unplug again...
1382- last_uri.clear();
1383 changed_params = ChangedParams();
1384 g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, nullptr);
1385 wait_msec();
1386 EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields);
1387 EXPECT_TRUE (changed_params.is_warning);
1388- EXPECT_EQ (low_power_uri, last_uri);
1389+ EXPECT_EQ (1, get_notify_call_count());
1390+ EXPECT_EQ (low_power_uri, get_notify_call_sound_file(0));
1391+ clear_method_calls();
1392
1393 // ...and that it's taken down if the power level is OK
1394- last_uri.clear();
1395 changed_params = ChangedParams();
1396 set_battery_percentage (battery, percent_low+1);
1397 wait_msec();
1398 EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields);
1399 EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str());
1400 EXPECT_FALSE (changed_params.is_warning);
1401- EXPECT_TRUE(last_uri.empty());
1402+ EXPECT_EQ (0, get_notify_call_count());
1403
1404 // cleanup
1405 g_dbus_connection_signal_unsubscribe (bus, sub_tag);
1406 g_object_unref (notifier);
1407 g_object_unref (battery);
1408- g_object_unref (sound_player);
1409 }

Subscribers

People subscribed via source and target branches