on xenial help sign in
Better window management mocks
commitd70f5f531956434fdf2e3da15ee0338d08ff40cf authorNick Dedekind <nick.dedekind@canonical.com> dateWed 1 Mar 2017, 18:34 (10 years ago) parent8b8a6fa
11 files changed+203−302
plugins/WindowManager/Screens.cpp+2−4 plugins/WindowManager/Screens.h+2−2 plugins/WindowManager/WindowManagerPlugin.cpp+2−1 tests/mocks/WindowManager/CMakeLists.txt+4−2 tests/mocks/WindowManager/MockScreens.cpp+140 tests/mocks/WindowManager/MockScreens.h+46 tests/mocks/WindowManager/Screen.cpp−65 tests/mocks/WindowManager/Screen.h−56 tests/mocks/WindowManager/Screens.cpp−101 tests/mocks/WindowManager/Screens.h−68 tests/mocks/WindowManager/WindowManagerPlugin.cpp+7−3
plugins/WindowManager/Screens.cpp+2−4
@@ -19,7 +19,6 @@
1919#include "WorkspaceManager.h"
2020
2121// qtmirserver
22−#include <qtmir/qtmir.h>
2322#include <qtmir/screens.h>
2423#include <QGuiApplication>
2524
@@ -27,9 +26,8 @@
2726#include <QScreen>
2827#include <QWindow>
2928
30−Screens::Screens(QObject *parent)
31− : QAbstractListModel(parent)
32− , m_wrapped(qtmir::get_screen_model())
29+Screens::Screens(const QSharedPointer<qtmir::Screens>& model)
30+ : m_wrapped(model)
3331{
3432 if (qGuiApp->platformName() != QLatin1String("mirserver")) {
3533 qCritical("Not using 'mirserver' QPA plugin. Using qGuiApp may produce unknown results.");
plugins/WindowManager/Screens.h+2−2
@@ -21,13 +21,13 @@
2121#include <QSharedPointer>
2222#include <QPointer>
2323
24−class Screen;
2524namespace qtmir
2625{
2726class Screen;
2827class Screens;
2928}
3029
30+class Screen;
3131class ScreensProxy;
3232
3333class Screens : public QAbstractListModel
@@ -41,7 +41,7 @@ public:
4141 ScreenRole = Qt::UserRole + 1
4242 };
4343
44− explicit Screens(QObject *parent = 0);
44+ explicit Screens(const QSharedPointer<qtmir::Screens>& model);
4545 ~Screens();
4646
4747 Q_INVOKABLE ScreensProxy *createProxy();
plugins/WindowManager/WindowManagerPlugin.cpp+2−1
@@ -26,6 +26,7 @@
2626#include "WorkspaceModel.h"
2727
2828#include <QtQml>
29+#include <qtmir/qtmir.h>
2930
3031static QObject *workspace_manager(QQmlEngine *engine, QJSEngine *scriptEngine)
3132{
@@ -36,7 +37,7 @@ static QObject *workspace_manager(QQmlEngine *engine, QJSEngine *scriptEngine)
3637QObject* screensSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
3738 Q_UNUSED(engine);
3839 Q_UNUSED(scriptEngine);
39− return new Screens();
40+ return new Screens(qtmir::get_screen_model());
4041}
4142
4243void WindowManagerPlugin::registerTypes(const char *uri)
tests/mocks/WindowManager/CMakeLists.txt+4−2
@@ -5,17 +5,19 @@ include_directories(
55
66include_directories(
77 ${CMAKE_CURRENT_SOURCE_DIR}
8+ ${CMAKE_CURRENT_BINARY_DIR}
89 ${CMAKE_SOURCE_DIR}/plugins/WindowManager
910)
1011
1112set(WINDOWMANAGER_SRC
13+ ${CMAKE_SOURCE_DIR}/plugins/WindowManager/Screen.cpp
14+ ${CMAKE_SOURCE_DIR}/plugins/WindowManager/Screens.cpp
1215 ${CMAKE_SOURCE_DIR}/plugins/WindowManager/TopLevelWindowModel.cpp
1316 ${CMAKE_SOURCE_DIR}/plugins/WindowManager/Window.cpp
1417 ${CMAKE_SOURCE_DIR}/plugins/WindowManager/WorkspaceManager.cpp
1518 ${CMAKE_SOURCE_DIR}/plugins/WindowManager/WorkspaceModel.cpp
1619 ${CMAKE_SOURCE_DIR}/plugins/WindowManager/Workspace.cpp
17− Screens.cpp
18− Screen.cpp
20+ MockScreens.cpp
1921 ScreenWindow.cpp
2022 WindowManagerPlugin.cpp
2123 windowmanagementpolicy.h
tests/mocks/WindowManager/MockScreens.cppnew file+140
@@ -0,0 +1,140 @@
1+/*
2+ * Copyright (C) 2016-2017 Canonical, Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it under
5+ * the terms of the GNU Lesser General Public License version 3, as published by
6+ * the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but WITHOUT
9+ * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10+ * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+ * Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "MockScreens.h"
18+
19+// qtmirserver
20+#include <qtmir/screen.h>
21+
22+namespace {
23+
24+class MockScreen : public qtmir::Screen
25+{
26+ Q_OBJECT
27+public:
28+ MockScreen()
29+ {
30+ m_sizes.append(new qtmir::ScreenMode(50, QSize(640,480)));
31+ m_sizes.append(new qtmir::ScreenMode(60, QSize(1280,1024)));
32+ m_sizes.append(new qtmir::ScreenMode(60, QSize(1440,900)));
33+ m_sizes.append(new qtmir::ScreenMode(60, QSize(1920,1080)));
34+ m_physicalSize = QSize(800,568);
35+ }
36+
37+ qtmir::OutputId outputId() const override { return m_id; }
38+ bool used() const override { return m_used; }
39+ QString name() const override { return m_name; }
40+ float scale() const override { return m_scale; }
41+ QSizeF physicalSize() const override { return m_physicalSize; }
42+ qtmir::FormFactor formFactor() const override { return m_formFactor; }
43+ qtmir::OutputTypes outputType() const override { return m_outputType; }
44+ MirPowerMode powerMode() const override { return m_powerMode; }
45+ Qt::ScreenOrientation orientation() const override { return m_orientation; }
46+ QPoint position() const override { return m_position; }
47+ uint currentModeIndex() const override { return m_currentModeIndex; }
48+ bool isActive() const override { return m_active; }
49+
50+ QQmlListProperty<qtmir::ScreenMode> availableModes() override {
51+ return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
52+ }
53+
54+ void setActive(bool active) override {
55+ if (m_active != active) {
56+ m_active = active;
57+ Q_EMIT activeChanged(m_active);
58+ }
59+ }
60+
61+ QScreen* qscreen() const override { return nullptr; }
62+
63+ qtmir::ScreenConfiguration *beginConfiguration() const override {
64+ auto config = new qtmir::ScreenConfiguration;
65+ config->valid = true;
66+ config->id = m_id;
67+ config->used = m_used;
68+ config->topLeft = m_position;
69+ config->currentModeIndex = m_currentModeIndex;
70+ config->powerMode = m_powerMode;
71+ config->scale = m_scale;
72+ config->formFactor = m_formFactor;
73+ return config;
74+ }
75+
76+ bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
77+ m_used = configuration->used;
78+ m_position = configuration->topLeft;
79+ m_currentModeIndex = configuration->currentModeIndex;
80+ m_powerMode = configuration->powerMode;
81+ m_scale = configuration->scale;
82+ m_formFactor = configuration->formFactor;
83+ return true;
84+ }
85+
86+public:
87+ qtmir::OutputId m_id{0};
88+ bool m_active{false};
89+ bool m_used{true};
90+ QString m_name;
91+ qtmir::OutputTypes m_outputType{qtmir::Unknown};
92+ MirPowerMode m_powerMode{mir_power_mode_on};
93+ Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
94+ float m_scale{1.0};
95+ qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
96+ QPoint m_position;
97+ uint m_currentModeIndex{0};
98+ QList<qtmir::ScreenMode*> m_sizes;
99+ QSizeF m_physicalSize;
100+};
101+}
102+
103+MockScreens::MockScreens()
104+{
105+ bool ok = false;
106+ int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
107+ if (!ok) screenCount = 1;
108+ QPoint lastPoint(0,0);
109+ for (int i = 0; i < screenCount; ++i) {
110+ auto screen = new MockScreen();
111+ screen->m_id = qtmir::OutputId{i};
112+ screen->m_active = i == 0;
113+ screen->m_name = QString("Monitor %1").arg(i);
114+ screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
115+ screen->m_currentModeIndex = 3;
116+ m_mocks.append(screen);
117+
118+ lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
119+ }
120+}
121+
122+MockScreens::~MockScreens()
123+{
124+ qDeleteAll(m_mocks);
125+}
126+
127+QVector<qtmir::Screen *> MockScreens::screens() const
128+{
129+ return m_mocks;
130+}
131+
132+qtmir::Screen *MockScreens::activeScreen() const
133+{
134+ Q_FOREACH(auto screen, m_mocks) {
135+ if (screen->isActive()) return screen;
136+ }
137+ return nullptr;
138+}
139+
140+#include "MockScreens.moc"
tests/mocks/WindowManager/MockScreens.hnew file+46
@@ -0,0 +1,46 @@
1+/*
2+ * Copyright (C) 2016 Canonical, Ltd.
3+ *
4+ * This program is free software: you can redistribute it and/or modify it under
5+ * the terms of the GNU Lesser General Public License version 3, as published by
6+ * the Free Software Foundation.
7+ *
8+ * This program is distributed in the hope that it will be useful, but WITHOUT
9+ * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10+ * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+ * Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef MOCK_SCREENS_H
18+#define MOCK_SCREENS_H
19+
20+#include <QAbstractListModel>
21+#include <QSharedPointer>
22+
23+#include <qtmir/screens.h>
24+
25+class Screen;
26+namespace qtmir
27+{
28+class Screens;
29+}
30+
31+class MockScreens : public qtmir::Screens
32+{
33+ Q_OBJECT
34+public:
35+ MockScreens();
36+ ~MockScreens();
37+
38+ QVector<qtmir::Screen*> screens() const override;
39+
40+ qtmir::Screen *activeScreen() const override;
41+
42+private:
43+ QVector<qtmir::Screen*> m_mocks;
44+};
45+
46+#endif // MOCK_SCREENS_H
tests/mocks/WindowManager/Screen.cppdeleted−65
@@ -1,65 +0,0 @@
1−/*
2− * Copyright (C) 2017 Canonical, Ltd.
3− *
4− * This program is free software: you can redistribute it and/or modify it under
5− * the terms of the GNU Lesser General Public License version 3, as published by
6− * the Free Software Foundation.
7− *
8− * This program is distributed in the hope that it will be useful, but WITHOUT
9− * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10− * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11− * Lesser General Public License for more details.
12− *
13− * You should have received a copy of the GNU Lesser General Public License
14− * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− */
16−
17−#include "Screen.h"
18−#include "WorkspaceModel.h"
19−#include "WorkspaceManager.h"
20−#include "Workspace.h"
21−
22−Screen::Screen()
23− : m_workspaces(new WorkspaceModel)
24−{
25− WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
26− WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
27−}
28−
29−QQmlListProperty<qtmir::ScreenMode> Screen::availableModes()
30−{
31− return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
32−}
33−
34−qtmir::ScreenConfiguration *Screen::beginConfiguration() const
35−{
36− auto config = new qtmir::ScreenConfiguration;
37− config->valid = true;
38− config->id = m_id;
39− config->used = m_used;
40− config->topLeft = m_position;
41− config->currentModeIndex = m_currentModeIndex;
42− config->powerMode = m_powerMode;
43− config->scale = m_scale;
44− config->formFactor = m_formFactor;
45− return config;
46−}
47−
48−bool Screen::applyConfiguration(qtmir::ScreenConfiguration *configuration)
49−{
50− m_used = configuration->used;
51− m_position = configuration->topLeft;
52− m_currentModeIndex = configuration->currentModeIndex;
53− m_powerMode = configuration->powerMode;
54− m_scale = configuration->scale;
55− m_formFactor = configuration->formFactor;
56−
57−// m_orientation = configuration->orientation;
58− return true;
59−}
60−
61−void Screen::setActive(bool active)
62−{
63− m_active = active;
64− Q_EMIT activeChanged(active);
65−}
tests/mocks/WindowManager/Screen.hdeleted−56
@@ -1,56 +0,0 @@
1−#ifndef MOCK_SCREEN_H
2−#define MOCK_SCREEN_H
3−
4−#include <qtmir/screen.h>
5−#include <QScopedPointer>
6−
7−class WorkspaceModel;
8−
9−class Screen : public qtmir::Screen
10−{
11− Q_OBJECT
12− Q_PROPERTY(WorkspaceModel* workspaces READ workspaces CONSTANT)
13−
14−public:
15− Screen();
16−
17− qtmir::OutputId outputId() const override { return m_id; }
18− bool used() const override { return m_used; }
19− QString name() const override { return m_name; }
20− float scale() const override { return m_scale; }
21− QSizeF physicalSize() const override { return m_physicalSize; }
22− qtmir::FormFactor formFactor() const override { return m_formFactor; }
23− qtmir::OutputTypes outputType() const override { return m_outputType; }
24− MirPowerMode powerMode() const override { return m_powerMode; }
25− Qt::ScreenOrientation orientation() const override { return m_orientation; }
26− QPoint position() const override { return m_position; }
27− QQmlListProperty<qtmir::ScreenMode> availableModes() override;
28− uint currentModeIndex() const override { return m_currentModeIndex; }
29− bool isActive() const override { return m_active; }
30− void setActive(bool active) override;
31−
32− QScreen* qscreen() const override { return nullptr; }
33−
34− qtmir::ScreenConfiguration *beginConfiguration() const override;
35− bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override;
36−
37− WorkspaceModel* workspaces() const { return m_workspaces.data(); }
38−
39−public:
40− qtmir::OutputId m_id{0};
41− bool m_active{false};
42− bool m_used{true};
43− QString m_name;
44− qtmir::OutputTypes m_outputType{qtmir::Unknown};
45− MirPowerMode m_powerMode{mir_power_mode_on};
46− Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
47− float m_scale{1.0};
48− qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
49− QPoint m_position;
50− uint m_currentModeIndex{0};
51− QList<qtmir::ScreenMode*> m_sizes;
52− QSizeF m_physicalSize;
53− const QScopedPointer<WorkspaceModel> m_workspaces;
54−};
55−
56−#endif // MOCK_SCREEN_H
tests/mocks/WindowManager/Screens.cppdeleted−101
@@ -1,101 +0,0 @@
1−/*
2− * Copyright (C) 2016-2017 Canonical, Ltd.
3− *
4− * This program is free software: you can redistribute it and/or modify it under
5− * the terms of the GNU Lesser General Public License version 3, as published by
6− * the Free Software Foundation.
7− *
8− * This program is distributed in the hope that it will be useful, but WITHOUT
9− * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10− * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11− * Lesser General Public License for more details.
12− *
13− * You should have received a copy of the GNU Lesser General Public License
14− * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− */
16−
17−#include "Screens.h"
18−#include "Screen.h"
19−
20−// qtmirserver
21−#include <qtmir/qtmir.h>
22−#include <qtmir/screens.h>
23−#include <QGuiApplication>
24−
25−// Qt
26−#include <QScreen>
27−#include <QWindow>
28−
29−Screens::Screens(QObject *parent)
30− : QAbstractListModel(parent)
31−{
32− bool ok = false;
33− int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
34− if (!ok) screenCount = 1;
35− QPoint lastPoint(0,0);
36− for (int i = 0; i < screenCount; ++i) {
37− auto screen = new Screen();
38− screen->m_id = qtmir::OutputId{i};
39− screen->m_active = i == 0;
40− screen->m_name = QString("Monitor %1").arg(i);
41− screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
42− screen->m_sizes.append(new qtmir::ScreenMode(50, QSize(640,480)));
43− screen->m_sizes.append(new qtmir::ScreenMode(60, QSize(1280,1024)));
44− screen->m_sizes.append(new qtmir::ScreenMode(60, QSize(1440,900)));
45− screen->m_sizes.append(new qtmir::ScreenMode(60, QSize(1920,1080)));
46− screen->m_currentModeIndex = 3;
47− screen->m_physicalSize = QSize(800,568);
48− m_screenList.append(screen);
49−
50− lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
51− }
52−}
53−
54−QHash<int, QByteArray> Screens::roleNames() const
55−{
56− QHash<int, QByteArray> roles;
57− roles[ScreenRole] = "screen";
58− return roles;
59−}
60−
61−QVariant Screens::data(const QModelIndex &index, int role) const
62−{
63− if (!index.isValid() || index.row() >= m_screenList.size()) {
64− return QVariant();
65− }
66−
67− switch(role) {
68− case ScreenRole:
69− return QVariant::fromValue(m_screenList.at(index.row()));
70− } // switch
71−
72− return QVariant();
73−}
74−
75−int Screens::rowCount(const QModelIndex &) const
76−{
77− return count();
78−}
79−
80−int Screens::count() const
81−{
82− return m_screenList.size();
83−}
84−
85−QVariant Screens::activeScreen() const
86−{
87− for (int i = 0; i < m_screenList.count(); i++) {
88− if (m_screenList[i]->isActive()) return i;
89− }
90− return QVariant();
91−}
92−
93−void Screens::activateScreen(const QVariant& vindex)
94−{
95− bool ok = false;
96− int index = vindex.toInt(&ok);
97− if (!ok || index < 0 || m_screenList.count() <= index) return;
98−
99− auto screen = m_screenList.at(index);
100− screen->setActive(true);
101−}
tests/mocks/WindowManager/Screens.hdeleted−68
@@ -1,68 +0,0 @@
1−/*
2− * Copyright (C) 2016 Canonical, Ltd.
3− *
4− * This program is free software: you can redistribute it and/or modify it under
5− * the terms of the GNU Lesser General Public License version 3, as published by
6− * the Free Software Foundation.
7− *
8− * This program is distributed in the hope that it will be useful, but WITHOUT
9− * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10− * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11− * Lesser General Public License for more details.
12− *
13− * You should have received a copy of the GNU Lesser General Public License
14− * along with this program. If not, see <http://www.gnu.org/licenses/>.
15− */
16−
17−#ifndef UNITY_SCREENS_H
18−#define UNITY_SCREENS_H
19−
20−#include <QAbstractListModel>
21−#include <QSharedPointer>
22−
23−class Screen;
24−namespace qtmir
25−{
26−class Screens;
27−}
28−
29−class Screens : public QAbstractListModel
30−{
31− Q_OBJECT
32− Q_PROPERTY(int count READ count NOTIFY countChanged)
33− Q_PROPERTY(QVariant activeScreen READ activeScreen WRITE activateScreen NOTIFY activeScreenChanged)
34−
35−public:
36− enum ItemRoles {
37− ScreenRole = Qt::UserRole + 1
38− };
39−
40− explicit Screens(QObject *parent = 0);
41− virtual ~Screens() noexcept = default;
42−
43− Q_INVOKABLE Screens* createProxy() { return this; }
44− Q_INVOKABLE void sync(Screens *) {}
45−
46− /* QAbstractItemModel */
47− QHash<int, QByteArray> roleNames() const override;
48− QVariant data(const QModelIndex &index, int role = ScreenRole) const override;
49− int rowCount(const QModelIndex &parent = QModelIndex()) const override;
50−
51− int count() const;
52− QVariant activeScreen() const;
53−
54−public Q_SLOTS:
55− void activateScreen(const QVariant& index);
56−
57−Q_SIGNALS:
58− void countChanged();
59− void activeScreenChanged();
60−
61− void screenAdded(Screen* screen);
62− void screenRemoved(Screen* screen);
63−
64−private:
65− QList<Screen*> m_screenList;
66−};
67−
68−#endif // SCREENS_H
tests/mocks/WindowManager/WindowManagerPlugin.cpp+7−3
@@ -18,6 +18,7 @@
1818
1919#include "Screens.h"
2020#include "Screen.h"
21+#include "MockScreens.h"
2122#include "ScreenWindow.h"
2223#include "TopLevelWindowModel.h"
2324#include "Window.h"
@@ -36,7 +37,8 @@ static QObject *workspace_manager(QQmlEngine *engine, QJSEngine *scriptEngine)
3637QObject* screensSingleton(QQmlEngine* engine, QJSEngine* scriptEngine) {
3738 Q_UNUSED(engine);
3839 Q_UNUSED(scriptEngine);
39− return new Screens();
40+ QSharedPointer<qtmir::Screens> mockScreens(new MockScreens());
41+ return new Screens(mockScreens);
4042}
4143
4244void WindowManagerPlugin::registerTypes(const char *uri)
@@ -46,12 +48,14 @@ void WindowManagerPlugin::registerTypes(const char *uri)
4648 qmlRegisterUncreatableType<WorkspaceModel>(uri, 1, 0, "WorkspaceModel", "Not a creatable type");
4749 qmlRegisterSingletonType<Screens>(uri, 1, 0, "Screens", screensSingleton);
4850 qmlRegisterUncreatableType<qtmir::ScreenMode>(uri, 1, 0, "ScreenMode", "ScreenMode is not creatable.");
51+ qmlRegisterUncreatableType<Workspace>(uri, 1, 0, "Workspace", "Workspace is not creatable.");
4952
50− qRegisterMetaType<Window*>("Window*");
53+ qRegisterMetaType<Screen*>("Screen*");
54+ qRegisterMetaType<ScreensProxy*>("ScreensProxy*");
5155 qRegisterMetaType<Workspace*>("Workspace*");
5256
57+ qRegisterMetaType<Window*>("Window*");
5358 qRegisterMetaType<QAbstractListModel*>("QAbstractListModel*");
54− qRegisterMetaType<Screen*>("Screen*");
5559
5660 qmlRegisterType<ScreenWindow>(uri, 1, 0, "ScreenWindow");
5761 qmlRegisterRevision<QWindow,1>(uri, 1, 0);