From 4eadc9daefd65af2e9577b7a7ef012e29bf87a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Wed, 17 Sep 2014 10:29:03 +0200 Subject: [PATCH] [auto-tests] Add an initial test for Screens This is a very interesting auto test as Screens uses both Workspace and Client. Thus it operates in the "impossible to mock" area. The solution is to provide mock includes in autotests and ensure that when building the auto-test the mock header includes will be picked first. There is now a mock class for Workspace and Client providing just the API pieces used inside Screens. As Screens is abstract and we cannot properly interact with QDesktopWidget there is also a MockScreens class inheriting from Screens and mocking the required functionality (by just operating on a list of QRects). The auto-test itself is only performing checks on the abstract class. The mock class is indirectly tested by Screens calling into the virtual methods. The test case is not yet complete, but looking quite good already. --- autotests/CMakeLists.txt | 26 ++++ autotests/client.h | 1 + autotests/mock_client.cpp | 60 +++++++++ autotests/mock_client.h | 49 ++++++++ autotests/mock_screens.cpp | 86 +++++++++++++ autotests/mock_screens.h | 51 ++++++++ autotests/mock_workspace.cpp | 51 ++++++++ autotests/mock_workspace.h | 58 +++++++++ autotests/test_screens.cpp | 231 +++++++++++++++++++++++++++++++++++ autotests/workspace.h | 1 + screens.cpp | 11 +- 11 files changed, 623 insertions(+), 2 deletions(-) create mode 100644 autotests/client.h create mode 100644 autotests/mock_client.cpp create mode 100644 autotests/mock_client.h create mode 100644 autotests/mock_screens.cpp create mode 100644 autotests/mock_screens.h create mode 100644 autotests/mock_workspace.cpp create mode 100644 autotests/mock_workspace.h create mode 100644 autotests/test_screens.cpp create mode 100644 autotests/workspace.h diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 74fbb7cc2..52f5fb863 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -184,3 +184,29 @@ target_link_libraries(fakeeffectplugin kwineffects) add_library(effectversionplugin MODULE fakeeffectplugin_version.cpp) set_target_properties(effectversionplugin PROPERTIES PREFIX "") target_link_libraries(effectversionplugin kwineffects) + +######################################################## +# Test Screens +######################################################## +set( testScreens_SRCS + test_screens.cpp + mock_client.cpp + mock_screens.cpp + mock_workspace.cpp + ../screens.cpp +) +kconfig_add_kcfg_files(testScreens_SRCS ../settings.kcfgc) + +add_executable( testScreens ${testScreens_SRCS}) +target_include_directories(testScreens BEFORE PRIVATE ./) +target_link_libraries(testScreens + Qt5::Test + Qt5::X11Extras + KF5::ConfigCore + KF5::ConfigGui + KF5::WindowSystem + KF5::Service +) + +add_test(kwin_testScreens testScreens) +ecm_mark_as_test(testScreens) diff --git a/autotests/client.h b/autotests/client.h new file mode 100644 index 000000000..b821e1ba1 --- /dev/null +++ b/autotests/client.h @@ -0,0 +1 @@ +#include "mock_client.h" diff --git a/autotests/mock_client.cpp b/autotests/mock_client.cpp new file mode 100644 index 000000000..ce9546ebe --- /dev/null +++ b/autotests/mock_client.cpp @@ -0,0 +1,60 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#include "mock_client.h" + +namespace KWin +{ + +Client::Client(QObject *parent) + : QObject(parent) + , m_active(false) + , m_screen(0) +{ +} + +Client::~Client() = default; + +bool Client::isActive() const +{ + return m_active; +} + +void Client::setActive(bool active) +{ + m_active = active; +} + +void Client::setScreen(int screen) +{ + m_screen = screen; +} + +bool Client::isOnScreen(int screen) const +{ + // TODO: mock checking client geometry + return screen == m_screen; +} + +int Client::screen() const +{ + return m_screen; +} + +} diff --git a/autotests/mock_client.h b/autotests/mock_client.h new file mode 100644 index 000000000..513d141c4 --- /dev/null +++ b/autotests/mock_client.h @@ -0,0 +1,49 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#ifndef KWIN_MOCK_CLIENT_H +#define KWIN_MOCK_CLIENT_H + +#include + +namespace KWin +{ + +class Client : public QObject +{ + Q_OBJECT +public: + explicit Client(QObject *parent); + virtual ~Client(); + + int screen() const; + bool isOnScreen(int screen) const; + bool isActive() const; + + void setActive(bool active); + void setScreen(int screen); + +private: + bool m_active; + int m_screen; +}; + +} + +#endif diff --git a/autotests/mock_screens.cpp b/autotests/mock_screens.cpp new file mode 100644 index 000000000..84fce2833 --- /dev/null +++ b/autotests/mock_screens.cpp @@ -0,0 +1,86 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#include "mock_screens.h" + +namespace KWin +{ + +MockScreens::MockScreens(QObject *parent) + : Screens(parent) +{ +} + +MockScreens::~MockScreens() = default; + +QRect MockScreens::geometry(int screen) const +{ + if (screen >= m_geometries.count()) { + return QRect(); + } + return m_geometries.at(screen); +} + +QSize MockScreens::size(int screen) const +{ + return geometry(screen).size(); +} + +int MockScreens::number(const QPoint &pos) const +{ + int bestScreen = 0; + int minDistance = INT_MAX; + for (int i = 0; i < m_geometries.size(); ++i) { + const QRect &geo = m_geometries.at(i); + if (geo.contains(pos)) { + return i; + } + int distance = QPoint(geo.topLeft() - pos).manhattanLength(); + distance = qMin(distance, QPoint(geo.topRight() - pos).manhattanLength()); + distance = qMin(distance, QPoint(geo.bottomRight() - pos).manhattanLength()); + distance = qMin(distance, QPoint(geo.bottomLeft() - pos).manhattanLength()); + if (distance < minDistance) { + minDistance = distance; + bestScreen = i; + } + } + return bestScreen; +} + +void MockScreens::init() +{ + Screens::init(); + m_scheduledGeometries << QRect(0, 0, 100, 100); + updateCount(); +} + +void MockScreens::updateCount() +{ + m_geometries = m_scheduledGeometries; + setCount(m_geometries.size()); + emit changed(); +} + +void MockScreens::setGeometries(const QList< QRect > &geometries) +{ + m_scheduledGeometries = geometries; + startChangedTimer(); +} + +} diff --git a/autotests/mock_screens.h b/autotests/mock_screens.h new file mode 100644 index 000000000..3bc6c3828 --- /dev/null +++ b/autotests/mock_screens.h @@ -0,0 +1,51 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#ifndef KWIN_MOCK_SCREENS_H +#define KWIN_MOCK_SCREENS_H + +#include "../screens.h" + +namespace KWin +{ + +class MockScreens : public Screens +{ + Q_OBJECT +public: + explicit MockScreens(QObject *parent = nullptr); + virtual ~MockScreens(); + QRect geometry(int screen) const override; + int number(const QPoint &pos) const override; + QSize size(int screen) const override; + void init() override; + + void setGeometries(const QList &geometries); + +protected Q_SLOTS: + void updateCount() override; + +private: + QList m_scheduledGeometries; + QList m_geometries; +}; + +} + +#endif diff --git a/autotests/mock_workspace.cpp b/autotests/mock_workspace.cpp new file mode 100644 index 000000000..5e3941ae7 --- /dev/null +++ b/autotests/mock_workspace.cpp @@ -0,0 +1,51 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#include "mock_workspace.h" +#include "mock_client.h" + +namespace KWin +{ + +Workspace *MockWorkspace::s_self = nullptr; + +MockWorkspace::MockWorkspace(QObject *parent) + : QObject(parent) + , m_activeClient(nullptr) +{ + s_self = this; +} + +MockWorkspace::~MockWorkspace() +{ + s_self = nullptr; +} + +Client *MockWorkspace::activeClient() const +{ + return m_activeClient; +} + +void MockWorkspace::setActiveClient(Client *c) +{ + m_activeClient = c; +} + +} + diff --git a/autotests/mock_workspace.h b/autotests/mock_workspace.h new file mode 100644 index 000000000..402d83e6f --- /dev/null +++ b/autotests/mock_workspace.h @@ -0,0 +1,58 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#ifndef KWIN_MOCK_WORKSPACE_H +#define KWIN_MOCK_WORKSPACE_H + +#include + +namespace KWin +{ + +class Client; + +class MockWorkspace; +typedef MockWorkspace Workspace; + +class MockWorkspace : public QObject +{ + Q_OBJECT +public: + explicit MockWorkspace(QObject *parent = nullptr); + virtual ~MockWorkspace(); + Client *activeClient() const; + + void setActiveClient(Client *c); + + static Workspace *self(); + +private: + Client *m_activeClient; + static Workspace *s_self; +}; + +inline +Workspace *MockWorkspace::self() +{ + return s_self; +} + +} + +#endif diff --git a/autotests/test_screens.cpp b/autotests/test_screens.cpp new file mode 100644 index 000000000..cfdf428df --- /dev/null +++ b/autotests/test_screens.cpp @@ -0,0 +1,231 @@ +/******************************************************************** +KWin - the KDE window manager +This file is part of the KDE project. + +Copyright (C) 2014 Martin Gräßlin + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +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 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 . +*********************************************************************/ +#include "mock_workspace.h" +#include "../cursor.h" +#include "mock_screens.h" +#include "mock_client.h" +// Qt +#include + +// Mock +namespace KWin +{ + +QPoint Cursor::pos() +{ + return QPoint(); +} + +} + +class TestScreens : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void testCurrentFollowsMouse(); + void testSize_data(); + void testSize(); + void testCount(); + void testIntersecting_data(); + void testIntersecting(); + void testCurrent_data(); + void testCurrent(); + void testCurrentClient(); +}; + +void TestScreens::testCurrentFollowsMouse() +{ + KWin::MockWorkspace ws; + KWin::Screens *screens = KWin::Screens::create(&ws); + QVERIFY(!screens->isCurrentFollowsMouse()); + screens->setCurrentFollowsMouse(true); + QVERIFY(screens->isCurrentFollowsMouse()); + screens->setCurrentFollowsMouse(false); + QVERIFY(!screens->isCurrentFollowsMouse()); +} + +void TestScreens::testSize_data() +{ + QTest::addColumn< QList >("geometries"); + QTest::addColumn("expectedSize"); + + QTest::newRow("empty") << QList{{QRect()}} << QSize(0, 0); + QTest::newRow("cloned") << QList{{QRect{0, 0, 200, 100}, QRect{0, 0, 200, 100}}} << QSize(200, 100); + QTest::newRow("adjacent") << QList{{QRect{0, 0, 200, 100}, QRect{200, 100, 400, 300}}} << QSize(600, 400); + QTest::newRow("overlapping") << QList{{QRect{-10, -20, 50, 100}, QRect{0, 0, 100, 200}}} << QSize(110, 220); + QTest::newRow("gap") << QList{{QRect{0, 0, 10, 20}, QRect{20, 40, 10, 20}}} << QSize(30, 60); +} + +void TestScreens::testSize() +{ + using namespace KWin; + MockWorkspace ws; + MockScreens *mockScreens = static_cast(Screens::create(&ws)); + QSignalSpy sizeChangedSpy(screens(), SIGNAL(sizeChanged())); + QVERIFY(sizeChangedSpy.isValid()); + + QCOMPARE(screens()->size(), QSize(100, 100)); + QFETCH(QList, geometries); + QVERIFY(!screens()->isChanging()); + mockScreens->setGeometries(geometries); + QVERIFY(screens()->isChanging()); + + QVERIFY(sizeChangedSpy.wait()); + QVERIFY(!screens()->isChanging()); + QTEST(screens()->size(), "expectedSize"); +} + +void TestScreens::testCount() +{ + using namespace KWin; + MockWorkspace ws; + MockScreens *mockScreens = static_cast(Screens::create(&ws)); + QSignalSpy countChangedSpy(screens(), SIGNAL(countChanged(int,int))); + QVERIFY(countChangedSpy.isValid()); + + QCOMPARE(screens()->count(), 1); + + // change to two screens + QList geometries{{QRect{0, 0, 100, 200}, QRect{100, 0, 100, 200}}}; + mockScreens->setGeometries(geometries); + + QVERIFY(countChangedSpy.wait()); + QCOMPARE(countChangedSpy.count(), 1); + QCOMPARE(countChangedSpy.first().first().toInt(), 1); + QCOMPARE(countChangedSpy.first().last().toInt(), 2); + QCOMPARE(screens()->count(), 2); + + // go back to one screen + geometries.takeLast(); + mockScreens->setGeometries(geometries); + QVERIFY(countChangedSpy.wait()); + QCOMPARE(countChangedSpy.count(), 2); + QCOMPARE(countChangedSpy.last().first().toInt(), 2); + QCOMPARE(countChangedSpy.last().last().toInt(), 1); + QCOMPARE(screens()->count(), 1); + + // setting the same geometries shouldn't emit the signal, but we should get a changed signal + QSignalSpy changedSpy(screens(), SIGNAL(changed())); + QVERIFY(changedSpy.isValid()); + mockScreens->setGeometries(geometries); + QVERIFY(changedSpy.wait()); + QCOMPARE(countChangedSpy.count(), 2); +} + +void TestScreens::testIntersecting_data() +{ + QTest::addColumn>("geometries"); + QTest::addColumn("testGeometry"); + QTest::addColumn("expectedCount"); + + QTest::newRow("null-rect") << QList{{QRect{0, 0, 100, 100}}} << QRect() << 0; + QTest::newRow("non-overlapping") << QList{{QRect{0, 0, 100, 100}}} << QRect(100, 0, 100, 100) << 0; + QTest::newRow("in-between") << QList{{QRect{0, 0, 10, 20}, QRect{20, 40, 10, 20}}} << QRect(15, 0, 2, 2) << 0; + QTest::newRow("gap-overlapping") << QList{{QRect{0, 0, 10, 20}, QRect{20, 40, 10, 20}}} << QRect(9, 10, 200, 200) << 2; + QTest::newRow("larger") << QList{{QRect{0, 0, 100, 100}}} << QRect(-10, -10, 200, 200) << 1; + QTest::newRow("several") << QList{{QRect{0, 0, 100, 100}, QRect{100, 0, 100, 100}, QRect{200, 100, 100, 100}, QRect{300, 100, 100, 100}}} << QRect(0, 0, 300, 300) << 3; +} + +void TestScreens::testIntersecting() +{ + using namespace KWin; + MockWorkspace ws; + MockScreens *mockScreens = static_cast(Screens::create(&ws)); + QSignalSpy changedSpy(screens(), SIGNAL(changed())); + QVERIFY(changedSpy.isValid()); + QFETCH(QList, geometries); + mockScreens->setGeometries(geometries); + // first is before it's updated + QVERIFY(changedSpy.wait()); + // second is after it's updated + QVERIFY(changedSpy.wait()); + + QFETCH(QRect, testGeometry); + QCOMPARE(screens()->count(), geometries.count()); + QTEST(screens()->intersecting(testGeometry), "expectedCount"); +} + +void TestScreens::testCurrent_data() +{ + QTest::addColumn("current"); + QTest::addColumn("signal"); + + QTest::newRow("unchanged") << 0 << false; + QTest::newRow("changed") << 1 << true; +} + +void TestScreens::testCurrent() +{ + using namespace KWin; + MockWorkspace ws; + Screens::create(&ws); + QSignalSpy currentChangedSpy(screens(), SIGNAL(currentChanged())); + QVERIFY(currentChangedSpy.isValid()); + + QFETCH(int, current); + screens()->setCurrent(current); + QCOMPARE(screens()->current(), current); + QTEST(!currentChangedSpy.isEmpty(), "signal"); +} + +void TestScreens::testCurrentClient() +{ + using namespace KWin; + MockWorkspace ws; + MockScreens *mockScreens = static_cast(Screens::create(&ws)); + QSignalSpy changedSpy(screens(), SIGNAL(changed())); + QVERIFY(changedSpy.isValid()); + mockScreens->setGeometries(QList{{QRect{0, 0, 100, 100}, QRect{100, 0, 100, 100}}}); + // first is before it's updated + QVERIFY(changedSpy.wait()); + // second is after it's updated + QVERIFY(changedSpy.wait()); + + QSignalSpy currentChangedSpy(screens(), SIGNAL(currentChanged())); + QVERIFY(currentChangedSpy.isValid()); + + // create a mock client + Client *client = new Client(&ws); + client->setScreen(1); + + // it's not the active client, so changing won't work + screens()->setCurrent(client); + QVERIFY(currentChangedSpy.isEmpty()); + QCOMPARE(screens()->current(), 0); + + // making the client active should affect things + client->setActive(true); + ws.setActiveClient(client); + + // first of all current should be changed just by the fact that there is an active client + QCOMPARE(screens()->current(), 1); + // but also calling setCurrent should emit the changed signal + screens()->setCurrent(client); + QCOMPARE(currentChangedSpy.count(), 1); + QCOMPARE(screens()->current(), 1); + + // and it should even still be on screen 1 if we make the client non-current again + ws.setActiveClient(nullptr); + client->setActive(false); + QCOMPARE(screens()->current(), 1); +} + +QTEST_MAIN(TestScreens) +#include "test_screens.moc" diff --git a/autotests/workspace.h b/autotests/workspace.h new file mode 100644 index 000000000..0ab1a6f16 --- /dev/null +++ b/autotests/workspace.h @@ -0,0 +1 @@ +#include "mock_workspace.h" diff --git a/screens.cpp b/screens.cpp index ee76051fa..5b9cd34ae 100644 --- a/screens.cpp +++ b/screens.cpp @@ -18,14 +18,17 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ #include "screens.h" -#include "client.h" +#include #include "cursor.h" #include "settings.h" -#include "workspace.h" +#include #include #if HAVE_WAYLAND #include "screens_wayland.h" #endif +#ifdef KWIN_UNIT_TEST +#include +#endif #include #include @@ -38,6 +41,9 @@ Screens *Screens::s_self = nullptr; Screens *Screens::create(QObject *parent) { Q_ASSERT(!s_self); +#ifdef KWIN_UNIT_TEST + s_self = new MockScreens(parent); +#else #if HAVE_WAYLAND if (kwinApp()->operationMode() == Application::OperationModeWaylandAndX11) { s_self = new WaylandScreens(parent); @@ -46,6 +52,7 @@ Screens *Screens::create(QObject *parent) if (kwinApp()->operationMode() == Application::OperationModeX11) { s_self = new DesktopWidgetScreens(parent); } +#endif s_self->init(); return s_self; }