1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <gtest/gtest.h>
#include "test_helper.hpp"
#include <DAppLoader>
#include <DPathBuf>
#include <QSignalSpy>
#include "private/dapploader_p.h"
#include <DQmlAppPreloadInterface>
#include <DQmlAppMainWindowInterface>
#include <QQmlApplicationEngine>
DQUICK_USE_NAMESPACE
DCORE_USE_NAMESPACE
static constexpr char const *AppId = "org.deepin.dtkdeclarative.unit-test";
static const QString envPath(DPathBuf("./env").toString());
static const QStringList ldPaths{DPathBuf("./ld1").toString(), DPathBuf("ld2").toString()};
static const QStringList apiPaths{DPathBuf("./api1").toString(), DPathBuf("./api2").toString()};
static const QString constructPath(DPathBuf("./cpath").toString());
class ut_AppLoader : public ::testing::Test
{
public:
};
TEST_F(ut_AppLoader, instance)
{
DAppLoader loader(AppId);
ASSERT_EQ(DAppLoader::instance(), &loader);
}
TEST_F(ut_AppLoader, buildinPluginPath)
{
#ifdef DTK_QML_APP_PLUGIN_PATH
DAppLoader loader(AppId);
ASSERT_EQ(loader.pluginPaths().last(), DTK_QML_APP_PLUGIN_PATH);
#endif
{
EnvGuard guard("DTK_QML_PLUGIN_PATH", envPath);
Q_UNUSED(guard)
DAppLoader loader(AppId);
ASSERT_EQ(loader.pluginPaths().first(), envPath);
}
{
EnvGuard guard("LD_LIBRARY_PATH", ldPaths.join(':'));
Q_UNUSED(guard)
DAppLoader loader(AppId);
const QString path = loader.pluginPaths().first();
ASSERT_TRUE(path.startsWith(ldPaths[0]));
}
{
EnvGuard guard("DTK_QML_PLUGIN_PATH", envPath);
Q_UNUSED(guard)
EnvGuard guard2("LD_LIBRARY_PATH", ldPaths.join(':'));
Q_UNUSED(guard2)
DAppLoader loader(AppId);
ASSERT_EQ(loader.pluginPaths().first(), envPath);
}
}
TEST_F(ut_AppLoader, addPluginPath)
{
EnvGuard guard("DTK_QML_PLUGIN_PATH", envPath);
Q_UNUSED(guard)
EnvGuard guard2("LD_LIBRARY_PATH", ldPaths.join(':'));
Q_UNUSED(guard2)
{
DAppLoader loader(AppId, constructPath);
const QString path = loader.pluginPaths().first();
ASSERT_TRUE(path.startsWith(constructPath));
}
{
DAppLoader loader(AppId, constructPath);
loader.addPluginPath(apiPaths[0]);
loader.addPluginPath(apiPaths[1]);
const QString path = loader.pluginPaths().first();
ASSERT_TRUE(path.startsWith(apiPaths[1]));
}
}
class AppLoaderSimulator : public DAppLoader
{
public:
AppLoaderSimulator()
: DAppLoader(AppId, APPLOADER_PLUGINDUMP_OUTPUT_DIR)
{
d = DAppLoader::d_func();
d->engine = new QQmlApplicationEngine();
updateEngineImportPath(*d->engine);
}
~AppLoaderSimulator()
{
// don't release app.
d->app.take();
}
int load()
{
d->ensureLoadPreload();
int argc = 0;
d->app.reset(d->preloadInstance->creatApplication(argc, nullptr));
if (!d->app)
return -1;
d->preloadInstance->aboutToPreload(d->engine);
QObject::connect(d->engine, &QQmlApplicationEngine::objectCreated, this, [this](QObject *obj, const QUrl &url) {
d->_q_onPreloadCreated(obj, url);
});
d->engine->load(d->preloadInstance->preloadComponentPath());
if (d->engine->rootObjects().isEmpty())
return -1;
return 0;
}
DAppLoaderPrivate *d;
};
TEST_F(ut_AppLoader, exec)
{
AppLoaderSimulator loader;
QSignalSpy spy(&loader, SIGNAL(loadFinished()));
int exitCode = loader.load();
ASSERT_EQ(exitCode, 0);
EXPECT_TRUE(QTest::qWaitFor([this, &spy]() {
return spy.count() >= 1;
}));
}
|