diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4eaadad2..793ca723 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -617,7 +617,7 @@ if(KF6Notifications_FOUND AND Qt6DBus_FOUND) KF6::Notifications KF6::I18n) target_compile_definitions(tst_weknotifier PRIVATE - WEK_SOURCE_DIR="${CMAKE_SOURCE_DIR}/.." + WEK_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/.." TRANSLATION_DOMAIN="${WEK_TRANSLATION_DOMAIN}") if(COVERAGE) target_compile_options(tst_weknotifier PRIVATE ${_cov_compile_flags}) @@ -839,6 +839,8 @@ find_package(Qt6 6.7 COMPONENTS QuickTest Qml QUIET) find_package(KF6I18n QUIET) if(Qt6QuickTest_FOUND AND Qt6Qml_FOUND AND KF6I18n_FOUND) add_executable(wek_qml_test_runner qml_test_runner.cpp) + target_compile_definitions(wek_qml_test_runner PRIVATE + WEK_QML_STUB_DIR="${CMAKE_CURRENT_SOURCE_DIR}/qml/_stubs/com/github/captsilver/wallpaperEngineKde") target_link_libraries(wek_qml_test_runner PRIVATE Qt6::QuickTest Qt6::Qml KF6::I18n) set(QML_TEST_RUNNER "$") @@ -977,10 +979,11 @@ if(Qt6QuickTest_FOUND AND Qt6Qml_FOUND AND Qt6Quick_FOUND) "${CMAKE_CURRENT_SOURCE_DIR}/qml_integration" "${_e2e_gen_dir}") target_compile_definitions(tst_main_integration PRIVATE + WEK_QML_STUB_DIR="${CMAKE_CURRENT_SOURCE_DIR}/qml/_stubs/com/github/captsilver/wallpaperEngineKde" WEKDE_REPO_DIR="${_wekde_repo_root_e2e}" QUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/qml_integration") target_link_libraries(tst_main_integration PRIVATE - Qt6::QuickTest Qt6::Qml Qt6::Quick) + Qt6::QuickTest Qt6::Qml Qt6::Quick KF6::I18n) add_test(NAME tst_main_integration COMMAND tst_main_integration) set_tests_properties(tst_main_integration PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen") diff --git a/tests/QmlTestStubs.h b/tests/QmlTestStubs.h new file mode 100644 index 00000000..943cdaa2 --- /dev/null +++ b/tests/QmlTestStubs.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include +#include +#include + +// Qt can discover a second, installed qmldir when mixing versioned and +// unversioned imports, even with _stubs first on the import path. Redirect +// only this module's descriptor; leave Qt/KDE and production QML untouched. +class QmlTestStubs : public QQmlAbstractUrlInterceptor { +public: + QUrl intercept(const QUrl& url, DataType type) override { + if (type == QmldirFile && + url.path().endsWith("/com/github/captsilver/wallpaperEngineKde/qmldir")) { + return QUrl::fromLocalFile(QStringLiteral(WEK_QML_STUB_DIR "/qmldir")); + } + return url; + } +}; diff --git a/tests/qml_integration/main.cpp b/tests/qml_integration/main.cpp index e7866abe..41397289 100644 --- a/tests/qml_integration/main.cpp +++ b/tests/qml_integration/main.cpp @@ -1,11 +1,14 @@ #include +#include "../QmlTestStubs.h" #include #include #include #include +#include #include "FakeWallpaper.h" +#include "../TestSandbox.h" // QuickTest setup: inject `wallpaper` as a context property backed by real C++ // objects, reusing the existing tests/qml/_stubs. main.qml then loads exactly as @@ -13,12 +16,21 @@ // whose per-key NOTIFY signals drive main.qml's reactive Connections). class Setup : public QObject { Q_OBJECT + QmlTestStubs stubs; public slots: + void applicationAvailable() { wek::test_sandbox::enableIsolated(); } + void qmlEngineAvailable(QQmlEngine* engine) { + engine->addUrlInterceptor(&stubs); const QString repo = QStringLiteral(WEKDE_REPO_DIR); engine->addImportPath(repo + "/tests/qml/_stubs"); engine->addImportPath(repo + "/tests/qml"); + QT_WARNING_PUSH + QT_WARNING_DISABLE_DEPRECATED + engine->rootContext()->setContextObject(new KLocalizedContext(engine)); + QT_WARNING_POP + auto* wallpaper = buildOwnedFakeWallpaperTree(engine); // Test-friendly overrides differing from main.xml defaults. diff --git a/tests/qml_integration/tst_main_integration.qml b/tests/qml_integration/tst_main_integration.qml index 4f052c99..3705ddf3 100644 --- a/tests/qml_integration/tst_main_integration.qml +++ b/tests/qml_integration/tst_main_integration.qml @@ -12,6 +12,17 @@ TestCase { Item { id: host; anchors.fill: parent } + function test_mixed_imports_use_stubs() { + for (const version of ["", " 1.2"]) { + const object = Qt.createQmlObject( + 'import QtQuick; import com.github.captsilver.wallpaperEngineKde' + + version + '; WekDiagnostics {}', host); + // Present only on the stub; native diagnostics must never run here. + compare(object.nextBundlePath, "/tmp/wekde-stub-diag.tar.gz"); + object.destroy(); + } + } + function _find(node, pred) { if (!node) return null; const buckets = [node.children || [], node.data || []]; @@ -40,7 +51,7 @@ TestCase { function _mkMain(w, h) { const comp = Qt.createComponent("../../plugin/contents/ui/main.qml"); - compare(comp.status, Component.Ready); + compare(comp.status, Component.Ready, comp.errorString()); const m = comp.createObject(host, { width: w, height: h }); verify(m !== null); return m; diff --git a/tests/qml_test_runner.cpp b/tests/qml_test_runner.cpp index 429789ba..d453ba5c 100644 --- a/tests/qml_test_runner.cpp +++ b/tests/qml_test_runner.cpp @@ -14,6 +14,7 @@ // harness, matching how production actually gets its bindings, not in // restoring an import nothing else uses. #include "TestSandbox.h" +#include "QmlTestStubs.h" #include #include @@ -22,15 +23,14 @@ class I18nQuickTestSetup : public QObject { Q_OBJECT + QmlTestStubs stubs; public slots: - // Suites that end up on a real plugin type -- the installed module can - // win over the stubs on the import path -- write through QStandardPaths - // (PlaylistManager persists playlists.json). Give the whole run its own - // HOME in Qt's test mode so those writes never reach the user's config. + // Keep QStandardPaths writes isolated even if a suite uses a real type. void applicationAvailable() { wek::test_sandbox::enableIsolated(); } void qmlEngineAvailable(QQmlEngine* engine) { + engine->addUrlInterceptor(&stubs); // Production (corebindingsplugin.so) installs KLocalizedQmlContext, // not this class -- KLocalizedContext has been deprecated in its // favour since KF 6.8. We still use the deprecated one here: it diff --git a/tests/scripts/test_mutation.sh b/tests/scripts/test_mutation.sh index 43679c38..3348834b 100755 --- a/tests/scripts/test_mutation.sh +++ b/tests/scripts/test_mutation.sh @@ -82,6 +82,7 @@ make_gitrepo() { # git init -q git config user.email t@example.com git config user.name test + git config commit.gpgsign false for f in "$@"; do mkdir -p "$(dirname "$f")"; echo v1 > "$f"; done echo base > BASEFILE git add -A && git commit -qm base