aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt9
-rw-r--r--tests/glib-fixture.h201
-rw-r--r--tests/test-device.cc4
-rw-r--r--tests/test-notify.cc71
4 files changed, 219 insertions, 66 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 1dc8a8f..9c0d222 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -14,6 +14,11 @@ set_directory_properties (PROPERTIES
ADDITIONAL_MAKE_CLEAN_FILES gschemas.compiled)
set_source_files_properties (gschemas.compiled GENERATED)
+# make a XDG_DATA_HOME for sounds/
+set(XDG_DATA_HOME "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_DATADIR}")
+add_definitions(-DXDG_DATA_HOME="${XDG_DATA_HOME}")
+file(COPY "${CMAKE_SOURCE_DIR}/data/sounds" DESTINATION "${XDG_DATA_HOME}/${CMAKE_PROJECT_NAME}")
+
# GSettings:
# compile the ayatana-indicator-power schema into a gschemas.compiled file in this directory,
# and help the tests to find that file by setting -DSCHEMA_DIR
@@ -45,8 +50,8 @@ function(add_test_by_name name)
add_executable (${TEST_NAME} ${TEST_NAME}.cc)
target_link_options(${TEST_NAME} PRIVATE -no-pie)
add_test (${TEST_NAME} ${TEST_NAME})
- add_dependencies (${TEST_NAME} ayatanaindicatorpowerservice gschemas-compiled)
- target_link_libraries (${TEST_NAME} ayatanaindicatorpowerservice gtest ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS} ${URLDISPATCHER_LIBRARIES} ${GMOCK_LIBRARIES})
+ add_dependencies (${TEST_NAME} ${SERVICE_LIB} gschemas-compiled)
+ target_link_libraries (${TEST_NAME} ${SERVICE_LIB} ${DBUSTEST_LIBRARIES} ${SERVICE_DEPS_LIBRARIES} ${URLDISPATCHER_LIBRARIES} ${GMOCK_LIBRARIES})
endfunction()
add_test_by_name(test-notify)
add_test(NAME dear-reader-the-next-test-takes-80-seconds COMMAND true)
diff --git a/tests/glib-fixture.h b/tests/glib-fixture.h
index 09c37a3..2350d49 100644
--- a/tests/glib-fixture.h
+++ b/tests/glib-fixture.h
@@ -1,5 +1,8 @@
/*
- * Copyright 2014 Canonical Ltd.
+ * Copyright 2013-2016 Canonical Ltd.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
*
* 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
@@ -12,12 +15,14 @@
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * Authors:
- * Charles Kerr <charles.kerr@canonical.com>
*/
+#pragma once
+
+#include <chrono>
+#include <functional> // std::function
#include <map>
+#include <memory> // std::shared_ptr
#include <glib.h>
#include <glib/gstdio.h>
@@ -29,79 +34,48 @@
class GlibFixture : public ::testing::Test
{
- private:
+ public:
- GLogFunc realLogHandler;
-
- std::map<GLogLevelFlags,size_t> expected_log;
- std::map<GLogLevelFlags,std::vector<std::string>> log;
-
- void test_log_counts()
- {
- const GLogLevelFlags levels_to_test[] = { G_LOG_LEVEL_ERROR,
- G_LOG_LEVEL_CRITICAL,
- G_LOG_LEVEL_MESSAGE,
- G_LOG_LEVEL_WARNING };
-
- for(const auto& level : levels_to_test)
- {
- const auto& v = log[level];
- const auto n = v.size();
-
- EXPECT_EQ(expected_log[level], n);
-
- if (expected_log[level] != n)
- for (size_t i=0; i<n; ++i)
- g_print("%lu %s\n", (n+1), v[i].c_str());
- }
-
- expected_log.clear();
- log.clear();
- }
-
- static void default_log_handler(const gchar * log_domain,
- GLogLevelFlags log_level,
- const gchar * message,
- gpointer self)
- {
- auto tmp = g_strdup_printf ("%s:%d \"%s\"", log_domain, int(log_level), message);
- static_cast<GlibFixture*>(self)->log[log_level].push_back(tmp);
- g_free(tmp);
- }
+ virtual ~GlibFixture() =default;
protected:
- void increment_expected_errors(GLogLevelFlags level, size_t n=1)
- {
- expected_log[level] += n;
- }
-
- virtual void SetUp()
+ virtual void SetUp() override
{
setlocale(LC_ALL, "C.UTF-8");
loop = g_main_loop_new(nullptr, false);
- g_log_set_default_handler(default_log_handler, this);
+ // only use local, temporary settings
+ g_assert(g_setenv("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, true));
+ g_assert(g_setenv("GSETTINGS_BACKEND", "memory", true));
+ g_debug("SCHEMA_DIR is %s", SCHEMA_DIR);
+
+ // fail on unexpected messages from this domain
+ g_log_set_fatal_mask(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING);
g_unsetenv("DISPLAY");
+
}
- virtual void TearDown()
+ virtual void TearDown() override
{
- test_log_counts();
-
- g_log_set_default_handler(realLogHandler, this);
+ g_test_assert_expected_messages ();
g_clear_pointer(&loop, g_main_loop_unref);
}
+ void expectLogMessage (const gchar *domain, GLogLevelFlags level, const gchar *pattern)
+ {
+ g_test_expect_message (domain, level, pattern);
+ }
+
private:
static gboolean
wait_for_signal__timeout(gpointer name)
{
- g_error("%s: timed out waiting for signal '%s'", G_STRLOC, static_cast<char*>(name));
+ g_error("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name);
return G_SOURCE_REMOVE;
}
@@ -115,7 +89,7 @@ class GlibFixture : public ::testing::Test
protected:
/* convenience func to loop while waiting for a GObject's signal */
- void wait_for_signal(gpointer o, const gchar * signal, const guint timeout_seconds=5)
+ void wait_for_signal(gpointer o, const gchar * signal, const int timeout_seconds=5)
{
// wait for the signal or for timeout, whichever comes first
const auto handler_id = g_signal_connect_swapped(o, signal,
@@ -130,12 +104,125 @@ class GlibFixture : public ::testing::Test
}
/* convenience func to loop for N msec */
- void wait_msec(guint msec=50)
+ void wait_msec(int msec=50)
{
const auto id = g_timeout_add(msec, wait_msec__timeout, loop);
g_main_loop_run(loop);
g_source_remove(id);
}
- GMainLoop * loop;
+ bool wait_for(std::function<bool()> test_function, guint timeout_msec=1000)
+ {
+ auto timer = std::shared_ptr<GTimer>(g_timer_new(), [](GTimer* t){g_timer_destroy(t);});
+ const auto timeout_sec = timeout_msec / 1000.0;
+ for (;;) {
+ if (test_function())
+ return true;
+ //g_message("%f ... %f", g_timer_elapsed(timer.get(), nullptr), timeout_sec);
+ if (g_timer_elapsed(timer.get(), nullptr) >= timeout_sec)
+ return false;
+ wait_msec();
+ }
+ }
+
+ bool wait_for_name_owned(
+ GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ struct Data {
+ GMainLoop* loop = nullptr;
+ bool owned = false;
+ };
+ Data data;
+
+ auto on_name_appeared = [](GDBusConnection* /*connection*/,
+ const gchar* /*name_*/,
+ const gchar* name_owner,
+ gpointer gdata){
+ if (name_owner == nullptr)
+ return;
+ auto tmp = static_cast<Data*>(gdata);
+ tmp->owned = true;
+ g_main_loop_quit(tmp->loop);
+ };
+
+ const auto timeout_id = g_timeout_add(timeout_msec, wait_msec__timeout, loop);
+ data.loop = loop;
+ const auto watch_id = g_bus_watch_name_on_connection(
+ connection,
+ name,
+ flags,
+ on_name_appeared,
+ nullptr, // name_vanished
+ &data,
+ nullptr // user_data_free_func
+ );
+
+ g_main_loop_run(loop);
+
+ g_bus_unwatch_name(watch_id);
+ g_source_remove(timeout_id);
+
+ return data.owned;
+ }
+
+ void EXPECT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ EXPECT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void EXPECT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ EXPECT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void ASSERT_NAME_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ ASSERT_TRUE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ void ASSERT_NAME_NOT_OWNED_EVENTUALLY(GDBusConnection* connection,
+ const gchar* name,
+ guint timeout_msec=1000,
+ GBusNameWatcherFlags flags=G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
+ {
+ ASSERT_FALSE(wait_for_name_owned(connection, name, timeout_msec, flags)) << "name: " << name;
+ }
+
+ using source_func = std::function<gboolean()>;
+
+ guint idle_add(source_func&& func)
+ {
+ return g_idle_add_full(
+ G_PRIORITY_DEFAULT_IDLE,
+ [](gpointer gf){return (*static_cast<source_func*>(gf))();},
+ new std::function<gboolean()>(func),
+ [](gpointer gf){delete static_cast<source_func*>(gf);}
+ );
+ }
+
+ guint timeout_add(source_func&& func, std::chrono::milliseconds msec)
+ {
+ return g_timeout_add_full(
+ G_PRIORITY_DEFAULT,
+ msec.count(),
+ [](gpointer gf){return (*static_cast<source_func*>(gf))();},
+ new std::function<gboolean()>(func),
+ [](gpointer gf){delete static_cast<source_func*>(gf);}
+ );
+ }
+
+ GMainLoop* loop {};
};
+
diff --git a/tests/test-device.cc b/tests/test-device.cc
index bdc29d1..a948857 100644
--- a/tests/test-device.cc
+++ b/tests/test-device.cc
@@ -53,7 +53,7 @@ class DeviceTest : public ::testing::Test
virtual void SetUp()
{
const GLogLevelFlags flags = GLogLevelFlags(G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING);
- log_handler_id = g_log_set_handler ("ayatana-indicator-power", flags, log_count_func, this);
+ log_handler_id = g_log_set_handler(G_LOG_DOMAIN, flags, log_count_func, this);
log_count_ipower_expected = 0;
log_count_ipower_actual = 0;
}
@@ -61,7 +61,7 @@ class DeviceTest : public ::testing::Test
virtual void TearDown()
{
ASSERT_EQ (log_count_ipower_expected, log_count_ipower_actual);
- g_log_remove_handler ("ayatana-indicator-power", log_handler_id);
+ g_log_remove_handler (G_LOG_DOMAIN, log_handler_id);
}
protected:
diff --git a/tests/test-notify.cc b/tests/test-notify.cc
index 63214c2..8d7f0d8 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
@@ -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"};
@@ -77,6 +75,8 @@ protected:
{
super::SetUp();
+ g_setenv ("XDG_DATA_HOME", XDG_DATA_HOME, TRUE);
+
// init DBusMock / dbus-test-runner
service = dbus_test_service_new(nullptr);
@@ -133,7 +133,7 @@ protected:
g_dbus_connection_set_exit_on_close(bus, FALSE);
g_object_add_weak_pointer(G_OBJECT(bus), reinterpret_cast<gpointer*>(&bus));
- notify_init(APP_NAME);
+ notify_init(SERVICE_EXEC);
}
virtual void TearDown()
@@ -157,6 +157,50 @@ protected:
super::TearDown();
}
+
+ /***
+ ****
+ ***/
+
+ int get_notify_call_count() const
+ {
+ 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);
+ }
};
/***
@@ -317,8 +361,8 @@ 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);
}
/***
@@ -346,6 +390,12 @@ 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.
auto notifier = indicator_power_notifier_new ();
@@ -376,6 +426,9 @@ 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 (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...
changed_params = ChangedParams();
@@ -383,6 +436,9 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications)
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 (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...
changed_params = ChangedParams();
@@ -390,6 +446,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications)
wait_msec();
EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields);
EXPECT_FALSE (changed_params.is_warning);
+ EXPECT_EQ (0, get_notify_call_count());
// ...and that it comes back if we unplug again...
changed_params = ChangedParams();
@@ -397,6 +454,9 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications)
wait_msec();
EXPECT_EQ (FIELD_IS_WARNING, changed_params.fields);
EXPECT_TRUE (changed_params.is_warning);
+ 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
changed_params = ChangedParams();
@@ -405,6 +465,7 @@ TEST_F(NotifyFixture, EventsThatChangeNotifications)
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_EQ (0, get_notify_call_count());
// cleanup
g_dbus_connection_signal_unsubscribe (bus, sub_tag);