~ps-jenkins/ubuntu-ui-extras/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/unittests/tst_ExampleModelTests.cpp

  • Committer: Ugo Riboni
  • Date: 2013-07-02 19:40:38 UTC
  • Revision ID: ugo.riboni@canonical.com-20130702194038-v9l2q3v2xxjjxn8v
Remove all browser work and leave only an example component and skeleton build system

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright 2013 Canonical Ltd.
3
3
 *
4
 
 * This file is part of webbrowser-app.
 
4
 * This file is part of ubuntu-ui-extras.
5
5
 *
6
 
 * webbrowser-app is free software; you can redistribute it and/or modify
 
6
 * ubuntu-ui-extras is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
8
 * the Free Software Foundation; version 3.
9
9
 *
10
 
 * webbrowser-app is distributed in the hope that it will be useful,
 
10
 * ubuntu-ui-extras is distributed in the hope that it will be useful,
11
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
 * GNU General Public License for more details.
23
23
#include <QtTest/QtTest>
24
24
 
25
25
// local
26
 
#include "history-model.h"
 
26
#include "example-model.h"
27
27
 
28
 
class HistoryModelTests : public QObject
 
28
class ExampleModelTests : public QObject
29
29
{
30
30
    Q_OBJECT
31
31
 
32
32
private:
33
 
    HistoryModel* model;
 
33
    ExampleModel* model;
34
34
 
35
35
private Q_SLOTS:
36
36
    void init()
37
37
    {
38
 
        model = new HistoryModel;
39
 
        model->setDatabasePath(":memory:");
 
38
        model = new ExampleModel;
40
39
    }
41
40
 
42
41
    void cleanup()
44
43
        delete model;
45
44
    }
46
45
 
47
 
    void shouldBeInitiallyEmpty()
48
 
    {
49
 
        QCOMPARE(model->rowCount(), 0);
50
 
    }
51
 
 
52
 
    void shouldNotAddEmptyUrl()
53
 
    {
54
 
        QCOMPARE(model->add(QUrl(), "empty URL", QUrl()), 0);
55
 
        QCOMPARE(model->rowCount(), 0);
56
 
    }
57
 
 
58
 
    void shouldAddNewEntries()
59
 
    {
60
 
        QCOMPARE(model->add(QUrl("http://example.org/"), "Example Domain", QUrl()), 1);
61
 
        QCOMPARE(model->rowCount(), 1);
62
 
        QCOMPARE(model->add(QUrl("http://example.com/"), "Example Domain", QUrl()), 1);
63
 
        QCOMPARE(model->rowCount(), 2);
64
 
        QCOMPARE(model->data(model->index(0, 0), HistoryModel::Url).toString(),
65
 
                 QString("http://example.com/"));
66
 
        QCOMPARE(model->data(model->index(0, 0), HistoryModel::Visits).toInt(), 1);
67
 
        QCOMPARE(model->data(model->index(1, 0), HistoryModel::Url).toString(),
68
 
                 QString("http://example.org/"));
69
 
        QCOMPARE(model->data(model->index(1, 0), HistoryModel::Visits).toInt(), 1);
70
 
    }
71
 
 
72
 
    void shouldNotifyWhenAddingNewEntries()
73
 
    {
74
 
        QSignalSpy spy(model, SIGNAL(rowsInserted(const QModelIndex&, int, int)));
75
 
        model->add(QUrl("http://example.org/"), "Example Domain", QUrl());
76
 
        QCOMPARE(spy.count(), 1);
77
 
        QList<QVariant> args = spy.takeFirst();
78
 
        QCOMPARE(args.at(1).toInt(), 0);
79
 
        QCOMPARE(args.at(2).toInt(), 0);
80
 
        model->add(QUrl("http://example.com/"), "Example Domain", QUrl());
81
 
        QCOMPARE(spy.count(), 1);
82
 
        args = spy.takeFirst();
83
 
        QCOMPARE(args.at(1).toInt(), 0);
84
 
        QCOMPARE(args.at(2).toInt(), 0);
85
 
    }
86
 
 
87
 
    void shouldUpdateExistingEntry()
88
 
    {
89
 
        QCOMPARE(model->add(QUrl("http://example.org/"), "Example Domain", QUrl()), 1);
90
 
        QCOMPARE(model->rowCount(), 1);
91
 
        QCOMPARE(model->add(QUrl("http://example.org/"), "Example Domain", QUrl("image://webicon/123")), 2);
92
 
        QCOMPARE(model->rowCount(), 1);
93
 
        QCOMPARE(model->data(model->index(0, 0), HistoryModel::Url).toString(),
94
 
                 QString("http://example.org/"));
95
 
        QCOMPARE(model->data(model->index(0, 0), HistoryModel::Visits).toInt(), 2);
96
 
    }
97
 
 
98
 
    void shouldNotifyWhenUpdatingExistingEntry()
99
 
    {
100
 
        QSignalSpy spyMoved(model, SIGNAL(rowsMoved(const QModelIndex&, int, int, const QModelIndex&, int)));
101
 
        qRegisterMetaType<QVector<int> >();
102
 
        QSignalSpy spyChanged(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));
103
 
        model->add(QUrl("http://example.org/"), "Example Domain", QUrl());
104
 
        QCOMPARE(spyMoved.count(), 0);
105
 
        QCOMPARE(spyChanged.count(), 0);
106
 
        model->add(QUrl("http://example.org/"), "Example Domain", QUrl("image://webicon/123"));
107
 
        QCOMPARE(spyMoved.count(), 0);
108
 
        QCOMPARE(spyChanged.count(), 1);
109
 
        QList<QVariant> args = spyChanged.takeFirst();
110
 
        QCOMPARE(args.at(0).toModelIndex().row(), 0);
111
 
        QCOMPARE(args.at(1).toModelIndex().row(), 0);
112
 
        QVector<int> roles = args.at(2).value<QVector<int> >();
113
 
        QVERIFY(roles.size() >= 2);
114
 
        QVERIFY(roles.contains(HistoryModel::Icon));
115
 
        QVERIFY(roles.contains(HistoryModel::Visits));
116
 
        model->add(QUrl("http://example.com/"), "Example Domain", QUrl());
117
 
        QCOMPARE(spyMoved.count(), 0);
118
 
        QCOMPARE(spyChanged.count(), 0);
119
 
        model->add(QUrl("http://example.org/"), "Example D0ma1n", QUrl("image://webicon/456"));
120
 
        QCOMPARE(spyMoved.count(), 1);
121
 
        args = spyMoved.takeFirst();
122
 
        QCOMPARE(args.at(1).toInt(), 1);
123
 
        QCOMPARE(args.at(2).toInt(), 1);
124
 
        QCOMPARE(args.at(4).toInt(), 0);
125
 
        QCOMPARE(spyChanged.count(), 1);
126
 
        args = spyChanged.takeFirst();
127
 
        QCOMPARE(args.at(0).toModelIndex().row(), 0);
128
 
        QCOMPARE(args.at(1).toModelIndex().row(), 0);
129
 
        roles = args.at(2).value<QVector<int> >();
130
 
        QVERIFY(roles.size() >= 3);
131
 
        QVERIFY(roles.contains(HistoryModel::Title));
132
 
        QVERIFY(roles.contains(HistoryModel::Icon));
133
 
        QVERIFY(roles.contains(HistoryModel::Visits));
134
 
    }
135
 
 
136
 
    void shouldUpdateTimestamp()
137
 
    {
138
 
        QDateTime now = QDateTime::currentDateTimeUtc();
139
 
        QTest::qWait(1001);
140
 
        model->add(QUrl("http://example.org/"), "Example Domain", QUrl());
141
 
        QTest::qWait(1001);
142
 
        model->add(QUrl("http://example.com/"), "Example Domain", QUrl());
143
 
        QDateTime ts0 = model->data(model->index(0, 0), HistoryModel::LastVisit).toDateTime();
144
 
        QDateTime ts1 = model->data(model->index(1, 0), HistoryModel::LastVisit).toDateTime();
145
 
        QVERIFY(ts0 > ts1);
146
 
        QVERIFY(ts1 > now);
147
 
    }
148
 
 
149
 
    void shouldReturnDatabasePath()
150
 
    {
151
 
        QCOMPARE(model->databasePath(), QString(":memory:"));
152
 
    }
153
 
 
154
 
    void shouldNotifyWhenSettingDatabasePath()
155
 
    {
156
 
        QSignalSpy spyPath(model, SIGNAL(databasePathChanged()));
157
 
        QSignalSpy spyReset(model, SIGNAL(modelReset()));
158
 
 
159
 
        model->setDatabasePath(":memory:");
160
 
        QVERIFY(spyPath.isEmpty());
161
 
        QVERIFY(spyReset.isEmpty());
162
 
 
163
 
        model->setDatabasePath("");
164
 
        QCOMPARE(spyPath.count(), 1);
165
 
        QCOMPARE(spyReset.count(), 1);
166
 
        QCOMPARE(model->databasePath(), QString(":memory:"));
167
 
    }
168
 
 
169
 
    void shouldSerializeOnDisk()
170
 
    {
171
 
        QTemporaryFile tempFile;
172
 
        tempFile.open();
173
 
        QString fileName = tempFile.fileName();
174
 
        delete model;
175
 
        model = new HistoryModel;
176
 
        model->setDatabasePath(fileName);
177
 
        model->add(QUrl("http://example.org/"), "Example Domain", QUrl());
178
 
        model->add(QUrl("http://example.com/"), "Example Domain", QUrl());
179
 
        delete model;
180
 
        model = new HistoryModel;
181
 
        model->setDatabasePath(fileName);
182
 
        QCOMPARE(model->rowCount(), 2);
183
 
    }
 
46
    void shouldHaveThreeItems()
 
47
    {
 
48
        QCOMPARE(model->rowCount(), 3);
 
49
    }
 
50
 
184
51
};
185
52
 
186
 
QTEST_MAIN(HistoryModelTests)
187
 
#include "tst_HistoryModelTests.moc"
 
53
QTEST_MAIN(ExampleModelTests)
 
54
#include "tst_ExampleModelTests.moc"