aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/jsonwizard/jsonwizard_test.cpp
blob: 2e1622879037f1b1aee16daa33dec55fd0ddb423 (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "jsonwizardfactory.h"

#include <coreplugin/icore.h>

#include <projectexplorer/projectexplorer.h>

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QTest>
#include <QCheckBox>
#include <QLineEdit>
#include <QComboBox>
#include <QListView>

#include <functional>

using namespace Utils;

namespace ProjectExplorer {

static QJsonObject createWidget(const QString &type, const QString &nameSuffix, const QJsonObject &data)
{
    return QJsonObject{
        {"name", QJsonValue(nameSuffix + type)},
        {"type", type},
        {"trDisplayName", QJsonValue(nameSuffix + "DisplayName")},
        {"data", data}
    };
}

static QJsonObject createFieldPageJsonObject(const QJsonArray &widgets)
{
    return QJsonObject{
        {"name", "testpage"},
        {"trDisplayName", "mytestpage"},
        {"typeId", "Fields"},
        {"data", widgets}
    };
}

QJsonObject createGeneralWizard(const QJsonObject &pages)
{
    return QJsonObject {
        {"category", "TestCategory"},
        {"enabled", true},
        {"id", "mytestwizard"},
        {"trDisplayName", "mytest"},
        {"trDisplayCategory", "mytestcategory"},
        {"trDescription", "this is a test wizard"},
        {"generators",
            QJsonObject{
                {"typeId", "File"},
                {"data",
                    QJsonObject{
                        {"source", "myFile.txt"}
                    }
                }
            }
        },
        {"pages", pages}
    };
}

QCheckBox *findCheckBox(Wizard *wizard, const QString &objectName)
{
    return wizard->findChild<QCheckBox *>(objectName + "CheckBox");
}

QLineEdit *findLineEdit(Wizard *wizard, const QString &objectName)
{
    return wizard->findChild<QLineEdit *>(objectName + "LineEdit");
}

QComboBox *findComboBox(Wizard *wizard, const QString &objectName)
{
    return wizard->findChild<QComboBox *>(objectName + "ComboBox");
};

struct FactoryDeleter { void operator()(JsonWizardFactory *f) { f->deleteLater(); } };

using FactoryPtr = std::unique_ptr<JsonWizardFactory, FactoryDeleter>;

void ProjectExplorerPlugin::testJsonWizardsEmptyWizard()
{
    QString errorMessage;
    const QJsonObject wizard = createGeneralWizard(QJsonObject());

    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), {}, &errorMessage));
    QVERIFY(factory == nullptr);
    QCOMPARE(qPrintable(errorMessage), "Page has no typeId set.");
}

void ProjectExplorerPlugin::testJsonWizardsEmptyPage()
{
    QString errorMessage;
    const QJsonObject pages = createFieldPageJsonObject(QJsonArray());
    const QJsonObject wizard = createGeneralWizard(pages);

    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), {}, &errorMessage));
    QVERIFY(factory == nullptr);
    QCOMPARE(qPrintable(errorMessage), "When parsing fields of page \"PE.Wizard.Page.Fields\": ");
}

void ProjectExplorerPlugin::testJsonWizardsUnusedKeyAtFields_data()
{
    const QPair<QString, QJsonValue> wrongData = {"wrong", false};

    QTest::addColumn<QJsonObject>("wrongDataJsonObect");
    QTest::newRow("Label") << QJsonObject({{wrongData, {"trText", "someText"}}});
    QTest::newRow("Spacer") << QJsonObject({wrongData});
    QTest::newRow("LineEdit") << QJsonObject({wrongData});
    QTest::newRow("TextEdit") << QJsonObject({wrongData});
    QTest::newRow("PathChooser") << QJsonObject({wrongData});
    QTest::newRow("CheckBox") << QJsonObject({wrongData});
    QTest::newRow("ComboBox") << QJsonObject({{wrongData, {"items", QJsonArray()}}});
}

void ProjectExplorerPlugin::testJsonWizardsUnusedKeyAtFields()
{
    QString fieldType(QString::fromLatin1(QTest::currentDataTag()));
    QFETCH(QJsonObject, wrongDataJsonObect);
    QString errorMessage;
    const QJsonObject pages = QJsonObject{
        {"name", "testpage"},
        {"trDisplayName", "mytestpage"},
        {"typeId", "Fields"},
        {"data", createWidget(fieldType, "WrongKey", wrongDataJsonObect)},
    };
    const QJsonObject wizard = createGeneralWizard(pages);

    QTest::ignoreMessage(QtWarningMsg, QRegularExpression("has unsupported keys: wrong"));
    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), {}, &errorMessage));
    QVERIFY(factory);
    QVERIFY(errorMessage.isEmpty());
}

void ProjectExplorerPlugin::testJsonWizardsCheckBox()
{
    QString errorMessage;

    QWidget parent;
    const QJsonArray widgets({
        createWidget("CheckBox", "Default", QJsonObject()),
        createWidget("CheckBox", "Checked", QJsonObject({{"checked", true}})),
        createWidget("CheckBox", "UnChecked", QJsonObject({{"checked", false}})),
        createWidget("CheckBox", "SpecialValueUnChecked", QJsonObject(
         {{"checked", false}, {"checkedValue", "SpecialCheckedValue"}, {"uncheckedValue", "SpecialUnCheckedValue"}})
        ),
        createWidget("CheckBox", "SpecialValueChecked", QJsonObject(
         {{"checked", true}, {"checkedValue", "SpecialCheckedValue"}, {"uncheckedValue", "SpecialUnCheckedValue"}})
        )
    });
    const QJsonObject pages = createFieldPageJsonObject(widgets);
    const QJsonObject wizardObject = createGeneralWizard(pages);
    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage));
    QVERIFY2(factory, qPrintable(errorMessage));

    Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap());

    QVERIFY(!findCheckBox(wizard, "Default")->isChecked());
    QCOMPARE(wizard->field("DefaultCheckBox"), QVariant(false));

    QVERIFY(findCheckBox(wizard, "Checked")->isChecked());
    QCOMPARE(wizard->field("CheckedCheckBox"), QVariant(true));

    QVERIFY(!findCheckBox(wizard, "UnChecked")->isChecked());
    QCOMPARE(wizard->field("UnCheckedCheckBox"), QVariant(false));

    QVERIFY(!findCheckBox(wizard, "SpecialValueUnChecked")->isChecked());
    QCOMPARE(qPrintable(wizard->field("SpecialValueUnCheckedCheckBox").toString()), "SpecialUnCheckedValue");

    QVERIFY(findCheckBox(wizard, "SpecialValueChecked")->isChecked());
    QCOMPARE(qPrintable(wizard->field("SpecialValueCheckedCheckBox").toString()), "SpecialCheckedValue");
}

void ProjectExplorerPlugin::testJsonWizardsLineEdit()
{
    QString errorMessage;

    QWidget parent;
    const QJsonArray widgets({
         createWidget("LineEdit", "Default", QJsonObject()),
         createWidget("LineEdit", "WithText", QJsonObject({{"trText", "some text"}}))
    });
    const QJsonObject pages = createFieldPageJsonObject(widgets);
    const QJsonObject wizardObject = createGeneralWizard(pages);
    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage));
    QVERIFY2(factory, qPrintable(errorMessage));

    Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap());
    QVERIFY(findLineEdit(wizard, "Default"));
    QVERIFY(findLineEdit(wizard, "Default")->text().isEmpty());
    QCOMPARE(qPrintable(findLineEdit(wizard, "WithText")->text()), "some text");

    QVERIFY(!wizard->page(0)->isComplete());
    findLineEdit(wizard, "Default")->setText("enable isComplete");
    QVERIFY(wizard->page(0)->isComplete());
}

void ProjectExplorerPlugin::testJsonWizardsComboBox()
{
    QString errorMessage;
    QWidget parent;

    const QJsonArray items({"abc", "cde", "fgh"});
    QJsonObject disabledComboBoxObject = createWidget("ComboBox", "Disabled", QJsonObject({ {{"disabledIndex", 2}, {"items", items}} }));
    disabledComboBoxObject.insert("enabled", false);
    const QJsonArray widgets({
        createWidget("ComboBox", "Default", QJsonObject({ {{"items", items}} })),
        createWidget("ComboBox", "Index2", QJsonObject({ {{"index", 2}, {"items", items}} })),
        disabledComboBoxObject
    });

    const QJsonObject pages = createFieldPageJsonObject(widgets);
    const QJsonObject wizardObject = createGeneralWizard(pages);
    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage));
    QVERIFY2(factory, qPrintable(errorMessage));
    Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap());

    QComboBox *defaultComboBox = findComboBox(wizard, "Default");
    QVERIFY(defaultComboBox);
    QCOMPARE(defaultComboBox->count(), items.count());
    QCOMPARE(qPrintable(defaultComboBox->currentText()), "abc");

    defaultComboBox->setCurrentIndex(2);
    QCOMPARE(qPrintable(defaultComboBox->currentText()), "fgh");

    QComboBox *index2ComboBox = findComboBox(wizard, "Index2");
    QVERIFY(index2ComboBox);
    QCOMPARE(qPrintable(index2ComboBox->currentText()), "fgh");

    QComboBox *disabledComboBox = findComboBox(wizard, "Disabled");
    QVERIFY(disabledComboBox);
    QCOMPARE(qPrintable(disabledComboBox->currentText()), "fgh");
}

static QString iconInsideResource(const QString &relativePathToIcon)
{
    return Core::ICore::resourcePath().resolvePath(relativePathToIcon).toString();
}

void ProjectExplorerPlugin::testJsonWizardsIconList()
{
    QString errorMessage;
    QWidget parent;

    const QJsonArray items({
        QJsonObject{
           {"trKey", "item no1"},
           {"condition", true},
           {"icon", iconInsideResource("templates/wizards/global/lib.png")}
        },
        QJsonObject{
           {"trKey", "item no2"},
           {"condition", false},
           {"icon", "not_existing_path"}

        },
        QJsonObject{
            {"trKey", "item no3"},
            {"condition", true},
            {"trToolTip", "MyToolTip"},
            {"icon", iconInsideResource("templates/wizards/global/lib.png")}
        }
    });

    const QJsonArray widgets({
        createWidget("IconList", "Fancy", QJsonObject{{"index", -1}, {"items", items}})
    });

    const QJsonObject pages = createFieldPageJsonObject(widgets);
    const QJsonObject wizardObject = createGeneralWizard(pages);
    const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage));
    QVERIFY2(factory, qPrintable(errorMessage));
    Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap());

    auto view = wizard->findChild<QListView *>("FancyIconList");
    QCOMPARE(view->model()->rowCount(), 2);
    QVERIFY(view->model()->index(0,0).data(Qt::DecorationRole).canConvert<QIcon>());
    QIcon icon = view->model()->index(0,0).data(Qt::DecorationRole).value<QIcon>();
    QVERIFY(!icon.isNull());
    QVERIFY(!wizard->page(0)->isComplete());
}

} // ProjectExplorer