Merge lp:~charlesk/indicator-power/lp-1470080-missing-icon-when-apple-devices-connected into lp:indicator-power
- lp-1470080-missing-icon-when-apple-devices-connected
- Merge into trunk.16.10
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | Ted Gould | ||||
Approved revision: | 302 | ||||
Merged at revision: | 296 | ||||
Proposed branch: | lp:~charlesk/indicator-power/lp-1470080-missing-icon-when-apple-devices-connected | ||||
Merge into: | lp:indicator-power | ||||
Diff against target: |
510 lines (+291/-112) 3 files modified
src/device.c (+2/-1) src/service.c (+19/-1) tests/test-device.cc (+270/-110) |
||||
To merge this branch: | bzr merge lp:~charlesk/indicator-power/lp-1470080-missing-icon-when-apple-devices-connected | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ted Gould (community) | Approve | ||
PS Jenkins bot | continuous-integration | Pending | |
Review via email: mp+295752@code.launchpad.net |
Commit message
Fix bug that chose the wrong header icon if a connected device has a charge but its charging/
Description of the change
1. If we know a upower device's charge level, but not its state (eg whether charging, discharging, etc), then use the same icons as when it’s discharging. (mpt in bug comment 10)
2. When choosing which upower device to show as the ``primary'' (ie the one used in the indicator icon), we shouldn’t choose a device in the menu title when we don’t know whether it’s charging or discharging, if for other things we *do* know whether they’re charging or discharging. (mpt in bug comment 10)
3. Clean up the device tests to be more readable. (ted)
4. If foo-charging icon isn't present, fall back to foo icon. (charles, seb128)
Ted Gould (ted) wrote : | # |
That's a crazy number of alternative icons. But works for me.
Preview Diff
1 | === modified file 'src/device.c' |
2 | --- src/device.c 2016-05-01 22:29:56 +0000 |
3 | +++ src/device.c 2016-05-26 19:05:02 +0000 |
4 | @@ -434,11 +434,12 @@ |
5 | } |
6 | g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging-symbolic", kind_str, suffix_str)); |
7 | g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging", kind_str, suffix_str)); |
8 | - break; |
9 | + // NB: fallthrough to use foo-bar as a fallback for foo-bar-charging |
10 | |
11 | case UP_DEVICE_STATE_PENDING_CHARGE: |
12 | case UP_DEVICE_STATE_DISCHARGING: |
13 | case UP_DEVICE_STATE_PENDING_DISCHARGE: |
14 | + case UP_DEVICE_STATE_UNKNOWN: /* http://pad.lv/1470080 */ |
15 | suffix_str = get_device_icon_suffix (percentage); |
16 | index_str = get_closest_10_percent_percentage (percentage); |
17 | g_ptr_array_add (names, g_strdup_printf ("%s-%s", kind_str, index_str)); |
18 | |
19 | === modified file 'src/service.c' |
20 | --- src/service.c 2016-01-02 02:19:45 +0000 |
21 | +++ src/service.c 2016-05-26 19:05:02 +0000 |
22 | @@ -245,7 +245,25 @@ |
23 | } |
24 | } |
25 | |
26 | - if (!ret) /* neither device is charging nor discharging... */ |
27 | + /* neither device is charging nor discharging... */ |
28 | + |
29 | + /* unless there's no other option, |
30 | + don't choose a device with an unknown state. |
31 | + https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080 */ |
32 | + state = UP_DEVICE_STATE_UNKNOWN; |
33 | + if (!ret && ((a_state == state) || (b_state == state))) |
34 | + { |
35 | + if (a_state != state) /* b is unknown */ |
36 | + { |
37 | + ret = -1; |
38 | + } |
39 | + else if (b_state != state) /* a is unknown */ |
40 | + { |
41 | + ret = 1; |
42 | + } |
43 | + } |
44 | + |
45 | + if (!ret) |
46 | { |
47 | const int weight_a = get_device_kind_weight (a); |
48 | const int weight_b = get_device_kind_weight (b); |
49 | |
50 | === modified file 'tests/test-device.cc' |
51 | --- tests/test-device.cc 2016-05-16 21:33:30 +0000 |
52 | +++ tests/test-device.cc 2016-05-26 19:05:02 +0000 |
53 | @@ -1,27 +1,32 @@ |
54 | /* |
55 | -Copyright 2012 Canonical Ltd. |
56 | - |
57 | -Authors: |
58 | - Charles Kerr <charles.kerr@canonical.com> |
59 | - |
60 | -This program is free software: you can redistribute it and/or modify it |
61 | -under the terms of the GNU General Public License version 3, as published |
62 | -by the Free Software Foundation. |
63 | - |
64 | -This program is distributed in the hope that it will be useful, but |
65 | -WITHOUT ANY WARRANTY; without even the implied warranties of |
66 | -MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
67 | -PURPOSE. See the GNU General Public License for more details. |
68 | - |
69 | -You should have received a copy of the GNU General Public License along |
70 | -with this program. If not, see <http://www.gnu.org/licenses/>. |
71 | -*/ |
72 | + * Copyright 2012-2016 Canonical Ltd. |
73 | + * |
74 | + * This program is free software: you can redistribute it and/or modify it |
75 | + * under the terms of the GNU General Public License version 3, as published |
76 | + * by the Free Software Foundation. |
77 | + * |
78 | + * This program is distributed in the hope that it will be useful, but |
79 | + * WITHOUT ANY WARRANTY; without even the implied warranties of |
80 | + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
81 | + * PURPOSE. See the GNU General Public License for more details. |
82 | + * |
83 | + * You should have received a copy of the GNU General Public License along |
84 | + * with this program. If not, see <http://www.gnu.org/licenses/>. |
85 | + * |
86 | + * Authors: |
87 | + * Charles Kerr <charles.kerr@canonical.com> |
88 | + */ |
89 | + |
90 | +#include "device.h" |
91 | +#include "service.h" |
92 | |
93 | #include <gio/gio.h> |
94 | #include <gtest/gtest.h> |
95 | + |
96 | +#include <algorithm> |
97 | #include <cmath> // ceil() |
98 | -#include "device.h" |
99 | -#include "service.h" |
100 | +#include <string> |
101 | + |
102 | |
103 | class DeviceTest : public ::testing::Test |
104 | { |
105 | @@ -337,7 +342,11 @@ |
106 | g_string_append_printf (expected, "%s-100-charging;", kind_str); |
107 | g_string_append_printf (expected, "gpm-%s-100-charging;", kind_str); |
108 | g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str); |
109 | - g_string_append_printf (expected, "%s-full-charging", kind_str); |
110 | + g_string_append_printf (expected, "%s-full-charging;", kind_str); |
111 | + g_string_append_printf (expected, "%s-100;", kind_str); |
112 | + g_string_append_printf (expected, "gpm-%s-100;", kind_str); |
113 | + g_string_append_printf (expected, "%s-full-symbolic;", kind_str); |
114 | + g_string_append_printf (expected, "%s-full", kind_str); |
115 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
116 | g_string_truncate (expected, 0); |
117 | |
118 | @@ -351,7 +360,13 @@ |
119 | g_string_append_printf (expected, "%s-080-charging;", kind_str); |
120 | g_string_append_printf (expected, "gpm-%s-080-charging;", kind_str); |
121 | g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str); |
122 | - g_string_append_printf (expected, "%s-full-charging", kind_str); |
123 | + g_string_append_printf (expected, "%s-full-charging;", kind_str); |
124 | + g_string_append_printf (expected, "%s-090;", kind_str); |
125 | + g_string_append_printf (expected, "gpm-%s-090;", kind_str); |
126 | + g_string_append_printf (expected, "%s-080;", kind_str); |
127 | + g_string_append_printf (expected, "gpm-%s-080;", kind_str); |
128 | + g_string_append_printf (expected, "%s-full-symbolic;", kind_str); |
129 | + g_string_append_printf (expected, "%s-full", kind_str); |
130 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
131 | g_string_truncate (expected, 0); |
132 | |
133 | @@ -365,7 +380,13 @@ |
134 | g_string_append_printf (expected, "%s-060-charging;", kind_str); |
135 | g_string_append_printf (expected, "gpm-%s-060-charging;", kind_str); |
136 | g_string_append_printf (expected, "%s-good-charging-symbolic;", kind_str); |
137 | - g_string_append_printf (expected, "%s-good-charging", kind_str); |
138 | + g_string_append_printf (expected, "%s-good-charging;", kind_str); |
139 | + g_string_append_printf (expected, "%s-050;", kind_str); |
140 | + g_string_append_printf (expected, "gpm-%s-050;", kind_str); |
141 | + g_string_append_printf (expected, "%s-060;", kind_str); |
142 | + g_string_append_printf (expected, "gpm-%s-060;", kind_str); |
143 | + g_string_append_printf (expected, "%s-good-symbolic;", kind_str); |
144 | + g_string_append_printf (expected, "%s-good", kind_str); |
145 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
146 | g_string_truncate (expected, 0); |
147 | |
148 | @@ -379,7 +400,13 @@ |
149 | g_string_append_printf (expected, "%s-040-charging;", kind_str); |
150 | g_string_append_printf (expected, "gpm-%s-040-charging;", kind_str); |
151 | g_string_append_printf (expected, "%s-low-charging-symbolic;", kind_str); |
152 | - g_string_append_printf (expected, "%s-low-charging", kind_str); |
153 | + g_string_append_printf (expected, "%s-low-charging;", kind_str); |
154 | + g_string_append_printf (expected, "%s-030;", kind_str); |
155 | + g_string_append_printf (expected, "gpm-%s-030;", kind_str); |
156 | + g_string_append_printf (expected, "%s-040;", kind_str); |
157 | + g_string_append_printf (expected, "gpm-%s-040;", kind_str); |
158 | + g_string_append_printf (expected, "%s-low-symbolic;", kind_str); |
159 | + g_string_append_printf (expected, "%s-low", kind_str); |
160 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
161 | g_string_truncate (expected, 0); |
162 | |
163 | @@ -393,7 +420,13 @@ |
164 | g_string_append_printf (expected, "%s-000-charging;", kind_str); |
165 | g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str); |
166 | g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str); |
167 | - g_string_append_printf (expected, "%s-caution-charging", kind_str); |
168 | + g_string_append_printf (expected, "%s-caution-charging;", kind_str); |
169 | + g_string_append_printf (expected, "%s-010;", kind_str); |
170 | + g_string_append_printf (expected, "gpm-%s-010;", kind_str); |
171 | + g_string_append_printf (expected, "%s-000;", kind_str); |
172 | + g_string_append_printf (expected, "gpm-%s-000;", kind_str); |
173 | + g_string_append_printf (expected, "%s-caution-symbolic;", kind_str); |
174 | + g_string_append_printf (expected, "%s-caution", kind_str); |
175 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
176 | g_string_truncate (expected, 0); |
177 | |
178 | @@ -496,15 +529,13 @@ |
179 | g_string_append_printf (expected, "%s-caution-symbolic;", kind_str); |
180 | g_string_append_printf (expected, "%s-caution", kind_str); |
181 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
182 | - g_string_truncate (expected, 0); |
183 | |
184 | - // state unknown |
185 | - g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind, |
186 | - INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, |
187 | + // if we know the charge level, but not that it’s charging, |
188 | + // then we should use the same icons as when it’s discharging. |
189 | + // https://wiki.ubuntu.com/Power?action=diff&rev2=78&rev1=77 |
190 | + // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080 |
191 | + g_object_set (o, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN, |
192 | NULL); |
193 | - g_string_append_printf (expected, "%s-missing-symbolic;", kind_str); |
194 | - g_string_append_printf (expected, "gpm-%s-missing;", kind_str); |
195 | - g_string_append_printf (expected, "%s-missing", kind_str); |
196 | EXPECT_ICON_NAMES_EQ(expected->str, device); |
197 | g_string_truncate (expected, 0); |
198 | } |
199 | @@ -704,6 +735,101 @@ |
200 | g_free (real_lang); |
201 | } |
202 | |
203 | +namespace |
204 | +{ |
205 | + const std::array<std::pair<std::string,UpDeviceKind>,UP_DEVICE_KIND_LAST> kinds = { |
206 | + std::make_pair("unknown", UP_DEVICE_KIND_UNKNOWN), |
207 | + std::make_pair("line-power", UP_DEVICE_KIND_LINE_POWER), |
208 | + std::make_pair("battery", UP_DEVICE_KIND_BATTERY), |
209 | + std::make_pair("ups", UP_DEVICE_KIND_UPS), |
210 | + std::make_pair("monitor", UP_DEVICE_KIND_MONITOR), |
211 | + std::make_pair("mouse", UP_DEVICE_KIND_MOUSE), |
212 | + std::make_pair("keyboard", UP_DEVICE_KIND_KEYBOARD), |
213 | + std::make_pair("pda", UP_DEVICE_KIND_PDA), |
214 | + std::make_pair("phone", UP_DEVICE_KIND_PHONE), |
215 | + std::make_pair("media-player", UP_DEVICE_KIND_MEDIA_PLAYER), |
216 | + std::make_pair("tablet", UP_DEVICE_KIND_TABLET), |
217 | + std::make_pair("computer", UP_DEVICE_KIND_COMPUTER) |
218 | + }; |
219 | + const std::string& kind2str(UpDeviceKind kind) |
220 | + { |
221 | + return std::find_if( |
222 | + kinds.begin(), |
223 | + kinds.end(), |
224 | + [kind](decltype(kinds[0])& i){return i.second==kind;} |
225 | + )->first; |
226 | + } |
227 | + UpDeviceKind str2kind(const std::string& str) |
228 | + { |
229 | + return std::find_if( |
230 | + kinds.begin(), |
231 | + kinds.end(), |
232 | + [str](decltype(kinds[0])& i){return i.first==str;} |
233 | + )->second; |
234 | + } |
235 | + |
236 | + /** |
237 | + **/ |
238 | + |
239 | + const std::array<std::pair<std::string,UpDeviceState>,UP_DEVICE_STATE_LAST> states = |
240 | + { |
241 | + std::make_pair("unknown", UP_DEVICE_STATE_UNKNOWN), |
242 | + std::make_pair("charging", UP_DEVICE_STATE_CHARGING), |
243 | + std::make_pair("discharging", UP_DEVICE_STATE_DISCHARGING), |
244 | + std::make_pair("empty", UP_DEVICE_STATE_EMPTY), |
245 | + std::make_pair("charged", UP_DEVICE_STATE_FULLY_CHARGED), |
246 | + std::make_pair("pending-charge", UP_DEVICE_STATE_PENDING_CHARGE), |
247 | + std::make_pair("pending-discharge", UP_DEVICE_STATE_PENDING_DISCHARGE) |
248 | + }; |
249 | + const std::string& state2str(UpDeviceState state) |
250 | + { |
251 | + return std::find_if( |
252 | + states.begin(), |
253 | + states.end(), |
254 | + [state](decltype(states[0])& i){return i.second==state;} |
255 | + )->first; |
256 | + } |
257 | + UpDeviceState str2state(const std::string& str) |
258 | + { |
259 | + return std::find_if( |
260 | + states.begin(), |
261 | + states.end(), |
262 | + [str](decltype(states[0])& i){return i.first==str;} |
263 | + )->second; |
264 | + } |
265 | + |
266 | + /** |
267 | + **/ |
268 | + |
269 | + std::string device2str(IndicatorPowerDevice* device) |
270 | + { |
271 | + std::ostringstream o; |
272 | + const auto path = indicator_power_device_get_object_path(device); |
273 | + |
274 | + o << kind2str(indicator_power_device_get_kind(device)) |
275 | + << ' ' << state2str(indicator_power_device_get_state(device)) |
276 | + << ' ' << indicator_power_device_get_time(device)<<'m' |
277 | + << ' ' << int(ceil(indicator_power_device_get_percentage(device)))<<'%' |
278 | + << ' ' << (path ? path : "nopath"); |
279 | + |
280 | + return o.str(); |
281 | + } |
282 | + |
283 | + IndicatorPowerDevice* str2device(const std::string& str) |
284 | + { |
285 | + auto tokens = g_strsplit(str.c_str(), " ", 0); |
286 | + g_assert(5u == g_strv_length(tokens)); |
287 | + const auto kind = str2kind(tokens[0]); |
288 | + const auto state = str2state(tokens[1]); |
289 | + const time_t time = atoi(tokens[2]); |
290 | + const double pct = strtod(tokens[3],nullptr); |
291 | + const char* path = !g_strcmp0(tokens[4],"nopath") ? nullptr : tokens[4]; |
292 | + auto ret = indicator_power_device_new(path, kind, pct, state, time); |
293 | + g_strfreev(tokens); |
294 | + return ret; |
295 | + } |
296 | +} |
297 | + |
298 | /* If a device has multiple batteries and uses only one of them at a time, |
299 | they should be presented as separate items inside the battery menu, |
300 | but everywhere else they should be aggregated (bug 880881). |
301 | @@ -714,96 +840,130 @@ |
302 | should be the the maximum of the times for all those that are charging. */ |
303 | TEST_F(DeviceTest, ChoosePrimary) |
304 | { |
305 | - struct Description |
306 | - { |
307 | - const char * path; |
308 | - UpDeviceKind kind; |
309 | - UpDeviceState state; |
310 | - guint64 time; |
311 | - double percentage; |
312 | - }; |
313 | - |
314 | - const Description descriptions[] = { |
315 | - { "/some/path/d0", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 10, 60.0 }, // 0 |
316 | - { "/some/path/d1", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 20, 80.0 }, // 1 |
317 | - { "/some/path/d2", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 30, 100.0 }, // 2 |
318 | - |
319 | - { "/some/path/c0", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 10, 60.0 }, // 3 |
320 | - { "/some/path/c1", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 20, 80.0 }, // 4 |
321 | - { "/some/path/c2", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 30, 100.0 }, // 5 |
322 | - |
323 | - { "/some/path/f0", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_FULLY_CHARGED, 0, 100.0 }, // 6 |
324 | - { "/some/path/m0", UP_DEVICE_KIND_MOUSE, UP_DEVICE_STATE_DISCHARGING, 20, 80.0 }, // 7 |
325 | - { "/some/path/m1", UP_DEVICE_KIND_MOUSE, UP_DEVICE_STATE_FULLY_CHARGED, 0, 100.0 }, // 8 |
326 | - { "/some/path/pw", UP_DEVICE_KIND_LINE_POWER, UP_DEVICE_STATE_UNKNOWN, 0, 0.0 } // 9 |
327 | - }; |
328 | - |
329 | - std::vector<IndicatorPowerDevice*> devices; |
330 | - for(const auto& desc : descriptions) |
331 | - devices.push_back(indicator_power_device_new(desc.path, desc.kind, desc.percentage, desc.state, time_t(desc.time))); |
332 | - |
333 | const struct { |
334 | - std::vector<unsigned int> device_indices; |
335 | - Description expected; |
336 | + std::string description; |
337 | + std::string expected; |
338 | + std::vector<std::string> devices; |
339 | } tests[] = { |
340 | - |
341 | - { { 0 }, descriptions[0] }, // 1 discharging |
342 | - { { 0, 1 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 20, 70.0 } }, // 2 discharging |
343 | - { { 1, 2 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 30, 90.0 } }, // 2 discharging |
344 | - { { 0, 1, 2 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 30, 80.0 } }, // 3 discharging |
345 | - |
346 | - { { 3 }, descriptions[3] }, // 1 charging |
347 | - { { 3, 4 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 20, 70.0 } }, // 2 charging |
348 | - { { 4, 5 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 30, 90.0 } }, // 2 charging |
349 | - { { 3, 4, 5 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 30, 80.0 } }, // 3 charging |
350 | - |
351 | - { { 6 }, descriptions[6] }, // 1 charged |
352 | - { { 6, 0 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 10, 80.0 } }, // 1 charged, 1 discharging |
353 | - { { 6, 3 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 10, 80.0 } }, // 1 charged, 1 charging |
354 | - { { 6, 0, 3 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 10, 73.3 } }, // 1 charged, 1 charging, 1 discharging |
355 | - |
356 | - { { 0, 7 }, descriptions[0] }, // 1 discharging battery, 1 discharging mouse. pick the one with the least time left. |
357 | - { { 2, 7 }, descriptions[7] }, // 1 discharging battery, 1 discharging mouse. pick the one with the least time left. |
358 | - |
359 | - { { 0, 8 }, descriptions[0] }, // 1 discharging battery, 1 fully-charged mouse. pick the one that's discharging. |
360 | - { { 6, 7 }, descriptions[7] }, // 1 discharging mouse, 1 fully-charged battery. pick the one that's discharging. |
361 | - |
362 | - { { 0, 9 }, descriptions[0] }, // everything comes before power lines |
363 | - { { 3, 9 }, descriptions[3] }, |
364 | - { { 7, 9 }, descriptions[7] } |
365 | + { |
366 | + "one discharging battery", |
367 | + "battery discharging 10m 60% bat01", |
368 | + { "battery discharging 10m 60% bat01" } |
369 | + }, |
370 | + { |
371 | + "merge two discharging batteries", |
372 | + "battery discharging 20m 70% nopath", |
373 | + { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02" } |
374 | + }, |
375 | + { |
376 | + "merge two other discharging batteries", |
377 | + "battery discharging 30m 90% nopath", |
378 | + { "battery discharging 20m 80% bat01", "battery discharging 30m 100% bat02" } |
379 | + }, |
380 | + { |
381 | + "merge three discharging batteries", |
382 | + "battery discharging 30m 80% nopath", |
383 | + { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02", "battery discharging 30m 100% bat03" } |
384 | + }, |
385 | + { |
386 | + "one charging battery", |
387 | + "battery charging 10m 60% bat01", |
388 | + { "battery charging 10m 60% bat01" } |
389 | + }, |
390 | + { |
391 | + "merge two charging batteries", |
392 | + "battery charging 20m 70% nopath", |
393 | + { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02" } |
394 | + }, |
395 | + { |
396 | + "merge two other charging batteries", |
397 | + "battery charging 30m 90% nopath", |
398 | + { "battery charging 20m 80% bat01", "battery charging 30m 100% bat02" } |
399 | + }, |
400 | + { |
401 | + "merge three charging batteries", |
402 | + "battery charging 30m 80% nopath", |
403 | + { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02", "battery charging 30m 100% bat03" } |
404 | + }, |
405 | + { |
406 | + "one charged battery", |
407 | + "battery charged 0m 100% bat01", |
408 | + { "battery charged 0m 100% bat01" } |
409 | + }, |
410 | + { |
411 | + "merge one charged, one discharging", |
412 | + "battery discharging 10m 80% nopath", |
413 | + { "battery charged 0m 100% bat01", "battery discharging 10m 60% bat02" } |
414 | + }, |
415 | + { |
416 | + "merged one charged, one charging", |
417 | + "battery charging 10m 80% nopath", |
418 | + { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02" } |
419 | + }, |
420 | + { |
421 | + "merged one charged, one charging, one discharging", |
422 | + "battery discharging 10m 74% nopath", |
423 | + { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02", "battery discharging 10m 60% bat03" } |
424 | + }, |
425 | + { |
426 | + "one discharging mouse and one discharging battery. pick the one with the least time left", |
427 | + "battery discharging 10m 60% bat01", |
428 | + { "battery discharging 10m 60% bat01", "mouse discharging 20m 80% mouse01" } |
429 | + }, |
430 | + { |
431 | + "one discharging mouse and a different discharging battery. pick the one with the least time left", |
432 | + "mouse discharging 20m 80% mouse01", |
433 | + { "battery discharging 30m 100% bat01", "mouse discharging 20m 80% mouse01" } |
434 | + }, |
435 | + { |
436 | + "everything comes before power lines #1", |
437 | + "battery discharging 10m 60% bat01", |
438 | + { "battery discharging 10m 60% bat01", "line-power unknown 0m 0% lp01" } |
439 | + }, |
440 | + { |
441 | + "everything comes before power lines #2", |
442 | + "battery charging 10m 60% bat01", |
443 | + { "battery charging 10m 60% bat01", "line-power unknown 0m 0% lp01" } |
444 | + }, |
445 | + { |
446 | + "everything comes before power lines #2", |
447 | + "mouse discharging 20m 80% mouse01", |
448 | + { "mouse discharging 20m 80% mouse01", "line-power unknown 0m 0% lp01" } |
449 | + }, |
450 | + { |
451 | + // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10 |
452 | + "don't select a device with unknown state when we have another device with a known state...", |
453 | + "battery charged 0m 100% bat01", |
454 | + { "battery charged 0m 100% bat01", "phone unknown 0m 61% phone01" } |
455 | + }, |
456 | + { |
457 | + // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10 |
458 | + "...but do select the unknown state device if nothing else is available", |
459 | + "phone unknown 0m 61% phone01", |
460 | + { "phone unknown 0m 61% phone01" } |
461 | + } |
462 | }; |
463 | |
464 | for(const auto& test : tests) |
465 | { |
466 | - const auto& x = test.expected; |
467 | - |
468 | - GList * device_glist = nullptr; |
469 | - for(const auto& i : test.device_indices) |
470 | - device_glist = g_list_append(device_glist, devices[i]); |
471 | - |
472 | + // build the device list |
473 | + GList* device_glist {}; |
474 | + for (const auto& description : test.devices) |
475 | + device_glist = g_list_append(device_glist, str2device(description)); |
476 | + |
477 | + // run the test |
478 | auto primary = indicator_power_service_choose_primary_device(device_glist); |
479 | - EXPECT_STREQ(x.path, indicator_power_device_get_object_path(primary)); |
480 | - EXPECT_EQ(x.kind, indicator_power_device_get_kind(primary)); |
481 | - EXPECT_EQ(x.state, indicator_power_device_get_state(primary)); |
482 | - EXPECT_EQ(x.time, indicator_power_device_get_time(primary)); |
483 | - EXPECT_EQ(int(ceil(x.percentage)), int(ceil(indicator_power_device_get_percentage(primary)))); |
484 | - g_object_unref(primary); |
485 | + EXPECT_EQ(test.expected, device2str(primary)); |
486 | + g_clear_object(&primary); |
487 | |
488 | // reverse the list and repeat the test |
489 | // to confirm that list order doesn't matter |
490 | - device_glist = g_list_reverse (device_glist); |
491 | - primary = indicator_power_service_choose_primary_device (device_glist); |
492 | - EXPECT_STREQ(x.path, indicator_power_device_get_object_path(primary)); |
493 | - EXPECT_EQ(x.kind, indicator_power_device_get_kind(primary)); |
494 | - EXPECT_EQ(x.state, indicator_power_device_get_state(primary)); |
495 | - EXPECT_EQ(x.time, indicator_power_device_get_time(primary)); |
496 | - EXPECT_EQ(int(ceil(x.percentage)), int(ceil(indicator_power_device_get_percentage(primary)))); |
497 | - g_object_unref(primary); |
498 | + device_glist = g_list_reverse(device_glist); |
499 | + primary = indicator_power_service_choose_primary_device(device_glist); |
500 | + EXPECT_EQ(test.expected, device2str(primary)); |
501 | + g_clear_object(&primary); |
502 | |
503 | // cleanup |
504 | - g_list_free(device_glist); |
505 | + g_list_free_full(device_glist, g_object_unref); |
506 | } |
507 | - |
508 | - for (auto& device : devices) |
509 | - g_object_unref (device); |
510 | } |
The code looks fine, but we might consider refactoring some of the magic numbers to enums at some point. The tables of data are getting a little hard to read.