on xenial help sign in
Make mock gsettings closer to real gsettings objects
commit21b7b0d3543bf3561a49b85eafa72d409df37055 authorMichael Terry <michael.terry@canonical.com> dateFri 1 Apr 2016, 14:41 (11 years ago) parentc31ba8d
3 files changed+92−63
tests/mocks/GSettings.1.0/fake_gsettings.cpp+50−37 tests/mocks/GSettings.1.0/fake_gsettings.h+28−21 tests/qmltests/tst_OrientedShell.qml+14−5
tests/mocks/GSettings.1.0/fake_gsettings.cpp+50−37
@@ -146,9 +146,22 @@ void GSettingsSchemaQml::setPath(const QByteArray &path) {
146146}
147147
148148GSettingsQml::GSettingsQml(QObject *parent)
149− : QObject(parent)
149+ : QObject(parent),
150+ m_valid(false)
150151{
151152 m_schema = new GSettingsSchemaQml(this);
153+}
154+
155+void GSettingsQml::classBegin()
156+{
157+}
158+
159+void GSettingsQml::componentComplete()
160+{
161+ // Emulate what the real GSettings module does, and only return undefined
162+ // values until we are completed loading.
163+ m_valid = true;
164+
152165 connect(GSettingsControllerQml::instance(), &GSettingsControllerQml::pictureUriChanged,
153166 this, &GSettingsQml::pictureUriChanged);
154167 connect(GSettingsControllerQml::instance(), &GSettingsControllerQml::usageModeChanged,
@@ -167,98 +180,98 @@ GSettingsSchemaQml * GSettingsQml::schema() const {
167180 return m_schema;
168181}
169182
170−QString GSettingsQml::pictureUri() const
183+QVariant GSettingsQml::pictureUri() const
171184{
172− if (m_schema->id() == "org.gnome.desktop.background") {
185+ if (m_valid && m_schema->id() == "org.gnome.desktop.background") {
173186 return GSettingsControllerQml::instance()->pictureUri();
174187 } else {
175− return "";
188+ return QVariant();
176189 }
177190}
178191
179−void GSettingsQml::setPictureUri(const QString &str)
192+void GSettingsQml::setPictureUri(const QVariant &str)
180193{
181− if (m_schema->id() == "org.gnome.desktop.background") {
182− GSettingsControllerQml::instance()->setPictureUri(str);
194+ if (m_valid && m_schema->id() == "org.gnome.desktop.background") {
195+ GSettingsControllerQml::instance()->setPictureUri(str.toString());
183196 }
184197}
185198
186−QString GSettingsQml::usageMode() const
199+QVariant GSettingsQml::usageMode() const
187200{
188− if (m_schema->id() == "com.canonical.Unity8") {
201+ if (m_valid && m_schema->id() == "com.canonical.Unity8") {
189202 return GSettingsControllerQml::instance()->usageMode();
190203 } else {
191− return "";
204+ return QVariant();
192205 }
193206}
194207
195−void GSettingsQml::setUsageMode(const QString &usageMode)
208+void GSettingsQml::setUsageMode(const QVariant &usageMode)
196209{
197− if (m_schema->id() == "com.canonical.Unity8") {
198− GSettingsControllerQml::instance()->setUsageMode(usageMode);
210+ if (m_valid && m_schema->id() == "com.canonical.Unity8") {
211+ GSettingsControllerQml::instance()->setUsageMode(usageMode.toString());
199212 }
200213}
201214
202−qint64 GSettingsQml::lockedOutTime() const
215+QVariant GSettingsQml::lockedOutTime() const
203216{
204− if (m_schema->id() == "com.canonical.Unity8.Greeter") {
217+ if (m_valid && m_schema->id() == "com.canonical.Unity8.Greeter") {
205218 return GSettingsControllerQml::instance()->lockedOutTime();
206219 } else {
207− return 0;
220+ return QVariant();
208221 }
209222}
210223
211−void GSettingsQml::setLockedOutTime(qint64 timestamp)
224+void GSettingsQml::setLockedOutTime(const QVariant &timestamp)
212225{
213− if (m_schema->id() == "com.canonical.Unity8.Greeter") {
214− GSettingsControllerQml::instance()->setLockedOutTime(timestamp);
226+ if (m_valid && m_schema->id() == "com.canonical.Unity8.Greeter") {
227+ GSettingsControllerQml::instance()->setLockedOutTime(timestamp.value<qint64>());
215228 }
216229}
217230
218−QStringList GSettingsQml::lifecycleExemptAppids() const
231+QVariant GSettingsQml::lifecycleExemptAppids() const
219232{
220− if (m_schema->id() == "com.canonical.qtmir") {
233+ if (m_valid && m_schema->id() == "com.canonical.qtmir") {
221234 return GSettingsControllerQml::instance()->lifecycleExemptAppids();
222235 } else {
223− return QStringList();
236+ return QVariant();
224237 }
225238}
226239
227−bool GSettingsQml::autohideLauncher() const
240+QVariant GSettingsQml::autohideLauncher() const
228241{
229− if (m_schema->id() == "com.canonical.Unity8") {
242+ if (m_valid && m_schema->id() == "com.canonical.Unity8") {
230243 return GSettingsControllerQml::instance()->autohideLauncher();
231244 } else {
232− return false;
245+ return QVariant();
233246 }
234247}
235248
236−int GSettingsQml::launcherWidth() const
249+QVariant GSettingsQml::launcherWidth() const
237250{
238− if (m_schema->id() == "com.canonical.Unity8") {
251+ if (m_valid && m_schema->id() == "com.canonical.Unity8") {
239252 return GSettingsControllerQml::instance()->launcherWidth();
240253 } else {
241− return false;
254+ return QVariant();
242255 }
243256}
244257
245−void GSettingsQml::setLifecycleExemptAppids(const QStringList &appIds)
258+void GSettingsQml::setLifecycleExemptAppids(const QVariant &appIds)
246259{
247− if (m_schema->id() == "com.canonical.qtmir") {
248− GSettingsControllerQml::instance()->setLifecycleExemptAppids(appIds);
260+ if (m_valid && m_schema->id() == "com.canonical.qtmir") {
261+ GSettingsControllerQml::instance()->setLifecycleExemptAppids(appIds.toStringList());
249262 }
250263}
251264
252−void GSettingsQml::setAutohideLauncher(bool autohideLauncher)
265+void GSettingsQml::setAutohideLauncher(const QVariant &autohideLauncher)
253266{
254− if (m_schema->id() == "com.canonical.Unity8") {
255− GSettingsControllerQml::instance()->setAutohideLauncher(autohideLauncher);
267+ if (m_valid && m_schema->id() == "com.canonical.Unity8") {
268+ GSettingsControllerQml::instance()->setAutohideLauncher(autohideLauncher.toBool());
256269 }
257270}
258271
259−void GSettingsQml::setLauncherWidth(int launcherWidth)
272+void GSettingsQml::setLauncherWidth(const QVariant &launcherWidth)
260273{
261− if (m_schema->id() == "com.canonical.Unity8") {
262− GSettingsControllerQml::instance()->setLauncherWidth(launcherWidth);
274+ if (m_valid && m_schema->id() == "com.canonical.Unity8") {
275+ GSettingsControllerQml::instance()->setLauncherWidth(launcherWidth.toInt());
263276 }
264277}
tests/mocks/GSettings.1.0/fake_gsettings.h+28−21
@@ -19,7 +19,9 @@
1919
2020#include <QList>
2121#include <QObject>
22+#include <QQmlParserStatus>
2223#include <QStringList>
24+#include <QVariant>
2325
2426class GSettingsSchemaQml: public QObject
2527{
@@ -41,35 +43,39 @@ private:
4143 QByteArray m_path;
4244};
4345
44−class GSettingsQml: public QObject
46+class GSettingsQml: public QObject, public QQmlParserStatus
4547{
4648 Q_OBJECT
49+ Q_INTERFACES(QQmlParserStatus)
4750
4851 Q_PROPERTY(GSettingsSchemaQml* schema READ schema NOTIFY schemaChanged)
49− Q_PROPERTY(QString pictureUri READ pictureUri WRITE setPictureUri NOTIFY pictureUriChanged)
50− Q_PROPERTY(QString usageMode READ usageMode WRITE setUsageMode NOTIFY usageModeChanged)
51− Q_PROPERTY(qint64 lockedOutTime READ lockedOutTime WRITE setLockedOutTime NOTIFY lockedOutTimeChanged)
52− Q_PROPERTY(QStringList lifecycleExemptAppids READ lifecycleExemptAppids WRITE setLifecycleExemptAppids NOTIFY lifecycleExemptAppidsChanged)
53− Q_PROPERTY(bool autohideLauncher READ autohideLauncher WRITE setAutohideLauncher NOTIFY autohideLauncherChanged)
54− Q_PROPERTY(int launcherWidth READ launcherWidth WRITE setLauncherWidth NOTIFY launcherWidthChanged)
52+ Q_PROPERTY(QVariant pictureUri READ pictureUri WRITE setPictureUri NOTIFY pictureUriChanged)
53+ Q_PROPERTY(QVariant usageMode READ usageMode WRITE setUsageMode NOTIFY usageModeChanged)
54+ Q_PROPERTY(QVariant lockedOutTime READ lockedOutTime WRITE setLockedOutTime NOTIFY lockedOutTimeChanged)
55+ Q_PROPERTY(QVariant lifecycleExemptAppids READ lifecycleExemptAppids WRITE setLifecycleExemptAppids NOTIFY lifecycleExemptAppidsChanged)
56+ Q_PROPERTY(QVariant autohideLauncher READ autohideLauncher WRITE setAutohideLauncher NOTIFY autohideLauncherChanged)
57+ Q_PROPERTY(QVariant launcherWidth READ launcherWidth WRITE setLauncherWidth NOTIFY launcherWidthChanged)
5558
5659public:
5760 GSettingsQml(QObject *parent = nullptr);
5861
59− GSettingsSchemaQml * schema() const;
60− QString pictureUri() const;
61− QString usageMode() const;
62− qint64 lockedOutTime() const;
63− QStringList lifecycleExemptAppids() const;
64− bool autohideLauncher() const;
65− int launcherWidth() const;
62+ void classBegin();
63+ void componentComplete();
6664
67− void setPictureUri(const QString &str);
68− void setUsageMode(const QString &usageMode);
69− void setLockedOutTime(qint64 timestamp);
70− void setLifecycleExemptAppids(const QStringList &appIds);
71− void setAutohideLauncher(bool autohideLauncher);
72− void setLauncherWidth(int launcherWidth);
65+ GSettingsSchemaQml * schema() const;
66+ QVariant pictureUri() const;
67+ QVariant usageMode() const;
68+ QVariant lockedOutTime() const;
69+ QVariant lifecycleExemptAppids() const;
70+ QVariant autohideLauncher() const;
71+ QVariant launcherWidth() const;
72+
73+ void setPictureUri(const QVariant &str);
74+ void setUsageMode(const QVariant &usageMode);
75+ void setLockedOutTime(const QVariant &timestamp);
76+ void setLifecycleExemptAppids(const QVariant &appIds);
77+ void setAutohideLauncher(const QVariant &autohideLauncher);
78+ void setLauncherWidth(const QVariant &launcherWidth);
7379
7480Q_SIGNALS:
7581 void schemaChanged();
@@ -82,6 +88,7 @@ Q_SIGNALS:
8288
8389private:
8490 GSettingsSchemaQml* m_schema;
91+ bool m_valid;
8592
8693 friend class GSettingsSchemaQml;
8794};
@@ -114,7 +121,7 @@ public:
114121
115122Q_SIGNALS:
116123 void pictureUriChanged(const QString&);
117− void usageModeChanged(const QString&);
124+ void usageModeChanged(const QString &usageMode);
118125 void lockedOutTimeChanged(qint64 timestamp);
119126 void lifecycleExemptAppidsChanged(const QStringList&);
120127 void autohideLauncherChanged(bool autohideLauncher);
tests/qmltests/tst_OrientedShell.qml+14−5
@@ -17,6 +17,7 @@
1717import QtQuick 2.4
1818import QtQuick.Layouts 1.1
1919import QtTest 1.0
20+import GSettings 1.0
2021import Ubuntu.Components 1.3
2122import Ubuntu.Components.ListItems 1.3 as ListItem
2223import Unity.Application 0.1
@@ -44,9 +45,8 @@ Rectangle {
4445 property int savedOrientation
4546 }
4647
47− QtObject {
48− id: mockUnity8Settings
49− property string usageMode: usageModeSelector.model[usageModeSelector.selectedIndex]
48+ Connections {
49+ target: GSettingsController
5050 onUsageModeChanged: {
5151 usageModeSelector.selectedIndex = usageModeSelector.model.indexOf(usageMode)
5252 }
@@ -157,7 +157,6 @@ Rectangle {
157157 sourceComponent: Component {
158158 OrientedShell {
159159 anchors.fill: parent
160− unity8Settings: mockUnity8Settings
161160 oskSettings: mockOskSettings
162161 physicalOrientation: root.physicalOrientation0
163162 orientationLocked: orientationLockedCheckBox.checked
@@ -311,7 +310,7 @@ Rectangle {
311310 function selectWindowed() {selectedIndex = 1;}
312311 function selectAutomatic() {selectedIndex = 2;}
313312 onSelectedIndexChanged: {
314− mockUnity8Settings.usageMode = usageModeSelector.model[usageModeSelector.selectedIndex]
313+ GSettingsController.setUsageMode(usageModeSelector.model[usageModeSelector.selectedIndex]);
315314 }
316315 }
317316 MouseTouchEmulationCheckbox {
@@ -1140,6 +1139,16 @@ Rectangle {
11401139 orientedShellLoader.width = oldWidth;
11411140 }
11421141
1142+ function test_setsUsageModeOnStartup() {
1143+ // Prepare inconsistent beginning (mouse & staged mode)
1144+ MockInputDeviceBackend.addMockDevice("/mouse0", InputInfo.Mouse);
1145+ usageModeSelector.selectStaged();
1146+
1147+ // Load shell, and have it pick desktop
1148+ loadShell("desktop");
1149+ compare(shell.usageScenario, "desktop");
1150+ }
1151+
11431152 function test_overrideWindowed() {
11441153 loadShell("mako")
11451154