diff options
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/CMakeLists.txt | 5 | ||||
-rw-r--r-- | tests/unit/greeter-test.cpp | 159 | ||||
-rw-r--r-- | tests/unit/usb-snap-test.cpp | 12 |
3 files changed, 167 insertions, 9 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/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 53548f4..2be4b27 100644 --- a/tests/unit/usb-snap-test.cpp +++ b/tests/unit/usb-snap-test.cpp @@ -130,15 +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 - connection.disconnect(); + // 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)); } } |