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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "EnvironmentDialogTest.h"
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QObject>
#include <QPushButton>
#include <QString>
#include <QtTest>
#include "CatchShow.h"
#include "EnvironmentDialog.h"
EnvironmentDialogTest::EnvironmentDialogTest(QObject* parent)
: QObject(parent)
{
}
void EnvironmentDialogTest::environmentDialog()
{
CatchShow catcher;
catcher.setCallback<QMessageBox>([](QMessageBox* box) { box->accept(); });
QProcessEnvironment env;
env.insert("DELETED_VARIABLE_1", "Deleted variable 1");
env.insert("DELETED_VARIABLE_2", "Deleted variable 2");
env.insert("KEPT_VARIABLE", "Kept variable");
env.insert("CHANGED_VARIABLE", "This will be changed");
EnvironmentDialog dialog(env);
{
QStringList expected{
"CHANGED_VARIABLE=This will be changed",
"DELETED_VARIABLE_1=Deleted variable 1",
"DELETED_VARIABLE_2=Deleted variable 2",
"KEPT_VARIABLE=Kept variable",
};
QCOMPARE(dialog.environment().toStringList(), expected);
QCOMPARE(catcher.count(), 0);
}
{
CatchShow catcher2;
bool done = false;
catcher2.setCallback<QDialog>([&catcher, &done](QDialog* box) {
if (done) {
return;
}
done = true;
auto name = box->findChild<QLineEdit*>("name");
auto value = box->findChild<QLineEdit*>("value");
auto acceptReject = box->findChild<QDialogButtonBox*>();
name->setText("");
value->setText("");
acceptReject->button(QDialogButtonBox::Ok)->click();
QCOMPARE(catcher.count(), 1);
name->setText("KEPT_VARIABLE");
value->setText("");
acceptReject->button(QDialogButtonBox::Ok)->click();
QCOMPARE(catcher.count(), 2);
name->setText("ADDED_VARIABLE");
value->setText("Added variable");
acceptReject->button(QDialogButtonBox::Ok)->click();
QCOMPARE(catcher.count(), 2);
});
dialog.AddEntry->click();
QStringList expected{
"ADDED_VARIABLE=Added variable",
"CHANGED_VARIABLE=This will be changed",
"DELETED_VARIABLE_1=Deleted variable 1",
"DELETED_VARIABLE_2=Deleted variable 2",
"KEPT_VARIABLE=Kept variable",
};
QCOMPARE(dialog.environment().toStringList(), expected);
QCOMPARE(catcher.count(), 2);
QVERIFY(done);
}
{
CatchShow catcher2;
bool done = false;
catcher2.setCallback<QDialog>([&done](QDialog* box) {
if (done) {
return;
}
done = true;
auto name = box->findChild<QLineEdit*>("name");
auto value = box->findChild<QLineEdit*>("value");
auto acceptReject = box->findChild<QDialogButtonBox*>();
name->setText("DISCARDED_VARIABLE");
value->setText("Discarded variable");
acceptReject->button(QDialogButtonBox::Cancel)->click();
});
dialog.AddEntry->click();
QStringList expected{
"ADDED_VARIABLE=Added variable",
"CHANGED_VARIABLE=This will be changed",
"DELETED_VARIABLE_1=Deleted variable 1",
"DELETED_VARIABLE_2=Deleted variable 2",
"KEPT_VARIABLE=Kept variable",
};
QCOMPARE(dialog.environment().toStringList(), expected);
QCOMPARE(catcher.count(), 2);
QVERIFY(done);
}
{
auto* model = dialog.Environment->model();
auto* selectionModel = dialog.Environment->selectionModel();
for (int i = 0; i < model->rowCount(); ++i) {
auto index1 = model->index(i, 0);
auto index2 = model->buddy(index1);
auto name = model->data(index1, Qt::DisplayRole).toString();
if (name == "DELETED_VARIABLE_1" || name == "DELETED_VARIABLE_2") {
selectionModel->select(index1, QItemSelectionModel::Select);
selectionModel->select(index2, QItemSelectionModel::Select);
} else if (name == "CHANGED_VARIABLE") {
model->setData(index2, "Changed variable", Qt::DisplayRole);
}
}
dialog.RemoveEntry->click();
QStringList expected{
"ADDED_VARIABLE=Added variable",
"CHANGED_VARIABLE=Changed variable",
"KEPT_VARIABLE=Kept variable",
};
QCOMPARE(dialog.environment().toStringList(), expected);
}
}
QTEST_MAIN(EnvironmentDialogTest)
|