~ci-train-bot/ubuntu-system-settings/ubuntu-system-settings-ubuntu-yakkety-landing-027

« back to all changes in this revision

Viewing changes to tests/plugins/system-update/tst_updatemodel.cpp

  • Committer: Bileto Bot
  • Author(s): jonas-drange
  • Date: 2016-08-17 11:18:05 UTC
  • mfrom: (1631.6.245 updates-rewrite)
  • Revision ID: ci-train-bot@canonical.com-20160817111805-moyvik9wloy79ul3
rewrite the system update panel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of system-settings
 
3
 *
 
4
 * Copyright (C) 2016 Canonical Ltd.
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify it
 
7
 * under the terms of the GNU General Public License version 3, as published
 
8
 * by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
12
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
13
 * PURPOSE.  See the GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <QDate>
 
20
#include <QSignalSpy>
 
21
#include <QSqlQuery>
 
22
#include <QTime>
 
23
#include <QTimeZone>
 
24
#include <QTest>
 
25
 
 
26
#include "update.h"
 
27
#include "updatemodel.h"
 
28
 
 
29
using namespace UpdatePlugin;
 
30
 
 
31
Q_DECLARE_METATYPE(QList<QSharedPointer<Update> >)
 
32
Q_DECLARE_METATYPE(QSharedPointer<Update>)
 
33
Q_DECLARE_METATYPE(Update::Kind)
 
34
Q_DECLARE_METATYPE(UpdateModel::Roles)
 
35
 
 
36
class TstUpdateModel : public QObject
 
37
{
 
38
    Q_OBJECT
 
39
private slots:
 
40
    void init()
 
41
    {
 
42
        m_model = new UpdateModel(":memory:");
 
43
        m_db = m_model->db();
 
44
        m_filter = new UpdateModelFilter(m_model);
 
45
        m_filter->setSourceModel(m_model);
 
46
    }
 
47
    void cleanup()
 
48
    {
 
49
        QSignalSpy destroyedSpy(m_model, SIGNAL(destroyed(QObject*)));
 
50
        m_model->deleteLater();
 
51
        QTRY_COMPARE(destroyedSpy.count(), 1);
 
52
    }
 
53
    QSharedPointer<Update> createUpdate()
 
54
    {
 
55
        return QSharedPointer<Update>(new Update);
 
56
    }
 
57
    QSharedPointer<Update> createUpdate(QString id, int rev)
 
58
    {
 
59
        auto update = createUpdate();
 
60
        update->setIdentifier(id);
 
61
        update->setRevision(rev);
 
62
        return update;
 
63
    }
 
64
    QSharedPointer<Update> createClickUpdate(QString id, int rev)
 
65
    {
 
66
        auto update = createUpdate(id, rev);
 
67
        update->setKind(Update::Kind::KindClick);
 
68
        return update;
 
69
    }
 
70
    QSharedPointer<Update> createImageUpdate(QString id, int rev)
 
71
    {
 
72
        auto update = createUpdate(id, rev);
 
73
        update->setKind(Update::Kind::KindImage);
 
74
        return update;
 
75
    }
 
76
    void testNoUpdates()
 
77
    {
 
78
        QCOMPARE(m_model->rowCount(), 0);
 
79
    }
 
80
    void testAdd()
 
81
    {
 
82
        auto update = createUpdate();
 
83
        update->setIdentifier("test.app");
 
84
        update->setRevision(1);
 
85
 
 
86
        QSignalSpy insertedSpy(m_model, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int, int)));
 
87
        m_db->add(update);
 
88
        QTRY_COMPARE(insertedSpy.count(), 1);
 
89
        QCOMPARE(m_model->rowCount(), 1);
 
90
    }
 
91
    void testAddMultiple()
 
92
    {
 
93
        auto a = createUpdate();
 
94
        a->setIdentifier("a.app");
 
95
        a->setRevision(1);
 
96
        auto b = createUpdate();
 
97
        b->setIdentifier("b.app");
 
98
        b->setRevision(1);
 
99
        auto c = createUpdate();
 
100
        c->setIdentifier("c.app");
 
101
        c->setRevision(1);
 
102
 
 
103
        m_db->add(a);
 
104
        m_db->add(b);
 
105
        m_db->add(c);
 
106
 
 
107
        QCOMPARE(m_model->rowCount(), 3);
 
108
    }
 
109
    void testRemove()
 
110
    {
 
111
        auto update = createUpdate();
 
112
        update->setIdentifier("a.app");
 
113
        update->setRevision(1);
 
114
 
 
115
        m_db->add(update);
 
116
        QSignalSpy removeSpy(m_model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)));
 
117
        m_model->remove(update);
 
118
        QTRY_COMPARE(removeSpy.count(), 1);
 
119
        QCOMPARE(m_model->rowCount(), 0);
 
120
 
 
121
        m_db->add(update);
 
122
        m_model->remove(update->identifier(), update->revision());
 
123
        QTRY_COMPARE(removeSpy.count(), 2);
 
124
        QCOMPARE(m_model->rowCount(), 0);
 
125
    }
 
126
    void testChange()
 
127
    {
 
128
        auto update = createUpdate();
 
129
        update->setIdentifier("test.app");
 
130
        update->setRevision(1);
 
131
        update->setTitle("old");
 
132
        m_db->add(update);
 
133
 
 
134
        update->setTitle("updated");
 
135
        QSignalSpy dataChangedSpy(m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
 
136
        m_db->add(update);
 
137
        QTRY_COMPARE(dataChangedSpy.count(), 1);
 
138
        QList<QVariant> args = dataChangedSpy.takeFirst();
 
139
        QCOMPARE(args.at(0).toInt(), 0);
 
140
    }
 
141
    void testMultipleChanges()
 
142
    {
 
143
        auto a = createUpdate();
 
144
        a->setIdentifier("a.app");
 
145
        a->setRevision(1);
 
146
        a->setTitle("a");
 
147
 
 
148
        auto b = createUpdate();
 
149
        b->setIdentifier("b.app");
 
150
        b->setRevision(1);
 
151
        b->setTitle("b");
 
152
 
 
153
        auto c = createUpdate();
 
154
        c->setIdentifier("c.app");
 
155
        c->setRevision(1);
 
156
        c->setTitle("c");
 
157
 
 
158
        m_db->add(a);
 
159
        m_db->add(b);
 
160
        m_db->add(c);
 
161
 
 
162
        QVector<QSharedPointer<Update>> list;
 
163
        list << a << b << c;
 
164
 
 
165
        // Change three titles.
 
166
        Q_FOREACH(auto update, list) {
 
167
            QSqlQuery q(m_db->db());
 
168
            q.prepare("UPDATE updates SET title=:title WHERE id=:id AND revision=:revision");
 
169
            q.bindValue(":title", update->title() + "-new");
 
170
            q.bindValue(":id", update->identifier());
 
171
            q.bindValue(":revision", update->revision());
 
172
            q.exec();
 
173
            q.finish();
 
174
        }
 
175
 
 
176
        QSignalSpy dataChangedSpy(m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
 
177
        m_model->refresh();
 
178
        QTRY_COMPARE(dataChangedSpy.count(), 3);
 
179
        QList<QVariant> args = dataChangedSpy.takeFirst();
 
180
    }
 
181
    void testRoles_data()
 
182
    {
 
183
        QTest::addColumn<UpdateModel::Roles>("role");
 
184
        QTest::addColumn<QVariant>("value");
 
185
        QTest::addColumn<QSharedPointer<Update>>("update");
 
186
 
 
187
        {
 
188
            auto update = createUpdate("test", 1);
 
189
            UpdateModel::Roles role(UpdateModel::KindRole);
 
190
            QVariant value((uint) Update::Kind::KindClick);
 
191
            update->setKind(Update::Kind::KindClick);
 
192
            QTest::newRow("KindRole") << role << value << update;
 
193
        }
 
194
        {
 
195
            auto update = createUpdate("test", 1);
 
196
            UpdateModel::Roles role(UpdateModel::IdRole);
 
197
            QVariant value("test");
 
198
            QTest::newRow("IdRole") << role << value << update;
 
199
        }
 
200
        {
 
201
            auto update = createUpdate("test", 1);
 
202
            UpdateModel::Roles role(UpdateModel::LocalVersionRole);
 
203
            QVariant value("v1");
 
204
            update->setLocalVersion("v1");
 
205
            QTest::newRow("IdRole") << role << value << update;
 
206
        }
 
207
        {
 
208
            auto update = createUpdate("test", 1);
 
209
            UpdateModel::Roles role(UpdateModel::RemoteVersionRole);
 
210
            QVariant value("0.2");
 
211
            update->setRemoteVersion("0.2");
 
212
            QTest::newRow("RemoteVersionRole") << role << value << update;
 
213
        }
 
214
        {
 
215
            auto update = createUpdate("test", 1);
 
216
            UpdateModel::Roles role(UpdateModel::RevisionRole);
 
217
            QVariant value(42);
 
218
            update->setRevision(42);
 
219
            QTest::newRow("RevisionRole") << role << value << update;
 
220
        }
 
221
        {
 
222
            auto update = createUpdate("test", 1);
 
223
            UpdateModel::Roles role(UpdateModel::TitleRole);
 
224
            QVariant value("Test App");
 
225
            update->setTitle("Test App");
 
226
            QTest::newRow("TitleRole") << role << value << update;
 
227
        }
 
228
        {
 
229
            auto update = createUpdate("test", 1);
 
230
            UpdateModel::Roles role(UpdateModel::DownloadHashRole);
 
231
            QVariant value("987654323456789");
 
232
            update->setDownloadHash("987654323456789");
 
233
            QTest::newRow("DownloadHashRole") << role << value << update;
 
234
        }
 
235
        {
 
236
            auto update = createUpdate("test", 1);
 
237
            UpdateModel::Roles role(UpdateModel::DownloadIdRole);
 
238
            QVariant value("someuuid");
 
239
            update->setDownloadId("someuuid");
 
240
            QTest::newRow("DownloadIdRole") << role << value << update;
 
241
        }
 
242
        {
 
243
            auto update = createUpdate("test", 1);
 
244
            UpdateModel::Roles role(UpdateModel::SizeRole);
 
245
            QVariant value(1000);
 
246
            update->setBinaryFilesize(1000);
 
247
            QTest::newRow("SizeRole") << role << value << update;
 
248
        }
 
249
        {
 
250
            auto update = createUpdate("test", 1);
 
251
            UpdateModel::Roles role(UpdateModel::IconUrlRole);
 
252
            QVariant value("http://example.org/testapp.png");
 
253
            update->setIconUrl("http://example.org/testapp.png");
 
254
            QTest::newRow("IconUrlRole") << role << value << update;
 
255
        }
 
256
        {
 
257
            auto update = createUpdate("test", 1);
 
258
            UpdateModel::Roles role(UpdateModel::DownloadUrlRole);
 
259
            QVariant value("http://example.org/testapp.click");
 
260
            update->setDownloadUrl("http://example.org/testapp.click");
 
261
            QTest::newRow("DownloadUrlRole") << role << value << update;
 
262
        }
 
263
        {
 
264
            auto update = createUpdate("test", 1);
 
265
            UpdateModel::Roles role(UpdateModel::CommandRole);
 
266
            QStringList mc;
 
267
            mc << "ls" << "-la";
 
268
            QVariant value(mc);
 
269
            update->setCommand(mc);
 
270
            QTest::newRow("CommandRole") << role << value << update;
 
271
        }
 
272
        {
 
273
            auto update = createUpdate("test", 1);
 
274
            UpdateModel::Roles role(UpdateModel::ChangelogRole);
 
275
            QVariant value("* Fixed all bugs * Introduced new bugs");
 
276
            update->setChangelog("* Fixed all bugs * Introduced new bugs");
 
277
            QTest::newRow("ChangelogRole") << role << value << update;
 
278
        }
 
279
        {
 
280
            auto update = createUpdate("test", 1);
 
281
            UpdateModel::Roles role(UpdateModel::TokenRole);
 
282
            QVariant value("token");
 
283
            update->setToken("token");
 
284
            QTest::newRow("TokenRole") << role << value << update;
 
285
        }
 
286
        {
 
287
            auto update = createUpdate("test", 1);
 
288
            UpdateModel::Roles role(UpdateModel::ProgressRole);
 
289
            QVariant value(50);
 
290
            update->setProgress(50);
 
291
            QTest::newRow("ProgressRole") << role << value << update;
 
292
        }
 
293
        {
 
294
            auto update = createUpdate("test", 1);
 
295
            UpdateModel::Roles role(UpdateModel::UpdateStateRole);
 
296
            QVariant value((uint) Update::State::StateInstallPaused);
 
297
            update->setState(Update::State::StateInstallPaused);
 
298
            QTest::newRow("UpdateStateRole") << role << value << update;
 
299
        }
 
300
        {
 
301
            auto update = createUpdate("test", 1);
 
302
            UpdateModel::Roles role(UpdateModel::ErrorRole);
 
303
            QVariant value("Failure");
 
304
            update->setError("Failure");
 
305
            QTest::newRow("ErrorRole") << role << value << update;
 
306
        }
 
307
        {
 
308
            auto update = createUpdate("test", 1);
 
309
            UpdateModel::Roles role(UpdateModel::PackageNameRole);
 
310
            QVariant value("testapp");
 
311
            update->setPackageName("testapp");
 
312
            QTest::newRow("PackageNameRole") << role << value << update;
 
313
        }
 
314
        {
 
315
            auto update = createUpdate("test", 1);
 
316
            UpdateModel::Roles role(UpdateModel::SignedDownloadUrlRole);
 
317
            QVariant value("signedUrl");
 
318
            update->setSignedDownloadUrl("signedUrl");
 
319
            QTest::newRow("SignedDownloadUrlRole") << role << value << update;
 
320
        }
 
321
        {
 
322
            auto update = createUpdate("test", 1);
 
323
            auto target = QDateTime::currentDateTimeUtc();
 
324
            UpdateModel::Roles role(UpdateModel::UpdatedAtRole);
 
325
            QVariant value(target);
 
326
            update->setUpdatedAt(target);
 
327
            QTest::newRow("UpdatedAtRole") << role << value << update;
 
328
        }
 
329
    }
 
330
    void testRoles()
 
331
    {
 
332
        QFETCH(UpdateModel::Roles, role);
 
333
        QFETCH(QVariant, value);
 
334
        QFETCH(QSharedPointer<Update>, update);
 
335
 
 
336
        m_model->add(update);
 
337
        QCOMPARE(m_model->data(m_model->index(0), role), value);
 
338
    }
 
339
    void testCreatedAtRole()
 
340
    {
 
341
        m_model->add(createUpdate("test", 1));
 
342
        // Just make sure the createdAt role isn't empty.
 
343
        QVERIFY(m_model->data(
 
344
            m_model->index(0),
 
345
            UpdateModel::CreatedAtRole).toDateTime().isValid()
 
346
        );
 
347
    }
 
348
    void testNotFound()
 
349
    {
 
350
        auto notFound = m_model->get("notfound", 0);
 
351
        QVERIFY(notFound.isNull());
 
352
    }
 
353
    void testImageUpdate()
 
354
    {
 
355
        m_model->setImageUpdate("ubuntu", 350, 400000);
 
356
        QCOMPARE(m_model->rowCount(), 1);
 
357
        QCOMPARE(m_model->data(
 
358
            m_model->index(0), UpdateModel::IdRole
 
359
        ).toString(), QString("ubuntu"));
 
360
        QCOMPARE(m_model->data(
 
361
            m_model->index(0), UpdateModel::RevisionRole
 
362
        ).toUInt(), (uint) 350);
 
363
        QCOMPARE(m_model->data(
 
364
            m_model->index(0), UpdateModel::AutomaticRole
 
365
        ).toBool(), false);
 
366
    }
 
367
    void testSetAvailable()
 
368
    {
 
369
        auto update = createUpdate("id", 42);
 
370
        update->setError("Some error");
 
371
        update->setProgress(95);
 
372
        update->setToken("foobar");
 
373
        m_db->add(update);
 
374
        m_model->setAvailable(update->identifier(), update->revision());
 
375
 
 
376
        auto update1 = m_model->get(update->identifier(), update->revision());
 
377
 
 
378
        QVERIFY(!update1.isNull());
 
379
 
 
380
        QCOMPARE(update1->error(), QString(""));
 
381
        QCOMPARE(update1->progress(), 0);
 
382
        QCOMPARE(update1->token(), QString(""));
 
383
        QCOMPARE(update1->downloadId(), QString(""));
 
384
        QCOMPARE(update1->state(), Update::State::StateAvailable);
 
385
    }
 
386
    void testSetInstalled()
 
387
    {
 
388
        auto update = createUpdate("id", 42);
 
389
        m_db->add(update);
 
390
        m_model->setInstalled(update->identifier(), update->revision());
 
391
        auto update1 = m_db->get(update->identifier(), update->revision());
 
392
        QCOMPARE(update1->state(), Update::State::StateInstallFinished);
 
393
        QCOMPARE(update1->downloadId(), QString(""));
 
394
    }
 
395
    void testSetError()
 
396
    {
 
397
        auto update = createUpdate("id", 42);
 
398
        m_db->add(update);
 
399
        m_model->setError(update->identifier(), update->revision(), "fail");
 
400
 
 
401
        auto update1 = m_db->get(update->identifier(), update->revision());
 
402
        QCOMPARE(update1->state(), Update::State::StateFailed);
 
403
        QCOMPARE(update1->downloadId(), QString(""));
 
404
    }
 
405
    void testSetProgress()
 
406
    {
 
407
        auto update = createUpdate("id", 42);
 
408
        m_db->add(update);
 
409
        m_model->setProgress(update->identifier(), update->revision(), 5);
 
410
        auto update1 = m_db->get(update->identifier(), update->revision());
 
411
        QCOMPARE(update1->state(), Update::State::StateDownloading);
 
412
        QCOMPARE(update1->progress(), 5);
 
413
    }
 
414
    void testSetInstalling()
 
415
    {
 
416
        auto update = createUpdate("id", 42);
 
417
        m_db->add(update);
 
418
        m_model->setInstalling(update->identifier(), update->revision(), 5);
 
419
        auto update1 = m_db->get(update->identifier(), update->revision());
 
420
        QCOMPARE(update1->state(), Update::State::StateInstalling);
 
421
        QCOMPARE(update1->progress(), 5);
 
422
    }
 
423
    void testSetDownloaded()
 
424
    {
 
425
        auto update = createUpdate("id", 42);
 
426
        m_db->add(update);
 
427
        m_model->setDownloaded(update->identifier(), update->revision());
 
428
        QCOMPARE(m_db->get(update->identifier(), update->revision())->state(),
 
429
                 Update::State::StateDownloaded);
 
430
    }
 
431
    void testStartUpdate()
 
432
    {
 
433
        auto update = createUpdate("id", 42);
 
434
        m_db->add(update);
 
435
        m_model->startUpdate(update->identifier(), update->revision());
 
436
        QCOMPARE(m_db->get(update->identifier(), update->revision())->state(),
 
437
                 Update::State::StateDownloading);
 
438
    }
 
439
    void testQueueUpdate()
 
440
    {
 
441
        auto update = createUpdate("id", 42);
 
442
        QString targetDid("someId");
 
443
        m_db->add(update);
 
444
        m_model->queueUpdate(update->identifier(), update->revision(), targetDid);
 
445
        auto update1 = m_db->get(update->identifier(), update->revision());
 
446
        QCOMPARE(update1->state(), Update::State::StateQueuedForDownload);
 
447
        QCOMPARE(update1->downloadId(), targetDid);
 
448
    }
 
449
    void testProcessUpdate()
 
450
    {
 
451
        auto update = createUpdate("id", 42);
 
452
        m_db->add(update);
 
453
        m_model->processUpdate(update->identifier(), update->revision());
 
454
        QCOMPARE(m_db->get(update->identifier(), update->revision())->state(),
 
455
                 Update::State::StateInstalling);
 
456
    }
 
457
    void testPauseUpdate()
 
458
    {
 
459
        auto update = createUpdate("id", 42);
 
460
        m_db->add(update);
 
461
        m_model->pauseUpdate(update->identifier(), update->revision());
 
462
        QCOMPARE(m_db->get(update->identifier(), update->revision())->state(),
 
463
                 Update::State::StateDownloadPaused);
 
464
    }
 
465
    void testResumeUpdate()
 
466
    {
 
467
        auto update = createUpdate("id", 42);
 
468
        m_db->add(update);
 
469
        m_model->resumeUpdate(update->identifier(), update->revision());
 
470
        QCOMPARE(m_db->get(update->identifier(), update->revision())->state(),
 
471
                 Update::State::StateDownloading);
 
472
    }
 
473
    void testCancelUpdate()
 
474
    {
 
475
        auto update = createUpdate("id", 42);
 
476
        m_db->add(update);
 
477
        m_model->cancelUpdate(update->identifier(), update->revision());
 
478
 
 
479
        auto update1 = m_model->get(update->identifier(), update->revision());
 
480
        QCOMPARE(update1->error(), QString(""));
 
481
        QCOMPARE(update1->progress(), 0);
 
482
        QCOMPARE(update1->token(), QString(""));
 
483
        QCOMPARE(update1->downloadId(), QString(""));
 
484
        QCOMPARE(update1->state(), Update::State::StateAvailable);
 
485
    }
 
486
    void testRoleNames()
 
487
    {
 
488
        QHash<int, QByteArray> names = m_model->roleNames();
 
489
        QVERIFY(names[Qt::DisplayRole] == "displayName");
 
490
        QVERIFY(names[UpdateModel::Roles::KindRole] == "kind");
 
491
        QVERIFY(names[UpdateModel::Roles::IconUrlRole] == "iconUrl");
 
492
        QVERIFY(names[UpdateModel::Roles::IdRole] == "identifier");
 
493
        QVERIFY(names[UpdateModel::Roles::LocalVersionRole] == "localVersion");
 
494
        QVERIFY(names[UpdateModel::Roles::RemoteVersionRole] == "remoteVersion");
 
495
        QVERIFY(names[UpdateModel::Roles::RevisionRole] == "revision");
 
496
        QVERIFY(names[UpdateModel::Roles::InstalledRole] == "installed");
 
497
        QVERIFY(names[UpdateModel::Roles::CreatedAtRole] == "createdAt");
 
498
        QVERIFY(names[UpdateModel::Roles::UpdatedAtRole] == "updatedAt");
 
499
        QVERIFY(names[UpdateModel::Roles::TitleRole] == "title");
 
500
        QVERIFY(names[UpdateModel::Roles::DownloadHashRole] == "downloadHash");
 
501
        QVERIFY(names[UpdateModel::Roles::SizeRole] == "size");
 
502
        QVERIFY(names[UpdateModel::Roles::DownloadUrlRole] == "downloadUrl");
 
503
        QVERIFY(names[UpdateModel::Roles::ChangelogRole] == "changelog");
 
504
        QVERIFY(names[UpdateModel::Roles::CommandRole] == "command");
 
505
        QVERIFY(names[UpdateModel::Roles::TokenRole] == "token");
 
506
        QVERIFY(names[UpdateModel::Roles::UpdateStateRole] == "updateState");
 
507
        QVERIFY(names[UpdateModel::Roles::ProgressRole] == "progress");
 
508
        QVERIFY(names[UpdateModel::Roles::AutomaticRole] == "automatic");
 
509
        QVERIFY(names[UpdateModel::Roles::ErrorRole] == "error");
 
510
        QVERIFY(names[UpdateModel::Roles::PackageNameRole] == "packageName");
 
511
        QVERIFY(names[UpdateModel::Roles::SignedDownloadUrlRole] == "signedDownloadUrl");
 
512
    }
 
513
    void testFilterKinds_data()
 
514
    {
 
515
        QTest::addColumn<Update::Kind>("kind");
 
516
        QTest::newRow("Unknown") << Update::Kind::KindUnknown;
 
517
        QTest::newRow("Click") << Update::Kind::KindClick;
 
518
        QTest::newRow("Images") << Update::Kind::KindImage;
 
519
    }
 
520
    void testFilterKinds()
 
521
    {
 
522
        QFETCH(Update::Kind, kind);
 
523
 
 
524
        m_filter->filterOnKind((uint) kind);
 
525
        QCOMPARE((uint) m_filter->kindFilter(), (uint) kind);
 
526
    }
 
527
    void testFilterInstalled_data()
 
528
    {
 
529
        QTest::addColumn<bool>("installed");
 
530
 
 
531
        QTest::newRow("Installed") << true;
 
532
        QTest::newRow("Not installed") << false;
 
533
    }
 
534
    void testFilterInstalled()
 
535
    {
 
536
        QFETCH(bool, installed);
 
537
 
 
538
        m_filter->filterOnInstalled(installed);
 
539
        QCOMPARE(m_filter->installed(), installed);
 
540
    }
 
541
    void testFilterKindsIntegration_data()
 
542
    {
 
543
        QTest::addColumn<QList<QSharedPointer<Update>> >("updates");
 
544
        QTest::addColumn<Update::Kind>("kindFilter");
 
545
        QTest::addColumn<int>("targetCount");
 
546
 
 
547
        QList<QSharedPointer<Update>> sample;
 
548
        sample << createClickUpdate("a", 1) << createClickUpdate("b", 2)
 
549
               << createImageUpdate("u", 1);
 
550
 
 
551
        QTest::newRow("Filter on clicks") << sample << Update::Kind::KindClick << 2;
 
552
        QTest::newRow("Filter on images") << sample << Update::Kind::KindImage << 1;
 
553
 
 
554
    }
 
555
    void testFilterKindsIntegration()
 
556
    {
 
557
        QFETCH(QList<QSharedPointer<Update> >, updates);
 
558
        QFETCH(Update::Kind, kindFilter);
 
559
        QFETCH(int, targetCount);
 
560
 
 
561
        Q_FOREACH(auto update, updates) {
 
562
            m_model->add(update);
 
563
        }
 
564
 
 
565
        m_filter->filterOnKind((uint) kindFilter);
 
566
        QCOMPARE(m_filter->rowCount(), targetCount);
 
567
    }
 
568
    void testFilteringInstalledIntegration_data()
 
569
    {
 
570
        QTest::addColumn<QList<QSharedPointer<Update>> >("updates");
 
571
        QTest::addColumn<bool>("installedFilter");
 
572
        QTest::addColumn<int>("targetCount");
 
573
 
 
574
        auto installed = createUpdate("a", 1);
 
575
        installed->setInstalled(true);
 
576
 
 
577
        QList<QSharedPointer<Update>> sample;
 
578
        sample << createUpdate("b", 1) << createUpdate("c", 1) << installed;
 
579
 
 
580
        QTest::newRow("Filter not installed") << sample << false << 2;
 
581
        QTest::newRow("Filter installed") << sample << true << 1;
 
582
    }
 
583
    void testFilteringInstalledIntegration()
 
584
    {
 
585
        QFETCH(QList<QSharedPointer<Update> >, updates);
 
586
        QFETCH(bool, installedFilter);
 
587
        QFETCH(int, targetCount);
 
588
 
 
589
        Q_FOREACH(auto update, updates) {
 
590
            m_model->add(update);
 
591
        }
 
592
 
 
593
        m_filter->filterOnInstalled(installedFilter);
 
594
        QCOMPARE(m_filter->rowCount(), targetCount);
 
595
    }
 
596
    void testPendingSort_data()
 
597
    {
 
598
        QTest::addColumn<QList<QSharedPointer<Update>> >("updates");
 
599
        QTest::addColumn<QStringList>("titleOrder");
 
600
 
 
601
        auto a = createUpdate("a", 1);
 
602
        a->setTitle("A");
 
603
        a->setInstalled(false);
 
604
        auto b = createUpdate("b", 1);
 
605
        b->setTitle("B");
 
606
        b->setInstalled(false);
 
607
        auto c = createUpdate("c", 1);
 
608
        c->setTitle("C");
 
609
        c->setInstalled(false);
 
610
        QList<QSharedPointer<Update> > updates1; updates1 << a << b << c;
 
611
        QStringList order1; order1 << "A" << "B" << "C";
 
612
        QTest::newRow("abc") << updates1 << order1;
 
613
 
 
614
        QList<QSharedPointer<Update> > updates2; updates2 << c << b << a;
 
615
        QTest::newRow("cba") << updates2 << order1;
 
616
    }
 
617
    void testPendingSort()
 
618
    {
 
619
        QFETCH(QList<QSharedPointer<Update>>, updates);
 
620
        QFETCH(QStringList, titleOrder);
 
621
 
 
622
        m_filter->filterOnInstalled(false);
 
623
        Q_FOREACH(auto update, updates) {
 
624
            m_model->add(update);
 
625
        }
 
626
 
 
627
        QCOMPARE(m_filter->rowCount(), 3);
 
628
        for (int i = 0; i < m_filter->rowCount(); i++) {
 
629
            QCOMPARE(
 
630
                 m_filter->data(m_filter->index(i, 0), UpdateModel::TitleRole).toString(),
 
631
                 titleOrder.at(i)
 
632
            );
 
633
        }
 
634
    }
 
635
    void testInstalledSort_data()
 
636
    {
 
637
        QTest::addColumn<QList<QSharedPointer<Update>> >("updates");
 
638
        QTest::addColumn<QStringList>("idOrder");
 
639
 
 
640
        auto old = createUpdate("old", 1);
 
641
        old->setUpdatedAt(QDateTime(QDate(2012, 1, 1)));
 
642
        old->setInstalled(true);
 
643
        auto older = createUpdate("older", 1);
 
644
        older->setUpdatedAt(QDateTime(QDate(2011, 1, 1)));
 
645
        older->setInstalled(true);
 
646
        auto oldest = createUpdate("oldest", 1);
 
647
        oldest->setUpdatedAt(QDateTime(QDate(2010, 1, 1)));
 
648
        oldest->setInstalled(true);
 
649
        QList<QSharedPointer<Update> > updates1; updates1 << old << older << oldest;
 
650
        QStringList order1; order1 << "old" << "older" << "oldest";
 
651
        QTest::newRow("old, older, oldest") << updates1 << order1;
 
652
 
 
653
        QList<QSharedPointer<Update> > updates2; updates2 << oldest << older << old;
 
654
        QTest::newRow("oldest, older, old") << updates2 << order1;
 
655
    }
 
656
    void testInstalledSort()
 
657
    {
 
658
        QFETCH(QList<QSharedPointer<Update>>, updates);
 
659
        QFETCH(QStringList, idOrder);
 
660
 
 
661
        m_filter->filterOnInstalled(true);
 
662
        Q_FOREACH(auto update, updates) {
 
663
            m_model->add(update);
 
664
        }
 
665
 
 
666
        QCOMPARE(m_filter->rowCount(), 3);
 
667
        for (int i = 0; i < m_filter->rowCount(); i++) {
 
668
            QCOMPARE(
 
669
                 m_filter->data(m_filter->index(i, 0), UpdateModel::IdRole).toString(),
 
670
                 idOrder.at(i)
 
671
            );
 
672
        }
 
673
    }
 
674
private:
 
675
    UpdateDb *m_db = nullptr;
 
676
    UpdateModel *m_model = nullptr;
 
677
    UpdateModelFilter *m_filter = nullptr;
 
678
};
 
679
 
 
680
QTEST_GUILESS_MAIN(TstUpdateModel)
 
681
#include "tst_updatemodel.moc"