47
void shouldBeInitiallyEmpty()
49
QCOMPARE(model->rowCount(), 0);
52
void shouldNotAddEmptyUrl()
54
QCOMPARE(model->add(QUrl(), "empty URL", QUrl()), 0);
55
QCOMPARE(model->rowCount(), 0);
58
void shouldAddNewEntries()
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);
72
void shouldNotifyWhenAddingNewEntries()
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);
87
void shouldUpdateExistingEntry()
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);
98
void shouldNotifyWhenUpdatingExistingEntry()
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));
136
void shouldUpdateTimestamp()
138
QDateTime now = QDateTime::currentDateTimeUtc();
140
model->add(QUrl("http://example.org/"), "Example Domain", QUrl());
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();
149
void shouldReturnDatabasePath()
151
QCOMPARE(model->databasePath(), QString(":memory:"));
154
void shouldNotifyWhenSettingDatabasePath()
156
QSignalSpy spyPath(model, SIGNAL(databasePathChanged()));
157
QSignalSpy spyReset(model, SIGNAL(modelReset()));
159
model->setDatabasePath(":memory:");
160
QVERIFY(spyPath.isEmpty());
161
QVERIFY(spyReset.isEmpty());
163
model->setDatabasePath("");
164
QCOMPARE(spyPath.count(), 1);
165
QCOMPARE(spyReset.count(), 1);
166
QCOMPARE(model->databasePath(), QString(":memory:"));
169
void shouldSerializeOnDisk()
171
QTemporaryFile tempFile;
173
QString fileName = tempFile.fileName();
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());
180
model = new HistoryModel;
181
model->setDatabasePath(fileName);
182
QCOMPARE(model->rowCount(), 2);
46
void shouldHaveThreeItems()
48
QCOMPARE(model->rowCount(), 3);
186
QTEST_MAIN(HistoryModelTests)
187
#include "tst_HistoryModelTests.moc"
53
QTEST_MAIN(ExampleModelTests)
54
#include "tst_ExampleModelTests.moc"