From cd8e633cc850d0c9f1bc16533d5962cdfda16856 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 26 Oct 2012 18:03:46 +0200 Subject: add test (currently failing) to detect the logic error reported in bug #1071757 --- tests/test-device.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tests/test-device.cc') diff --git a/tests/test-device.cc b/tests/test-device.cc index 88c43fc..b5fa466 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -602,6 +602,22 @@ TEST_F(DeviceTest, ChoosePrimary) set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0); ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + /* discharging always comes before charging */ + set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 50, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); + ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); + set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 50, 50.0); + ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + + /* charging always comes before charged */ + set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_FULLY_CHARGED, 50, 50.0); + ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); + set_device_charge_state (a, UP_DEVICE_STATE_FULLY_CHARGED, 50, 50.0); + set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); + ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + // cleanup g_slist_free_full (devices, g_object_unref); } -- cgit v1.2.3 From 7c0d274cb162da46198c63f288b67b1a54e70238 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 28 Oct 2012 01:48:30 +0200 Subject: expand DeviceTest.ChoosePrimary to test choosing from a wider variety of device states. --- tests/test-device.cc | 111 ++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 67 deletions(-) (limited to 'tests/test-device.cc') diff --git a/tests/test-device.cc b/tests/test-device.cc index b5fa466..a32e0e5 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -532,22 +532,12 @@ TEST_F(DeviceTest, Labels) g_free (real_lang); } -static void -set_device_charge_state (IndicatorPowerDevice * device, int state, int time, double pct) -{ - g_object_set (device, INDICATOR_POWER_DEVICE_STATE, state, - INDICATOR_POWER_DEVICE_PERCENTAGE, pct, - INDICATOR_POWER_DEVICE_TIME, guint64(time), - NULL); -} - - /* 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 * devices; + GSList * device_list; IndicatorPowerDevice * a; IndicatorPowerDevice * b; @@ -562,62 +552,49 @@ TEST_F(DeviceTest, ChoosePrimary) UP_DEVICE_STATE_DISCHARGING, 0); - devices = NULL; - devices = g_slist_append (devices, a); - devices = g_slist_append (devices, b); - - /* Both discharging, same charge %, different times left before empty. - Confirm that the one with less time is chosen. */ - set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - - /* Both discharging, different charge % and times left. - Confirm that the one with less time is chosen. */ - set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 99, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 49.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - - /* Both discharging, different charge %, same times left. - Confirm that the one with less charge is chosen. */ - set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 100, 49.0); - set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 100, 50.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - - /* Both are charging, have the same charge percentage, and different times left (to charge). - * Confirm that the one with the most time left is chosen. */ - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); - - /* Both are charging, with different charges and time left. - Confirm that the one with the most time left is chosen. */ - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 49, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); - - /* Both are charging, have the same time left, and different charges. - * Confirm that the one with less charge is chosen. */ - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 49.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); - - /* discharging always comes before charging */ - set_device_charge_state (a, UP_DEVICE_STATE_DISCHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_DISCHARGING, 50, 50.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); - - /* charging always comes before charged */ - set_device_charge_state (a, UP_DEVICE_STATE_CHARGING, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_FULLY_CHARGED, 50, 50.0); - ASSERT_EQ (a, indicator_power_choose_primary_device(devices)); - set_device_charge_state (a, UP_DEVICE_STATE_FULLY_CHARGED, 50, 50.0); - set_device_charge_state (b, UP_DEVICE_STATE_CHARGING, 50, 50.0); - ASSERT_EQ (b, indicator_power_choose_primary_device(devices)); + /* 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 state; + guint64 time; + double percentage; + } tests[] = { + { UP_DEVICE_STATE_DISCHARGING, 49, 50.0 }, + { UP_DEVICE_STATE_DISCHARGING, 50, 50.0 }, + { UP_DEVICE_STATE_DISCHARGING, 50, 100.0 }, + { UP_DEVICE_STATE_DISCHARGING, 51, 50.0 }, + { UP_DEVICE_STATE_CHARGING, 50, 50.0 }, + { UP_DEVICE_STATE_CHARGING, 49, 50.0 }, + { UP_DEVICE_STATE_CHARGING, 49, 100.0 }, + { UP_DEVICE_STATE_CHARGING, 48, 50.0 }, + { UP_DEVICE_STATE_FULLY_CHARGED, 0, 50.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 Date: Sun, 28 Oct 2012 01:54:45 +0200 Subject: set DeviceTest's gwarning/gcritical log func to one that prints no messages but accumulates the log count. This way we can both (a) silence in-console warnings that look like bugs in the test but are actually desirable warnings generated by fuzz testing, and (b) assert that we get exactly as many warnings/criticals as we expect to get. --- tests/test-device.cc | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/test-device.cc') diff --git a/tests/test-device.cc b/tests/test-device.cc index a32e0e5..02493dc 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -37,15 +37,40 @@ namespace class DeviceTest : public ::testing::Test { + private: + + guint handler_id; + + int log_count; + + static void log_count_func (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) + { + reinterpret_cast(user_data)->log_count++; + } + + protected: + + int expected_ipower_log_messages; + protected: virtual void SetUp() { + const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING); + handler_id = g_log_set_handler ("Indicator-Power", flags, log_count_func, this); + expected_ipower_log_messages = 0; + log_count = 0; + ensure_glib_initialized (); } virtual void TearDown() { + ASSERT_EQ (expected_ipower_log_messages, log_count); + g_log_remove_handler ("Indicator-Power", handler_id); } protected: @@ -205,6 +230,7 @@ TEST_F(DeviceTest, BadAccessors) indicator_power_device_get_state (device); indicator_power_device_get_percentage (device); indicator_power_device_get_object_path (device); + expected_ipower_log_messages += 5; // test that these functions can handle being passed non-device GObjects device = reinterpret_cast(g_cancellable_new ()); @@ -213,6 +239,8 @@ TEST_F(DeviceTest, BadAccessors) indicator_power_device_get_state (device); indicator_power_device_get_percentage (device); indicator_power_device_get_object_path (device); + expected_ipower_log_messages += 5; + g_object_unref (device); } @@ -226,6 +254,7 @@ TEST_F(DeviceTest, IconNames) GObject * o = G_OBJECT(device); // bad arguments + expected_ipower_log_messages++; ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL); // power @@ -452,9 +481,11 @@ TEST_F(DeviceTest, Labels) g_setenv ("LANG", "en_US.UTF-8", TRUE); // bad args: NULL device + expected_ipower_log_messages++; check_strings (NULL, NULL, NULL, NULL); // bad args: a GObject that isn't a device + expected_ipower_log_messages++; GObject * o = G_OBJECT(g_cancellable_new()); check_strings ((IndicatorPowerDevice*)o, NULL, NULL, NULL); g_object_unref (o); -- cgit v1.2.3 From a76e4749d62fb24b89c879d2bdf1164dbdac233f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 28 Oct 2012 08:40:44 +0100 Subject: in TestDevice, make the log variable names a little more consistent --- tests/test-device.cc | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'tests/test-device.cc') diff --git a/tests/test-device.cc b/tests/test-device.cc index 02493dc..18bdc08 100644 --- a/tests/test-device.cc +++ b/tests/test-device.cc @@ -39,38 +39,38 @@ class DeviceTest : public ::testing::Test { private: - guint handler_id; + guint log_handler_id; - int log_count; + 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(user_data)->log_count++; + reinterpret_cast(user_data)->log_count_ipower_actual++; } protected: - int expected_ipower_log_messages; + int log_count_ipower_expected; protected: virtual void SetUp() { const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING); - handler_id = g_log_set_handler ("Indicator-Power", flags, log_count_func, this); - expected_ipower_log_messages = 0; - log_count = 0; + 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 (expected_ipower_log_messages, log_count); - g_log_remove_handler ("Indicator-Power", handler_id); + ASSERT_EQ (log_count_ipower_expected, log_count_ipower_actual); + g_log_remove_handler ("Indicator-Power", log_handler_id); } protected: @@ -230,7 +230,7 @@ TEST_F(DeviceTest, BadAccessors) indicator_power_device_get_state (device); indicator_power_device_get_percentage (device); indicator_power_device_get_object_path (device); - expected_ipower_log_messages += 5; + log_count_ipower_expected += 5; // test that these functions can handle being passed non-device GObjects device = reinterpret_cast(g_cancellable_new ()); @@ -239,7 +239,7 @@ TEST_F(DeviceTest, BadAccessors) indicator_power_device_get_state (device); indicator_power_device_get_percentage (device); indicator_power_device_get_object_path (device); - expected_ipower_log_messages += 5; + log_count_ipower_expected += 5; g_object_unref (device); } @@ -254,7 +254,7 @@ TEST_F(DeviceTest, IconNames) GObject * o = G_OBJECT(device); // bad arguments - expected_ipower_log_messages++; + log_count_ipower_expected++; ASSERT_TRUE (indicator_power_device_get_icon_names (NULL) == NULL); // power @@ -481,11 +481,11 @@ TEST_F(DeviceTest, Labels) g_setenv ("LANG", "en_US.UTF-8", TRUE); // bad args: NULL device - expected_ipower_log_messages++; + log_count_ipower_expected++; check_strings (NULL, NULL, NULL, NULL); // bad args: a GObject that isn't a device - expected_ipower_log_messages++; + log_count_ipower_expected++; GObject * o = G_OBJECT(g_cancellable_new()); check_strings ((IndicatorPowerDevice*)o, NULL, NULL, NULL); g_object_unref (o); -- cgit v1.2.3