Merge lp:~charlesk/indicator-power/lp-1066208 into lp:indicator-power/13.04

Proposed by Charles Kerr
Status: Merged
Approved by: Lars Karlitski
Approved revision: 157
Merged at revision: 151
Proposed branch: lp:~charlesk/indicator-power/lp-1066208
Merge into: lp:indicator-power/13.04
Diff against target: 779 lines (+402/-233)
5 files modified
src/device.c (+18/-14)
src/indicator-power.c (+94/-71)
src/indicator-power.h (+2/-0)
tests/test-device.cc (+274/-148)
tests/test-indicator.cc (+14/-0)
To merge this branch: bzr merge lp:~charlesk/indicator-power/lp-1066208
Reviewer Review Type Date Requested Status
Lars Karlitski (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+131547@code.launchpad.net

Commit message

Choose the correct primary device, and choose the correct icon for it.

Description of the change

Choose the correct primary device, and choose the correct icon for it.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Lars Karlitski (larsu) wrote :

+ case UP_DEVICE_KIND_UNKNOWN:
+ /* TRANSLATORS: tablet device */
+ text = _("Unknown");
+ break;

That should be "TRANSLATORS: Unkown device".

review: Needs Fixing
156. By Charles Kerr

fix comment typo

157. By Charles Kerr

fix copy-paste comment error

Revision history for this message
Charles Kerr (charlesk) wrote :

> + case UP_DEVICE_KIND_UNKNOWN:
> + /* TRANSLATORS: tablet device */
> + text = _("Unknown");
> + break;
>
> That should be "TRANSLATORS: Unkown device".

That should be "TRANSLATORS: Unknown device".

Revision history for this message
Lars Karlitski (larsu) wrote :

Cool, thanks.

review: Approve
Revision history for this message
Charles Kerr (charlesk) wrote :

Jenkins seems to be asleep, so I've merged this in revision 151

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/device.c'
2--- src/device.c 2012-10-03 16:30:18 +0000
3+++ src/device.c 2012-10-26 08:55:25 +0000
4@@ -334,7 +334,7 @@
5 gdouble percentage = indicator_power_device_get_percentage (device);
6 const UpDeviceKind kind = indicator_power_device_get_kind (device);
7 const UpDeviceState state = indicator_power_device_get_state (device);
8- const gchar * kind_str = kind_str = up_device_kind_to_string (kind);
9+ const gchar * kind_str = up_device_kind_to_string (kind);
10
11 GPtrArray * names = g_ptr_array_new ();
12
13@@ -351,19 +351,19 @@
14 else switch (state)
15 {
16 case UP_DEVICE_STATE_EMPTY:
17- g_ptr_array_add (names, g_strdup("battery-empty-symbolic"));
18+ g_ptr_array_add (names, g_strdup_printf("%s-empty-symbolic", kind_str));
19 g_ptr_array_add (names, g_strdup_printf("gpm-%s-empty", kind_str));
20 g_ptr_array_add (names, g_strdup_printf("gpm-%s-000", kind_str));
21- g_ptr_array_add (names, g_strdup("battery-empty"));
22+ g_ptr_array_add (names, g_strdup_printf("%s-empty", kind_str));
23 break;
24
25 case UP_DEVICE_STATE_FULLY_CHARGED:
26- g_ptr_array_add (names, g_strdup("battery-full-charged-symbolic"));
27- g_ptr_array_add (names, g_strdup("battery-full-charging-symbolic"));
28+ g_ptr_array_add (names, g_strdup_printf("%s-full-charged-symbolic", kind_str));
29+ g_ptr_array_add (names, g_strdup_printf("%s-full-charging-symbolic", kind_str));
30 g_ptr_array_add (names, g_strdup_printf("gpm-%s-full", kind_str));
31 g_ptr_array_add (names, g_strdup_printf("gpm-%s-100", kind_str));
32- g_ptr_array_add (names, g_strdup("battery-full-charged"));
33- g_ptr_array_add (names, g_strdup("battery-full-charging"));
34+ g_ptr_array_add (names, g_strdup_printf("%s-full-charged", kind_str));
35+ g_ptr_array_add (names, g_strdup_printf("%s-full-charging", kind_str));
36 break;
37
38 case UP_DEVICE_STATE_CHARGING:
39@@ -374,9 +374,9 @@
40
41 suffix_str = get_device_icon_suffix (percentage);
42 index_str = get_device_icon_index (percentage);
43- g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging-symbolic", suffix_str));
44+ g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging-symbolic", kind_str, suffix_str));
45 g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s-charging", kind_str, index_str));
46- g_ptr_array_add (names, g_strdup_printf ("battery-%s-charging", suffix_str));
47+ g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging", kind_str, suffix_str));
48 break;
49
50 case UP_DEVICE_STATE_DISCHARGING:
51@@ -392,14 +392,14 @@
52 index_str = get_device_icon_index (percentage);
53 g_ptr_array_add (names, g_strdup_printf ("%s-%s", kind_str, index_str));
54 g_ptr_array_add (names, g_strdup_printf ("gpm-%s-%s", kind_str, index_str));
55- g_ptr_array_add (names, g_strdup_printf ("battery-%s-symbolic", suffix_str));
56- g_ptr_array_add (names, g_strdup_printf ("battery-%s", suffix_str));
57+ g_ptr_array_add (names, g_strdup_printf ("%s-%s-symbolic", kind_str, suffix_str));
58+ g_ptr_array_add (names, g_strdup_printf ("%s-%s", kind_str, suffix_str));
59 break;
60
61 default:
62- g_ptr_array_add (names, g_strdup("battery-missing-symbolic"));
63- g_ptr_array_add (names, g_strdup("gpm-battery-missing"));
64- g_ptr_array_add (names, g_strdup("battery-missing"));
65+ g_ptr_array_add (names, g_strdup_printf("%s-missing-symbolic", kind_str));
66+ g_ptr_array_add (names, g_strdup_printf("gpm-%s-missing", kind_str));
67+ g_ptr_array_add (names, g_strdup_printf("%s-missing", kind_str));
68 }
69
70 g_ptr_array_add (names, NULL); /* terminates the strv */
71@@ -528,6 +528,10 @@
72 /* TRANSLATORS: tablet device */
73 text = _("Computer");
74 break;
75+ case UP_DEVICE_KIND_UNKNOWN:
76+ /* TRANSLATORS: unknown device */
77+ text = _("Unknown");
78+ break;
79 default:
80 g_warning ("enum unrecognised: %i", kind);
81 text = up_device_kind_to_string (kind);
82
83=== modified file 'src/indicator-power.c'
84--- src/indicator-power.c 2012-09-25 07:58:35 +0000
85+++ src/indicator-power.c 2012-10-26 08:55:25 +0000
86@@ -150,6 +150,8 @@
87
88 dispose_devices (self);
89
90+ g_clear_object (&priv->label);
91+ g_clear_object (&priv->status_image);
92 g_clear_object (&priv->dbus_listener);
93 g_clear_object (&priv->settings);
94
95@@ -350,76 +352,95 @@
96 gtk_widget_show_all (GTK_WIDGET (priv->menu));
97 }
98
99-static IndicatorPowerDevice*
100-get_primary_device (GSList * devices)
101-{
102- IndicatorPowerDevice * primary_device = NULL;
103- IndicatorPowerDevice * primary_device_charging = NULL;
104- IndicatorPowerDevice * primary_device_discharging = NULL;
105- gboolean charging = FALSE;
106- gboolean discharging = FALSE;
107- guint64 min_discharging_time = G_MAXUINT64;
108- guint64 max_charging_time = 0;
109- GSList * l;
110-
111- for (l=devices; l!=NULL; l=l->next)
112- {
113- IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE(l->data);
114- const UpDeviceKind kind = indicator_power_device_get_kind (device);
115- const UpDeviceState state = indicator_power_device_get_state (device);
116- const gdouble percentage = indicator_power_device_get_percentage (device);
117- const time_t time = indicator_power_device_get_time (device);
118-
119- /* Try to fix the case when we get a empty battery bay as a real battery */
120- if (state == UP_DEVICE_STATE_UNKNOWN &&
121- percentage == 0)
122- continue;
123-
124- /* not battery */
125- if (kind != UP_DEVICE_KIND_BATTERY)
126- continue;
127-
128- if (state == UP_DEVICE_STATE_DISCHARGING)
129- {
130- discharging = TRUE;
131- if (time < min_discharging_time)
132- {
133- min_discharging_time = time;
134- primary_device_discharging = device;
135- }
136- }
137- else if (state == UP_DEVICE_STATE_CHARGING)
138- {
139- charging = TRUE;
140- if (time == 0) /* Battery broken */
141- {
142- primary_device_charging = device;
143- }
144- if (time > max_charging_time)
145- {
146- max_charging_time = time;
147- primary_device_charging = device;
148- }
149- }
150- else
151- {
152- primary_device = device;
153- }
154- }
155-
156- if (discharging)
157- {
158- primary_device = primary_device_discharging;
159- }
160- else if (charging)
161- {
162- primary_device = primary_device_charging;
163- }
164-
165- if (primary_device != NULL)
166- g_object_ref (primary_device);
167-
168- return primary_device;
169+/* sort devices from most interesting to least interesting on this criteria:
170+ 1. discharging items from least time remaining until most time remaining
171+ 2. discharging items with an unknown time remaining
172+ 3. charging items from most time left to charge to least time left to charge
173+ 4. charging items with an unknown time remaining
174+ 5. everything else */
175+static gint
176+device_compare_func (gconstpointer ga, gconstpointer gb)
177+{
178+ int ret;
179+ int state;
180+ const IndicatorPowerDevice * a = INDICATOR_POWER_DEVICE(ga);
181+ const IndicatorPowerDevice * b = INDICATOR_POWER_DEVICE(gb);
182+ const int a_state = indicator_power_device_get_state (a);
183+ const int b_state = indicator_power_device_get_state (b);
184+ const gdouble a_percentage = indicator_power_device_get_percentage (a);
185+ const gdouble b_percentage = indicator_power_device_get_percentage (b);
186+ const time_t a_time = indicator_power_device_get_time (a);
187+ const time_t b_time = indicator_power_device_get_time (b);
188+
189+ ret = 0;
190+
191+ state = UP_DEVICE_STATE_DISCHARGING;
192+ if (!ret && ((a_state == state) || (b_state == state)))
193+ {
194+ if (a_state != state) /* b is discharging */
195+ {
196+ ret = 1;
197+ }
198+ else if (b_state != state) /* a is discharging */
199+ {
200+ ret = -1;
201+ }
202+ else /* both are discharging; least-time-left goes first */
203+ {
204+ if (!a_time || !b_time) /* known time always trumps unknown time */
205+ ret = a_time ? -1 : 1;
206+ else if (a_time != b_time)
207+ ret = a_time < b_time ? -1 : 1;
208+ else
209+ ret = a_percentage < b_percentage ? -1 : 1;
210+ }
211+ }
212+
213+ state = UP_DEVICE_STATE_CHARGING;
214+ if (!ret && (((a_state == state) && a_time) || ((b_state == state) && b_time)))
215+ {
216+ if (b_state != state) /* a is charging */
217+ {
218+ ret = 1;
219+ }
220+ if (a_state != state) /* b is charging */
221+ {
222+ ret = -1;
223+ }
224+ else /* both are discharging; most-time-to-charge goes first */
225+ {
226+ if (!a_time || !b_time) /* known time always trumps unknown time */
227+ ret = a_time ? -1 : 1;
228+ else if (a_time != b_time)
229+ ret = a_time > b_time ? -1 : 1;
230+ else
231+ ret = a_percentage < b_percentage ? -1 : 1;
232+ }
233+ }
234+
235+ if (!ret)
236+ ret = a_state - b_state;
237+
238+ return ret;
239+}
240+
241+IndicatorPowerDevice *
242+indicator_power_choose_primary_device (GSList * devices)
243+{
244+ IndicatorPowerDevice * primary = NULL;
245+
246+ if (devices != NULL)
247+ {
248+ GSList * tmp;
249+
250+ tmp = g_slist_copy (devices);
251+ tmp = g_slist_sort (tmp, device_compare_func);
252+ primary = g_object_ref (tmp->data);
253+
254+ g_slist_free (tmp);
255+ }
256+
257+ return primary;
258 }
259
260 static void
261@@ -458,7 +479,7 @@
262 g_slist_foreach (devices, (GFunc)g_object_ref, NULL);
263 dispose_devices (self);
264 priv->devices = g_slist_copy (devices);
265- priv->device = get_primary_device (priv->devices);
266+ priv->device = indicator_power_choose_primary_device (devices);
267
268 /* and our menus/visibility from the new device list */
269 if (priv->device != NULL)
270@@ -489,6 +510,7 @@
271 {
272 /* Create the label if it doesn't exist already */
273 priv->label = GTK_LABEL (gtk_label_new (""));
274+ g_object_ref_sink (priv->label);
275 gtk_widget_set_visible (GTK_WIDGET (priv->label), FALSE);
276 }
277
278@@ -508,6 +530,7 @@
279 gicon = g_themed_icon_new (DEFAULT_ICON);
280 priv->status_image = GTK_IMAGE (gtk_image_new_from_gicon (gicon,
281 GTK_ICON_SIZE_LARGE_TOOLBAR));
282+ g_object_ref_sink (priv->status_image);
283 g_object_unref (gicon);
284 }
285
286
287=== modified file 'src/indicator-power.h'
288--- src/indicator-power.h 2012-06-06 20:24:53 +0000
289+++ src/indicator-power.h 2012-10-26 08:55:25 +0000
290@@ -55,4 +55,6 @@
291 void indicator_power_set_devices (IndicatorPower * power,
292 GSList * devices);
293
294+IndicatorPowerDevice* indicator_power_choose_primary_device (GSList * devices);
295+
296 G_END_DECLS
297
298=== modified file 'tests/test-device.cc'
299--- tests/test-device.cc 2012-10-03 16:30:18 +0000
300+++ tests/test-device.cc 2012-10-26 08:55:25 +0000
301@@ -19,6 +19,7 @@
302
303 #include <gtest/gtest.h>
304 #include "device.h"
305+#include "indicator-power.h"
306
307 namespace
308 {
309@@ -239,154 +240,205 @@
310 check_icon_names (device, "gpm-monitor-symbolic;"
311 "gpm-monitor");
312
313- // empty battery
314- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
315- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY,
316- NULL);
317- check_icon_names (device, "battery-empty-symbolic;"
318- "gpm-battery-empty;"
319- "gpm-battery-000;"
320- "battery-empty");
321-
322- // charged battery
323- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
324- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
325- NULL);
326- check_icon_names (device, "battery-full-charged-symbolic;"
327- "battery-full-charging-symbolic;"
328- "gpm-battery-full;"
329- "gpm-battery-100;"
330- "battery-full-charged;"
331- "battery-full-charging");
332-
333- // charging battery, 95%
334- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
335- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
336- INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
337- NULL);
338- check_icon_names (device, "battery-caution-charging-symbolic;"
339- "gpm-battery-000-charging;"
340- "battery-caution-charging");
341-
342- // charging battery, 85%
343- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
344- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
345- INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
346- NULL);
347- check_icon_names (device, "battery-caution-charging-symbolic;"
348- "gpm-battery-000-charging;"
349- "battery-caution-charging");
350-
351- // charging battery, 50%
352- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
353- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
354- INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
355- NULL);
356- check_icon_names (device, "battery-caution-charging-symbolic;"
357- "gpm-battery-000-charging;"
358- "battery-caution-charging");
359-
360- // charging battery, 25%
361- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
362- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
363- INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
364- NULL);
365- check_icon_names (device, "battery-caution-charging-symbolic;"
366- "gpm-battery-000-charging;"
367- "battery-caution-charging");
368-
369- // charging battery, 5%
370- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
371- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
372- INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
373- NULL);
374- check_icon_names (device, "battery-caution-charging-symbolic;"
375- "gpm-battery-000-charging;"
376- "battery-caution-charging");
377-
378-
379- // discharging battery, 95%
380- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
381- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
382- INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
383- NULL);
384- check_icon_names (device, "battery-100;"
385- "gpm-battery-100;"
386- "battery-full-symbolic;"
387- "battery-full");
388-
389- // discharging battery, 85%
390- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
391- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
392- INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
393- NULL);
394- check_icon_names (device, "battery-080;"
395- "gpm-battery-080;"
396- "battery-full-symbolic;"
397- "battery-full");
398-
399- // discharging battery, 50% -- 1 hour left
400- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
401- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
402- INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
403- INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
404- NULL);
405- check_icon_names (device, "battery-060;"
406- "gpm-battery-060;"
407- "battery-good-symbolic;"
408- "battery-good");
409-
410- // discharging battery, 25% -- 1 hour left
411- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
412- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
413- INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
414- INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
415- NULL);
416- check_icon_names (device, "battery-040;"
417- "gpm-battery-040;"
418- "battery-good-symbolic;"
419- "battery-good");
420-
421- // discharging battery, 25% -- 15 minutes left
422- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
423- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
424- INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
425- INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
426- NULL);
427- check_icon_names (device, "battery-020;"
428- "gpm-battery-020;"
429- "battery-low-symbolic;"
430- "battery-low");
431-
432- // discharging battery, 5% -- 1 hour left
433- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
434- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
435- INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
436- INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
437- NULL);
438- check_icon_names (device, "battery-040;"
439- "gpm-battery-040;"
440- "battery-good-symbolic;"
441- "battery-good");
442-
443- // discharging battery, 5% -- 15 minutes left
444- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
445- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
446- INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
447- INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
448- NULL);
449- check_icon_names (device, "battery-000;"
450- "gpm-battery-000;"
451- "battery-caution-symbolic;"
452- "battery-caution");
453-
454- // state unknown
455- g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
456- INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
457- NULL);
458- check_icon_names (device, "battery-missing-symbolic;"
459- "gpm-battery-missing;"
460- "battery-missing");
461+ // devices that hold a charge
462+ struct {
463+ int kind;
464+ const gchar * kind_str;
465+ } devices[] = {
466+ { UP_DEVICE_KIND_BATTERY, "battery" },
467+ { UP_DEVICE_KIND_UPS, "ups" },
468+ { UP_DEVICE_KIND_MOUSE, "mouse" },
469+ { UP_DEVICE_KIND_KEYBOARD, "keyboard" },
470+ { UP_DEVICE_KIND_PHONE, "phone" }
471+ };
472+
473+ GString * expected = g_string_new (NULL);
474+ for (int i=0, n=G_N_ELEMENTS(devices); i<n; i++)
475+ {
476+ const int kind = devices[i].kind;
477+ const gchar * kind_str = devices[i].kind_str;
478+
479+ // empty
480+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
481+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY,
482+ NULL);
483+
484+ g_string_append_printf (expected, "%s-empty-symbolic;", kind_str);
485+ g_string_append_printf (expected, "gpm-%s-empty;", kind_str);
486+ g_string_append_printf (expected, "gpm-%s-000;", kind_str);
487+ g_string_append_printf (expected, "%s-empty", kind_str);
488+ check_icon_names (device, expected->str);
489+ g_string_truncate (expected, 0);
490+
491+ // charged
492+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
493+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
494+ NULL);
495+ g_string_append_printf (expected, "%s-full-charged-symbolic;", kind_str);
496+ g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str);
497+ g_string_append_printf (expected, "gpm-%s-full;", kind_str);
498+ g_string_append_printf (expected, "gpm-%s-100;", kind_str);
499+ g_string_append_printf (expected, "%s-full-charged;", kind_str);
500+ g_string_append_printf (expected, "%s-full-charging", kind_str);
501+ check_icon_names (device, expected->str);
502+ g_string_truncate (expected, 0);
503+
504+ // charging, 95%
505+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
506+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
507+ INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
508+ NULL);
509+
510+ g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
511+ g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
512+ g_string_append_printf (expected, "%s-caution-charging", kind_str);
513+ check_icon_names (device, expected->str);
514+ g_string_truncate (expected, 0);
515+
516+ // charging, 85%
517+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
518+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
519+ INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
520+ NULL);
521+ g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
522+ g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
523+ g_string_append_printf (expected, "%s-caution-charging", kind_str);
524+ check_icon_names (device, expected->str);
525+ g_string_truncate (expected, 0);
526+
527+ // charging, 50%
528+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
529+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
530+ INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
531+ NULL);
532+ g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
533+ g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
534+ g_string_append_printf (expected, "%s-caution-charging", kind_str);
535+ check_icon_names (device, expected->str);
536+ g_string_truncate (expected, 0);
537+
538+ // charging, 25%
539+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
540+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
541+ INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
542+ NULL);
543+ g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
544+ g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
545+ g_string_append_printf (expected, "%s-caution-charging", kind_str);
546+ check_icon_names (device, expected->str);
547+ g_string_truncate (expected, 0);
548+
549+ // charging, 5%
550+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
551+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
552+ INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
553+ NULL);
554+ g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
555+ g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
556+ g_string_append_printf (expected, "%s-caution-charging", kind_str);
557+ check_icon_names (device, expected->str);
558+ g_string_truncate (expected, 0);
559+
560+ // discharging, 95%
561+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
562+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
563+ INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
564+ NULL);
565+ g_string_append_printf (expected, "%s-100;", kind_str);
566+ g_string_append_printf (expected, "gpm-%s-100;", kind_str);
567+ g_string_append_printf (expected, "%s-full-symbolic;", kind_str);
568+ g_string_append_printf (expected, "%s-full", kind_str);
569+ check_icon_names (device, expected->str);
570+ g_string_truncate (expected, 0);
571+
572+ // discharging, 85%
573+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
574+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
575+ INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
576+ NULL);
577+ g_string_append_printf (expected, "%s-080;", kind_str);
578+ g_string_append_printf (expected, "gpm-%s-080;", kind_str);
579+ g_string_append_printf (expected, "%s-full-symbolic;", kind_str);
580+ g_string_append_printf (expected, "%s-full", kind_str);
581+ check_icon_names (device, expected->str);
582+ g_string_truncate (expected, 0);
583+
584+ // discharging, 50% -- 1 hour left
585+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
586+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
587+ INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
588+ INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
589+ NULL);
590+ g_string_append_printf (expected, "%s-060;", kind_str);
591+ g_string_append_printf (expected, "gpm-%s-060;", kind_str);
592+ g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
593+ g_string_append_printf (expected, "%s-good", kind_str);
594+ check_icon_names (device, expected->str);
595+ g_string_truncate (expected, 0);
596+
597+ // discharging, 25% -- 1 hour left
598+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
599+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
600+ INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
601+ INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
602+ NULL);
603+ g_string_append_printf (expected, "%s-040;", kind_str);
604+ g_string_append_printf (expected, "gpm-%s-040;", kind_str);
605+ g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
606+ g_string_append_printf (expected, "%s-good", kind_str);
607+ check_icon_names (device, expected->str);
608+ g_string_truncate (expected, 0);
609+
610+ // discharging, 25% -- 15 minutes left
611+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
612+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
613+ INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
614+ INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
615+ NULL);
616+ g_string_append_printf (expected, "%s-020;", kind_str);
617+ g_string_append_printf (expected, "gpm-%s-020;", kind_str);
618+ g_string_append_printf (expected, "%s-low-symbolic;", kind_str);
619+ g_string_append_printf (expected, "%s-low", kind_str);
620+ check_icon_names (device, expected->str);
621+ g_string_truncate (expected, 0);
622+
623+ // discharging, 5% -- 1 hour left
624+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
625+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
626+ INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
627+ INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
628+ NULL);
629+ g_string_append_printf (expected, "%s-040;", kind_str);
630+ g_string_append_printf (expected, "gpm-%s-040;", kind_str);
631+ g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
632+ g_string_append_printf (expected, "%s-good", kind_str);
633+ check_icon_names (device, expected->str);
634+ g_string_truncate (expected, 0);
635+
636+ // discharging, 5% -- 15 minutes left
637+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
638+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
639+ INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
640+ INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
641+ NULL);
642+ g_string_append_printf (expected, "%s-000;", kind_str);
643+ g_string_append_printf (expected, "gpm-%s-000;", kind_str);
644+ g_string_append_printf (expected, "%s-caution-symbolic;", kind_str);
645+ g_string_append_printf (expected, "%s-caution", kind_str);
646+ check_icon_names (device, expected->str);
647+ g_string_truncate (expected, 0);
648+
649+ // state unknown
650+ g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
651+ INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
652+ NULL);
653+ g_string_append_printf (expected, "%s-missing-symbolic;", kind_str);
654+ g_string_append_printf (expected, "gpm-%s-missing;", kind_str);
655+ g_string_append_printf (expected, "%s-missing", kind_str);
656+ check_icon_names (device, expected->str);
657+ g_string_truncate (expected, 0);
658+ }
659+ g_string_free (expected, TRUE);
660
661 // cleanup
662 g_object_unref(o);
663@@ -479,3 +531,77 @@
664 g_setenv ("LANG", real_lang, TRUE);
665 g_free (real_lang);
666 }
667+
668+static void
669+set_device_charge_state (IndicatorPowerDevice * device, int state, int time, double pct)
670+{
671+ g_object_set (device, INDICATOR_POWER_DEVICE_STATE, state,
672+ INDICATOR_POWER_DEVICE_PERCENTAGE, pct,
673+ INDICATOR_POWER_DEVICE_TIME, guint64(time),
674+ NULL);
675+}
676+
677+
678+/* The menu title should tell you at a glance what you need to know most: what
679+ device will lose power soonest (and optionally when), or otherwise which
680+ device will take longest to charge (and optionally how long it will take). */
681+TEST_F(DeviceTest, ChoosePrimary)
682+{
683+ GSList * devices;
684+ IndicatorPowerDevice * a;
685+ IndicatorPowerDevice * b;
686+
687+ a = indicator_power_device_new ("/org/freedesktop/UPower/devices/mouse",
688+ UP_DEVICE_KIND_MOUSE,
689+ 0.0,
690+ UP_DEVICE_STATE_DISCHARGING,
691+ 0);
692+ b = indicator_power_device_new ("/org/freedesktop/UPower/devices/battery",
693+ UP_DEVICE_KIND_BATTERY,
694+ 0.0,
695+ UP_DEVICE_STATE_DISCHARGING,
696+ 0);
697+
698+ devices = NULL;
699+ devices = g_slist_append (devices, a);
700+ devices = g_slist_append (devices, b);
701+
702+ /* Both discharging, same charge %, different times left before empty.
703+ Confirm that the one with less time is chosen. */
704+ set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0);
705+ set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0);
706+ ASSERT_EQ (a, indicator_power_choose_primary_device(devices));
707+
708+ /* Both discharging, different charge % and times left.
709+ Confirm that the one with less time is chosen. */
710+ set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0);
711+ set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 49.0);
712+ ASSERT_EQ (a, indicator_power_choose_primary_device(devices));
713+
714+ /* Both discharging, different charge %, same times left.
715+ Confirm that the one with less charge is chosen. */
716+ set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 100, 49.0);
717+ set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0);
718+ ASSERT_EQ (a, indicator_power_choose_primary_device(devices));
719+
720+ /* Both are charging, have the same charge percentage, and different times left (to charge).
721+ * Confirm that the one with the most time left is chosen. */
722+ set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0);
723+ set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0);
724+ ASSERT_EQ (b, indicator_power_choose_primary_device(devices));
725+
726+ /* Both are charging, with different charges and time left.
727+ Confirm that the one with the most time left is chosen. */
728+ set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0);
729+ set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0);
730+ ASSERT_EQ (b, indicator_power_choose_primary_device(devices));
731+
732+ /* Both are charging, have the same time left, and different charges.
733+ * Confirm that the one with less charge is chosen. */
734+ set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0);
735+ set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0);
736+ ASSERT_EQ (b, indicator_power_choose_primary_device(devices));
737+
738+ // cleanup
739+ g_slist_free_full (devices, g_object_unref);
740+}
741
742=== modified file 'tests/test-indicator.cc'
743--- tests/test-indicator.cc 2012-05-31 18:08:27 +0000
744+++ tests/test-indicator.cc 2012-10-26 08:55:25 +0000
745@@ -29,6 +29,16 @@
746
747 namespace
748 {
749+ void quiet_log_func (const gchar *log_domain,
750+ GLogLevelFlags log_level,
751+ const gchar *message,
752+ gpointer user_data)
753+ {
754+ // instantiating an indicator w/o a window causes lots
755+ // of glib/gtk warnings... silence them so that they don't
756+ // obscure any other warnings generated by the tests.
757+ }
758+
759 void ensure_glib_initialized ()
760 {
761 static bool initialized = false;
762@@ -37,6 +47,8 @@
763 {
764 initialized = true;
765 g_type_init();
766+ g_log_set_handler ("Gtk", (GLogLevelFlags)(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING), quiet_log_func, NULL);
767+ g_log_set_handler ("GLib-GObject", (GLogLevelFlags)(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING), quiet_log_func, NULL);
768 }
769 }
770 }
771@@ -71,6 +83,8 @@
772
773 virtual void TearDown()
774 {
775+ ASSERT_EQ (1, G_OBJECT(battery_device)->ref_count);
776+ ASSERT_EQ (1, G_OBJECT(ac_device)->ref_count);
777 g_object_unref (battery_device);
778 g_object_unref (ac_device);
779 }

Subscribers

People subscribed via source and target branches