| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | #include <QtTest/QtTest> |
| 29 | #include <QtCore/QCoreApplication> |
| 30 | #include <QtCore/QScopedPointer> |
| 31 | #include <QtCore/QSettings> |
| 32 | #include <QtCore/QVariant> |
| 33 | #include <QtGui/QColor> |
| 34 | #include <QtGui/QFont> |
| 35 | #include <QtQml/QQmlEngine> |
| 36 | #include <QtQml/QQmlComponent> |
| 37 | #include "../../shared/util.h" |
| 38 | |
| 39 | class tst_QQmlSettings : public QQmlDataTest |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | |
| 43 | private slots: |
| 44 | void initTestCase(); |
| 45 | |
| 46 | void init(); |
| 47 | void cleanup(); |
| 48 | |
| 49 | void basic(); |
| 50 | void types(); |
| 51 | void aliases_data(); |
| 52 | void aliases(); |
| 53 | void categories(); |
| 54 | void siblings(); |
| 55 | void initial(); |
| 56 | void noApplicationIdentifiersSet(); |
| 57 | }; |
| 58 | |
| 59 | // ### Replace keyValueMap("foo", "bar") with QVariantMap({{"foo", "bar"}}) |
| 60 | // when C++11 uniform initialization can be used (not supported by MSVC 2013). |
| 61 | static QVariantMap keyValueMap(const QString &key, const QString &value) |
| 62 | { |
| 63 | QVariantMap var; |
| 64 | var.insert(akey: key, avalue: value); |
| 65 | return var; |
| 66 | } |
| 67 | |
| 68 | class CppObject : public QObject |
| 69 | { |
| 70 | Q_OBJECT |
| 71 | Q_PROPERTY(int intProperty MEMBER m_intProperty NOTIFY intPropertyChanged) |
| 72 | Q_PROPERTY(bool boolProperty MEMBER m_boolProperty NOTIFY boolPropertyChanged) |
| 73 | Q_PROPERTY(qreal realProperty MEMBER m_realProperty NOTIFY realPropertyChanged) |
| 74 | Q_PROPERTY(double doubleProperty MEMBER m_doubleProperty NOTIFY doublePropertyChanged) |
| 75 | Q_PROPERTY(QString stringProperty MEMBER m_stringProperty NOTIFY stringPropertyChanged) |
| 76 | Q_PROPERTY(QUrl urlProperty MEMBER m_urlProperty NOTIFY urlPropertyChanged) |
| 77 | Q_PROPERTY(QVariant varProperty MEMBER m_varProperty NOTIFY varPropertyChanged) |
| 78 | Q_PROPERTY(QVariantMap objectProperty MEMBER m_objectProperty NOTIFY objectPropertyChanged) |
| 79 | Q_PROPERTY(QVariantList intListProperty MEMBER m_intListProperty NOTIFY intListPropertyChanged) |
| 80 | Q_PROPERTY(QVariantList stringListProperty MEMBER m_stringListProperty NOTIFY stringListPropertyChanged) |
| 81 | Q_PROPERTY(QVariantList objectListProperty MEMBER m_objectListProperty NOTIFY objectListPropertyChanged) |
| 82 | Q_PROPERTY(QDate dateProperty MEMBER m_dateProperty NOTIFY datePropertyChanged) |
| 83 | // QTBUG-32295: Q_PROPERTY(QTime timeProperty MEMBER m_timeProperty NOTIFY timePropertyChanged) |
| 84 | Q_PROPERTY(QSizeF sizeProperty MEMBER m_sizeProperty NOTIFY sizePropertyChanged) |
| 85 | Q_PROPERTY(QPointF pointProperty MEMBER m_pointProperty NOTIFY pointPropertyChanged) |
| 86 | Q_PROPERTY(QRectF rectProperty MEMBER m_rectProperty NOTIFY rectPropertyChanged) |
| 87 | Q_PROPERTY(QColor colorProperty MEMBER m_colorProperty NOTIFY colorPropertyChanged) |
| 88 | Q_PROPERTY(QFont fontProperty MEMBER m_fontProperty NOTIFY fontPropertyChanged) |
| 89 | |
| 90 | public: |
| 91 | CppObject(QObject *parent = nullptr) : QObject(parent), |
| 92 | |
| 93 | m_stringProperty("foo" ), |
| 94 | m_urlProperty("http://www.qt-project.org" ), |
| 95 | m_objectProperty(keyValueMap(key: "foo" , value: "bar" )), |
| 96 | m_intListProperty(QVariantList() << 1 << 2 << 3), |
| 97 | m_stringListProperty(QVariantList() << "a" << "b" << "c" ), |
| 98 | m_objectListProperty(QVariantList() << keyValueMap(key: "a" , value: "b" ) << keyValueMap(key: "c" , value: "d" )), |
| 99 | m_dateProperty(2000, 1, 2), |
| 100 | // QTBUG-32295: m_timeProperty(12, 34, 56), |
| 101 | m_sizeProperty(12, 34), |
| 102 | m_pointProperty(12, 34), |
| 103 | m_rectProperty(1, 2, 3, 4), |
| 104 | m_colorProperty("red" ) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | signals: |
| 109 | void intPropertyChanged(int arg); |
| 110 | void boolPropertyChanged(bool arg); |
| 111 | void realPropertyChanged(qreal arg); |
| 112 | void doublePropertyChanged(double arg); |
| 113 | void stringPropertyChanged(const QString &arg); |
| 114 | void urlPropertyChanged(const QUrl &arg); |
| 115 | void varPropertyChanged(const QVariant &arg); |
| 116 | void objectPropertyChanged(const QVariantMap &arg); |
| 117 | void intListPropertyChanged(const QVariantList &arg); |
| 118 | void stringListPropertyChanged(const QVariantList &arg); |
| 119 | void objectListPropertyChanged(const QVariantList &arg); |
| 120 | void datePropertyChanged(const QDate &arg); |
| 121 | void sizePropertyChanged(const QSizeF &arg); |
| 122 | void pointPropertyChanged(const QPointF &arg); |
| 123 | void rectPropertyChanged(const QRectF &arg); |
| 124 | void colorPropertyChanged(const QColor &arg); |
| 125 | void fontPropertyChanged(const QFont &arg); |
| 126 | |
| 127 | private: |
| 128 | int m_intProperty = 123; |
| 129 | bool m_boolProperty = true; |
| 130 | qreal m_realProperty = 1.23; |
| 131 | double m_doubleProperty = 3.45; |
| 132 | QString m_stringProperty; |
| 133 | QUrl m_urlProperty; |
| 134 | QVariant m_varProperty; |
| 135 | QVariantMap m_objectProperty; |
| 136 | QVariantList m_intListProperty; |
| 137 | QVariantList m_stringListProperty; |
| 138 | QVariantList m_objectListProperty; |
| 139 | QDate m_dateProperty; |
| 140 | QSizeF m_sizeProperty; |
| 141 | QPointF m_pointProperty; |
| 142 | QRectF m_rectProperty; |
| 143 | QColor m_colorProperty; |
| 144 | QFont m_fontProperty; |
| 145 | }; |
| 146 | |
| 147 | void tst_QQmlSettings::initTestCase() |
| 148 | { |
| 149 | QQmlDataTest::initTestCase(); |
| 150 | |
| 151 | qmlRegisterType<CppObject>(uri: "Qt.test" , versionMajor: 1, versionMinor: 0, qmlName: "CppObject" ); |
| 152 | } |
| 153 | |
| 154 | void tst_QQmlSettings::init() |
| 155 | { |
| 156 | QSettings settings; |
| 157 | settings.clear(); |
| 158 | |
| 159 | QCoreApplication::setApplicationName("tst_QQmlSettings" ); |
| 160 | QCoreApplication::setOrganizationName("QtProject" ); |
| 161 | QCoreApplication::setOrganizationDomain("qt-project.org" ); |
| 162 | } |
| 163 | |
| 164 | void tst_QQmlSettings::cleanup() |
| 165 | { |
| 166 | QSettings settings; |
| 167 | settings.clear(); |
| 168 | } |
| 169 | |
| 170 | void tst_QQmlSettings::basic() |
| 171 | { |
| 172 | QQmlEngine engine; |
| 173 | QQmlComponent component(&engine, testFileUrl(fileName: "basic.qml" )); |
| 174 | QScopedPointer<QObject> root(component.create()); |
| 175 | QVERIFY(root.data()); |
| 176 | QVERIFY(root->property("success" ).toBool()); |
| 177 | QSettings settings; |
| 178 | QTRY_VERIFY(settings.value("success" ).toBool()); |
| 179 | } |
| 180 | |
| 181 | void tst_QQmlSettings::types() |
| 182 | { |
| 183 | QQmlEngine engine; |
| 184 | QQmlComponent component(&engine, testFileUrl(fileName: "types.qml" )); |
| 185 | QScopedPointer<QObject> root(component.create()); |
| 186 | QVERIFY(root.data()); |
| 187 | QObject *settings = root->property(name: "settings" ).value<QObject *>(); |
| 188 | QVERIFY(settings); |
| 189 | |
| 190 | // default property values |
| 191 | QCOMPARE(root->property("intProperty" ).toInt(), 0); |
| 192 | QCOMPARE(root->property("boolProperty" ).toBool(), false); |
| 193 | QCOMPARE(root->property("realProperty" ).toReal(), static_cast<qreal>(0.0)); |
| 194 | QCOMPARE(root->property("doubleProperty" ).toDouble(), static_cast<double>(0.0)); |
| 195 | QCOMPARE(root->property("stringProperty" ).toString(), QString()); |
| 196 | QCOMPARE(root->property("urlProperty" ).toUrl(), QUrl()); |
| 197 | QCOMPARE(root->property("objectProperty" ).toMap(), QVariantMap()); |
| 198 | QCOMPARE(root->property("intListProperty" ).toList(), QVariantList()); |
| 199 | QCOMPARE(root->property("stringListProperty" ).toList(), QVariantList()); |
| 200 | QCOMPARE(root->property("objectListProperty" ).toList(), QVariantList()); |
| 201 | QCOMPARE(root->property("dateProperty" ).toDate(), QDate()); |
| 202 | // QTBUG-32295: QCOMPARE(root->property("timeProperty").toDate(), QTime()); |
| 203 | QCOMPARE(root->property("sizeProperty" ).toSizeF(), QSizeF()); |
| 204 | QCOMPARE(root->property("pointProperty" ).toPointF(), QPointF()); |
| 205 | QCOMPARE(root->property("rectProperty" ).toRectF(), QRectF()); |
| 206 | QCOMPARE(root->property("colorProperty" ).value<QColor>(), QColor()); |
| 207 | QCOMPARE(root->property("fontProperty" ).value<QFont>(), QFont()); |
| 208 | |
| 209 | // default settings values |
| 210 | QCOMPARE(settings->property("intProperty" ).toInt(), 123); |
| 211 | QCOMPARE(settings->property("boolProperty" ).toBool(), true); |
| 212 | QCOMPARE(settings->property("realProperty" ).toReal(), static_cast<qreal>(1.23)); |
| 213 | QCOMPARE(settings->property("doubleProperty" ).toDouble(), static_cast<double>(3.45)); |
| 214 | QCOMPARE(settings->property("stringProperty" ).toString(), QStringLiteral("foo" )); |
| 215 | QCOMPARE(settings->property("urlProperty" ).toUrl(), QUrl("http://www.qt-project.org" )); |
| 216 | QCOMPARE(settings->property("objectProperty" ).toMap(), keyValueMap("foo" ,"bar" )); |
| 217 | QCOMPARE(settings->property("intListProperty" ).toList(), QVariantList() << 1 << 2 << 3); |
| 218 | QCOMPARE(settings->property("stringListProperty" ).toList(), QVariantList() << QStringLiteral("a" ) << QStringLiteral("b" ) << QStringLiteral("c" )); |
| 219 | QCOMPARE(settings->property("objectListProperty" ).toList(), QVariantList() << keyValueMap("a" , "b" ) << keyValueMap("c" ,"d" )); |
| 220 | QCOMPARE(settings->property("dateProperty" ).toDate(), QDate(2000, 01, 02)); |
| 221 | // QTBUG-32295: QCOMPARE(settings->property("timeProperty").toDate(), QTime(12, 34, 56)); |
| 222 | QCOMPARE(settings->property("sizeProperty" ).toSizeF(), QSizeF(12, 34)); |
| 223 | QCOMPARE(settings->property("pointProperty" ).toPointF(), QPointF(12, 34)); |
| 224 | QCOMPARE(settings->property("rectProperty" ).toRectF(), QRectF(1, 2, 3, 4)); |
| 225 | QCOMPARE(settings->property("colorProperty" ).value<QColor>(), QColor(Qt::red)); |
| 226 | QCOMPARE(settings->property("fontProperty" ).value<QFont>(), QFont()); |
| 227 | |
| 228 | // read settings |
| 229 | QVERIFY(QMetaObject::invokeMethod(root.data(), "readSettings" )); |
| 230 | QCOMPARE(root->property("intProperty" ).toInt(), 123); |
| 231 | QCOMPARE(root->property("boolProperty" ).toBool(), true); |
| 232 | QCOMPARE(root->property("realProperty" ).toReal(), static_cast<qreal>(1.23)); |
| 233 | QCOMPARE(root->property("doubleProperty" ).toDouble(), static_cast<double>(3.45)); |
| 234 | QCOMPARE(root->property("stringProperty" ).toString(), QStringLiteral("foo" )); |
| 235 | QCOMPARE(root->property("urlProperty" ).toUrl(), QUrl("http://www.qt-project.org" )); |
| 236 | QCOMPARE(root->property("objectProperty" ).toMap(), keyValueMap("foo" ,"bar" )); |
| 237 | QCOMPARE(root->property("intListProperty" ).toList(), QVariantList() << 1 << 2 << 3); |
| 238 | QCOMPARE(root->property("stringListProperty" ).toList(), QVariantList() << QStringLiteral("a" ) << QStringLiteral("b" ) << QStringLiteral("c" )); |
| 239 | QCOMPARE(root->property("dateProperty" ).toDate(), QDate(2000, 01, 02)); |
| 240 | QCOMPARE(root->property("objectListProperty" ).toList(), QVariantList() << keyValueMap("a" , "b" ) << keyValueMap("c" ,"d" )); |
| 241 | // QTBUG-32295: QCOMPARE(root->property("timeProperty").toDate(), QTime(12, 34, 56)); |
| 242 | QCOMPARE(root->property("sizeProperty" ).toSizeF(), QSizeF(12, 34)); |
| 243 | QCOMPARE(root->property("pointProperty" ).toPointF(), QPointF(12, 34)); |
| 244 | QCOMPARE(root->property("rectProperty" ).toRectF(), QRectF(1, 2, 3, 4)); |
| 245 | QCOMPARE(root->property("colorProperty" ).value<QColor>(), QColor(Qt::red)); |
| 246 | QCOMPARE(root->property("fontProperty" ).value<QFont>(), QFont()); |
| 247 | |
| 248 | // change properties |
| 249 | QVERIFY(root->setProperty("intProperty" , 456)); |
| 250 | QVERIFY(root->setProperty("boolProperty" , false)); |
| 251 | QVERIFY(root->setProperty("realProperty" , static_cast<qreal>(4.56))); |
| 252 | QVERIFY(root->setProperty("doubleProperty" , static_cast<double>(6.78))); |
| 253 | QVERIFY(root->setProperty("stringProperty" , QStringLiteral("bar" ))); |
| 254 | QVERIFY(root->setProperty("urlProperty" , QUrl("https://codereview.qt-project.org" ))); |
| 255 | QVERIFY(root->setProperty("objectProperty" , keyValueMap("bar" , "baz" ))); |
| 256 | QVERIFY(root->setProperty("intListProperty" , QVariantList() << 4 << 5 << 6)); |
| 257 | QVERIFY(root->setProperty("stringListProperty" , QVariantList() << QStringLiteral("d" ) << QStringLiteral("e" ) << QStringLiteral("f" ))); |
| 258 | QVERIFY(root->setProperty("objectListProperty" , QVariantList() << keyValueMap("e" , "f" ) << keyValueMap("g" , "h" ))); |
| 259 | QVERIFY(root->setProperty("dateProperty" , QDate(2010, 02, 01))); |
| 260 | // QTBUG-32295: QVERIFY(root->setProperty("timeProperty", QTime(6, 56, 34))); |
| 261 | QVERIFY(root->setProperty("sizeProperty" , QSizeF(56, 78))); |
| 262 | QVERIFY(root->setProperty("pointProperty" , QPointF(56, 78))); |
| 263 | QVERIFY(root->setProperty("rectProperty" , QRectF(5, 6, 7, 8))); |
| 264 | QVERIFY(root->setProperty("colorProperty" , QColor(Qt::blue))); |
| 265 | QFont boldFont; boldFont.setBold(true); |
| 266 | QVERIFY(root->setProperty("fontProperty" , boldFont)); |
| 267 | |
| 268 | // write settings |
| 269 | QVERIFY(QMetaObject::invokeMethod(root.data(), "writeSettings" )); |
| 270 | QTRY_COMPARE(settings->property("intProperty" ).toInt(), 456); |
| 271 | QTRY_COMPARE(settings->property("boolProperty" ).toBool(), false); |
| 272 | QTRY_COMPARE(settings->property("realProperty" ).toReal(), static_cast<qreal>(4.56)); |
| 273 | QTRY_COMPARE(settings->property("doubleProperty" ).toDouble(), static_cast<double>(6.78)); |
| 274 | QTRY_COMPARE(settings->property("stringProperty" ).toString(), QStringLiteral("bar" )); |
| 275 | QTRY_COMPARE(settings->property("urlProperty" ).toUrl(), QUrl("https://codereview.qt-project.org" )); |
| 276 | QTRY_COMPARE(settings->property("objectProperty" ).toMap(), keyValueMap("bar" , "baz" )); |
| 277 | QTRY_COMPARE(settings->property("intListProperty" ).toList(), QVariantList() << 4 << 5 << 6); |
| 278 | QTRY_COMPARE(settings->property("stringListProperty" ).toList(), QVariantList() << QStringLiteral("d" ) << QStringLiteral("e" ) << QStringLiteral("f" )); |
| 279 | QTRY_COMPARE(settings->property("objectListProperty" ).toList(), QVariantList() << keyValueMap("e" , "f" ) << keyValueMap("g" , "h" )); |
| 280 | QTRY_COMPARE(settings->property("dateProperty" ).toDate(), QDate(2010, 02, 01)); |
| 281 | // QTBUG-32295: QTRY_COMPARE(settings->property("timeProperty").toDate(), QTime(6, 56, 34)); |
| 282 | QTRY_COMPARE(settings->property("sizeProperty" ).toSizeF(), QSizeF(56, 78)); |
| 283 | QTRY_COMPARE(settings->property("pointProperty" ).toPointF(), QPointF(56, 78)); |
| 284 | QTRY_COMPARE(settings->property("rectProperty" ).toRectF(), QRectF(5, 6, 7, 8)); |
| 285 | QTRY_COMPARE(settings->property("colorProperty" ).value<QColor>(), QColor(Qt::blue)); |
| 286 | QTRY_COMPARE(settings->property("fontProperty" ).value<QFont>(), boldFont); |
| 287 | |
| 288 | QSettings qs; |
| 289 | QTRY_COMPARE(qs.value("intProperty" ).toInt(), 456); |
| 290 | QTRY_COMPARE(qs.value("boolProperty" ).toBool(), false); |
| 291 | QTRY_COMPARE(qs.value("realProperty" ).toReal(), static_cast<qreal>(4.56)); |
| 292 | QTRY_COMPARE(qs.value("doubleProperty" ).toDouble(), static_cast<double>(6.78)); |
| 293 | QTRY_COMPARE(qs.value("stringProperty" ).toString(), QStringLiteral("bar" )); |
| 294 | QTRY_COMPARE(qs.value("urlProperty" ).toUrl(), QUrl("https://codereview.qt-project.org" )); |
| 295 | QTRY_COMPARE(qs.value("objectProperty" ).toMap(), keyValueMap("bar" , "baz" )); |
| 296 | QTRY_COMPARE(qs.value("intListProperty" ).toList(), QVariantList() << 4 << 5 << 6); |
| 297 | QTRY_COMPARE(qs.value("stringListProperty" ).toList(), QVariantList() << QStringLiteral("d" ) << QStringLiteral("e" ) << QStringLiteral("f" )); |
| 298 | QTRY_COMPARE(qs.value("objectListProperty" ).toList(), QVariantList() << keyValueMap("e" , "f" ) << keyValueMap("g" , "h" )); |
| 299 | QTRY_COMPARE(qs.value("dateProperty" ).toDate(), QDate(2010, 02, 01)); |
| 300 | // QTBUG-32295: QTRY_COMPARE(qs.value("timeProperty").toDate(), QTime(6, 56, 34)); |
| 301 | QTRY_COMPARE(qs.value("sizeProperty" ).toSizeF(), QSizeF(56, 78)); |
| 302 | QTRY_COMPARE(qs.value("pointProperty" ).toPointF(), QPointF(56, 78)); |
| 303 | QTRY_COMPARE(qs.value("rectProperty" ).toRectF(), QRectF(5, 6, 7, 8)); |
| 304 | QTRY_COMPARE(qs.value("colorProperty" ).value<QColor>(), QColor(Qt::blue)); |
| 305 | QTRY_COMPARE(qs.value("fontProperty" ).value<QFont>(), boldFont); |
| 306 | } |
| 307 | |
| 308 | void tst_QQmlSettings::aliases_data() |
| 309 | { |
| 310 | QTest::addColumn<QString>(name: "testFile" ); |
| 311 | QTest::newRow(dataTag: "qml" ) << "aliases.qml" ; |
| 312 | QTest::newRow(dataTag: "cpp" ) << "cpp-aliases.qml" ; |
| 313 | } |
| 314 | |
| 315 | void tst_QQmlSettings::aliases() |
| 316 | { |
| 317 | QFETCH(QString, testFile); |
| 318 | |
| 319 | QQmlEngine engine; |
| 320 | QQmlComponent component(&engine, testFileUrl(fileName: testFile)); |
| 321 | QScopedPointer<QObject> root(component.create()); |
| 322 | QVERIFY(root.data()); |
| 323 | QObject *settings = root->property(name: "settings" ).value<QObject *>(); |
| 324 | QVERIFY(settings); |
| 325 | |
| 326 | // default property values |
| 327 | QCOMPARE(root->property("intProperty" ).toInt(), 123); |
| 328 | QCOMPARE(root->property("boolProperty" ).toBool(), true); |
| 329 | QCOMPARE(root->property("realProperty" ).toReal(), static_cast<qreal>(1.23)); |
| 330 | QCOMPARE(root->property("doubleProperty" ).toDouble(), static_cast<double>(3.45)); |
| 331 | QCOMPARE(root->property("stringProperty" ).toString(), QStringLiteral("foo" )); |
| 332 | QCOMPARE(root->property("urlProperty" ).toUrl(), QUrl("http://www.qt-project.org" )); |
| 333 | QCOMPARE(root->property("objectProperty" ).toMap(), keyValueMap("foo" ,"bar" )); |
| 334 | QCOMPARE(root->property("intListProperty" ).toList(), QVariantList() << 1 << 2 << 3); |
| 335 | QCOMPARE(root->property("stringListProperty" ).toList(), QVariantList() << QStringLiteral("a" ) << QStringLiteral("b" ) << QStringLiteral("c" )); |
| 336 | QCOMPARE(root->property("objectListProperty" ).toList(), QVariantList() << keyValueMap("a" , "b" ) << keyValueMap("c" ,"d" )); |
| 337 | QCOMPARE(root->property("dateProperty" ).toDate(), QDate(2000, 01, 02)); |
| 338 | // QTBUG-32295: QCOMPARE(root->property("timeProperty").toDate(), QTime(12, 34, 56)); |
| 339 | QCOMPARE(root->property("sizeProperty" ).toSizeF(), QSizeF(12, 34)); |
| 340 | QCOMPARE(root->property("pointProperty" ).toPointF(), QPointF(12, 34)); |
| 341 | QCOMPARE(root->property("rectProperty" ).toRectF(), QRectF(1, 2, 3, 4)); |
| 342 | QCOMPARE(root->property("colorProperty" ).value<QColor>(), QColor(Qt::red)); |
| 343 | QCOMPARE(root->property("fontProperty" ).value<QFont>(), QFont()); |
| 344 | |
| 345 | // default settings values |
| 346 | QCOMPARE(settings->property("intProperty" ).toInt(), 123); |
| 347 | QCOMPARE(settings->property("boolProperty" ).toBool(), true); |
| 348 | QCOMPARE(settings->property("realProperty" ).toReal(), static_cast<qreal>(1.23)); |
| 349 | QCOMPARE(settings->property("doubleProperty" ).toDouble(), static_cast<double>(3.45)); |
| 350 | QCOMPARE(settings->property("stringProperty" ).toString(), QStringLiteral("foo" )); |
| 351 | QCOMPARE(settings->property("urlProperty" ).toUrl(), QUrl("http://www.qt-project.org" )); |
| 352 | QCOMPARE(settings->property("objectProperty" ).toMap(), keyValueMap("foo" ,"bar" )); |
| 353 | QCOMPARE(settings->property("intListProperty" ).toList(), QVariantList() << 1 << 2 << 3); |
| 354 | QCOMPARE(settings->property("stringListProperty" ).toList(), QVariantList() << QStringLiteral("a" ) << QStringLiteral("b" ) << QStringLiteral("c" )); |
| 355 | QCOMPARE(settings->property("objectListProperty" ).toList(), QVariantList() << keyValueMap("a" , "b" ) << keyValueMap("c" ,"d" )); |
| 356 | QCOMPARE(settings->property("dateProperty" ).toDate(), QDate(2000, 01, 02)); |
| 357 | // QTBUG-32295: QCOMPARE(settings->property("timeProperty").toDate(), QTime(12, 34, 56)); |
| 358 | QCOMPARE(settings->property("sizeProperty" ).toSizeF(), QSizeF(12, 34)); |
| 359 | QCOMPARE(settings->property("pointProperty" ).toPointF(), QPointF(12, 34)); |
| 360 | QCOMPARE(settings->property("rectProperty" ).toRectF(), QRectF(1, 2, 3, 4)); |
| 361 | QCOMPARE(settings->property("colorProperty" ).value<QColor>(), QColor(Qt::red)); |
| 362 | QCOMPARE(settings->property("fontProperty" ).value<QFont>(), QFont()); |
| 363 | |
| 364 | // change settings |
| 365 | QVERIFY(settings->setProperty("intProperty" , 456)); |
| 366 | QVERIFY(settings->setProperty("boolProperty" , false)); |
| 367 | QVERIFY(settings->setProperty("realProperty" , static_cast<qreal>(4.56))); |
| 368 | QVERIFY(settings->setProperty("doubleProperty" , static_cast<double>(6.78))); |
| 369 | QVERIFY(settings->setProperty("stringProperty" , QStringLiteral("bar" ))); |
| 370 | QVERIFY(settings->setProperty("urlProperty" , QUrl("https://codereview.qt-project.org" ))); |
| 371 | QVERIFY(settings->setProperty("objectProperty" , keyValueMap("bar" , "baz" ))); |
| 372 | QVERIFY(settings->setProperty("intListProperty" , QVariantList() << 4 << 5 << 6)); |
| 373 | QVERIFY(settings->setProperty("stringListProperty" , QVariantList() << QStringLiteral("d" ) << QStringLiteral("e" ) << QStringLiteral("f" ))); |
| 374 | QVERIFY(settings->setProperty("objectListProperty" , QVariantList() << keyValueMap("e" , "f" ) << keyValueMap("g" , "h" ))); |
| 375 | QVERIFY(settings->setProperty("dateProperty" , QDate(2010, 02, 01))); |
| 376 | // QTBUG-32295: QVERIFY(settings->setProperty("timeProperty", QTime(6, 56, 34))); |
| 377 | QVERIFY(settings->setProperty("sizeProperty" , QSizeF(56, 78))); |
| 378 | QVERIFY(settings->setProperty("pointProperty" , QPointF(56, 78))); |
| 379 | QVERIFY(settings->setProperty("rectProperty" , QRectF(5, 6, 7, 8))); |
| 380 | QVERIFY(settings->setProperty("colorProperty" , QColor(Qt::blue))); |
| 381 | QFont boldFont; boldFont.setBold(true); |
| 382 | QVERIFY(settings->setProperty("fontProperty" , boldFont)); |
| 383 | |
| 384 | QSettings qs; |
| 385 | QTRY_COMPARE(qs.value("intProperty" ).toInt(), 456); |
| 386 | QTRY_COMPARE(qs.value("boolProperty" ).toBool(), false); |
| 387 | QTRY_COMPARE(qs.value("realProperty" ).toReal(), static_cast<qreal>(4.56)); |
| 388 | QTRY_COMPARE(qs.value("doubleProperty" ).toDouble(), static_cast<double>(6.78)); |
| 389 | QTRY_COMPARE(qs.value("stringProperty" ).toString(), QStringLiteral("bar" )); |
| 390 | QTRY_COMPARE(qs.value("urlProperty" ).toUrl(), QUrl("https://codereview.qt-project.org" )); |
| 391 | QTRY_COMPARE(qs.value("objectProperty" ).toMap(), keyValueMap("bar" , "baz" )); |
| 392 | QTRY_COMPARE(qs.value("intListProperty" ).toList(), QVariantList() << 4 << 5 << 6); |
| 393 | QTRY_COMPARE(qs.value("stringListProperty" ).toList(), QVariantList() << QStringLiteral("d" ) << QStringLiteral("e" ) << QStringLiteral("f" )); |
| 394 | QTRY_COMPARE(qs.value("objectListProperty" ).toList(), QVariantList() << keyValueMap("e" , "f" ) << keyValueMap("g" , "h" )); |
| 395 | QTRY_COMPARE(qs.value("dateProperty" ).toDate(), QDate(2010, 02, 01)); |
| 396 | // QTBUG-32295: QTRY_COMPARE(qs.value("timeProperty").toDate(), QTime(6, 56, 34)); |
| 397 | QTRY_COMPARE(qs.value("sizeProperty" ).toSizeF(), QSizeF(56, 78)); |
| 398 | QTRY_COMPARE(qs.value("pointProperty" ).toPointF(), QPointF(56, 78)); |
| 399 | QTRY_COMPARE(qs.value("rectProperty" ).toRectF(), QRectF(5, 6, 7, 8)); |
| 400 | QTRY_COMPARE(qs.value("colorProperty" ).value<QColor>(), QColor(Qt::blue)); |
| 401 | QTRY_COMPARE(qs.value("fontProperty" ).value<QFont>(), boldFont); |
| 402 | } |
| 403 | |
| 404 | void tst_QQmlSettings::categories() |
| 405 | { |
| 406 | QQmlEngine engine; |
| 407 | QQmlComponent component(&engine, testFileUrl(fileName: "categories.qml" )); |
| 408 | |
| 409 | { |
| 410 | // initial state |
| 411 | QSettings qs; |
| 412 | QVERIFY(!qs.childGroups().contains("initialCategory" )); |
| 413 | QVERIFY(!qs.childGroups().contains("changedCategory" )); |
| 414 | QVERIFY(!qs.childKeys().contains("value" )); |
| 415 | } |
| 416 | |
| 417 | { |
| 418 | // load & write default values |
| 419 | QScopedPointer<QObject> settings(component.create()); |
| 420 | QVERIFY(settings.data()); |
| 421 | QCOMPARE(settings->property("category" ).toString(), QStringLiteral("initialCategory" )); |
| 422 | QCOMPARE(settings->property("value" ).toString(), QStringLiteral("initialValue" )); |
| 423 | } |
| 424 | |
| 425 | { |
| 426 | // verify written settings & change the value |
| 427 | QSettings qs; |
| 428 | QVERIFY(qs.childGroups().contains("initialCategory" )); |
| 429 | qs.beginGroup(prefix: "initialCategory" ); |
| 430 | QVERIFY(qs.childKeys().contains("value" )); |
| 431 | QCOMPARE(qs.value("value" ).toString(), QStringLiteral("initialValue" )); |
| 432 | qs.setValue(key: "value" , QStringLiteral("changedValue" )); |
| 433 | } |
| 434 | |
| 435 | { |
| 436 | // load changed value & change the category |
| 437 | QScopedPointer<QObject> settings(component.create()); |
| 438 | QVERIFY(settings.data()); |
| 439 | QCOMPARE(settings->property("category" ).toString(), QStringLiteral("initialCategory" )); |
| 440 | QCOMPARE(settings->property("value" ).toString(), QStringLiteral("changedValue" )); |
| 441 | QVERIFY(settings->setProperty("category" , QStringLiteral("changedCategory" ))); |
| 442 | } |
| 443 | |
| 444 | { |
| 445 | // verify written settings |
| 446 | QSettings qs; |
| 447 | QVERIFY(qs.childGroups().contains("changedCategory" )); |
| 448 | qs.beginGroup(prefix: "changedCategory" ); |
| 449 | QVERIFY(qs.childKeys().contains("value" )); |
| 450 | QCOMPARE(qs.value("value" ).toString(), QStringLiteral("changedValue" )); |
| 451 | qs.setValue(key: "value" , QStringLiteral("changedValue" )); |
| 452 | qs.endGroup(); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void tst_QQmlSettings::siblings() |
| 457 | { |
| 458 | QQmlEngine engine; |
| 459 | QQmlComponent component(&engine, testFileUrl(fileName: "siblings.qml" )); |
| 460 | delete component.create(); |
| 461 | |
| 462 | // verify setting aliases to destructed siblings |
| 463 | QSettings settings; |
| 464 | QCOMPARE(settings.value("alias1" ).toString(), QStringLiteral("value1" )); |
| 465 | QCOMPARE(settings.value("alias2" ).toString(), QStringLiteral("value2" )); |
| 466 | } |
| 467 | |
| 468 | void tst_QQmlSettings::initial() |
| 469 | { |
| 470 | QSettings qs; |
| 471 | qs.setValue(key: "value" , QStringLiteral("initial" )); |
| 472 | qs.sync(); |
| 473 | |
| 474 | QQmlEngine engine; |
| 475 | QQmlComponent component(&engine); |
| 476 | component.setData("import Qt.labs.settings 1.0; Settings { property var value }" , baseUrl: QUrl()); |
| 477 | QScopedPointer<QObject> settings(component.create()); |
| 478 | QVERIFY(settings.data()); |
| 479 | |
| 480 | // verify that the initial value from QSettings gets properly loaded |
| 481 | // even if no initial value is set in QML |
| 482 | QCOMPARE(settings->property("value" ).toString(), QStringLiteral("initial" )); |
| 483 | } |
| 484 | |
| 485 | void tst_QQmlSettings::() |
| 486 | { |
| 487 | #ifdef Q_OS_MACOS |
| 488 | QSKIP("macOS doesn't complain about empty application identifiers" ); |
| 489 | #endif |
| 490 | |
| 491 | QCoreApplication::setApplicationName(QString()); |
| 492 | QCoreApplication::setOrganizationName(QString()); |
| 493 | QCoreApplication::setOrganizationDomain(QString()); |
| 494 | |
| 495 | QTest::ignoreMessage(type: QtWarningMsg, messagePattern: QRegularExpression(".*QML Settings: Failed to initialize QSettings instance. Status code is: 1" )); |
| 496 | // Can't set an empty applicationName because QCoreApplication won't allow it, which is why it's not listed here. |
| 497 | QTest::ignoreMessage(type: QtWarningMsg, messagePattern: QRegularExpression(".*QML Settings: The following application identifiers have not been set: QVector\\(\"organizationName\", \"organizationDomain\"\\)" )); |
| 498 | |
| 499 | QQmlEngine engine; |
| 500 | QQmlComponent component(&engine, testFileUrl(fileName: "basic.qml" )); |
| 501 | QScopedPointer<QObject> root(component.create()); |
| 502 | QVERIFY(root.data()); |
| 503 | // The value of the QML property will be true because it defaults to it... |
| 504 | QVERIFY(root->property("success" ).toBool()); |
| 505 | QSettings settings; |
| 506 | // ... but the settings' value should be false because it was never loaded. |
| 507 | QVERIFY(!settings.value("success" ).toBool()); |
| 508 | } |
| 509 | |
| 510 | QTEST_MAIN(tst_QQmlSettings) |
| 511 | |
| 512 | #include "tst_qqmlsettings.moc" |
| 513 | |