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

Proposed by Charles Kerr
Status: Merged
Approved by: Charles Kerr
Approved revision: 157
Merged at revision: 152
Proposed branch: lp:~charlesk/indicator-power/lp-1071757
Merge into: lp:indicator-power/13.04
Diff against target: 217 lines (+80/-56)
2 files modified
src/indicator-power.c (+4/-4)
tests/test-device.cc (+76/-52)
To merge this branch: bzr merge lp:~charlesk/indicator-power/lp-1071757
Reviewer Review Type Date Requested Status
Lars Karlitski (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+131767@code.launchpad.net

Commit message

Fix the logic error. Add tests to confirm the fix.

Description of the change

Fix the logic error. Add tests to confirm the fix.

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

in TestDevice, make the log variable names a little more consistent

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Lars Karlitski (larsu) wrote :

Looks good, except that one of the `b is charging` should be `a is charging`.

review: Approve
157. By Charles Kerr

copyediting: fix comment text

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

Jenkins and Lars have approved, so marking the merge request as Approved for Jenkins

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/indicator-power.c'
2--- src/indicator-power.c 2012-10-26 08:02:14 +0000
3+++ src/indicator-power.c 2012-10-29 11:03:23 +0000
4@@ -399,12 +399,12 @@
5 state = UP_DEVICE_STATE_CHARGING;
6 if (!ret && (((a_state == state) && a_time) || ((b_state == state) && b_time)))
7 {
8- if (b_state != state) /* a is charging */
9- {
10- ret = 1;
11- }
12 if (a_state != state) /* b is charging */
13 {
14+ ret = 1;
15+ }
16+ else if (b_state != state) /* a is charging */
17+ {
18 ret = -1;
19 }
20 else /* both are discharging; most-time-to-charge goes first */
21
22=== modified file 'tests/test-device.cc'
23--- tests/test-device.cc 2012-10-26 08:53:32 +0000
24+++ tests/test-device.cc 2012-10-29 11:03:23 +0000
25@@ -37,15 +37,40 @@
26
27 class DeviceTest : public ::testing::Test
28 {
29+ private:
30+
31+ guint log_handler_id;
32+
33+ int log_count_ipower_actual;
34+
35+ static void log_count_func (const gchar *log_domain,
36+ GLogLevelFlags log_level,
37+ const gchar *message,
38+ gpointer user_data)
39+ {
40+ reinterpret_cast<DeviceTest*>(user_data)->log_count_ipower_actual++;
41+ }
42+
43+ protected:
44+
45+ int log_count_ipower_expected;
46+
47 protected:
48
49 virtual void SetUp()
50 {
51+ const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
52+ log_handler_id = g_log_set_handler ("Indicator-Power", flags, log_count_func, this);
53+ log_count_ipower_expected = 0;
54+ log_count_ipower_actual = 0;
55+
56 ensure_glib_initialized ();
57 }
58
59 virtual void TearDown()
60 {
61+ ASSERT_EQ (log_count_ipower_expected, log_count_ipower_actual);
62+ g_log_remove_handler ("Indicator-Power", log_handler_id);
63 }
64
65 protected:
66@@ -205,6 +230,7 @@
67 indicator_power_device_get_state (device);
68 indicator_power_device_get_percentage (device);
69 indicator_power_device_get_object_path (device);
70+ log_count_ipower_expected += 5;
71
72 // test that these functions can handle being passed non-device GObjects
73 device = reinterpret_cast<IndicatorPowerDevice*>(g_cancellable_new ());
74@@ -213,6 +239,8 @@
75 indicator_power_device_get_state (device);
76 indicator_power_device_get_percentage (device);
77 indicator_power_device_get_object_path (device);
78+ log_count_ipower_expected += 5;
79+
80 g_object_unref (device);
81 }
82
83@@ -226,6 +254,7 @@
84 GObject * o = G_OBJECT(device);
85
86 // bad arguments
87+ log_count_ipower_expected++;
88 ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL);
89
90 // power
91@@ -452,9 +481,11 @@
92 g_setenv ("LANG", "en_US.UTF-8", TRUE);
93
94 // bad args: NULL device
95+ log_count_ipower_expected++;
96 check_strings (NULL, NULL, NULL, NULL);
97
98 // bad args: a GObject that isn't a device
99+ log_count_ipower_expected++;
100 GObject * o = G_OBJECT(g_cancellable_new());
101 check_strings ((IndicatorPowerDevice*)o, NULL, NULL, NULL);
102 g_object_unref (o);
103@@ -532,22 +563,12 @@
104 g_free (real_lang);
105 }
106
107-static void
108-set_device_charge_state (IndicatorPowerDevice * device, int state, int time, double pct)
109-{
110- g_object_set (device, INDICATOR_POWER_DEVICE_STATE, state,
111- INDICATOR_POWER_DEVICE_PERCENTAGE, pct,
112- INDICATOR_POWER_DEVICE_TIME, guint64(time),
113- NULL);
114-}
115-
116-
117 /* The menu title should tell you at a glance what you need to know most: what
118 device will lose power soonest (and optionally when), or otherwise which
119 device will take longest to charge (and optionally how long it will take). */
120 TEST_F(DeviceTest, ChoosePrimary)
121 {
122- GSList * devices;
123+ GSList * device_list;
124 IndicatorPowerDevice * a;
125 IndicatorPowerDevice * b;
126
127@@ -562,46 +583,49 @@
128 UP_DEVICE_STATE_DISCHARGING,
129 0);
130
131- devices = NULL;
132- devices = g_slist_append (devices, a);
133- devices = g_slist_append (devices, b);
134-
135- /* Both discharging, same charge %, different times left before empty.
136- Confirm that the one with less time is chosen. */
137- set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0);
138- set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0);
139- ASSERT_EQ (a, indicator_power_choose_primary_device(devices));
140-
141- /* Both discharging, different charge % and times left.
142- Confirm that the one with less time is chosen. */
143- set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0);
144- set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 49.0);
145- ASSERT_EQ (a, indicator_power_choose_primary_device(devices));
146-
147- /* Both discharging, different charge %, same times left.
148- Confirm that the one with less charge is chosen. */
149- set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 100, 49.0);
150- set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0);
151- ASSERT_EQ (a, indicator_power_choose_primary_device(devices));
152-
153- /* Both are charging, have the same charge percentage, and different times left (to charge).
154- * Confirm that the one with the most time left is chosen. */
155- set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0);
156- set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0);
157- ASSERT_EQ (b, indicator_power_choose_primary_device(devices));
158-
159- /* Both are charging, with different charges and time left.
160- Confirm that the one with the most time left is chosen. */
161- set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0);
162- set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0);
163- ASSERT_EQ (b, indicator_power_choose_primary_device(devices));
164-
165- /* Both are charging, have the same time left, and different charges.
166- * Confirm that the one with less charge is chosen. */
167- set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0);
168- set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0);
169- ASSERT_EQ (b, indicator_power_choose_primary_device(devices));
170-
171+ /* device states + time left to {discharge,charge} + % of charge left,
172+ sorted in order of preference wrt the spec's criteria.
173+ So tests[i] should be picked over any test with an index greater than i */
174+ struct {
175+ int state;
176+ guint64 time;
177+ double percentage;
178+ } tests[] = {
179+ { UP_DEVICE_STATE_DISCHARGING, 49, 50.0 },
180+ { UP_DEVICE_STATE_DISCHARGING, 50, 50.0 },
181+ { UP_DEVICE_STATE_DISCHARGING, 50, 100.0 },
182+ { UP_DEVICE_STATE_DISCHARGING, 51, 50.0 },
183+ { UP_DEVICE_STATE_CHARGING, 50, 50.0 },
184+ { UP_DEVICE_STATE_CHARGING, 49, 50.0 },
185+ { UP_DEVICE_STATE_CHARGING, 49, 100.0 },
186+ { UP_DEVICE_STATE_CHARGING, 48, 50.0 },
187+ { UP_DEVICE_STATE_FULLY_CHARGED, 0, 50.0 }
188+ };
189+
190+ device_list = NULL;
191+ device_list = g_slist_append (device_list, a);
192+ device_list = g_slist_append (device_list, b);
193+
194+ for (int i=0, n=G_N_ELEMENTS(tests); i<n; i++)
195+ {
196+ for (int j=i+1; j<n; j++)
197+ {
198+ g_object_set (a, INDICATOR_POWER_DEVICE_STATE, tests[i].state,
199+ INDICATOR_POWER_DEVICE_TIME, guint64(tests[i].time),
200+ INDICATOR_POWER_DEVICE_PERCENTAGE, tests[i].percentage,
201+ NULL);
202+ g_object_set (b, INDICATOR_POWER_DEVICE_STATE, tests[j].state,
203+ INDICATOR_POWER_DEVICE_TIME, guint64(tests[j].time),
204+ INDICATOR_POWER_DEVICE_PERCENTAGE, tests[j].percentage,
205+ NULL);
206+ ASSERT_EQ (a, indicator_power_choose_primary_device(device_list));
207+
208+ /* reverse the list to check that list order doesn't matter */
209+ device_list = g_slist_reverse (device_list);
210+ ASSERT_EQ (a, indicator_power_choose_primary_device(device_list));
211+ }
212+ }
213+
214 // cleanup
215- g_slist_free_full (devices, g_object_unref);
216+ g_slist_free_full (device_list, g_object_unref);
217 }

Subscribers

People subscribed via source and target branches