1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
/*
Copyright 2012 Canonical Ltd.
Authors:
Charles Kerr <charles.kerr@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include "device.h"
#include "indicator-power.h"
namespace
{
void ensure_glib_initialized ()
{
static bool initialized = false;
if (G_UNLIKELY(!initialized))
{
initialized = true;
g_type_init();
}
}
}
class DeviceTest : public ::testing::Test
{
private:
guint log_handler_id;
int log_count_ipower_actual;
static void log_count_func (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
reinterpret_cast<DeviceTest*>(user_data)->log_count_ipower_actual++;
}
protected:
int log_count_ipower_expected;
protected:
virtual void SetUp()
{
const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
log_handler_id = g_log_set_handler ("Indicator-Power", flags, log_count_func, this);
log_count_ipower_expected = 0;
log_count_ipower_actual = 0;
ensure_glib_initialized ();
}
virtual void TearDown()
{
ASSERT_EQ (log_count_ipower_expected, log_count_ipower_actual);
g_log_remove_handler ("Indicator-Power", log_handler_id);
}
protected:
void check_icon_names (const IndicatorPowerDevice * device, const char * expected)
{
char ** names = indicator_power_device_get_icon_names (device);
char * str = g_strjoinv (";", names);
ASSERT_STREQ (expected, str);
g_free (str);
g_strfreev (names);
}
void check_strings (const IndicatorPowerDevice * device,
const char * expected_timestring,
const char * expected_details,
const char * expected_accessible)
{
char * timestring = NULL;
char * details = NULL;
char * accessible = NULL;
indicator_power_device_get_time_details (device, ×tring, &details, &accessible);
EXPECT_STREQ (expected_timestring, timestring);
EXPECT_STREQ (expected_details, details);
EXPECT_STREQ (expected_accessible, accessible);
g_free (accessible);
g_free (details);
g_free (timestring);
}
};
/***
****
***/
TEST_F(DeviceTest, GObjectNew)
{
ensure_glib_initialized ();
GObject * o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
ASSERT_TRUE (o != NULL);
ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o));
g_object_run_dispose (o); // used to get coverage of both branches in the object's dispose func's g_clear_*() calls
g_object_unref (o);
}
TEST_F(DeviceTest, Properties)
{
int i;
gdouble d;
GObject * o;
gchar * str;
guint64 u64;
const gchar * key;
ensure_glib_initialized ();
o = G_OBJECT (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
ASSERT_TRUE (o != NULL);
ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(o));
// Test getting & setting a Device's properties.
// KIND
key = INDICATOR_POWER_DEVICE_KIND;
g_object_set (o, key, UP_DEVICE_KIND_BATTERY, NULL);
g_object_get (o, key, &i, NULL);
ASSERT_EQ (i, UP_DEVICE_KIND_BATTERY);
// STATE
key = INDICATOR_POWER_DEVICE_STATE;
g_object_set (o, key, UP_DEVICE_STATE_CHARGING, NULL);
g_object_get (o, key, &i, NULL);
ASSERT_EQ (i, UP_DEVICE_STATE_CHARGING);
// OBJECT_PATH
key = INDICATOR_POWER_DEVICE_OBJECT_PATH;
g_object_set (o, key, "/object/path", NULL);
g_object_get (o, key, &str, NULL);
ASSERT_STREQ (str, "/object/path");
g_free (str);
// PERCENTAGE
key = INDICATOR_POWER_DEVICE_PERCENTAGE;
g_object_set (o, key, 50.0, NULL);
g_object_get (o, key, &d, NULL);
ASSERT_EQ((int)d, 50);
// TIME
key = INDICATOR_POWER_DEVICE_TIME;
g_object_set (o, key, (guint64)30, NULL);
g_object_get (o, key, &u64, NULL);
ASSERT_EQ(u64, 30);
// cleanup
g_object_unref (o);
}
TEST_F(DeviceTest, New)
{
ensure_glib_initialized ();
IndicatorPowerDevice * device = indicator_power_device_new ("/object/path",
UP_DEVICE_KIND_BATTERY,
50.0,
UP_DEVICE_STATE_CHARGING,
30);
ASSERT_TRUE (device != NULL);
ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device));
ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY);
ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING);
ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path");
ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50);
ASSERT_EQ (indicator_power_device_get_time(device), 30);
// cleanup
g_object_unref (device);
}
TEST_F(DeviceTest, NewFromVariant)
{
ensure_glib_initialized ();
GVariant * variant = g_variant_new ("(susdut)",
"/object/path",
UP_DEVICE_KIND_BATTERY,
"icon",
50.0,
UP_DEVICE_STATE_CHARGING,
30);
IndicatorPowerDevice * device = indicator_power_device_new_from_variant (variant);
ASSERT_TRUE (variant != NULL);
ASSERT_TRUE (device != NULL);
ASSERT_TRUE (INDICATOR_IS_POWER_DEVICE(device));
ASSERT_EQ (indicator_power_device_get_kind(device), UP_DEVICE_KIND_BATTERY);
ASSERT_EQ (indicator_power_device_get_state(device), UP_DEVICE_STATE_CHARGING);
ASSERT_STREQ (indicator_power_device_get_object_path(device), "/object/path");
ASSERT_EQ ((int)indicator_power_device_get_percentage(device), 50);
ASSERT_EQ (indicator_power_device_get_time(device), 30);
// cleanup
g_object_unref (device);
g_variant_unref (variant);
}
TEST_F(DeviceTest, BadAccessors)
{
ensure_glib_initialized ();
// test that these functions can handle being passed NULL pointers
IndicatorPowerDevice * device = NULL;
indicator_power_device_get_kind (device);
indicator_power_device_get_time (device);
indicator_power_device_get_state (device);
indicator_power_device_get_percentage (device);
indicator_power_device_get_object_path (device);
log_count_ipower_expected += 5;
// test that these functions can handle being passed non-device GObjects
device = reinterpret_cast<IndicatorPowerDevice*>(g_cancellable_new ());
indicator_power_device_get_kind (device);
indicator_power_device_get_time (device);
indicator_power_device_get_state (device);
indicator_power_device_get_percentage (device);
indicator_power_device_get_object_path (device);
log_count_ipower_expected += 5;
g_object_unref (device);
}
/***
****
***/
TEST_F(DeviceTest, IconNames)
{
IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
GObject * o = G_OBJECT(device);
// bad arguments
log_count_ipower_expected++;
ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL);
// power
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER,
NULL);
check_icon_names (device, "ac-adapter-symbolic;"
"ac-adapter");
// monitor
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_MONITOR,
NULL);
check_icon_names (device, "gpm-monitor-symbolic;"
"gpm-monitor");
// devices that hold a charge
struct {
int kind;
const gchar * kind_str;
} devices[] = {
{ UP_DEVICE_KIND_BATTERY, "battery" },
{ UP_DEVICE_KIND_UPS, "ups" },
{ UP_DEVICE_KIND_MOUSE, "mouse" },
{ UP_DEVICE_KIND_KEYBOARD, "keyboard" },
{ UP_DEVICE_KIND_PHONE, "phone" }
};
GString * expected = g_string_new (NULL);
for (int i=0, n=G_N_ELEMENTS(devices); i<n; i++)
{
const int kind = devices[i].kind;
const gchar * kind_str = devices[i].kind_str;
// empty
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_EMPTY,
NULL);
g_string_append_printf (expected, "%s-empty-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-empty;", kind_str);
g_string_append_printf (expected, "gpm-%s-000;", kind_str);
g_string_append_printf (expected, "%s-empty", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// charged
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
NULL);
g_string_append_printf (expected, "%s-full-charged-symbolic;", kind_str);
g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-full;", kind_str);
g_string_append_printf (expected, "gpm-%s-100;", kind_str);
g_string_append_printf (expected, "%s-full-charged;", kind_str);
g_string_append_printf (expected, "%s-full-charging", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// charging, 95%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
NULL);
g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
g_string_append_printf (expected, "%s-caution-charging", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// charging, 85%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
NULL);
g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
g_string_append_printf (expected, "%s-caution-charging", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// charging, 50%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
NULL);
g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
g_string_append_printf (expected, "%s-caution-charging", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// charging, 25%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
NULL);
g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
g_string_append_printf (expected, "%s-caution-charging", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// charging, 5%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
NULL);
g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
g_string_append_printf (expected, "%s-caution-charging", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 95%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 95.0,
NULL);
g_string_append_printf (expected, "%s-100;", kind_str);
g_string_append_printf (expected, "gpm-%s-100;", kind_str);
g_string_append_printf (expected, "%s-full-symbolic;", kind_str);
g_string_append_printf (expected, "%s-full", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 85%
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 85.0,
NULL);
g_string_append_printf (expected, "%s-080;", kind_str);
g_string_append_printf (expected, "gpm-%s-080;", kind_str);
g_string_append_printf (expected, "%s-full-symbolic;", kind_str);
g_string_append_printf (expected, "%s-full", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 50% -- 1 hour left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
NULL);
g_string_append_printf (expected, "%s-060;", kind_str);
g_string_append_printf (expected, "gpm-%s-060;", kind_str);
g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
g_string_append_printf (expected, "%s-good", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 25% -- 1 hour left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
NULL);
g_string_append_printf (expected, "%s-040;", kind_str);
g_string_append_printf (expected, "gpm-%s-040;", kind_str);
g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
g_string_append_printf (expected, "%s-good", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 25% -- 15 minutes left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 25.0,
INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
NULL);
g_string_append_printf (expected, "%s-020;", kind_str);
g_string_append_printf (expected, "gpm-%s-020;", kind_str);
g_string_append_printf (expected, "%s-low-symbolic;", kind_str);
g_string_append_printf (expected, "%s-low", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 5% -- 1 hour left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
INDICATOR_POWER_DEVICE_TIME, (guint64)(60*60),
NULL);
g_string_append_printf (expected, "%s-040;", kind_str);
g_string_append_printf (expected, "gpm-%s-040;", kind_str);
g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
g_string_append_printf (expected, "%s-good", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// discharging, 5% -- 15 minutes left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 5.0,
INDICATOR_POWER_DEVICE_TIME, (guint64)(60*15),
NULL);
g_string_append_printf (expected, "%s-000;", kind_str);
g_string_append_printf (expected, "gpm-%s-000;", kind_str);
g_string_append_printf (expected, "%s-caution-symbolic;", kind_str);
g_string_append_printf (expected, "%s-caution", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
// state unknown
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
NULL);
g_string_append_printf (expected, "%s-missing-symbolic;", kind_str);
g_string_append_printf (expected, "gpm-%s-missing;", kind_str);
g_string_append_printf (expected, "%s-missing", kind_str);
check_icon_names (device, expected->str);
g_string_truncate (expected, 0);
}
g_string_free (expected, TRUE);
// cleanup
g_object_unref(o);
}
TEST_F(DeviceTest, Labels)
{
// set our language so that i18n won't break these tests
char * real_lang = g_strdup(g_getenv ("LANG"));
g_setenv ("LANG", "en_US.UTF-8", TRUE);
// bad args: NULL device
log_count_ipower_expected++;
check_strings (NULL, NULL, NULL, NULL);
// bad args: a GObject that isn't a device
log_count_ipower_expected++;
GObject * o = G_OBJECT(g_cancellable_new());
check_strings ((IndicatorPowerDevice*)o, NULL, NULL, NULL);
g_object_unref (o);
/**
***
**/
IndicatorPowerDevice * device = INDICATOR_POWER_DEVICE (g_object_new (INDICATOR_POWER_DEVICE_TYPE, NULL));
o = G_OBJECT(device);
// charging
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*61),
NULL);
check_strings (device, "(1:01)",
"Battery (1:01 to charge)",
"Battery (1 hour 1 minute to charge (50%))");
// discharging, < 12 hours left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*61),
NULL);
check_strings (device, "1:01",
"Battery (1:01 left)",
"Battery (1 hour 1 minute left (50%))");
// discharging, > 12 hours left
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(60*60*13),
NULL);
check_strings (device, "13:00", "Battery", "Battery");
// fully charged
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_FULLY_CHARGED,
INDICATOR_POWER_DEVICE_PERCENTAGE, 100.0,
INDICATOR_POWER_DEVICE_TIME, guint64(0),
NULL);
check_strings (device, "", "Battery (charged)", "Battery (charged)");
// percentage but no time estimate
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 50.0,
INDICATOR_POWER_DEVICE_TIME, guint64(0),
NULL);
check_strings (device, "(50%)", "Battery (50%)", "Battery (50%)");
// no percentage, no time estimate
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_BATTERY,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING,
INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
INDICATOR_POWER_DEVICE_TIME, guint64(0),
NULL);
check_strings (device, "(not present)", "Battery (not present)", "Battery (not present)");
// power line
g_object_set (o, INDICATOR_POWER_DEVICE_KIND, UP_DEVICE_KIND_LINE_POWER,
INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
INDICATOR_POWER_DEVICE_PERCENTAGE, 0.0,
INDICATOR_POWER_DEVICE_TIME, guint64(0),
NULL);
check_strings (device, "AC Adapter", "AC Adapter", "AC Adapter");
// cleanup
g_object_unref(o);
g_setenv ("LANG", real_lang, TRUE);
g_free (real_lang);
}
/* The menu title should tell you at a glance what you need to know most: what
device will lose power soonest (and optionally when), or otherwise which
device will take longest to charge (and optionally how long it will take). */
TEST_F(DeviceTest, ChoosePrimary)
{
GSList * device_list;
IndicatorPowerDevice * a;
IndicatorPowerDevice * b;
a = indicator_power_device_new ("/org/freedesktop/UPower/devices/mouse",
UP_DEVICE_KIND_MOUSE,
0.0,
UP_DEVICE_STATE_DISCHARGING,
0);
b = indicator_power_device_new ("/org/freedesktop/UPower/devices/battery",
UP_DEVICE_KIND_BATTERY,
0.0,
UP_DEVICE_STATE_DISCHARGING,
0);
/* device states + time left to {discharge,charge} + % of charge left,
sorted in order of preference wrt the spec's criteria.
So tests[i] should be picked over any test with an index greater than i */
struct {
int kind;
int state;
guint64 time;
double percentage;
} tests[] = {
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 49, 50.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 50, 50.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 50, 100.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 51, 50.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 50, 50.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 49, 50.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 49, 100.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 48, 50.0 },
{ UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_FULLY_CHARGED, 0, 50.0 },
{ UP_DEVICE_KIND_KEYBOARD, UP_DEVICE_STATE_FULLY_CHARGED, 0, 50.0 },
{ UP_DEVICE_KIND_LINE_POWER, UP_DEVICE_STATE_UNKNOWN, 0, 0.0 }
};
device_list = NULL;
device_list = g_slist_append (device_list, a);
device_list = g_slist_append (device_list, b);
for (int i=0, n=G_N_ELEMENTS(tests); i<n; i++)
{
for (int j=i+1; j<n; j++)
{
g_object_set (a, INDICATOR_POWER_DEVICE_KIND, tests[i].kind,
INDICATOR_POWER_DEVICE_STATE, tests[i].state,
INDICATOR_POWER_DEVICE_TIME, guint64(tests[i].time),
INDICATOR_POWER_DEVICE_PERCENTAGE, tests[i].percentage,
NULL);
g_object_set (b, INDICATOR_POWER_DEVICE_KIND, tests[j].kind,
INDICATOR_POWER_DEVICE_STATE, tests[j].state,
INDICATOR_POWER_DEVICE_TIME, guint64(tests[j].time),
INDICATOR_POWER_DEVICE_PERCENTAGE, tests[j].percentage,
NULL);
ASSERT_EQ (a, indicator_power_choose_primary_device(device_list));
/* reverse the list to check that list order doesn't matter */
device_list = g_slist_reverse (device_list);
ASSERT_EQ (a, indicator_power_choose_primary_device(device_list));
}
}
// cleanup
g_slist_free_full (device_list, g_object_unref);
}
|