From f8a5d99b5ac03b5b759f67b33ed2c989fc0d0ceb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 12:13:20 -0600 Subject: cmake and test directory cleanup --- tests/utils/adbd-server.h | 148 +++++++++++++++++++++++++++++ tests/utils/dbus-types.h | 47 ++++++++++ tests/utils/glib-fixture.h | 203 ++++++++++++++++++++++++++++++++++++++++ tests/utils/qmain.cpp | 60 ++++++++++++ tests/utils/test-dbus-fixture.h | 106 +++++++++++++++++++++ 5 files changed, 564 insertions(+) create mode 100644 tests/utils/adbd-server.h create mode 100644 tests/utils/dbus-types.h create mode 100644 tests/utils/glib-fixture.h create mode 100644 tests/utils/qmain.cpp create mode 100644 tests/utils/test-dbus-fixture.h (limited to 'tests/utils') diff --git a/tests/utils/adbd-server.h b/tests/utils/adbd-server.h new file mode 100644 index 0000000..740faaa --- /dev/null +++ b/tests/utils/adbd-server.h @@ -0,0 +1,148 @@ +/* + * Copyright 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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include +#include + +#include +#include +#include + + +/** + * A Mock ADBD server. + * + * Binds to a local domain socket, sends public key requests across it, + * and reads back the client's responses. + */ +class GAdbdServer +{ +public: + + GAdbdServer(const std::string& socket_path, + const std::vector& requests): + m_requests{requests}, + m_socket_path{socket_path}, + m_cancellable{g_cancellable_new()}, + m_worker_thread{&GAdbdServer::worker_func, this} + { + } + + ~GAdbdServer() + { + // tell the worker thread to stop whatever it's doing and exit. + g_cancellable_cancel(m_cancellable); + m_worker_thread.join(); + g_clear_object(&m_cancellable); + } + + const std::vector m_requests; + std::vector m_responses; + +private: + + void worker_func() // runs in worker thread + { + auto server_socket = create_server_socket(m_socket_path); + auto requests = m_requests; + + GError* error {}; + g_socket_listen (server_socket, &error); + g_assert_no_error (error); + + while (!g_cancellable_is_cancelled(m_cancellable) && !requests.empty()) + { + // wait for a client connection + g_message("GAdbdServer::Impl::worker_func() calling g_socket_accept()"); + auto client_socket = g_socket_accept(server_socket, m_cancellable, &error); + if (error != nullptr) { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("GAdbdServer: Error accepting socket connection: %s", error->message); + g_clear_error(&error); + break; + } + + // pop the next request off the stack + auto request = requests.front(); + requests.erase(requests.begin()); + + // send the request + g_message("GAdbdServer::Impl::worker_func() sending req [%s]", request.c_str()); + g_socket_send(client_socket, + request.c_str(), + request.size(), + m_cancellable, + &error); + if (error != nullptr) { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("GAdbdServer: Error sending request: %s", error->message); + g_clear_error(&error); + g_clear_object(&client_socket); + break; + } + + // read the response + g_message("GAdbdServer::Impl::worker_func() reading response"); + char buf[4096]; + const auto n_bytes = g_socket_receive(client_socket, + buf, + sizeof(buf), + m_cancellable, + &error); + if (error != nullptr) { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("GAdbdServer: Error reading response: %s", error->message); + g_clear_error(&error); + g_clear_object(&client_socket); + break; + } + const std::string response(buf, std::string::size_type(n_bytes)); + g_message("server got response: %s", response.c_str()); + m_responses.push_back(response); + + // cleanup + g_clear_object(&client_socket); + } + + g_clear_object(&server_socket); + } + + // bind to a local domain socket + static GSocket* create_server_socket(const std::string& socket_path) + { + GError* error {}; + auto socket = g_socket_new(G_SOCKET_FAMILY_UNIX, + G_SOCKET_TYPE_STREAM, + G_SOCKET_PROTOCOL_DEFAULT, + &error); + g_assert_no_error(error); + auto address = g_unix_socket_address_new (socket_path.c_str()); + g_socket_bind (socket, address, false, &error); + g_assert_no_error (error); + g_clear_object (&address); + + return socket; + } + + const std::string m_socket_path; + GCancellable* m_cancellable = nullptr; + std::thread m_worker_thread; +}; + + diff --git a/tests/utils/dbus-types.h b/tests/utils/dbus-types.h new file mode 100644 index 0000000..c2dfb81 --- /dev/null +++ b/tests/utils/dbus-types.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2013-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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Author: Pete Woods + */ + +#pragma once + +#include +#include +#include +#include + +typedef QMap QVariantDictMap; +Q_DECLARE_METATYPE(QVariantDictMap) + +typedef QMap QStringMap; +Q_DECLARE_METATYPE(QStringMap) + +namespace DBusTypes +{ + inline void registerMetaTypes() + { + qRegisterMetaType("QVariantDictMap"); + qRegisterMetaType("QStringMap"); + + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); + } + static constexpr char const* NOTIFY_DBUS_NAME = "org.freedesktop.Notifications"; + + static constexpr char const* NOTIFY_DBUS_INTERFACE = "org.freedesktop.Notifications"; + + static constexpr char const* NOTIFY_DBUS_PATH = "/org/freedesktop/Notifications"; +} diff --git a/tests/utils/glib-fixture.h b/tests/utils/glib-fixture.h new file mode 100644 index 0000000..ccdeccd --- /dev/null +++ b/tests/utils/glib-fixture.h @@ -0,0 +1,203 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * 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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#pragma once + +#include // std::function +#include +#include // std::shared_ptr + +#include +#include +#include + +#include + +#include // setlocale() + +class GlibFixture : public ::testing::Test +{ + public: + + virtual ~GlibFixture() =default; + + protected: + + virtual void SetUp() override + { + setlocale(LC_ALL, "C.UTF-8"); + + loop = g_main_loop_new(nullptr, false); + +#ifdef SCHEMA_DIR + // 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); +#endif + + // 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() override + { + 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(name)); + return G_SOURCE_REMOVE; + } + + static gboolean + wait_msec__timeout(gpointer loop) + { + g_main_loop_quit(static_cast(loop)); + return G_SOURCE_CONTINUE; + } + + protected: + + /* convenience func to loop while waiting for a GObject's signal */ + void wait_for_signal(gpointer o, const gchar * signal, guint timeout_seconds=5) + { + // wait for the signal or for timeout, whichever comes first + const auto handler_id = g_signal_connect_swapped(o, signal, + G_CALLBACK(g_main_loop_quit), + loop); + const auto timeout_id = g_timeout_add_seconds(timeout_seconds, + wait_for_signal__timeout, + loop); + g_main_loop_run(loop); + g_source_remove(timeout_id); + g_signal_handler_disconnect(o, handler_id); + } + + /* convenience func to loop for N msec */ + void wait_msec(guint msec=50) + { + const auto id = g_timeout_add(msec, wait_msec__timeout, loop); + g_main_loop_run(loop); + g_source_remove(id); + } + + bool wait_for(std::function test_function, guint timeout_msec=1000) + { + auto timer = std::shared_ptr(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(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; + } + + GMainLoop * loop; +}; + diff --git a/tests/utils/qmain.cpp b/tests/utils/qmain.cpp new file mode 100644 index 0000000..72a49b1 --- /dev/null +++ b/tests/utils/qmain.cpp @@ -0,0 +1,60 @@ +/* + * Copyright © 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Authors: + * Pete Woods + */ + +//#include + +#include +#include +#include + +#include + +using namespace QtDBusMock; + +class Runner: public QObject +{ + Q_OBJECT +public Q_SLOTS: + void run() + { + QCoreApplication::exit(RUN_ALL_TESTS()); + } +}; + +int main(int argc, char **argv) +{ + qputenv("LANG", "C.UTF-8"); + unsetenv("LC_ALL"); + + // boilerplate i18n + setlocale(LC_ALL, ""); + bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR); + textdomain(GETTEXT_PACKAGE); + + QCoreApplication application(argc, argv); + DBusMock::registerMetaTypes(); + ::testing::InitGoogleTest(&argc, argv); + + Runner runner; + QTimer::singleShot(0, &runner, SLOT(run())); + + return application.exec(); +} + +#include "qmain.moc" diff --git a/tests/utils/test-dbus-fixture.h b/tests/utils/test-dbus-fixture.h new file mode 100644 index 0000000..3947e58 --- /dev/null +++ b/tests/utils/test-dbus-fixture.h @@ -0,0 +1,106 @@ +/* + * Copyright 2013 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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#pragma once + +#include "glib-fixture.h" + +/*** +**** +***/ + +class TestDBusFixture: public GlibFixture +{ + public: + + TestDBusFixture() =default; + virtual ~TestDBusFixture() =default; + + explicit TestDBusFixture(const std::vector& service_dirs_in): service_dirs(service_dirs_in) {} + + private: + + typedef GlibFixture super; + + static void + on_bus_opened (GObject* /*object*/, GAsyncResult * res, gpointer gself) + { + auto self = static_cast(gself); + + GError * err = 0; + self->system_bus = g_bus_get_finish (res, &err); + g_assert_no_error (err); + + g_main_loop_quit (self->loop); + } + + static void + on_bus_closed (GObject* /*object*/, GAsyncResult * res, gpointer gself) + { + auto self = static_cast(gself); + + GError * err = 0; + g_dbus_connection_close_finish (self->system_bus, res, &err); + g_assert_no_error (err); + + g_main_loop_quit (self->loop); + } + + protected: + + GTestDBus * test_dbus = nullptr; + GDBusConnection * system_bus = nullptr; + const std::vector service_dirs; + + virtual void SetUp() override + { + super::SetUp (); + + // pull up a test dbus + test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE); + for (const auto& dir : service_dirs) + g_test_dbus_add_service_dir (test_dbus, dir.c_str()); + g_test_dbus_up (test_dbus); + const char * address = g_test_dbus_get_bus_address (test_dbus); + g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", address, true); + g_setenv ("DBUS_SESSION_BUS_ADDRESS", address, true); + g_debug ("test_dbus's address is %s", address); + + // wait for the GDBusConnection before returning + g_bus_get (G_BUS_TYPE_SYSTEM, nullptr, on_bus_opened, this); + g_main_loop_run (loop); + } + + virtual void TearDown() override + { + wait_msec(); + + // close the system bus + g_dbus_connection_close(system_bus, nullptr, on_bus_closed, this); + g_main_loop_run(loop); + g_clear_object(&system_bus); + + // tear down the test dbus + g_test_dbus_down(test_dbus); + g_clear_object(&test_dbus); + + super::TearDown(); + } +}; + -- cgit v1.2.3 From d8ef8e68805ab7f53258427c79ee5aaafec916ba Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Mar 2016 17:09:59 -0600 Subject: add UsbManager --- tests/utils/gtest-qt-print-helpers.h | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/utils/gtest-qt-print-helpers.h (limited to 'tests/utils') diff --git a/tests/utils/gtest-qt-print-helpers.h b/tests/utils/gtest-qt-print-helpers.h new file mode 100644 index 0000000..7a0897e --- /dev/null +++ b/tests/utils/gtest-qt-print-helpers.h @@ -0,0 +1,45 @@ + +#pragma once + +#include +#include +#include +#include + +inline QString qVariantToString(const QVariant& variant) { + QString output; + QDebug dbg(&output); + dbg << variant; + return output; +} + +inline void PrintTo(const QVariant& variant, std::ostream* os) { + QString output; + QDebug dbg(&output); + dbg << variant; + + *os << "QVariant(" << output.toStdString() << ")"; +} + +inline void PrintTo(const QString& s, std::ostream* os) { + *os << "\"" << s.toStdString() << "\""; +} + +inline void PrintTo(const QStringList& list, std::ostream* os) { + QString output; + QDebug dbg(&output); + dbg << list; + + *os << "QStringList(" << output.toStdString() << ")"; +} + +inline void PrintTo(const QList& list, std::ostream* os) { + QString output; + for (const auto& path: list) + { + output.append("\"" + path.path() + "\","); + } + + *os << "QList(" << output.toStdString() << ")"; +} + -- cgit v1.2.3 From bb4fe392f34fc6f839870967fe273dd1e3d1ed53 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 15 Mar 2016 13:38:58 -0500 Subject: add new CMakeLists.txt files in tests/utils/ and tests/unit/ --- tests/utils/CMakeLists.txt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/utils/CMakeLists.txt (limited to 'tests/utils') diff --git a/tests/utils/CMakeLists.txt b/tests/utils/CMakeLists.txt new file mode 100644 index 0000000..e458c82 --- /dev/null +++ b/tests/utils/CMakeLists.txt @@ -0,0 +1,17 @@ +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_library( + test-utils + STATIC + qmain.cpp +) + +qt5_use_modules( + test-utils + Core + DBus +) + -- cgit v1.2.3 From 65eba5e107b0499aec4bed55be202bf1df6710bb Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 09:25:23 -0500 Subject: add tests/utils/qdbus-helpers.h so that we only define qDBusArgumentToMap() in one place --- tests/utils/qdbus-helpers.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/utils/qdbus-helpers.h (limited to 'tests/utils') diff --git a/tests/utils/qdbus-helpers.h b/tests/utils/qdbus-helpers.h new file mode 100644 index 0000000..f873e23 --- /dev/null +++ b/tests/utils/qdbus-helpers.h @@ -0,0 +1,21 @@ +#pragma once + +#define QT_NO_KEYWORDS +#include +#include + +bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map) +{ + if (variant.canConvert()) + { + QDBusArgument value(variant.value()); + if (value.currentType() == QDBusArgument::MapType) + { + value >> map; + return true; + } + } + + return false; +} + -- cgit v1.2.3 From c63d90da0f1d9cbd1eee5dd66a9828c51cc8dcc9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 09:59:32 -0500 Subject: de-dupe use of dbus names --- tests/utils/dbus-types.h | 5 ----- 1 file changed, 5 deletions(-) (limited to 'tests/utils') diff --git a/tests/utils/dbus-types.h b/tests/utils/dbus-types.h index c2dfb81..3b3a02d 100644 --- a/tests/utils/dbus-types.h +++ b/tests/utils/dbus-types.h @@ -39,9 +39,4 @@ namespace DBusTypes qDBusRegisterMetaType(); qDBusRegisterMetaType(); } - static constexpr char const* NOTIFY_DBUS_NAME = "org.freedesktop.Notifications"; - - static constexpr char const* NOTIFY_DBUS_INTERFACE = "org.freedesktop.Notifications"; - - static constexpr char const* NOTIFY_DBUS_PATH = "/org/freedesktop/Notifications"; } -- cgit v1.2.3 From 5b6008fa3b5a9b0373b2f03791140ca1fa8dc8de Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Mar 2016 10:24:41 -0500 Subject: introduce a QtFixture gtest base class to reduce redundancy in test fixtures' helper/util code --- tests/utils/qt-fixture.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/utils/qt-fixture.h (limited to 'tests/utils') diff --git a/tests/utils/qt-fixture.h b/tests/utils/qt-fixture.h new file mode 100644 index 0000000..321d56e --- /dev/null +++ b/tests/utils/qt-fixture.h @@ -0,0 +1,58 @@ +/* + * Copyright 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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#pragma once + +#define QT_NO_KEYWORDS + +#include +#include +#include +#include + +#include + +#include + +class QtFixture: public GlibFixture +{ + using super = GlibFixture; + +public: + + QtFixture() + { + DBusTypes::registerMetaTypes(); + } + + ~QtFixture() =default; + +protected: + + void wait_for_signals(QSignalSpy& signalSpy, int signalsExpected) + { + while (signalSpy.size() < signalsExpected) + { + ASSERT_TRUE(signalSpy.wait()); + } + + ASSERT_EQ(signalsExpected, signalSpy.size()); + } +}; + -- cgit v1.2.3 From 7a25132c125f6e5e413ad26ea950ae22bee982f5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 21 Mar 2016 13:40:11 -0500 Subject: if our USB device is disconnected while prompting the user for ADBD, cancel the prompt. --- tests/utils/mock-usb-monitor.h | 32 ++++++++++++++++++++++++++++++++ tests/utils/qdbus-helpers.h | 21 --------------------- tests/utils/qt-fixture.h | 18 +++++++++++++++++- 3 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 tests/utils/mock-usb-monitor.h delete mode 100644 tests/utils/qdbus-helpers.h (limited to 'tests/utils') diff --git a/tests/utils/mock-usb-monitor.h b/tests/utils/mock-usb-monitor.h new file mode 100644 index 0000000..92b89db --- /dev/null +++ b/tests/utils/mock-usb-monitor.h @@ -0,0 +1,32 @@ +/* + * Copyright 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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#pragma once + +#include + +class MockUsbMonitor: public UsbMonitor +{ +public: + MockUsbMonitor() =default; + virtual ~MockUsbMonitor() =default; + core::Signal& on_usb_disconnected() override {return m_on_usb_disconnected;} + core::Signal m_on_usb_disconnected; +}; + diff --git a/tests/utils/qdbus-helpers.h b/tests/utils/qdbus-helpers.h deleted file mode 100644 index f873e23..0000000 --- a/tests/utils/qdbus-helpers.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#define QT_NO_KEYWORDS -#include -#include - -bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map) -{ - if (variant.canConvert()) - { - QDBusArgument value(variant.value()); - if (value.currentType() == QDBusArgument::MapType) - { - value >> map; - return true; - } - } - - return false; -} - diff --git a/tests/utils/qt-fixture.h b/tests/utils/qt-fixture.h index 321d56e..0f5722b 100644 --- a/tests/utils/qt-fixture.h +++ b/tests/utils/qt-fixture.h @@ -22,12 +22,13 @@ #define QT_NO_KEYWORDS #include -#include #include #include #include +#include +#include #include class QtFixture: public GlibFixture @@ -54,5 +55,20 @@ protected: ASSERT_EQ(signalsExpected, signalSpy.size()); } + + bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map) + { + if (variant.canConvert()) + { + QDBusArgument value(variant.value()); + if (value.currentType() == QDBusArgument::MapType) + { + value >> map; + return true; + } + } + + return false; + } }; -- cgit v1.2.3 From 0b9a8d08b2ea7d245ffd00ce465e6c859905c547 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 22 Mar 2016 15:34:00 -0500 Subject: make wait_for_signals() a macro again so that the GTest log messages will give the test file and line number --- tests/utils/qt-fixture.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'tests/utils') diff --git a/tests/utils/qt-fixture.h b/tests/utils/qt-fixture.h index 0f5722b..95b9b14 100644 --- a/tests/utils/qt-fixture.h +++ b/tests/utils/qt-fixture.h @@ -31,6 +31,16 @@ #include #include +#define wait_for_signals(signalSpy,signalsExpected) \ +{ \ + while (signalSpy.size() < signalsExpected) \ + { \ + ASSERT_TRUE(signalSpy.wait()); \ + } \ + \ + ASSERT_EQ(signalsExpected, signalSpy.size()); \ +} + class QtFixture: public GlibFixture { using super = GlibFixture; @@ -46,16 +56,6 @@ public: protected: - void wait_for_signals(QSignalSpy& signalSpy, int signalsExpected) - { - while (signalSpy.size() < signalsExpected) - { - ASSERT_TRUE(signalSpy.wait()); - } - - ASSERT_EQ(signalsExpected, signalSpy.size()); - } - bool qDBusArgumentToMap(QVariant const& variant, QVariantMap& map) { if (variant.canConvert()) -- cgit v1.2.3 From ba426a05796244070d9cae1e18d1885bed261b55 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 22 Mar 2016 15:34:55 -0500 Subject: in the mock ADB server, keep making a request until a response is received. --- tests/utils/adbd-server.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'tests/utils') diff --git a/tests/utils/adbd-server.h b/tests/utils/adbd-server.h index 740faaa..968c1f0 100644 --- a/tests/utils/adbd-server.h +++ b/tests/utils/adbd-server.h @@ -80,7 +80,6 @@ private: // pop the next request off the stack auto request = requests.front(); - requests.erase(requests.begin()); // send the request g_message("GAdbdServer::Impl::worker_func() sending req [%s]", request.c_str()); @@ -114,7 +113,10 @@ private: } const std::string response(buf, std::string::size_type(n_bytes)); g_message("server got response: %s", response.c_str()); - m_responses.push_back(response); + if (!response.empty()) { + m_responses.push_back(response); + requests.erase(requests.begin()); + } // cleanup g_clear_object(&client_socket); -- cgit v1.2.3 From a5f330f6b73101d7bbdeadc6a5f53b8da3349999 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 22 Mar 2016 15:39:36 -0500 Subject: don't show the snap decision until we're out of the greeter --- tests/utils/mock-greeter.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/utils/mock-greeter.h (limited to 'tests/utils') diff --git a/tests/utils/mock-greeter.h b/tests/utils/mock-greeter.h new file mode 100644 index 0000000..5ac85a0 --- /dev/null +++ b/tests/utils/mock-greeter.h @@ -0,0 +1,32 @@ +/* + * Copyright 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 + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#pragma once + +#include + +class MockGreeter: public Greeter +{ +public: + MockGreeter() =default; + virtual ~MockGreeter() =default; + core::Property& is_active() override {return m_is_active;} + core::Property m_is_active {false}; +}; + -- cgit v1.2.3 From 4f8a17f23a0bba7da7654c147ca377e271abe0db Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 22 Mar 2016 16:01:47 -0500 Subject: add tests for not showing snap decisions in greeter mode --- tests/utils/adbd-server.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/utils') diff --git a/tests/utils/adbd-server.h b/tests/utils/adbd-server.h index 968c1f0..5e66379 100644 --- a/tests/utils/adbd-server.h +++ b/tests/utils/adbd-server.h @@ -73,7 +73,7 @@ private: auto client_socket = g_socket_accept(server_socket, m_cancellable, &error); if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("GAdbdServer: Error accepting socket connection: %s", error->message); + g_message("GAdbdServer: Error accepting socket connection: %s", error->message); g_clear_error(&error); break; } @@ -90,7 +90,7 @@ private: &error); if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("GAdbdServer: Error sending request: %s", error->message); + g_message("GAdbdServer: Error sending request: %s", error->message); g_clear_error(&error); g_clear_object(&client_socket); break; @@ -106,10 +106,10 @@ private: &error); if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning("GAdbdServer: Error reading response: %s", error->message); + g_message("GAdbdServer: Error reading response: %s", error->message); g_clear_error(&error); g_clear_object(&client_socket); - break; + continue; } const std::string response(buf, std::string::size_type(n_bytes)); g_message("server got response: %s", response.c_str()); -- cgit v1.2.3 From 194d7e85a52cbc0060a2d85b71b9ddd8b606aee4 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 24 Mar 2016 11:01:16 -0500 Subject: add tracer g_debug() calls for the benefit of the integration tests --- tests/utils/adbd-server.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/utils') diff --git a/tests/utils/adbd-server.h b/tests/utils/adbd-server.h index 5e66379..b574622 100644 --- a/tests/utils/adbd-server.h +++ b/tests/utils/adbd-server.h @@ -112,7 +112,7 @@ private: continue; } const std::string response(buf, std::string::size_type(n_bytes)); - g_message("server got response: %s", response.c_str()); + g_message("server read %d bytes, got response: '%s'", int(n_bytes), response.c_str()); if (!response.empty()) { m_responses.push_back(response); requests.erase(requests.begin()); -- cgit v1.2.3