aboutsummaryrefslogtreecommitdiff
path: root/tests/unit
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2016-10-21 14:19:54 +0000
committerBileto Bot <ci-train-bot@canonical.com>2016-10-21 14:19:54 +0000
commit3def83e1c5c34f63a3d36c4fff7f99a4d903a70f (patch)
tree337b9695c99dde56b25d6b20a20563adfb82bd41 /tests/unit
parent4f079d2faa6dd24f7ccd0566c4820a835fccce6a (diff)
parent7002fc4e6a6496fb5c0d3294540c957787689847 (diff)
downloadayatana-indicator-display-3def83e1c5c34f63a3d36c4fff7f99a4d903a70f.tar.gz
ayatana-indicator-display-3def83e1c5c34f63a3d36c4fff7f99a4d903a70f.tar.bz2
ayatana-indicator-display-3def83e1c5c34f63a3d36c4fff7f99a4d903a70f.zip
Fix test error in adbd-client-test
Approved by: unity-api-1-bot
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/CMakeLists.txt5
-rw-r--r--tests/unit/adbd-client-test.cpp5
-rw-r--r--tests/unit/greeter-test.cpp159
-rw-r--r--tests/unit/usb-snap-test.cpp13
4 files changed, 171 insertions, 11 deletions
diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt
index fe70461..9d8cad2 100644
--- a/tests/unit/CMakeLists.txt
+++ b/tests/unit/CMakeLists.txt
@@ -14,6 +14,10 @@ set(TEST_LINK_LIBRARIES
${GMOCK_LIBRARIES}
)
+add_definitions(
+ -DGREETER_TEMPLATE="${CMAKE_SOURCE_DIR}/tests/utils/mock-unity-greeter.py"
+)
+
function(add_test_by_name name)
set(TEST_NAME ${name})
add_executable (${TEST_NAME} ${TEST_NAME}.cpp)
@@ -31,4 +35,5 @@ function(add_qt_test_by_name name)
set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT ${CTEST_ENVIRONMENT})
target_link_libraries(${TEST_NAME} ${SERVICE_LINK_LIBRARIES} ${QT_LINK_LIBRARIES} ${TEST_LINK_LIBRARIES} ${THREAD_LINK_LIBRARIES})
endfunction()
+add_qt_test_by_name(greeter-test)
add_qt_test_by_name(usb-snap-test)
diff --git a/tests/unit/adbd-client-test.cpp b/tests/unit/adbd-client-test.cpp
index 754f76c..d1ce740 100644
--- a/tests/unit/adbd-client-test.cpp
+++ b/tests/unit/adbd-client-test.cpp
@@ -73,7 +73,7 @@ TEST_F(AdbdClientFixture, SocketPlumbing)
// start an AdbdClient that listens for PKRequests
std::string pk;
auto adbd_client = std::make_shared<GAdbdClient>(socket_path);
- adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){
+ auto connection = adbd_client->on_pk_request().connect([&pk, main_thread, test](const AdbdClient::PKRequest& req){
EXPECT_EQ(main_thread, g_thread_self());
g_message("in on_pk_request with %s", req.public_key.c_str());
pk = req.public_key;
@@ -82,12 +82,13 @@ TEST_F(AdbdClientFixture, SocketPlumbing)
// start a mock AdbdServer with to fire test key requests and wait for a response
auto adbd_server = std::make_shared<GAdbdServer>(socket_path, std::vector<std::string>{test.request});
- wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 2000);
+ wait_for([adbd_server](){return !adbd_server->m_responses.empty();}, 5000);
EXPECT_EQ(test.expected_pk, pk);
ASSERT_EQ(1, adbd_server->m_responses.size());
EXPECT_EQ(test.expected_response, adbd_server->m_responses.front());
// cleanup
+ connection.disconnect();
adbd_client.reset();
adbd_server.reset();
g_unlink(socket_path.c_str());
diff --git a/tests/unit/greeter-test.cpp b/tests/unit/greeter-test.cpp
new file mode 100644
index 0000000..bfa88e8
--- /dev/null
+++ b/tests/unit/greeter-test.cpp
@@ -0,0 +1,159 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Charles Kerr <charles.kerr@canonical.com>
+ */
+
+#include <tests/utils/qt-fixture.h>
+#include <tests/utils/gtest-print-helpers.h>
+
+#include <src/dbus-names.h>
+#include <src/greeter.h>
+
+#include <libqtdbustest/DBusTestRunner.h>
+#include <libqtdbustest/QProcessDBusService.h>
+#include <libqtdbusmock/DBusMock.h>
+
+class GreeterFixture: public QtFixture
+{
+private:
+
+ using super = QtFixture;
+
+public:
+
+ GreeterFixture() =default;
+ ~GreeterFixture() =default;
+
+protected:
+
+ std::shared_ptr<QtDBusTest::DBusTestRunner> m_dbus_runner;
+ std::shared_ptr<QtDBusMock::DBusMock> m_dbus_mock;
+ GDBusConnection* m_bus {};
+
+ void SetUp() override
+ {
+ super::SetUp();
+
+ // use a fresh bus for each test run
+ m_dbus_runner.reset(new QtDBusTest::DBusTestRunner());
+ m_dbus_mock.reset(new QtDBusMock::DBusMock(*m_dbus_runner.get()));
+
+ GError* error {};
+ m_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, nullptr, &error);
+ g_assert_no_error(error);
+ g_dbus_connection_set_exit_on_close(m_bus, FALSE);
+ }
+
+ void TearDown() override
+ {
+ g_clear_object(&m_bus);
+
+ super::TearDown();
+ }
+
+ void start_greeter_service(bool is_active)
+ {
+ // set a watcher to look for our mock greeter to appear
+ bool owned {};
+ QDBusServiceWatcher watcher(
+ DBusNames::UnityGreeter::NAME,
+ m_dbus_runner->sessionConnection()
+ );
+ QObject::connect(
+ &watcher,
+ &QDBusServiceWatcher::serviceRegistered,
+ [&owned](const QString&){owned = true;}
+ );
+
+ // start the mock greeter
+ QVariantMap parameters;
+ parameters["IsActive"] = QVariant(is_active);
+ m_dbus_mock->registerTemplate(
+ DBusNames::UnityGreeter::NAME,
+ GREETER_TEMPLATE,
+ parameters,
+ QDBusConnection::SessionBus
+ );
+ m_dbus_runner->startServices();
+
+ // wait for the watcher
+ ASSERT_TRUE(wait_for([&owned]{return owned;}));
+ }
+};
+
+#define ASSERT_PROPERTY_EQ_EVENTUALLY(expected_in, property_in) \
+ do { \
+ const auto& e = expected_in; \
+ const auto& p = property_in; \
+ ASSERT_TRUE(wait_for([e, &p](){return e == p.get();})) \
+ << "expected " << e << " but got " << p.get(); \
+ } while(0)
+
+/**
+ * Test startup timing by looking at four different cases:
+ * [unity greeter shows up on bus (before, after) we start listening]
+ * x [unity greeter is (active, inactive)]
+ */
+
+TEST_F(GreeterFixture, ActiveServiceStartsBeforeWatcher)
+{
+ constexpr bool is_active {true};
+ constexpr Greeter::State expected {Greeter::State::ACTIVE};
+
+ start_greeter_service(is_active);
+
+ UnityGreeter greeter;
+
+ ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
+}
+
+TEST_F(GreeterFixture, WatcherStartsBeforeActiveService)
+{
+ constexpr bool is_active {true};
+ constexpr Greeter::State expected {Greeter::State::ACTIVE};
+
+ UnityGreeter greeter;
+
+ start_greeter_service(is_active);
+
+ ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
+}
+
+TEST_F(GreeterFixture, InactiveServiceStartsBeforeWatcher)
+{
+ constexpr bool is_active {false};
+ constexpr Greeter::State expected {Greeter::State::INACTIVE};
+
+ start_greeter_service(is_active);
+
+ UnityGreeter greeter;
+
+ ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
+}
+
+TEST_F(GreeterFixture, WatcherStartsBeforeInactiveService)
+{
+ constexpr bool is_active {false};
+ constexpr Greeter::State expected {Greeter::State::INACTIVE};
+
+ UnityGreeter greeter;
+
+ start_greeter_service(is_active);
+
+ ASSERT_PROPERTY_EQ_EVENTUALLY(expected, greeter.state());
+}
+
diff --git a/tests/unit/usb-snap-test.cpp b/tests/unit/usb-snap-test.cpp
index 3b778dd..2be4b27 100644
--- a/tests/unit/usb-snap-test.cpp
+++ b/tests/unit/usb-snap-test.cpp
@@ -88,7 +88,7 @@ TEST_F(UsbSnapFixture, TestRoundTrip)
auto snap = std::make_shared<UsbSnap>(test.fingerprint);
AdbdClient::PKResponse user_response {};
bool user_response_set = false;
- snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){
+ auto connection = snap->on_user_response().connect([&user_response,&user_response_set](AdbdClient::PKResponse response, bool /*remember*/){
user_response = response;
user_response_set = true;
});
@@ -130,14 +130,9 @@ TEST_F(UsbSnapFixture, TestRoundTrip)
EXPECT_TRUE(user_response_set);
ASSERT_EQ(test.expected_response, user_response);
- // confirm that the snap dtor cleans up the notification
+ // confirm that the snap dtor doesn't try to close
+ // the notification that's already been closed by user choice
snap.reset();
- wait_for_signals(notificationsSpy, 1);
- {
- QVariantList const& call(notificationsSpy.at(0));
- EXPECT_EQ("CloseNotification", call.at(0));
- QVariantList const& args(call.at(1).toList());
- EXPECT_EQ(id, args.at(0));
- }
+ EXPECT_FALSE(notificationsSpy.wait(1000));
}
}