| 1 |
/* |
| 2 |
* Copyright 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU 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 <QtCore/QRegularExpression> |
| 18 |
#include <QtCore/QString> |
| 19 |
#include <QtQml/QQmlComponent> |
| 20 |
#include <QtQml/QQmlContext> |
| 21 |
#include <QtQml/QQmlEngine> |
| 22 |
#include <QtTest/QtTest> |
| 23 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 24 |
#include <LomiriToolkit-Qt6/lomiritoolkitmodule.h> |
| 25 |
#else |
| 26 |
#include <LomiriToolkit/lomiritoolkitmodule.h> |
| 27 |
#endif |
| 28 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 29 |
#include <LomiriToolkit-Qt6/private/quickutils_p.h> |
| 30 |
#else |
| 31 |
#include <LomiriToolkit/private/quickutils_p.h> |
| 32 |
#endif |
| 33 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 34 |
#include <LomiriToolkit-Qt6/private/uclabel_p.h> |
| 35 |
#else |
| 36 |
#include <LomiriToolkit/private/uclabel_p.h> |
| 37 |
#endif |
| 38 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 39 |
#include <LomiriToolkit-Qt6/private/ucstyleditembase_p_p.h> |
| 40 |
#else |
| 41 |
#include <LomiriToolkit/private/ucstyleditembase_p_p.h> |
| 42 |
#endif |
| 43 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 44 |
#include <LomiriToolkit-Qt6/private/ucunits_p.h> |
| 45 |
#else |
| 46 |
#include <LomiriToolkit/private/ucunits_p.h> |
| 47 |
#endif |
| 48 |
#define private public |
| 49 |
#define protected public |
| 50 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 51 |
#include <LomiriToolkit-Qt6/private/uctheme_p.h> |
| 52 |
#else |
| 53 |
#include <LomiriToolkit/private/uctheme_p.h> |
| 54 |
#endif |
| 55 |
#undef protected |
| 56 |
#undef private |
| 57 |
|
| 58 |
#include "uctestcase.h" |
| 59 |
|
| 60 |
UT_USE_NAMESPACE |
| 61 |
|
| 62 |
class ThemeTestCase : public LomiriTestCase |
| 63 |
{ |
| 64 |
Q_OBJECT |
| 65 |
public: |
| 66 |
ThemeTestCase(const QString& file, bool assertOnFailure = true, QWindow* parent = 0) |
| 67 |
: LomiriTestCase(file, QQuickView::SizeViewToRootObject, assertOnFailure, parent) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
~ThemeTestCase() |
| 72 |
{ |
| 73 |
// restore theme before quitting |
| 74 |
if (!rootContext()) { |
| 75 |
return; |
| 76 |
} |
| 77 |
UCTheme *theme = globalTheme(); |
| 78 |
if (theme) { |
| 79 |
theme->resetName(); |
| 80 |
} else { |
| 81 |
qWarning() << "No theme instance found!"; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void setGlobalTheme(const QString &theme) |
| 86 |
{ |
| 87 |
UCTheme *rootTheme = globalTheme(); |
| 88 |
QVERIFY(rootTheme); |
| 89 |
rootTheme->setName(theme); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
void setTheme(const QString &theme, QQuickItem *watchedItem) |
| 94 |
{ |
| 95 |
QSignalSpy spy(watchedItem, SIGNAL(themeChanged())); |
| 96 |
rootObject()->setProperty("themeName", theme); |
| 97 |
waitForSignal(&spy); |
| 98 |
} |
| 99 |
|
| 100 |
UCTheme *theme() |
| 101 |
{ |
| 102 |
return rootObject()->property("theme").value<UCTheme*>(); |
| 103 |
} |
| 104 |
|
| 105 |
UCTheme *globalTheme() |
| 106 |
{ |
| 107 |
return rootContext()->contextProperty("theme").value<UCTheme*>(); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
class tst_Subtheming : public QObject |
| 112 |
{ |
| 113 |
Q_OBJECT |
| 114 |
private: |
| 115 |
QString m_xdgDataPath; |
| 116 |
QString m_themesPath; |
| 117 |
|
| 118 |
private Q_SLOTS: |
| 119 |
void initTestCase() |
| 120 |
{ |
| 121 |
m_xdgDataPath = QLatin1String(getenv("XDG_DATA_DIRS")); |
| 122 |
m_themesPath = QLatin1String(getenv("LOMIRI_UI_TOOLKIT_THEMES_PATH")); |
| 123 |
qputenv("SUPPRESS_DEPRECATED_NOTE", "yes"); |
| 124 |
} |
| 125 |
|
| 126 |
void cleanup() |
| 127 |
{ |
| 128 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", m_themesPath.toLatin1()); |
| 129 |
qputenv("XDG_DATA_DIRS", m_xdgDataPath.toLocal8Bit()); |
| 130 |
UCTheme::previousVersion = 0; |
| 131 |
} |
| 132 |
|
| 133 |
void test_default_theme() |
| 134 |
{ |
| 135 |
QQmlEngine engine; |
| 136 |
LomiriToolkitModule::initializeContextProperties(&engine); |
| 137 |
UCTheme::defaultTheme(&engine); |
| 138 |
} |
| 139 |
|
| 140 |
void test_default_name() |
| 141 |
{ |
| 142 |
UCTheme theme; |
| 143 |
QCOMPARE(theme.name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 144 |
} |
| 145 |
|
| 146 |
void test_name_set() |
| 147 |
{ |
| 148 |
QTest::ignoreMessage(QtWarningMsg, "Theme not found: \"MyBeautifulTheme\""); |
| 149 |
|
| 150 |
QQmlEngine engine; |
| 151 |
LomiriToolkitModule::initializeContextProperties(&engine); |
| 152 |
UCTheme theme; |
| 153 |
QQmlEngine::setContextForObject(&theme, engine.rootContext()); |
| 154 |
theme.setName("MyBeautifulTheme"); |
| 155 |
QCOMPARE(theme.name(), QString("MyBeautifulTheme")); |
| 156 |
} |
| 157 |
|
| 158 |
void test_name_reset() |
| 159 |
{ |
| 160 |
QQmlEngine engine; |
| 161 |
LomiriToolkitModule::initializeContextProperties(&engine); |
| 162 |
UCTheme theme; |
| 163 |
QQmlEngine::setContextForObject(&theme, engine.rootContext()); |
| 164 |
theme.setName("Lomiri.Components.Themes.SuruDark"); |
| 165 |
QCOMPARE(theme.name(), QString("Lomiri.Components.Themes.SuruDark")); |
| 166 |
// reset |
| 167 |
theme.resetName(); |
| 168 |
QCOMPARE(theme.name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 169 |
} |
| 170 |
|
| 171 |
void test_create_without_engine() |
| 172 |
{ |
| 173 |
#if !defined(LOMIRITOOLKIT_NO_NAMESPACE) |
| 174 |
QTest::ignoreMessage(QtCriticalMsg, "The item LomiriToolkit::UCLabel was created without a valid QML Engine. Styling will not be possible."); |
| 175 |
#else |
| 176 |
QTest::ignoreMessage(QtCriticalMsg, "The item UCLabel was created without a valid QML Engine. Styling will not be possible."); |
| 177 |
#endif |
| 178 |
QScopedPointer<UCLabel> item(new UCLabel); |
| 179 |
QVERIFY(!item->getTheme()); |
| 180 |
} |
| 181 |
|
| 182 |
void test_default_theme_cleared_with_engine_bug1527546() |
| 183 |
{ |
| 184 |
qputenv("QV4_FORCE_INTERPRETER", "1"); |
| 185 |
qputenv("QV4_MM_AGGRESSIVE_GC", "1"); |
| 186 |
for (int i = 0; i < 2; i++) |
| 187 |
{ |
| 188 |
ThemeTestCase *view = new ThemeTestCase("DefaultTheme.qml"); |
| 189 |
QVERIFY(view->globalTheme()); |
| 190 |
QVERIFY(UCTheme::defaultTheme(view->engine())); |
| 191 |
QObjectCleanupHandler themeCleanup; |
| 192 |
themeCleanup.add(UCTheme::defaultTheme(view->engine())); |
| 193 |
delete view; |
| 194 |
QVERIFY(themeCleanup.isEmpty()); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void test_multiple_view_instances_bug1527546() |
| 199 |
{ |
| 200 |
qputenv("QV4_FORCE_INTERPRETER", "1"); |
| 201 |
qputenv("QV4_MM_AGGRESSIVE_GC", "1"); |
| 202 |
|
| 203 |
QScopedPointer<ThemeTestCase> view1(new ThemeTestCase("DefaultTheme.qml")); |
| 204 |
QScopedPointer<ThemeTestCase> view2(new ThemeTestCase("DefaultTheme.qml")); |
| 205 |
|
| 206 |
// the two views must have different default themes |
| 207 |
QVERIFY(view1->globalTheme() != view2->globalTheme()); |
| 208 |
} |
| 209 |
|
| 210 |
void test_multiple_themes_on_engine_bug1527546() |
| 211 |
{ |
| 212 |
qputenv("QV4_FORCE_INTERPRETER", "1"); |
| 213 |
qputenv("QV4_MM_AGGRESSIVE_GC", "1"); |
| 214 |
|
| 215 |
QQmlEngine engine; |
| 216 |
LomiriToolkitModule::initializeContextProperties(&engine); |
| 217 |
UCTheme *theme0 = UCTheme::defaultTheme(&engine); |
| 218 |
|
| 219 |
UCTheme *theme1 = new UCTheme(&engine); |
| 220 |
QVERIFY(theme0 != theme1); |
| 221 |
} |
| 222 |
|
| 223 |
void test_create_style_component_data() { |
| 224 |
QTest::addColumn<QString>("styleName"); |
| 225 |
QTest::addColumn<QString>("parentName"); |
| 226 |
QTest::addColumn<bool>("success"); |
| 227 |
QTest::addColumn<QString>("warning"); |
| 228 |
QTest::newRow("Existing style") << "TestStyle" << "SimpleItem.qml" << true << QString(); |
| 229 |
QTest::newRow("Non existing style") << "NotExistingTestStyle" << "SimpleItem.qml" << false << "QML SimpleItem: Warning: Style NotExistingTestStyle.qml not found in theme TestModule.TestTheme"; |
| 230 |
} |
| 231 |
void test_create_style_component() |
| 232 |
{ |
| 233 |
QFETCH(QString, styleName); |
| 234 |
QFETCH(QString, parentName); |
| 235 |
QFETCH(bool, success); |
| 236 |
QFETCH(QString, warning); |
| 237 |
|
| 238 |
if (!warning.isEmpty()) { |
| 239 |
ThemeTestCase::ignoreWarning(parentName, 20, 1, warning); |
| 240 |
} |
| 241 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", "./themes"); |
| 242 |
|
| 243 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase(parentName)); |
| 244 |
view->setTheme("TestModule.TestTheme", view->rootObject()); |
| 245 |
view->rootObject()->setProperty("styleName", styleName); |
| 246 |
QQuickItem *testStyle = view->rootObject()->findChild<QQuickItem*>("TestStyle"); |
| 247 |
QCOMPARE(testStyle != NULL, success); |
| 248 |
} |
| 249 |
|
| 250 |
void test_relative_theme_paths_environment_variables_data() |
| 251 |
{ |
| 252 |
QTest::addColumn<QString>("themePath"); |
| 253 |
QTest::addColumn<QString>("xdgPath"); |
| 254 |
QTest::addColumn<QString>("theme"); |
| 255 |
QTest::addColumn<QString>("style"); |
| 256 |
QTest::addColumn<QString>("warning"); |
| 257 |
QTest::addColumn<bool>("success"); |
| 258 |
|
| 259 |
QTest::newRow("One toolkit theme path") |
| 260 |
<< "./themes" << "" |
| 261 |
<< "TestModule.TestTheme" << "TestStyle" |
| 262 |
<< "" << true; |
| 263 |
QTest::newRow("Two toolkit theme paths") |
| 264 |
<< "./themes:./themes/TestModule" << "" |
| 265 |
<< "CustomTheme" << "TestStyle" |
| 266 |
<< "" << true; |
| 267 |
QTest::newRow("One XDG path") |
| 268 |
<< "" << "./themes" |
| 269 |
<< "TestModule.TestTheme" << "TestStyle" |
| 270 |
<< "" << true; |
| 271 |
QTest::newRow("Two XDG paths") |
| 272 |
<< "" << "./themes:./themes/TestModule" |
| 273 |
<< "CustomTheme" << "TestStyle" |
| 274 |
<< "" << true; |
| 275 |
QTest::newRow("No variables") |
| 276 |
<< "" << "" |
| 277 |
<< QString() << "TestStyle" |
| 278 |
<< "QML SimpleItem: Warning: Style TestStyle.qml not found in theme Lomiri.Components.Themes.Ambiance" << false; |
| 279 |
} |
| 280 |
|
| 281 |
void test_relative_theme_paths_environment_variables() |
| 282 |
{ |
| 283 |
QFETCH(QString, themePath); |
| 284 |
QFETCH(QString, xdgPath); |
| 285 |
QFETCH(QString, theme); |
| 286 |
QFETCH(QString, style); |
| 287 |
QFETCH(QString, warning); |
| 288 |
QFETCH(bool, success); |
| 289 |
|
| 290 |
if (!warning.isEmpty()) { |
| 291 |
ThemeTestCase::ignoreWarning("SimpleItem.qml", 20, 1, warning); |
| 292 |
} |
| 293 |
|
| 294 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", themePath.toLocal8Bit()); |
| 295 |
qputenv("XDG_DATA_DIRS", xdgPath.toLocal8Bit()); |
| 296 |
|
| 297 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SimpleItem.qml")); |
| 298 |
if (!theme.isEmpty()) { |
| 299 |
view->setTheme(theme, view->rootObject()); |
| 300 |
} |
| 301 |
view->rootObject()->setProperty("styleName", style); |
| 302 |
QQuickItem *styleItem = view->rootObject()->findChild<QQuickItem*>("TestStyle"); |
| 303 |
QCOMPARE(styleItem != NULL, success); |
| 304 |
} |
| 305 |
|
| 306 |
void test_import_path() |
| 307 |
{ |
| 308 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 309 |
if (!QFile(QLibraryInfo::path(QLibraryInfo::Qml2ImportsPath) + "/Lomiri/Components").exists()) |
| 310 |
#else |
| 311 |
if (!QFile(QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath) + "/Lomiri/Components").exists()) |
| 312 |
#endif |
| 313 |
QSKIP("This can only be tested if the UITK is installed"); |
| 314 |
|
| 315 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 316 |
qputenv("XDG_DATA_DIRS", ""); |
| 317 |
qputenv("QML2_IMPORT_PATH", ""); |
| 318 |
|
| 319 |
UCTheme theme; |
| 320 |
QCOMPARE(theme.name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 321 |
} |
| 322 |
|
| 323 |
void test_bogus_import_path_set() |
| 324 |
{ |
| 325 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 326 |
if (!QFile(QLibraryInfo::path(QLibraryInfo::Qml2ImportsPath) + "/Lomiri/Components").exists()) |
| 327 |
#else |
| 328 |
if (!QFile(QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath) + "/Lomiri/Components").exists()) |
| 329 |
#endif |
| 330 |
QSKIP("This can only be tested if the UITK is installed"); |
| 331 |
|
| 332 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 333 |
qputenv("XDG_DATA_DIRS", ""); |
| 334 |
qputenv("QML2_IMPORT_PATH", "/no/plugins/here"); |
| 335 |
|
| 336 |
UCTheme theme; |
| 337 |
QCOMPARE(theme.name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 338 |
} |
| 339 |
|
| 340 |
void test_multiple_import_paths_set() |
| 341 |
{ |
| 342 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 343 |
if (!QFile(QLibraryInfo::path(QLibraryInfo::Qml2ImportsPath) + "/Lomiri/Components").exists()) |
| 344 |
#else |
| 345 |
if (!QFile(QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath) + "/Lomiri/Components").exists()) |
| 346 |
#endif |
| 347 |
QSKIP("This can only be tested if the UITK is installed"); |
| 348 |
|
| 349 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 350 |
qputenv("XDG_DATA_DIRS", ""); |
| 351 |
qputenv("QML2_IMPORT_PATH", "/no/plugins/here"); |
| 352 |
|
| 353 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SimpleItem.qml")); |
| 354 |
view->setTheme("CustomModule.TestTheme", view->rootObject()); |
| 355 |
} |
| 356 |
|
| 357 |
void test_theme_not_root_theme() |
| 358 |
{ |
| 359 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", "."); |
| 360 |
|
| 361 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SimpleItem.qml")); |
| 362 |
view->setTheme("CustomModule.TestTheme", view->rootObject()); |
| 363 |
UCTheme *theme = view->theme(); |
| 364 |
UCTheme *globalTheme = view->globalTheme(); |
| 365 |
QVERIFY(theme); |
| 366 |
QVERIFY(globalTheme); |
| 367 |
QVERIFY(theme != globalTheme); |
| 368 |
} |
| 369 |
|
| 370 |
void test_theme_reset_name() |
| 371 |
{ |
| 372 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", "./themes"); |
| 373 |
|
| 374 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SimpleItem.qml")); |
| 375 |
view->setTheme("TestModule.TestTheme", view->rootObject()); |
| 376 |
UCTheme *theme = view->theme(); |
| 377 |
QCOMPARE(theme->name(), QString("TestModule.TestTheme")); |
| 378 |
// reset |
| 379 |
theme->resetName(); |
| 380 |
QCOMPARE(theme->name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 381 |
} |
| 382 |
|
| 383 |
void test_parent_change_when_assinged() |
| 384 |
{ |
| 385 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("DynamicAssignment.qml")); |
| 386 |
UCTheme *testSet = view->findItem<UCTheme*>("testSet"); |
| 387 |
UCStyledItemBase *testItem = view->findItem<UCStyledItemBase*>("testItem"); |
| 388 |
UCStyledItemBase *mainItem = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 389 |
|
| 390 |
QSignalSpy parentChangeSpy(testSet, SIGNAL(parentThemeChanged())); |
| 391 |
testItem->setTheme(testSet); |
| 392 |
parentChangeSpy.wait(200); |
| 393 |
QCOMPARE(parentChangeSpy.count(), 1); |
| 394 |
// test if the parent is correct |
| 395 |
QCOMPARE(testSet->parentTheme(), mainItem->getTheme()); |
| 396 |
} |
| 397 |
|
| 398 |
void test_parent_set_reset_triggers_parent_change() |
| 399 |
{ |
| 400 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ParentChanges.qml")); |
| 401 |
UCTheme *testSet = view->findItem<UCTheme*>("testSet"); |
| 402 |
UCTheme *topTheme = view->findItem<UCTheme*>("top"); |
| 403 |
UCStyledItemBase *mainItem = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 404 |
QCOMPARE(testSet->parentTheme(), topTheme); |
| 405 |
|
| 406 |
// reset mainItem's theme should trigger parentChanged on testSet |
| 407 |
QSignalSpy parentChangeSpy(testSet, SIGNAL(parentThemeChanged())); |
| 408 |
mainItem->resetTheme(); |
| 409 |
parentChangeSpy.wait(200); |
| 410 |
QCOMPARE(parentChangeSpy.count(), 1); |
| 411 |
QCOMPARE(mainItem->getTheme(), UCTheme::defaultTheme(view->engine())); |
| 412 |
QCOMPARE(testSet->parentTheme(), mainItem->getTheme()); |
| 413 |
} |
| 414 |
|
| 415 |
void test_parent_set_namechange_triggers_parent_change() |
| 416 |
{ |
| 417 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ParentChanges.qml")); |
| 418 |
UCTheme *testSet = view->findItem<UCTheme*>("testSet"); |
| 419 |
UCStyledItemBase *mainItem = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 420 |
|
| 421 |
// change mainItem.theme.name should trigger parentChanged on testSet |
| 422 |
QSignalSpy parentChangeSpy(testSet, SIGNAL(parentThemeChanged())); |
| 423 |
QSignalSpy themeChangeSpy(mainItem, SIGNAL(themeChanged())); |
| 424 |
mainItem->getTheme()->setName("Lomiri.Components.Themes.SuruDark"); |
| 425 |
LomiriTestCase::waitForSignal(&themeChangeSpy); |
| 426 |
parentChangeSpy.wait(200); |
| 427 |
QCOMPARE(parentChangeSpy.count(), 1); |
| 428 |
QCOMPARE(testSet->parentTheme(), mainItem->getTheme()); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
// testing global context property 'theme' name changes |
| 433 |
void test_set_global_theme_name() |
| 434 |
{ |
| 435 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("TestMain.qml")); |
| 436 |
QSignalSpy themeChaneSpy(view->rootObject(), SIGNAL(themeChanged())); |
| 437 |
view->setGlobalTheme("Lomiri.Components.Themes.SuruDark"); |
| 438 |
LomiriTestCase::waitForSignal(&themeChaneSpy); |
| 439 |
// verify theme changes |
| 440 |
UCStyledItemBase *styled = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 441 |
QVERIFY(styled); |
| 442 |
QCOMPARE(styled->getTheme()->name(), QString("Lomiri.Components.Themes.SuruDark")); |
| 443 |
// a deeper item's style name |
| 444 |
styled = view->findItem<UCStyledItemBase*>("secondLevelStyled"); |
| 445 |
QCOMPARE(styled->getTheme()->name(), QString("Lomiri.Components.Themes.SuruDark")); |
| 446 |
} |
| 447 |
|
| 448 |
// changing StyledItem.theme.name for different items within a tree where |
| 449 |
// no sub-theming si applied |
| 450 |
void test_set_styleditem_theme_name_data() |
| 451 |
{ |
| 452 |
QTest::addColumn<QString>("itemName"); |
| 453 |
|
| 454 |
QTest::newRow("Subtheming enabled, set firstLevelStyled") << "firstLevelStyled"; |
| 455 |
QTest::newRow("Subtheming enabled, set secondLevelStyled") << "secondLevelStyled"; |
| 456 |
} |
| 457 |
void test_set_styleditem_theme_name() |
| 458 |
{ |
| 459 |
QFETCH(QString, itemName); |
| 460 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("TestMain.qml")); |
| 461 |
// change theme name (theme) |
| 462 |
UCStyledItemBase *styled = view->findItem<UCStyledItemBase*>(itemName); |
| 463 |
QSignalSpy themeChangeSpy(styled, SIGNAL(themeChanged())); |
| 464 |
styled->getTheme()->setName("Lomiri.Components.Themes.SuruDark"); |
| 465 |
LomiriTestCase::waitForSignal(&themeChangeSpy); |
| 466 |
UCTheme *theme = view->globalTheme(); |
| 467 |
QCOMPARE(theme->name(), QString("Lomiri.Components.Themes.SuruDark")); |
| 468 |
} |
| 469 |
|
| 470 |
void test_theme_change_data() |
| 471 |
{ |
| 472 |
QTest::addColumn<QString>("themeName"); |
| 473 |
QTest::addColumn<QString>("applyOnItem"); // empty means root object |
| 474 |
QTest::addColumn<QStringList>("testItems"); // list of objectNames to be tested |
| 475 |
QTest::addColumn<QStringList>("expectedStyleNames"); // list of expected style names on the tested items |
| 476 |
QTest::addColumn< QList<bool> >("sameTheme"); // list of theme vs styledItem.theme camparison results expected |
| 477 |
|
| 478 |
QString suru("Lomiri.Components.Themes.SuruDark"); |
| 479 |
QString ambiance("Lomiri.Components.Themes.Ambiance"); |
| 480 |
QTest::newRow("Suru on mainStyled") |
| 481 |
<< suru << "" |
| 482 |
<< (QStringList() << "firstLevelStyled" << "secondLevelStyled" << "firstLevelLoaderStyled") |
| 483 |
<< (QStringList() << suru << suru << suru) |
| 484 |
<< (QList<bool>() << true << true << true); |
| 485 |
QTest::newRow("Suru on firstLevelStyled") |
| 486 |
<< suru << "firstLevelStyled" |
| 487 |
<< (QStringList() << "secondLevelStyled" << "firstLevelLoaderStyled") |
| 488 |
<< (QStringList() << suru << ambiance) |
| 489 |
<< (QList<bool>() << true << false); |
| 490 |
} |
| 491 |
void test_theme_change() |
| 492 |
{ |
| 493 |
QFETCH(QString, themeName); |
| 494 |
QFETCH(QString, applyOnItem); |
| 495 |
QFETCH(QStringList, testItems); |
| 496 |
QFETCH(QStringList, expectedStyleNames); |
| 497 |
QFETCH(QList<bool>, sameTheme); |
| 498 |
|
| 499 |
bool validTest = (testItems.count() == expectedStyleNames.count()) && |
| 500 |
(testItems.count() == sameTheme.count()); |
| 501 |
QVERIFY2(validTest, "testItems, expectedStyleNames, and sametheme must have same amount of values"); |
| 502 |
|
| 503 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("TestStyleChange.qml")); |
| 504 |
UCTheme *theme = view->findItem<UCTheme*>("Theme"); |
| 505 |
UCStyledItemBase *styledItem = applyOnItem.isEmpty() ? |
| 506 |
qobject_cast<UCStyledItemBase*>(view->rootObject()) : |
| 507 |
view->findItem<UCStyledItemBase*>(applyOnItem); |
| 508 |
QVERIFY(styledItem); |
| 509 |
// the theme is declared, but should not be set yet!!! |
| 510 |
QVERIFY2(styledItem->getTheme() != theme, "ThemeSettings should not be set yet!"); |
| 511 |
theme->setName(themeName); |
| 512 |
// set the style on the item |
| 513 |
QSignalSpy themeChangeSpy(styledItem, SIGNAL(themeChanged())); |
| 514 |
styledItem->setTheme(theme); |
| 515 |
LomiriTestCase::waitForSignal(&themeChangeSpy); |
| 516 |
// test on the items |
| 517 |
for (int i = 0; i < testItems.count(); i++) { |
| 518 |
QString itemName = testItems[i]; |
| 519 |
QString themeName = expectedStyleNames[i]; |
| 520 |
bool success = sameTheme[i]; |
| 521 |
styledItem = view->findItem<UCStyledItemBase*>(itemName); |
| 522 |
QCOMPARE(styledItem->getTheme() == theme, success); |
| 523 |
QCOMPARE(styledItem->getTheme()->name(), themeName); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
void test_reparent_styled_data() |
| 528 |
{ |
| 529 |
QTest::addColumn<QString>("suruStyledItem"); |
| 530 |
QTest::addColumn<QString>("testStyledItem"); |
| 531 |
QTest::addColumn<QStringList>("testStyledItemThemes"); |
| 532 |
QTest::addColumn<QString>("sourceItemName"); |
| 533 |
QTest::addColumn<QString>("newParentItemName"); |
| 534 |
|
| 535 |
QString suru("Lomiri.Components.Themes.SuruDark"); |
| 536 |
QString ambiance("Lomiri.Components.Themes.Ambiance"); |
| 537 |
QTest::newRow("Move item out of Suru sub-themed") |
| 538 |
<< "firstLevelStyled" << "secondLevelStyled" |
| 539 |
<< (QStringList() << ambiance << suru << ambiance) |
| 540 |
<< "secondLevelStyled" << "firstLevelLoader"; |
| 541 |
QTest::newRow("Move item into Suru sub-themed") |
| 542 |
<< "firstLevelStyled" << "firstLevelLoaderStyled" |
| 543 |
<< (QStringList() << ambiance << ambiance << suru) |
| 544 |
<< "firstLevelLoader" << "secondLevelStyled"; |
| 545 |
QTest::newRow("Move dynamic item out of Suru sub-themed") |
| 546 |
<< "firstLevelStyled" << "thirdLevelLoaderStyled" |
| 547 |
<< (QStringList() << ambiance << suru << ambiance) |
| 548 |
<< "secondLevelStyled" << "firstLevelLoader"; |
| 549 |
} |
| 550 |
void test_reparent_styled() |
| 551 |
{ |
| 552 |
QFETCH(QString, suruStyledItem); |
| 553 |
QFETCH(QString, testStyledItem); |
| 554 |
QFETCH(QStringList, testStyledItemThemes); |
| 555 |
QFETCH(QString, sourceItemName); |
| 556 |
QFETCH(QString, newParentItemName); |
| 557 |
|
| 558 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("TestStyleChange.qml")); |
| 559 |
UCStyledItemBase *testItem = view->findItem<UCStyledItemBase*>(testStyledItem); |
| 560 |
QCOMPARE(testItem->getTheme()->name(), testStyledItemThemes[0]); |
| 561 |
|
| 562 |
UCTheme *theme = view->findItem<UCTheme*>("Theme"); |
| 563 |
theme->setName("Lomiri.Components.Themes.SuruDark"); |
| 564 |
UCStyledItemBase *suruItem = view->findItem<UCStyledItemBase*>(suruStyledItem); |
| 565 |
QSignalSpy themeChangeSpy(suruItem, SIGNAL(themeChanged())); |
| 566 |
suruItem->setTheme(theme); |
| 567 |
LomiriTestCase::waitForSignal(&themeChangeSpy); |
| 568 |
QCOMPARE(testItem->getTheme()->name(), testStyledItemThemes[1]); |
| 569 |
|
| 570 |
// get items and reparent |
| 571 |
QQuickItem *sourceItem = view->findItem<QQuickItem*>(sourceItemName); |
| 572 |
QQuickItem *newParentItem = view->findItem<QQuickItem*>(newParentItemName); |
| 573 |
QVERIFY(sourceItem != newParentItem); |
| 574 |
QVERIFY(sourceItem->parentItem() != newParentItem); |
| 575 |
|
| 576 |
QSignalSpy themeSpy(testItem, SIGNAL(themeChanged())); |
| 577 |
sourceItem->setParentItem(newParentItem); |
| 578 |
themeSpy.wait(200); |
| 579 |
QCOMPARE(testItem->getTheme()->name(), testStyledItemThemes[2]); |
| 580 |
QCOMPARE(themeSpy.count(), 1); |
| 581 |
} |
| 582 |
|
| 583 |
void test_theme_name() |
| 584 |
{ |
| 585 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("DifferentThemes.qml")); |
| 586 |
UCStyledItemBase *main = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 587 |
QVERIFY(main); |
| 588 |
UCStyledItemBase *test = view->findItem<UCStyledItemBase*>("firstLevelStyled"); |
| 589 |
QCOMPARE(main->getTheme()->name(), |
| 590 |
test->getTheme()->name()); |
| 591 |
} |
| 592 |
|
| 593 |
void test_no_change_in_other_suru_dark() |
| 594 |
{ |
| 595 |
QSKIP("FIXME: Failing with Qt 5.12"); |
| 596 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SameNamedPaletteSettings.qml")); |
| 597 |
UCTheme *firstTheme = view->findItem<UCTheme*>("firstTheme"); |
| 598 |
UCTheme *secondTheme = view->findItem<UCTheme*>("secondTheme"); |
| 599 |
QVERIFY(firstTheme->getPaletteColor("normal", "background") != secondTheme->getPaletteColor("normal", "background")); |
| 600 |
} |
| 601 |
|
| 602 |
void test_keep_palette_value_when_theme_changes() |
| 603 |
{ |
| 604 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ChangePaletteValueWhenParentChanges.qml")); |
| 605 |
UCTheme *firstTheme = view->findItem<UCTheme*>("firstTheme"); |
| 606 |
|
| 607 |
QCOMPARE(firstTheme->getPaletteColor("normal", "background"), QColor("blue")); |
| 608 |
// change the theme |
| 609 |
QSignalSpy themeChaneSpy(view->rootObject(), SIGNAL(themeChanged())); |
| 610 |
view->setGlobalTheme("Lomiri.Components.Themes.SuruDark"); |
| 611 |
LomiriTestCase::waitForSignal(&themeChaneSpy); |
| 612 |
QCOMPARE(firstTheme->getPaletteColor("normal", "background"), QColor("blue")); |
| 613 |
} |
| 614 |
|
| 615 |
void test_change_default_palette_in_children_kept_after_child_deletion() |
| 616 |
{ |
| 617 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ChangeDefaultPaletteInChildren.qml")); |
| 618 |
QQuickItem *loader = view->findItem<QQuickItem*>("loader"); |
| 619 |
UCStyledItemBase *main = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 620 |
UCTheme *mainSet = main->getTheme(); |
| 621 |
QQuickItem *item = loader->property("item").value<QQuickItem*>(); |
| 622 |
QVERIFY(item); |
| 623 |
|
| 624 |
QCOMPARE(mainSet->getPaletteColor("normal", "background"), QColor("blue")); |
| 625 |
// unload component |
| 626 |
QSignalSpy itemSpy(item, SIGNAL(destroyed())); |
| 627 |
loader->setProperty("sourceComponent", QVariant()); |
| 628 |
itemSpy.wait(); |
| 629 |
// palette stays! |
| 630 |
QCOMPARE(mainSet->getPaletteColor("normal", "background"), QColor("blue")); |
| 631 |
} |
| 632 |
|
| 633 |
void test_reset_palette_to_theme_default() |
| 634 |
{ |
| 635 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ChangePaletteValueWhenParentChanges.qml")); |
| 636 |
UCTheme *firstTheme = view->findItem<UCTheme*>("firstTheme"); |
| 637 |
QColor prevColor = firstTheme->getPaletteColor("normal", "background"); |
| 638 |
|
| 639 |
QVERIFY(prevColor.isValid()); |
| 640 |
// reset palette |
| 641 |
QSignalSpy spy(firstTheme, SIGNAL(paletteChanged())); |
| 642 |
firstTheme->setPalette(NULL); |
| 643 |
spy.wait(200); |
| 644 |
QVERIFY(firstTheme->getPaletteColor("normal", "background") != prevColor); |
| 645 |
} |
| 646 |
|
| 647 |
void test_multiple_palette_instances() |
| 648 |
{ |
| 649 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("MultiplePaletteInstances.qml")); |
| 650 |
UCTheme *theme = view->findItem<UCTheme*>("theme"); |
| 651 |
QObject *palette1 = view->findItem<QObject*>("palette1"); |
| 652 |
QObject *palette2 = view->findItem<QObject*>("palette2"); |
| 653 |
QColor prevColor = theme->getPaletteColor("normal", "background"); |
| 654 |
|
| 655 |
// set the first palette |
| 656 |
QSignalSpy spy(theme, SIGNAL(paletteChanged())); |
| 657 |
theme->setPalette(palette1); |
| 658 |
spy.wait(200); |
| 659 |
QVERIFY(prevColor != theme->getPaletteColor("normal", "background")); |
| 660 |
QCOMPARE(theme->getPaletteColor("normal", "background"), QColor("blue")); |
| 661 |
|
| 662 |
spy.clear(); |
| 663 |
theme->setPalette(palette2); |
| 664 |
spy.wait(200); |
| 665 |
QVERIFY(prevColor != theme->getPaletteColor("normal", "background")); |
| 666 |
QCOMPARE(theme->getPaletteColor("normal", "background"), QColor("pink")); |
| 667 |
} |
| 668 |
|
| 669 |
void test_dynamic_palette() |
| 670 |
{ |
| 671 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("DynamicPalette.qml")); |
| 672 |
UCTheme *theme = view->findItem<UCTheme*>("theme"); |
| 673 |
QQuickItem *loader = view->findItem<QQuickItem*>("paletteLoader"); |
| 674 |
|
| 675 |
QVERIFY(theme->getPaletteColor("normal", "background") == QColor("blue")); |
| 676 |
|
| 677 |
QSignalSpy spy(loader, SIGNAL(itemChanged())); |
| 678 |
loader->setProperty("sourceComponent", QVariant()); |
| 679 |
spy.wait(200); |
| 680 |
// unloaded palette by Loader should remove palette configuration |
| 681 |
QVERIFY(theme->getPaletteColor("normal", "background") != QColor("blue")); |
| 682 |
} |
| 683 |
|
| 684 |
void test_invalid_palette_object() |
| 685 |
{ |
| 686 |
ThemeTestCase::ignoreWarning("InvalidPalette.qml", 22, 20, "QML QtObject: Not a Palette component."); |
| 687 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("InvalidPalette.qml")); |
| 688 |
} |
| 689 |
|
| 690 |
void test_invalid_palette_value() |
| 691 |
{ |
| 692 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 693 |
QString url(QUrl::fromLocalFile(QFileInfo("themes/BuggyTheme/2.0/Palette.qml").absoluteFilePath()).toEncoded()); |
| 694 |
#else |
| 695 |
QString url(QUrl::fromLocalFile(QFileInfo("themes/BuggyTheme/Palette.qml").absoluteFilePath()).toEncoded()); |
| 696 |
#endif |
| 697 |
QString warning(QString("<Unknown File>: QML QQmlEngine: %1:24 Cannot assign to non-existent property \"imaginary\"\n").arg(url)); |
| 698 |
QTest::ignoreMessage(QtWarningMsg, warning.toUtf8()); |
| 699 |
|
| 700 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", "./themes"); |
| 701 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SimpleItem.qml")); |
| 702 |
view->setTheme("BuggyTheme", view->rootObject()); |
| 703 |
} |
| 704 |
|
| 705 |
void test_removing_closest_parent_styled() |
| 706 |
{ |
| 707 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 708 |
qputenv("XDG_DATA_DIRS", "./themes:./themes/TestModule"); |
| 709 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ReparentStyledItemFollowsNewPathOnly.qml")); |
| 710 |
UCTheme *suruTheme = view->findItem<UCTheme*>("suruTheme"); |
| 711 |
UCStyledItemBase *root = static_cast<UCStyledItemBase*>(view->rootObject()); |
| 712 |
UCStyledItemBase *movableItem = view->findItem<UCStyledItemBase*>("movable"); |
| 713 |
|
| 714 |
// verify movableItem's theme |
| 715 |
QCOMPARE(root->getTheme(), movableItem->getTheme()); |
| 716 |
|
| 717 |
// set the theme for root |
| 718 |
QSignalSpy themeChangeSpy(root, SIGNAL(themeChanged())); |
| 719 |
root->setTheme(suruTheme); |
| 720 |
LomiriTestCase::waitForSignal(&themeChangeSpy); |
| 721 |
QCOMPARE(root->getTheme(), movableItem->getTheme()); |
| 722 |
|
| 723 |
// set the parent item of the test item to 0 |
| 724 |
QSignalSpy spy(movableItem, SIGNAL(themeChanged())); |
| 725 |
movableItem->setParentItem(Q_NULLPTR); |
| 726 |
spy.wait(500); |
| 727 |
QCOMPARE(spy.count(), 1); |
| 728 |
QCOMPARE(movableItem->getTheme(), UCTheme::defaultTheme(view->engine())); |
| 729 |
} |
| 730 |
|
| 731 |
void test_reparented_styleditem_special_case() |
| 732 |
{ |
| 733 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 734 |
qputenv("XDG_DATA_DIRS", "./themes:./themes/TestModule"); |
| 735 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("ReparentStyledItemFollowsNewPathOnly.qml")); |
| 736 |
UCStyledItemBase *root = static_cast<UCStyledItemBase*>(view->rootObject()); |
| 737 |
UCTheme *suruTheme = view->findItem<UCTheme*>("suruTheme"); |
| 738 |
UCStyledItemBase *customThemedItem = view->findItem<UCStyledItemBase*>("customThemed"); |
| 739 |
UCStyledItemBase *movableItem = view->findItem<UCStyledItemBase*>("movable"); |
| 740 |
|
| 741 |
// check the themes |
| 742 |
QCOMPARE(root->getTheme()->name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 743 |
QCOMPARE(customThemedItem->getTheme()->name(), QString("CustomTheme")); |
| 744 |
QCOMPARE(movableItem->getTheme()->name(), QString("Lomiri.Components.Themes.Ambiance")); |
| 745 |
|
| 746 |
// move the movableItem under customThemedItem |
| 747 |
movableItem->setParentItem(customThemedItem); |
| 748 |
QCOMPARE(movableItem->getTheme()->name(), QString("CustomTheme")); |
| 749 |
|
| 750 |
// set a new theme for the root, and make sure our theme stays the same |
| 751 |
QSignalSpy themeChangeSpy(root, SIGNAL(themeChanged())); |
| 752 |
root->setTheme(suruTheme); |
| 753 |
LomiriTestCase::waitForSignal(&themeChangeSpy); |
| 754 |
QCOMPARE(movableItem->getTheme()->name(), QString("CustomTheme")); |
| 755 |
} |
| 756 |
|
| 757 |
void test_theme_versions_data() |
| 758 |
{ |
| 759 |
QTest::addColumn<QString>("document"); |
| 760 |
QTest::addColumn<QString>("testValue"); |
| 761 |
|
| 762 |
QTest::newRow("Theming") |
| 763 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 764 |
<< "StyledItemThemed.qml" |
| 765 |
<< "version2.0"; |
| 766 |
#else |
| 767 |
<< "StyledItemV13.qml" |
| 768 |
<< "version1.3"; |
| 769 |
#endif |
| 770 |
QTest::newRow("App theme versioned") |
| 771 |
<< "StyledItemAppThemeVersioned.qml" |
| 772 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 773 |
<< "version2.0"; |
| 774 |
#else |
| 775 |
<< "version1.3"; |
| 776 |
#endif |
| 777 |
} |
| 778 |
void test_theme_versions() |
| 779 |
{ |
| 780 |
QFETCH(QString, document); |
| 781 |
QFETCH(QString, testValue); |
| 782 |
|
| 783 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 784 |
qputenv("XDG_DATA_DIRS", "./themes:./themes/TestModule"); |
| 785 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase(document)); |
| 786 |
UCStyledItemBase *styledItem = qobject_cast<UCStyledItemBase*>(view->rootObject()); |
| 787 |
QVERIFY(UCStyledItemBasePrivate::get(styledItem)->styleItem); |
| 788 |
QString newProperty(UCStyledItemBasePrivate::get(styledItem)->styleItem->property("newProperty").toString()); |
| 789 |
QCOMPARE(newProperty, testValue); |
| 790 |
// NOTE TestTheme resets the theme therefore the theming will look for the tested style under Ambiance theme |
| 791 |
// which will cause a warning; therefore we mark the warning to be ignored |
| 792 |
QRegularExpression warningRe(QStringLiteral( |
| 793 |
": QML (StyledItem|StyledItemV13|StyledItemThemed|StyledItemAppThemeVersioned): Warning: " |
| 794 |
"Style TestStyle\\.qml not found in theme Lomiri\\.Components\\.Themes\\.Ambiance$" |
| 795 |
)); |
| 796 |
QTest::ignoreMessage(QtWarningMsg, warningRe); |
| 797 |
} |
| 798 |
|
| 799 |
void test_mixed_versions() { |
| 800 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 801 |
QSKIP("Only version 2.0 is available with qt6"); |
| 802 |
#endif |
| 803 |
QRegularExpression warningRe(QStringLiteral( |
| 804 |
": QML (StyledItem|OtherVersion): Mixing of Lomiri\\.Components module " |
| 805 |
"versions 1\\.3 and 1\\.2 detected!$" |
| 806 |
)); |
| 807 |
QTest::ignoreMessage(QtWarningMsg, warningRe); |
| 808 |
|
| 809 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("OtherVersion.qml")); |
| 810 |
QCoreApplication::processEvents(); |
| 811 |
UCStyledItemBase *newStyled = static_cast<UCStyledItemBase*>(view->rootObject()); |
| 812 |
UCStyledItemBase *otherStyled = view->findItem<UCStyledItemBase*>("otherStyled"); |
| 813 |
QCOMPARE(UCStyledItemBasePrivate::get(newStyled)->styleInstance()->objectName(), QString("OptionSelector13")); |
| 814 |
QCOMPARE(UCStyledItemBasePrivate::get(otherStyled)->styleInstance()->objectName(), QString("OptionSelector12")); |
| 815 |
} |
| 816 |
|
| 817 |
void test_deprecated_theme() |
| 818 |
{ |
| 819 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 820 |
qputenv("XDG_DATA_DIRS", "./themes:./themes/TestModule:" + m_themesPath.toLatin1()); |
| 821 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("DeprecatedTheme.qml")); |
| 822 |
} |
| 823 |
|
| 824 |
void test_style_change_has_precedence() |
| 825 |
{ |
| 826 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("StyleOverride.qml")); |
| 827 |
QQmlComponent *style = view->rootObject()->property("customStyle").value<QQmlComponent*>(); |
| 828 |
QQuickItem *button = view->findItem<QQuickItem*>("TestButton"); |
| 829 |
|
| 830 |
button->setProperty("style", QVariant::fromValue(style)); |
| 831 |
QVERIFY(button->findChild<QQuickItem*>("TestStyle")); |
| 832 |
} |
| 833 |
|
| 834 |
void test_style_kept_when_stylename_changes_data() |
| 835 |
{ |
| 836 |
QTest::addColumn<QString>("styleName"); |
| 837 |
|
| 838 |
QTest::newRow("Empty style name") << QString(); |
| 839 |
QTest::newRow("Any style name") << "Bumblebee"; |
| 840 |
} |
| 841 |
void test_style_kept_when_stylename_changes() |
| 842 |
{ |
| 843 |
QFETCH(QString, styleName); |
| 844 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("StyleKept.qml")); |
| 845 |
QQuickItem *button = view->findItem<QQuickItem*>("TestButton"); |
| 846 |
|
| 847 |
button->setProperty("styleName", styleName); |
| 848 |
QVERIFY(button->findChild<QQuickItem*>("TestStyle")); |
| 849 |
} |
| 850 |
|
| 851 |
void test_style_kept_when_theme_changes() |
| 852 |
{ |
| 853 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("StyleKept.qml")); |
| 854 |
QQuickItem *button = view->findItem<QQuickItem*>("TestButton"); |
| 855 |
UCTheme *theme = button->property("theme").value<UCTheme*>(); |
| 856 |
QVERIFY(theme); |
| 857 |
|
| 858 |
theme->setName("Lomiri.Components.Themes.SuruDark"); |
| 859 |
QVERIFY(button->findChild<QQuickItem*>("TestStyle")); |
| 860 |
} |
| 861 |
|
| 862 |
void test_style_reset_to_theme_style() |
| 863 |
{ |
| 864 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("StyleKept.qml")); |
| 865 |
UCStyledItemBase *button = view->findItem<UCStyledItemBase*>("TestButton"); |
| 866 |
UCTheme *theme = button->getTheme(); |
| 867 |
QVERIFY(theme); |
| 868 |
|
| 869 |
QVERIFY(button->findChild<QQuickItem*>("TestStyle")); |
| 870 |
// reset style property, which should reset style to the theme style |
| 871 |
UCStyledItemBasePrivate::get(button)->resetStyle(); |
| 872 |
QQuickItem *styleItem = UCStyledItemBasePrivate::get(button)->styleInstance(); |
| 873 |
QCOMPARE(QuickUtils::className(styleItem), QString("ButtonStyle")); |
| 874 |
} |
| 875 |
|
| 876 |
void test_stylename_extension_failure() |
| 877 |
{ |
| 878 |
QRegularExpression warningRe(QStringLiteral( |
| 879 |
"QML (StyledItem|DeprecatedTheme): Warning: Style OptionSelectorStyle\\.qml\\.qml " |
| 880 |
"not found in theme Lomiri\\.Components\\.Themes\\.SuruGradient$" |
| 881 |
)); |
| 882 |
QTest::ignoreMessage(QtWarningMsg, warningRe); |
| 883 |
|
| 884 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("DeprecatedTheme.qml")); |
| 885 |
view->rootObject()->setProperty("styleName", "OptionSelectorStyle.qml"); |
| 886 |
} |
| 887 |
|
| 888 |
void test_stylehints_errors_data() |
| 889 |
{ |
| 890 |
QTest::addColumn<QString>("document"); |
| 891 |
QTest::addColumn<int>("row"); |
| 892 |
QTest::addColumn<int>("col"); |
| 893 |
QTest::addColumn<QString>("xfail"); |
| 894 |
|
| 895 |
QTest::newRow("No signals") |
| 896 |
<< "StyleHintsWithSignal.qml" << 26 << 13 << "Signal properties are not supported. \n" \ |
| 897 |
" onDefaultColorChanged: {} \n" \ |
| 898 |
" ^"; |
| 899 |
QTest::newRow("No embedded objects") |
| 900 |
<< "StyleHintsWithObject.qml" << 26 << 26 << "StyleHints does not support creating state-specific objects. \n" \ |
| 901 |
" anyProperty: QtObject{} \n" \ |
| 902 |
" ^"; |
| 903 |
QTest::newRow("StyleHints declared elsewhere") |
| 904 |
<< "StyleHintsElsewhere.qml" << 24 << 5 << "QML StyleHints: StyleHints must be declared in a StyledItem or a derivate of it."; |
| 905 |
QTest::newRow("Invalid property") |
| 906 |
<< "StyleHintsInvalidProperty.qml" << 25 << 9 << "QML StyleHints: Style 'ButtonStyle' has no property called 'invalidProperty'."; |
| 907 |
QTest::newRow("Invalid grouped property") |
| 908 |
<< "StyleHintsInvalidGroupedProperty.qml" << 25 << 9 << "QML StyleHints: Style 'ButtonStyle' has no property called 'gradientProxy.invalidProperty'."; |
| 909 |
|
| 910 |
} |
| 911 |
void test_stylehints_errors() |
| 912 |
{ |
| 913 |
QFETCH(QString, document); |
| 914 |
QFETCH(int, row); |
| 915 |
QFETCH(int, col); |
| 916 |
QFETCH(QString, xfail); |
| 917 |
|
| 918 |
if (!xfail.isEmpty()) { |
| 919 |
ThemeTestCase::ignoreWarning(document, row, col, xfail); |
| 920 |
} |
| 921 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase(document, false)); |
| 922 |
} |
| 923 |
|
| 924 |
void test_stylehints_simple_property() |
| 925 |
{ |
| 926 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("SimplePropertyHints.qml")); |
| 927 |
QQuickItem *button = view->findItem<QQuickItem*>("Button"); |
| 928 |
QColor color = button->property("color").value<QColor>(); |
| 929 |
QCOMPARE(color, QColor("blue")); |
| 930 |
} |
| 931 |
|
| 932 |
void test_stylehints_bindings() |
| 933 |
{ |
| 934 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase("PropertyBindingHints.qml")); |
| 935 |
QQuickItem *button = view->findItem<QQuickItem*>("Button"); |
| 936 |
QColor color = button->property("color").value<QColor>(); |
| 937 |
QCOMPARE(color, QColor("blue")); |
| 938 |
// press the button |
| 939 |
QPointF pressPt(button->width()/2, button->height()/2); |
| 940 |
pressPt = view->rootObject()->mapFromItem(button, pressPt); |
| 941 |
QTest::mousePress(view.data(), Qt::LeftButton, Qt::NoModifier, pressPt.toPoint()); |
| 942 |
color = button->property("color").value<QColor>(); |
| 943 |
QCOMPARE(color, QColor("tan")); |
| 944 |
QTest::mouseRelease(view.data(), Qt::LeftButton, Qt::NoModifier, pressPt.toPoint()); |
| 945 |
} |
| 946 |
|
| 947 |
void test_stylehints_multiple_data() |
| 948 |
{ |
| 949 |
QTest::addColumn<QString>("document"); |
| 950 |
QTest::addColumn<QString>("colorProperty"); |
| 951 |
QTest::addColumn<QColor>("colorReleased"); |
| 952 |
QTest::addColumn<QColor>("colorPressed"); |
| 953 |
QTest::addColumn<QString>("widthProperty"); |
| 954 |
QTest::addColumn<float>("width"); |
| 955 |
|
| 956 |
UCUnits units; |
| 957 |
|
| 958 |
QTest::newRow("Same document") |
| 959 |
<< "MoreStyleHints.qml" << "defaultColor" << QColor("brown") << QColor("brown") << "minimumWidth" << units.gu(20); |
| 960 |
QTest::newRow("Different document, binding") |
| 961 |
<< "GroupPropertyBindingHints.qml" << "gradientProxy.topColor" << QColor("blue") << QColor("tan") << "minimumWidth" << units.gu(20); |
| 962 |
// regression test for bug #1602836. |
| 963 |
QTest::newRow("Different document, value") |
| 964 |
<< "GroupPropertyValueHints.qml" << "gradientProxy.topColor" << QColor("orange") << QColor("orange") << "minimumWidth" << units.gu(20); |
| 965 |
} |
| 966 |
void test_stylehints_multiple() |
| 967 |
{ |
| 968 |
QFETCH(QString, document); |
| 969 |
QFETCH(QString, colorProperty); |
| 970 |
QFETCH(QColor, colorReleased); |
| 971 |
QFETCH(QColor, colorPressed); |
| 972 |
QFETCH(QString, widthProperty); |
| 973 |
QFETCH(float, width); |
| 974 |
|
| 975 |
QScopedPointer<ThemeTestCase> view(new ThemeTestCase(document)); |
| 976 |
UCStyledItemBase *button = view->findItem<UCStyledItemBase*>("Button"); |
| 977 |
QQuickItem *styleItem = UCStyledItemBasePrivate::get(button)->styleItem; |
| 978 |
QVERIFY(styleItem); |
| 979 |
QQmlProperty qmlProperty(styleItem, colorProperty, qmlContext(styleItem)); |
| 980 |
QCOMPARE(qmlProperty.read().value<QColor>(), colorReleased); |
| 981 |
QCOMPARE(styleItem->property(widthProperty.toUtf8()).toReal(), width); |
| 982 |
|
| 983 |
QPointF pressPt(button->width()/2, button->height()/2); |
| 984 |
pressPt = view->rootObject()->mapFromItem(button, pressPt); |
| 985 |
QTest::mousePress(view.data(), Qt::LeftButton, Qt::NoModifier, pressPt.toPoint()); |
| 986 |
QColor pressedColor = qmlProperty.read().value<QColor>(); |
| 987 |
QTest::mouseRelease(view.data(), Qt::LeftButton, Qt::NoModifier, pressPt.toPoint()); |
| 988 |
QCOMPARE(pressedColor, colorPressed); |
| 989 |
} |
| 990 |
|
| 991 |
void test_derived_theme_fallback_should_use_proper_style_bug1555797() { |
| 992 |
qputenv("LOMIRI_UI_TOOLKIT_THEMES_PATH", ""); |
| 993 |
qputenv("XDG_DATA_DIRS", "./themes:./themes/TestModule"); |
| 994 |
|
| 995 |
QQmlEngine engine; |
| 996 |
LomiriToolkitModule::initializeContextProperties(&engine); |
| 997 |
UCTheme theme; |
| 998 |
QQmlEngine::setContextForObject(&theme, engine.rootContext()); |
| 999 |
theme.setName("DerivedTheme"); |
| 1000 |
|
| 1001 |
// get the style URL |
| 1002 |
bool fallback = false; |
| 1003 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 1004 |
QUrl style = theme.styleUrl("TestStyle.qml", BUILD_VERSION(2, 0), &fallback); |
| 1005 |
QVERIFY(style.toString().endsWith("TestTheme/2.0/TestStyle.qml")); |
| 1006 |
#else |
| 1007 |
QUrl style = theme.styleUrl("TestStyle.qml", BUILD_VERSION(1, 3), &fallback); |
| 1008 |
QVERIFY(style.toString().endsWith("TestTheme/1.3/TestStyle.qml")); |
| 1009 |
#endif |
| 1010 |
} |
| 1011 |
}; |
| 1012 |
|
| 1013 |
QTEST_MAIN(tst_Subtheming) |
| 1014 |
|
| 1015 |
#include "tst_subtheming.moc" |