Merge lp:~charlesk/indicator-power/lp-1470080-missing-icon-when-apple-devices-connected into lp:indicator-power

Proposed by Charles Kerr
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
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/discharging state is unknown.

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)

To post a comment you must log in.
Revision history for this message
Ted Gould (ted) wrote :

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.

review: Approve
Revision history for this message
Ted Gould (ted) wrote :

That's a crazy number of alternative icons. But works for me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/device.c'
--- src/device.c 2016-05-01 22:29:56 +0000
+++ src/device.c 2016-05-26 19:05:02 +0000
@@ -434,11 +434,12 @@
434 }434 }
435 g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging-symbolic", kind_str, suffix_str));435 g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging-symbolic", kind_str, suffix_str));
436 g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging", kind_str, suffix_str));436 g_ptr_array_add (names, g_strdup_printf ("%s-%s-charging", kind_str, suffix_str));
437 break;437 // NB: fallthrough to use foo-bar as a fallback for foo-bar-charging
438438
439 case UP_DEVICE_STATE_PENDING_CHARGE:439 case UP_DEVICE_STATE_PENDING_CHARGE:
440 case UP_DEVICE_STATE_DISCHARGING:440 case UP_DEVICE_STATE_DISCHARGING:
441 case UP_DEVICE_STATE_PENDING_DISCHARGE:441 case UP_DEVICE_STATE_PENDING_DISCHARGE:
442 case UP_DEVICE_STATE_UNKNOWN: /* http://pad.lv/1470080 */
442 suffix_str = get_device_icon_suffix (percentage);443 suffix_str = get_device_icon_suffix (percentage);
443 index_str = get_closest_10_percent_percentage (percentage);444 index_str = get_closest_10_percent_percentage (percentage);
444 g_ptr_array_add (names, g_strdup_printf ("%s-%s", kind_str, index_str));445 g_ptr_array_add (names, g_strdup_printf ("%s-%s", kind_str, index_str));
445446
=== modified file 'src/service.c'
--- src/service.c 2016-01-02 02:19:45 +0000
+++ src/service.c 2016-05-26 19:05:02 +0000
@@ -245,7 +245,25 @@
245 }245 }
246 }246 }
247247
248 if (!ret) /* neither device is charging nor discharging... */248 /* neither device is charging nor discharging... */
249
250 /* unless there's no other option,
251 don't choose a device with an unknown state.
252 https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080 */
253 state = UP_DEVICE_STATE_UNKNOWN;
254 if (!ret && ((a_state == state) || (b_state == state)))
255 {
256 if (a_state != state) /* b is unknown */
257 {
258 ret = -1;
259 }
260 else if (b_state != state) /* a is unknown */
261 {
262 ret = 1;
263 }
264 }
265
266 if (!ret)
249 {267 {
250 const int weight_a = get_device_kind_weight (a);268 const int weight_a = get_device_kind_weight (a);
251 const int weight_b = get_device_kind_weight (b);269 const int weight_b = get_device_kind_weight (b);
252270
=== modified file 'tests/test-device.cc'
--- tests/test-device.cc 2016-05-16 21:33:30 +0000
+++ tests/test-device.cc 2016-05-26 19:05:02 +0000
@@ -1,27 +1,32 @@
1/*1/*
2Copyright 2012 Canonical Ltd.2 * Copyright 2012-2016 Canonical Ltd.
33 *
4Authors:4 * This program is free software: you can redistribute it and/or modify it
5 Charles Kerr <charles.kerr@canonical.com>5 * under the terms of the GNU General Public License version 3, as published
66 * by the Free Software Foundation.
7This program is free software: you can redistribute it and/or modify it7 *
8under the terms of the GNU General Public License version 3, as published8 * This program is distributed in the hope that it will be useful, but
9by the Free Software Foundation.9 * WITHOUT ANY WARRANTY; without even the implied warranties of
1010 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11This program is distributed in the hope that it will be useful, but11 * PURPOSE. See the GNU General Public License for more details.
12WITHOUT ANY WARRANTY; without even the implied warranties of12 *
13MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR13 * You should have received a copy of the GNU General Public License along
14PURPOSE. See the GNU General Public License for more details.14 * with this program. If not, see <http://www.gnu.org/licenses/>.
1515 *
16You should have received a copy of the GNU General Public License along16 * Authors:
17with this program. If not, see <http://www.gnu.org/licenses/>.17 * Charles Kerr <charles.kerr@canonical.com>
18*/18 */
19
20#include "device.h"
21#include "service.h"
1922
20#include <gio/gio.h>23#include <gio/gio.h>
21#include <gtest/gtest.h>24#include <gtest/gtest.h>
25
26#include <algorithm>
22#include <cmath> // ceil()27#include <cmath> // ceil()
23#include "device.h"28#include <string>
24#include "service.h"29
2530
26class DeviceTest : public ::testing::Test31class DeviceTest : public ::testing::Test
27{32{
@@ -337,7 +342,11 @@
337 g_string_append_printf (expected, "%s-100-charging;", kind_str);342 g_string_append_printf (expected, "%s-100-charging;", kind_str);
338 g_string_append_printf (expected, "gpm-%s-100-charging;", kind_str);343 g_string_append_printf (expected, "gpm-%s-100-charging;", kind_str);
339 g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str);344 g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str);
340 g_string_append_printf (expected, "%s-full-charging", kind_str);345 g_string_append_printf (expected, "%s-full-charging;", kind_str);
346 g_string_append_printf (expected, "%s-100;", kind_str);
347 g_string_append_printf (expected, "gpm-%s-100;", kind_str);
348 g_string_append_printf (expected, "%s-full-symbolic;", kind_str);
349 g_string_append_printf (expected, "%s-full", kind_str);
341 EXPECT_ICON_NAMES_EQ(expected->str, device);350 EXPECT_ICON_NAMES_EQ(expected->str, device);
342 g_string_truncate (expected, 0);351 g_string_truncate (expected, 0);
343352
@@ -351,7 +360,13 @@
351 g_string_append_printf (expected, "%s-080-charging;", kind_str);360 g_string_append_printf (expected, "%s-080-charging;", kind_str);
352 g_string_append_printf (expected, "gpm-%s-080-charging;", kind_str);361 g_string_append_printf (expected, "gpm-%s-080-charging;", kind_str);
353 g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str);362 g_string_append_printf (expected, "%s-full-charging-symbolic;", kind_str);
354 g_string_append_printf (expected, "%s-full-charging", kind_str);363 g_string_append_printf (expected, "%s-full-charging;", kind_str);
364 g_string_append_printf (expected, "%s-090;", kind_str);
365 g_string_append_printf (expected, "gpm-%s-090;", kind_str);
366 g_string_append_printf (expected, "%s-080;", kind_str);
367 g_string_append_printf (expected, "gpm-%s-080;", kind_str);
368 g_string_append_printf (expected, "%s-full-symbolic;", kind_str);
369 g_string_append_printf (expected, "%s-full", kind_str);
355 EXPECT_ICON_NAMES_EQ(expected->str, device);370 EXPECT_ICON_NAMES_EQ(expected->str, device);
356 g_string_truncate (expected, 0);371 g_string_truncate (expected, 0);
357372
@@ -365,7 +380,13 @@
365 g_string_append_printf (expected, "%s-060-charging;", kind_str);380 g_string_append_printf (expected, "%s-060-charging;", kind_str);
366 g_string_append_printf (expected, "gpm-%s-060-charging;", kind_str);381 g_string_append_printf (expected, "gpm-%s-060-charging;", kind_str);
367 g_string_append_printf (expected, "%s-good-charging-symbolic;", kind_str);382 g_string_append_printf (expected, "%s-good-charging-symbolic;", kind_str);
368 g_string_append_printf (expected, "%s-good-charging", kind_str);383 g_string_append_printf (expected, "%s-good-charging;", kind_str);
384 g_string_append_printf (expected, "%s-050;", kind_str);
385 g_string_append_printf (expected, "gpm-%s-050;", kind_str);
386 g_string_append_printf (expected, "%s-060;", kind_str);
387 g_string_append_printf (expected, "gpm-%s-060;", kind_str);
388 g_string_append_printf (expected, "%s-good-symbolic;", kind_str);
389 g_string_append_printf (expected, "%s-good", kind_str);
369 EXPECT_ICON_NAMES_EQ(expected->str, device);390 EXPECT_ICON_NAMES_EQ(expected->str, device);
370 g_string_truncate (expected, 0);391 g_string_truncate (expected, 0);
371392
@@ -379,7 +400,13 @@
379 g_string_append_printf (expected, "%s-040-charging;", kind_str);400 g_string_append_printf (expected, "%s-040-charging;", kind_str);
380 g_string_append_printf (expected, "gpm-%s-040-charging;", kind_str);401 g_string_append_printf (expected, "gpm-%s-040-charging;", kind_str);
381 g_string_append_printf (expected, "%s-low-charging-symbolic;", kind_str);402 g_string_append_printf (expected, "%s-low-charging-symbolic;", kind_str);
382 g_string_append_printf (expected, "%s-low-charging", kind_str);403 g_string_append_printf (expected, "%s-low-charging;", kind_str);
404 g_string_append_printf (expected, "%s-030;", kind_str);
405 g_string_append_printf (expected, "gpm-%s-030;", kind_str);
406 g_string_append_printf (expected, "%s-040;", kind_str);
407 g_string_append_printf (expected, "gpm-%s-040;", kind_str);
408 g_string_append_printf (expected, "%s-low-symbolic;", kind_str);
409 g_string_append_printf (expected, "%s-low", kind_str);
383 EXPECT_ICON_NAMES_EQ(expected->str, device);410 EXPECT_ICON_NAMES_EQ(expected->str, device);
384 g_string_truncate (expected, 0);411 g_string_truncate (expected, 0);
385412
@@ -393,7 +420,13 @@
393 g_string_append_printf (expected, "%s-000-charging;", kind_str);420 g_string_append_printf (expected, "%s-000-charging;", kind_str);
394 g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);421 g_string_append_printf (expected, "gpm-%s-000-charging;", kind_str);
395 g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);422 g_string_append_printf (expected, "%s-caution-charging-symbolic;", kind_str);
396 g_string_append_printf (expected, "%s-caution-charging", kind_str);423 g_string_append_printf (expected, "%s-caution-charging;", kind_str);
424 g_string_append_printf (expected, "%s-010;", kind_str);
425 g_string_append_printf (expected, "gpm-%s-010;", kind_str);
426 g_string_append_printf (expected, "%s-000;", kind_str);
427 g_string_append_printf (expected, "gpm-%s-000;", kind_str);
428 g_string_append_printf (expected, "%s-caution-symbolic;", kind_str);
429 g_string_append_printf (expected, "%s-caution", kind_str);
397 EXPECT_ICON_NAMES_EQ(expected->str, device);430 EXPECT_ICON_NAMES_EQ(expected->str, device);
398 g_string_truncate (expected, 0);431 g_string_truncate (expected, 0);
399432
@@ -496,15 +529,13 @@
496 g_string_append_printf (expected, "%s-caution-symbolic;", kind_str);529 g_string_append_printf (expected, "%s-caution-symbolic;", kind_str);
497 g_string_append_printf (expected, "%s-caution", kind_str);530 g_string_append_printf (expected, "%s-caution", kind_str);
498 EXPECT_ICON_NAMES_EQ(expected->str, device);531 EXPECT_ICON_NAMES_EQ(expected->str, device);
499 g_string_truncate (expected, 0);
500532
501 // state unknown533 // if we know the charge level, but not that it’s charging,
502 g_object_set (o, INDICATOR_POWER_DEVICE_KIND, kind,534 // then we should use the same icons as when it’s discharging.
503 INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,535 // https://wiki.ubuntu.com/Power?action=diff&rev2=78&rev1=77
536 // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080
537 g_object_set (o, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_UNKNOWN,
504 NULL);538 NULL);
505 g_string_append_printf (expected, "%s-missing-symbolic;", kind_str);
506 g_string_append_printf (expected, "gpm-%s-missing;", kind_str);
507 g_string_append_printf (expected, "%s-missing", kind_str);
508 EXPECT_ICON_NAMES_EQ(expected->str, device);539 EXPECT_ICON_NAMES_EQ(expected->str, device);
509 g_string_truncate (expected, 0);540 g_string_truncate (expected, 0);
510 }541 }
@@ -704,6 +735,101 @@
704 g_free (real_lang);735 g_free (real_lang);
705}736}
706737
738namespace
739{
740 const std::array<std::pair<std::string,UpDeviceKind>,UP_DEVICE_KIND_LAST> kinds = {
741 std::make_pair("unknown", UP_DEVICE_KIND_UNKNOWN),
742 std::make_pair("line-power", UP_DEVICE_KIND_LINE_POWER),
743 std::make_pair("battery", UP_DEVICE_KIND_BATTERY),
744 std::make_pair("ups", UP_DEVICE_KIND_UPS),
745 std::make_pair("monitor", UP_DEVICE_KIND_MONITOR),
746 std::make_pair("mouse", UP_DEVICE_KIND_MOUSE),
747 std::make_pair("keyboard", UP_DEVICE_KIND_KEYBOARD),
748 std::make_pair("pda", UP_DEVICE_KIND_PDA),
749 std::make_pair("phone", UP_DEVICE_KIND_PHONE),
750 std::make_pair("media-player", UP_DEVICE_KIND_MEDIA_PLAYER),
751 std::make_pair("tablet", UP_DEVICE_KIND_TABLET),
752 std::make_pair("computer", UP_DEVICE_KIND_COMPUTER)
753 };
754 const std::string& kind2str(UpDeviceKind kind)
755 {
756 return std::find_if(
757 kinds.begin(),
758 kinds.end(),
759 [kind](decltype(kinds[0])& i){return i.second==kind;}
760 )->first;
761 }
762 UpDeviceKind str2kind(const std::string& str)
763 {
764 return std::find_if(
765 kinds.begin(),
766 kinds.end(),
767 [str](decltype(kinds[0])& i){return i.first==str;}
768 )->second;
769 }
770
771 /**
772 **/
773
774 const std::array<std::pair<std::string,UpDeviceState>,UP_DEVICE_STATE_LAST> states =
775 {
776 std::make_pair("unknown", UP_DEVICE_STATE_UNKNOWN),
777 std::make_pair("charging", UP_DEVICE_STATE_CHARGING),
778 std::make_pair("discharging", UP_DEVICE_STATE_DISCHARGING),
779 std::make_pair("empty", UP_DEVICE_STATE_EMPTY),
780 std::make_pair("charged", UP_DEVICE_STATE_FULLY_CHARGED),
781 std::make_pair("pending-charge", UP_DEVICE_STATE_PENDING_CHARGE),
782 std::make_pair("pending-discharge", UP_DEVICE_STATE_PENDING_DISCHARGE)
783 };
784 const std::string& state2str(UpDeviceState state)
785 {
786 return std::find_if(
787 states.begin(),
788 states.end(),
789 [state](decltype(states[0])& i){return i.second==state;}
790 )->first;
791 }
792 UpDeviceState str2state(const std::string& str)
793 {
794 return std::find_if(
795 states.begin(),
796 states.end(),
797 [str](decltype(states[0])& i){return i.first==str;}
798 )->second;
799 }
800
801 /**
802 **/
803
804 std::string device2str(IndicatorPowerDevice* device)
805 {
806 std::ostringstream o;
807 const auto path = indicator_power_device_get_object_path(device);
808
809 o << kind2str(indicator_power_device_get_kind(device))
810 << ' ' << state2str(indicator_power_device_get_state(device))
811 << ' ' << indicator_power_device_get_time(device)<<'m'
812 << ' ' << int(ceil(indicator_power_device_get_percentage(device)))<<'%'
813 << ' ' << (path ? path : "nopath");
814
815 return o.str();
816 }
817
818 IndicatorPowerDevice* str2device(const std::string& str)
819 {
820 auto tokens = g_strsplit(str.c_str(), " ", 0);
821 g_assert(5u == g_strv_length(tokens));
822 const auto kind = str2kind(tokens[0]);
823 const auto state = str2state(tokens[1]);
824 const time_t time = atoi(tokens[2]);
825 const double pct = strtod(tokens[3],nullptr);
826 const char* path = !g_strcmp0(tokens[4],"nopath") ? nullptr : tokens[4];
827 auto ret = indicator_power_device_new(path, kind, pct, state, time);
828 g_strfreev(tokens);
829 return ret;
830 }
831}
832
707/* If a device has multiple batteries and uses only one of them at a time,833/* If a device has multiple batteries and uses only one of them at a time,
708 they should be presented as separate items inside the battery menu,834 they should be presented as separate items inside the battery menu,
709 but everywhere else they should be aggregated (bug 880881).835 but everywhere else they should be aggregated (bug 880881).
@@ -714,96 +840,130 @@
714 should be the the maximum of the times for all those that are charging. */840 should be the the maximum of the times for all those that are charging. */
715TEST_F(DeviceTest, ChoosePrimary)841TEST_F(DeviceTest, ChoosePrimary)
716{842{
717 struct Description
718 {
719 const char * path;
720 UpDeviceKind kind;
721 UpDeviceState state;
722 guint64 time;
723 double percentage;
724 };
725
726 const Description descriptions[] = {
727 { "/some/path/d0", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 10, 60.0 }, // 0
728 { "/some/path/d1", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 20, 80.0 }, // 1
729 { "/some/path/d2", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 30, 100.0 }, // 2
730
731 { "/some/path/c0", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 10, 60.0 }, // 3
732 { "/some/path/c1", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 20, 80.0 }, // 4
733 { "/some/path/c2", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 30, 100.0 }, // 5
734
735 { "/some/path/f0", UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_FULLY_CHARGED, 0, 100.0 }, // 6
736 { "/some/path/m0", UP_DEVICE_KIND_MOUSE, UP_DEVICE_STATE_DISCHARGING, 20, 80.0 }, // 7
737 { "/some/path/m1", UP_DEVICE_KIND_MOUSE, UP_DEVICE_STATE_FULLY_CHARGED, 0, 100.0 }, // 8
738 { "/some/path/pw", UP_DEVICE_KIND_LINE_POWER, UP_DEVICE_STATE_UNKNOWN, 0, 0.0 } // 9
739 };
740
741 std::vector<IndicatorPowerDevice*> devices;
742 for(const auto& desc : descriptions)
743 devices.push_back(indicator_power_device_new(desc.path, desc.kind, desc.percentage, desc.state, time_t(desc.time)));
744
745 const struct {843 const struct {
746 std::vector<unsigned int> device_indices;844 std::string description;
747 Description expected;845 std::string expected;
846 std::vector<std::string> devices;
748 } tests[] = {847 } tests[] = {
749848 {
750 { { 0 }, descriptions[0] }, // 1 discharging849 "one discharging battery",
751 { { 0, 1 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 20, 70.0 } }, // 2 discharging850 "battery discharging 10m 60% bat01",
752 { { 1, 2 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 30, 90.0 } }, // 2 discharging851 { "battery discharging 10m 60% bat01" }
753 { { 0, 1, 2 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 30, 80.0 } }, // 3 discharging852 },
754853 {
755 { { 3 }, descriptions[3] }, // 1 charging854 "merge two discharging batteries",
756 { { 3, 4 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 20, 70.0 } }, // 2 charging855 "battery discharging 20m 70% nopath",
757 { { 4, 5 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 30, 90.0 } }, // 2 charging856 { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02" }
758 { { 3, 4, 5 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 30, 80.0 } }, // 3 charging857 },
759858 {
760 { { 6 }, descriptions[6] }, // 1 charged859 "merge two other discharging batteries",
761 { { 6, 0 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 10, 80.0 } }, // 1 charged, 1 discharging860 "battery discharging 30m 90% nopath",
762 { { 6, 3 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_CHARGING, 10, 80.0 } }, // 1 charged, 1 charging861 { "battery discharging 20m 80% bat01", "battery discharging 30m 100% bat02" }
763 { { 6, 0, 3 }, { nullptr, UP_DEVICE_KIND_BATTERY, UP_DEVICE_STATE_DISCHARGING, 10, 73.3 } }, // 1 charged, 1 charging, 1 discharging862 },
764863 {
765 { { 0, 7 }, descriptions[0] }, // 1 discharging battery, 1 discharging mouse. pick the one with the least time left.864 "merge three discharging batteries",
766 { { 2, 7 }, descriptions[7] }, // 1 discharging battery, 1 discharging mouse. pick the one with the least time left.865 "battery discharging 30m 80% nopath",
767866 { "battery discharging 10m 60% bat01", "battery discharging 20m 80% bat02", "battery discharging 30m 100% bat03" }
768 { { 0, 8 }, descriptions[0] }, // 1 discharging battery, 1 fully-charged mouse. pick the one that's discharging.867 },
769 { { 6, 7 }, descriptions[7] }, // 1 discharging mouse, 1 fully-charged battery. pick the one that's discharging.868 {
770869 "one charging battery",
771 { { 0, 9 }, descriptions[0] }, // everything comes before power lines870 "battery charging 10m 60% bat01",
772 { { 3, 9 }, descriptions[3] },871 { "battery charging 10m 60% bat01" }
773 { { 7, 9 }, descriptions[7] }872 },
873 {
874 "merge two charging batteries",
875 "battery charging 20m 70% nopath",
876 { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02" }
877 },
878 {
879 "merge two other charging batteries",
880 "battery charging 30m 90% nopath",
881 { "battery charging 20m 80% bat01", "battery charging 30m 100% bat02" }
882 },
883 {
884 "merge three charging batteries",
885 "battery charging 30m 80% nopath",
886 { "battery charging 10m 60% bat01", "battery charging 20m 80% bat02", "battery charging 30m 100% bat03" }
887 },
888 {
889 "one charged battery",
890 "battery charged 0m 100% bat01",
891 { "battery charged 0m 100% bat01" }
892 },
893 {
894 "merge one charged, one discharging",
895 "battery discharging 10m 80% nopath",
896 { "battery charged 0m 100% bat01", "battery discharging 10m 60% bat02" }
897 },
898 {
899 "merged one charged, one charging",
900 "battery charging 10m 80% nopath",
901 { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02" }
902 },
903 {
904 "merged one charged, one charging, one discharging",
905 "battery discharging 10m 74% nopath",
906 { "battery charged 0m 100% bat01", "battery charging 10m 60% bat02", "battery discharging 10m 60% bat03" }
907 },
908 {
909 "one discharging mouse and one discharging battery. pick the one with the least time left",
910 "battery discharging 10m 60% bat01",
911 { "battery discharging 10m 60% bat01", "mouse discharging 20m 80% mouse01" }
912 },
913 {
914 "one discharging mouse and a different discharging battery. pick the one with the least time left",
915 "mouse discharging 20m 80% mouse01",
916 { "battery discharging 30m 100% bat01", "mouse discharging 20m 80% mouse01" }
917 },
918 {
919 "everything comes before power lines #1",
920 "battery discharging 10m 60% bat01",
921 { "battery discharging 10m 60% bat01", "line-power unknown 0m 0% lp01" }
922 },
923 {
924 "everything comes before power lines #2",
925 "battery charging 10m 60% bat01",
926 { "battery charging 10m 60% bat01", "line-power unknown 0m 0% lp01" }
927 },
928 {
929 "everything comes before power lines #2",
930 "mouse discharging 20m 80% mouse01",
931 { "mouse discharging 20m 80% mouse01", "line-power unknown 0m 0% lp01" }
932 },
933 {
934 // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10
935 "don't select a device with unknown state when we have another device with a known state...",
936 "battery charged 0m 100% bat01",
937 { "battery charged 0m 100% bat01", "phone unknown 0m 61% phone01" }
938 },
939 {
940 // https://bugs.launchpad.net/ubuntu/+source/indicator-power/+bug/1470080/comments/10
941 "...but do select the unknown state device if nothing else is available",
942 "phone unknown 0m 61% phone01",
943 { "phone unknown 0m 61% phone01" }
944 }
774 };945 };
775 946
776 for(const auto& test : tests)947 for(const auto& test : tests)
777 {948 {
778 const auto& x = test.expected;949 // build the device list
779950 GList* device_glist {};
780 GList * device_glist = nullptr;951 for (const auto& description : test.devices)
781 for(const auto& i : test.device_indices)952 device_glist = g_list_append(device_glist, str2device(description));
782 device_glist = g_list_append(device_glist, devices[i]);953
783954 // run the test
784 auto primary = indicator_power_service_choose_primary_device(device_glist);955 auto primary = indicator_power_service_choose_primary_device(device_glist);
785 EXPECT_STREQ(x.path, indicator_power_device_get_object_path(primary));956 EXPECT_EQ(test.expected, device2str(primary));
786 EXPECT_EQ(x.kind, indicator_power_device_get_kind(primary));957 g_clear_object(&primary);
787 EXPECT_EQ(x.state, indicator_power_device_get_state(primary));
788 EXPECT_EQ(x.time, indicator_power_device_get_time(primary));
789 EXPECT_EQ(int(ceil(x.percentage)), int(ceil(indicator_power_device_get_percentage(primary))));
790 g_object_unref(primary);
791958
792 // reverse the list and repeat the test959 // reverse the list and repeat the test
793 // to confirm that list order doesn't matter960 // to confirm that list order doesn't matter
794 device_glist = g_list_reverse (device_glist);961 device_glist = g_list_reverse(device_glist);
795 primary = indicator_power_service_choose_primary_device (device_glist);962 primary = indicator_power_service_choose_primary_device(device_glist);
796 EXPECT_STREQ(x.path, indicator_power_device_get_object_path(primary));963 EXPECT_EQ(test.expected, device2str(primary));
797 EXPECT_EQ(x.kind, indicator_power_device_get_kind(primary));964 g_clear_object(&primary);
798 EXPECT_EQ(x.state, indicator_power_device_get_state(primary));
799 EXPECT_EQ(x.time, indicator_power_device_get_time(primary));
800 EXPECT_EQ(int(ceil(x.percentage)), int(ceil(indicator_power_device_get_percentage(primary))));
801 g_object_unref(primary);
802965
803 // cleanup966 // cleanup
804 g_list_free(device_glist);967 g_list_free_full(device_glist, g_object_unref);
805 }968 }
806
807 for (auto& device : devices)
808 g_object_unref (device);
809}969}

Subscribers

People subscribed via source and target branches