From d7d630dd5b250c4db489d797552129093c0c3ab0 Mon Sep 17 00:00:00 2001 From: charles kerr Date: Fri, 1 Jan 2016 16:08:07 -0600 Subject: add play-sound tests to test-notify --- tests/test-notify.cc | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'tests/test-notify.cc') diff --git a/tests/test-notify.cc b/tests/test-notify.cc index 63214c2..cfa27bf 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -23,6 +23,7 @@ #include "dbus-shared.h" #include "device.h" #include "notifier.h" +#include "sound-player-mock.h" #include @@ -77,6 +78,8 @@ protected: { super::SetUp(); + g_setenv ("XDG_DATA_HOME", XDG_DATA_HOME, TRUE); + // init DBusMock / dbus-test-runner service = dbus_test_service_new(nullptr); @@ -275,7 +278,8 @@ TEST_F(NotifyFixture, LevelsDuringBatteryDrain) // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - auto notifier = indicator_power_notifier_new (); + auto sound_player = indicator_power_sound_player_mock_new (); + auto notifier = indicator_power_notifier_new (sound_player); indicator_power_notifier_set_battery (notifier, battery); indicator_power_notifier_set_bus (notifier, bus); wait_msec(); @@ -317,14 +321,23 @@ TEST_F(NotifyFixture, LevelsDuringBatteryDrain) // cleanup g_dbus_connection_signal_unsubscribe (bus, sub_tag); - g_object_unref (notifier); g_object_unref (battery); + g_object_unref (notifier); + g_object_unref (sound_player); } /*** **** ***/ +namespace +{ + static void on_uri_played(IndicatorPowerSoundPlayer*, const char* uri, gpointer glast_uri) + { + *static_cast(glast_uri) = uri; + } +} + TEST_F(NotifyFixture, EventsThatChangeNotifications) { // GetCapabilities returns an array containing 'actions', so that we'll @@ -348,7 +361,11 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - auto notifier = indicator_power_notifier_new (); + const std::string low_power_uri {"file://" XDG_DATA_HOME "/" GETTEXT_PACKAGE "/sounds/Low%20battery.ogg"}; + std::string last_uri; + auto sound_player = indicator_power_sound_player_mock_new (); + g_signal_connect(sound_player, "uri-played", G_CALLBACK(on_uri_played), &last_uri); + auto notifier = indicator_power_notifier_new (sound_player); indicator_power_notifier_set_battery (notifier, battery); indicator_power_notifier_set_bus (notifier, bus); ChangedParams changed_params; @@ -366,6 +383,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) // test setup case wait_msec(); EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); + EXPECT_TRUE(last_uri.empty()); // change the percent past the 'low' threshold and confirm that // a) the power level changes @@ -376,38 +394,48 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); EXPECT_EQ (indicator_power_notifier_get_power_level(battery), changed_params.power_level); EXPECT_TRUE (changed_params.is_warning); + EXPECT_EQ (low_power_uri, last_uri); // now test that the warning changes if the level goes down even lower... + last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_very_low); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL, changed_params.fields); EXPECT_STREQ (POWER_LEVEL_STR_VERY_LOW, changed_params.power_level.c_str()); + EXPECT_EQ (low_power_uri, last_uri); // ...and that the warning is taken down if the battery is plugged back in... + last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, nullptr); wait_msec(); EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields); EXPECT_FALSE (changed_params.is_warning); + EXPECT_TRUE(last_uri.empty()); // ...and that it comes back if we unplug again... + last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, nullptr); wait_msec(); EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields); EXPECT_TRUE (changed_params.is_warning); + EXPECT_EQ (low_power_uri, last_uri); // ...and that it's taken down if the power level is OK + last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_low+1); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); EXPECT_FALSE (changed_params.is_warning); + EXPECT_TRUE(last_uri.empty()); // cleanup g_dbus_connection_signal_unsubscribe (bus, sub_tag); g_object_unref (notifier); g_object_unref (battery); + g_object_unref (sound_player); } -- cgit v1.2.3 From bdc03e1cddb8811651f22e0bb423bf3385cbc793 Mon Sep 17 00:00:00 2001 From: charles kerr Date: Fri, 1 Jan 2016 16:12:16 -0600 Subject: update copyright years on changed/new files --- tests/test-notify.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test-notify.cc') diff --git a/tests/test-notify.cc b/tests/test-notify.cc index cfa27bf..6fe7c59 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -1,5 +1,5 @@ /* - * Copyright 2014 Canonical Ltd. + * Copyright 2014-2016 Canonical Ltd. * * 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 -- cgit v1.2.3 From 460f1464709a4c5e2c1594045961be9108ae2afd Mon Sep 17 00:00:00 2001 From: charles kerr Date: Fri, 1 Jan 2016 19:37:51 -0600 Subject: use a symbolic constant for the low battery sound's filename --- tests/test-notify.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'tests/test-notify.cc') diff --git a/tests/test-notify.cc b/tests/test-notify.cc index 6fe7c59..0d92d7a 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -359,9 +359,14 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) 30, TRUE); + // the file we expect to play on a low battery notification... + const char* expected_file = XDG_DATA_HOME "/" GETTEXT_PACKAGE "/sounds/" LOW_BATTERY_SOUND; + char* tmp = g_filename_to_uri(expected_file, nullptr, nullptr); + const std::string low_power_uri {tmp}; + g_clear_pointer(&tmp, g_free); + // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - const std::string low_power_uri {"file://" XDG_DATA_HOME "/" GETTEXT_PACKAGE "/sounds/Low%20battery.ogg"}; std::string last_uri; auto sound_player = indicator_power_sound_player_mock_new (); g_signal_connect(sound_player, "uri-played", G_CALLBACK(on_uri_played), &last_uri); -- cgit v1.2.3 From f4d66118c3a845892b32495efec3d6473e17baca Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 1 Feb 2016 10:53:07 -0600 Subject: for low power notifications, use libnotify's 'sound-file' property instead of indicator-power calling the sound player directly --- tests/test-notify.cc | 88 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 61 insertions(+), 27 deletions(-) (limited to 'tests/test-notify.cc') diff --git a/tests/test-notify.cc b/tests/test-notify.cc index 0d92d7a..d01ef5f 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -23,7 +23,6 @@ #include "dbus-shared.h" #include "device.h" #include "notifier.h" -#include "sound-player-mock.h" #include @@ -160,6 +159,51 @@ protected: super::TearDown(); } + + /*** + **** + ***/ + + int get_notify_call_count() const + { + // confirm that we got exactly one call + guint len {0u}; + GError* error {nullptr}; + dbus_test_dbus_mock_object_get_method_calls(mock, obj, METHOD_NOTIFY, &len, &error); + g_assert_no_error(error); + return len; + } + + std::string get_notify_call_sound_file(int call_number) + { + std::string ret; + + guint len {0u}; + GError* error {nullptr}; + auto calls = dbus_test_dbus_mock_object_get_method_calls(mock, obj, METHOD_NOTIFY, &len, &error); + g_return_val_if_fail(int(len) > call_number, ret); + g_assert_no_error(error); + + constexpr int HINTS_PARAM_POSITION {6}; + const auto& call = calls[call_number]; + g_return_val_if_fail(g_variant_n_children(call.params) > HINTS_PARAM_POSITION, ret); + auto hints = g_variant_get_child_value(call.params, HINTS_PARAM_POSITION); + const gchar* sound_file = nullptr; + auto success = g_variant_lookup(hints, "sound-file", "&s", &sound_file); + g_return_val_if_fail(success, ret); + if (sound_file != nullptr) + ret = sound_file; + g_clear_pointer(&hints, g_variant_unref); + + return ret; + } + + void clear_method_calls() + { + GError* error{nullptr}; + ASSERT_TRUE(dbus_test_dbus_mock_object_clear_method_calls(mock, obj, &error)); + g_assert_no_error(error); + } }; /*** @@ -278,8 +322,7 @@ TEST_F(NotifyFixture, LevelsDuringBatteryDrain) // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - auto sound_player = indicator_power_sound_player_mock_new (); - auto notifier = indicator_power_notifier_new (sound_player); + auto notifier = indicator_power_notifier_new (); indicator_power_notifier_set_battery (notifier, battery); indicator_power_notifier_set_bus (notifier, bus); wait_msec(); @@ -323,21 +366,12 @@ TEST_F(NotifyFixture, LevelsDuringBatteryDrain) g_dbus_connection_signal_unsubscribe (bus, sub_tag); g_object_unref (battery); g_object_unref (notifier); - g_object_unref (sound_player); } /*** **** ***/ -namespace -{ - static void on_uri_played(IndicatorPowerSoundPlayer*, const char* uri, gpointer glast_uri) - { - *static_cast(glast_uri) = uri; - } -} - TEST_F(NotifyFixture, EventsThatChangeNotifications) { // GetCapabilities returns an array containing 'actions', so that we'll @@ -367,10 +401,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) // set up a notifier and give it the battery so changing the battery's // charge should show up on the bus. - std::string last_uri; - auto sound_player = indicator_power_sound_player_mock_new (); - g_signal_connect(sound_player, "uri-played", G_CALLBACK(on_uri_played), &last_uri); - auto notifier = indicator_power_notifier_new (sound_player); + auto notifier = indicator_power_notifier_new (); indicator_power_notifier_set_battery (notifier, battery); indicator_power_notifier_set_bus (notifier, bus); ChangedParams changed_params; @@ -388,7 +419,6 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) // test setup case wait_msec(); EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); - EXPECT_TRUE(last_uri.empty()); // change the percent past the 'low' threshold and confirm that // a) the power level changes @@ -399,48 +429,52 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); EXPECT_EQ (indicator_power_notifier_get_power_level(battery), changed_params.power_level); EXPECT_TRUE (changed_params.is_warning); - EXPECT_EQ (low_power_uri, last_uri); + EXPECT_EQ (1, get_notify_call_count()); + EXPECT_EQ (low_power_uri, get_notify_call_sound_file(0)); + clear_method_calls(); // now test that the warning changes if the level goes down even lower... - last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_very_low); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL, changed_params.fields); EXPECT_STREQ (POWER_LEVEL_STR_VERY_LOW, changed_params.power_level.c_str()); - EXPECT_EQ (low_power_uri, last_uri); + EXPECT_EQ (1, get_notify_call_count()); + EXPECT_EQ (low_power_uri, get_notify_call_sound_file(0)); + clear_method_calls(); // ...and that the warning is taken down if the battery is plugged back in... - last_uri.clear(); + //last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, nullptr); wait_msec(); EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields); EXPECT_FALSE (changed_params.is_warning); - EXPECT_TRUE(last_uri.empty()); + EXPECT_EQ (0, get_notify_call_count()); // ...and that it comes back if we unplug again... - last_uri.clear(); + //last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, nullptr); wait_msec(); EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields); EXPECT_TRUE (changed_params.is_warning); - EXPECT_EQ (low_power_uri, last_uri); + EXPECT_EQ (1, get_notify_call_count()); + EXPECT_EQ (low_power_uri, get_notify_call_sound_file(0)); + clear_method_calls(); // ...and that it's taken down if the power level is OK - last_uri.clear(); + //last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_low+1); wait_msec(); EXPECT_EQ (FIELD_POWER_LEVEL|FIELD_IS_WARNING, changed_params.fields); EXPECT_STREQ (POWER_LEVEL_STR_OK, changed_params.power_level.c_str()); EXPECT_FALSE (changed_params.is_warning); - EXPECT_TRUE(last_uri.empty()); + EXPECT_EQ (0, get_notify_call_count()); // cleanup g_dbus_connection_signal_unsubscribe (bus, sub_tag); g_object_unref (notifier); g_object_unref (battery); - g_object_unref (sound_player); } -- cgit v1.2.3 From 557e10a031b886dc670bdf5918c3984edbace5ac Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 1 Feb 2016 12:24:34 -0600 Subject: remove dead code --- tests/test-notify.cc | 4 ---- 1 file changed, 4 deletions(-) (limited to 'tests/test-notify.cc') diff --git a/tests/test-notify.cc b/tests/test-notify.cc index d01ef5f..cd5b6f1 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -166,7 +166,6 @@ protected: int get_notify_call_count() const { - // confirm that we got exactly one call guint len {0u}; GError* error {nullptr}; dbus_test_dbus_mock_object_get_method_calls(mock, obj, METHOD_NOTIFY, &len, &error); @@ -444,7 +443,6 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) clear_method_calls(); // ...and that the warning is taken down if the battery is plugged back in... - //last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_CHARGING, nullptr); wait_msec(); @@ -453,7 +451,6 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) EXPECT_EQ (0, get_notify_call_count()); // ...and that it comes back if we unplug again... - //last_uri.clear(); changed_params = ChangedParams(); g_object_set (battery, INDICATOR_POWER_DEVICE_STATE, UP_DEVICE_STATE_DISCHARGING, nullptr); wait_msec(); @@ -464,7 +461,6 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications) clear_method_calls(); // ...and that it's taken down if the power level is OK - //last_uri.clear(); changed_params = ChangedParams(); set_battery_percentage (battery, percent_low+1); wait_msec(); -- cgit v1.2.3 From 39571b0e05c78627d233b2f77250de459d73f4a6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 16 May 2016 12:59:03 -0500 Subject: fix cmake warning of the test apps' dependency on the service library --- tests/test-notify.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'tests/test-notify.cc') diff --git a/tests/test-notify.cc b/tests/test-notify.cc index cd5b6f1..8d7f0d8 100644 --- a/tests/test-notify.cc +++ b/tests/test-notify.cc @@ -61,8 +61,6 @@ protected: static constexpr int NOTIFICATION_CLOSED_API {3}; static constexpr int NOTIFICATION_CLOSED_UNDEFINED {4}; - static constexpr char const * APP_NAME {"ayatana-indicator-power-service"}; - static constexpr char const * METHOD_CLOSE {"CloseNotification"}; static constexpr char const * METHOD_NOTIFY {"Notify"}; static constexpr char const * METHOD_GET_CAPS {"GetCapabilities"}; @@ -135,7 +133,7 @@ protected: g_dbus_connection_set_exit_on_close(bus, FALSE); g_object_add_weak_pointer(G_OBJECT(bus), reinterpret_cast(&bus)); - notify_init(APP_NAME); + notify_init(SERVICE_EXEC); } virtual void TearDown() -- cgit v1.2.3