summaryrefslogtreecommitdiffstats
path: root/tests/tst_UpdateChecker.cpp
blob: 8351ad75ba0bcba9af546be1843135add7f4fdb8 (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
#include <QTest>
#include <QSignalSpy>

#include "logic/settings/SettingsObject.h"
#include "logic/settings/Setting.h"

#include "BuildConfig.h"
#include "TestUtil.h"
#include "logic/updater/UpdateChecker.h"

Q_DECLARE_METATYPE(UpdateChecker::ChannelListEntry)

bool operator==(const UpdateChecker::ChannelListEntry &e1, const UpdateChecker::ChannelListEntry &e2)
{
	return e1.id == e2.id &&
			e1.name == e2.name &&
			e1.description == e2.description &&
			e1.url == e2.url;
}

QDebug operator<<(QDebug dbg, const UpdateChecker::ChannelListEntry &c)
{
	dbg.nospace() << "ChannelListEntry(id=" << c.id << " name=" << c.name << " description=" << c.description << " url=" << c.url << ")";
	return dbg.maybeSpace();
}

class ResetSetting
{
public:
	ResetSetting(std::shared_ptr<Setting> setting) : setting(setting), oldValue(setting->get()) {}
	~ResetSetting()
	{
		setting->set(oldValue);
	}

	std::shared_ptr<Setting> setting;
	QVariant oldValue;
};

class UpdateCheckerTest : public QObject
{
	Q_OBJECT
private
slots:
	void initTestCase()
	{

	}
	void cleanupTestCase()
	{

	}

	static QString findTestDataUrl(const char *file)
	{
		return QUrl::fromLocalFile(QFINDTESTDATA(file)).toString();
	}
	void tst_ChannelListParsing_data()
	{
		QTest::addColumn<QString>("channel");
		QTest::addColumn<QString>("channelUrl");
		QTest::addColumn<bool>("hasChannels");
		QTest::addColumn<bool>("valid");
		QTest::addColumn<QList<UpdateChecker::ChannelListEntry> >("result");

		QTest::newRow("garbage")
				<< QString()
				<< findTestDataUrl("tests/data/garbageChannels.json")
				<< false
				<< false
				<< QList<UpdateChecker::ChannelListEntry>();
		QTest::newRow("errors")
				<< QString()
				<< findTestDataUrl("tests/data/errorChannels.json")
				<< false
				<< true
				<< QList<UpdateChecker::ChannelListEntry>();
		QTest::newRow("no channels")
				<< QString()
				<< findTestDataUrl("tests/data/noChannels.json")
				<< false
				<< true
				<< QList<UpdateChecker::ChannelListEntry>();
		QTest::newRow("one channel")
				<< QString("develop")
				<< findTestDataUrl("tests/data/oneChannel.json")
				<< true
				<< true
				<< (QList<UpdateChecker::ChannelListEntry>() << UpdateChecker::ChannelListEntry{"develop", "Develop", "The channel called \"develop\"", "http://example.org/stuff"});
		QTest::newRow("several channels")
				<< QString("develop")
				<< findTestDataUrl("tests/data/channels.json")
				<< true
				<< true
				<< (QList<UpdateChecker::ChannelListEntry>()
					<< UpdateChecker::ChannelListEntry{"develop", "Develop", "The channel called \"develop\"", MultiMC_TEST_DATA_PATH}
					<< UpdateChecker::ChannelListEntry{"stable", "Stable", "It's stable at least", MultiMC_TEST_DATA_PATH}
					<< UpdateChecker::ChannelListEntry{"42", "The Channel", "This is the channel that is going to answer all of your questions", "https://dent.me/tea"});
	}
	void tst_ChannelListParsing()
	{
		ResetSetting resetUpdateChannel(MMC->settings()->getSetting("UpdateChannel"));

		QFETCH(QString, channel);
		QFETCH(QString, channelUrl);
		QFETCH(bool, hasChannels);
		QFETCH(bool, valid);
		QFETCH(QList<UpdateChecker::ChannelListEntry>, result);

		MMC->settings()->set("UpdateChannel", channel);

		UpdateChecker checker;

		QSignalSpy channelListLoadedSpy(&checker, SIGNAL(channelListLoaded()));
		QVERIFY(channelListLoadedSpy.isValid());

		checker.setChannelListUrl(channelUrl);

		checker.updateChanList();

		if (valid)
		{
			QVERIFY(channelListLoadedSpy.wait());
			QCOMPARE(channelListLoadedSpy.size(), 1);
		}
		else
		{
			channelListLoadedSpy.wait();
			QCOMPARE(channelListLoadedSpy.size(), 0);
		}

		QCOMPARE(checker.hasChannels(), hasChannels);
		QCOMPARE(checker.getChannelList(), result);
	}

	void tst_UpdateChecking_data()
	{
		QTest::addColumn<QString>("channel");
		QTest::addColumn<QString>("channelUrl");
		QTest::addColumn<int>("currentBuild");
		QTest::addColumn<QList<QVariant> >("result");

		QTest::newRow("valid channel")
				<< "develop" << findTestDataUrl("tests/data/channels.json")
				<< 2
				<< (QList<QVariant>() << QString() << "1.0.3" << 3);
	}
	void tst_UpdateChecking()
	{
		ResetSetting resetUpdateChannel(MMC->settings()->getSetting("UpdateChannel"));

		QFETCH(QString, channel);
		QFETCH(QString, channelUrl);
		QFETCH(int, currentBuild);
		QFETCH(QList<QVariant>, result);

		MMC->settings()->set("UpdateChannel", channel);
		BuildConfig.VERSION_BUILD = currentBuild;

		UpdateChecker checker;
		checker.setChannelListUrl(channelUrl);

		QSignalSpy updateAvailableSpy(&checker, SIGNAL(updateAvailable(QString,QString,int)));
		QVERIFY(updateAvailableSpy.isValid());
		QSignalSpy channelListLoadedSpy(&checker, SIGNAL(channelListLoaded()));
		QVERIFY(channelListLoadedSpy.isValid());

		checker.updateChanList();
		QVERIFY(channelListLoadedSpy.wait());

		checker.m_channels[0].url = QUrl::fromLocalFile(QDir::current().absoluteFilePath("tests/data/")).toString();

		checker.checkForUpdate(false);

		QVERIFY(updateAvailableSpy.wait());
		QList<QVariant> res = result;
		res[0] = checker.m_channels[0].url;
		QCOMPARE(updateAvailableSpy.first(), res);
	}
};

QTEST_GUILESS_MAIN_MULTIMC(UpdateCheckerTest)

#include "tst_UpdateChecker.moc"