~ci-train-bot/history-service/history-service-ubuntu-yakkety-landing-1774

« back to all changes in this revision

Viewing changes to tests/Ubuntu.History/HistoryGroupedThreadsModelTest.cpp

Initial cache implementation for grouped conversations.
Approved by: PS Jenkins bot, Gustavo Pichorim Boiko

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
private Q_SLOTS:
29
29
    void initTestCase();
30
30
    void testCanFetchMore();
 
31
    void testThreadsUpdated();
31
32
private:
32
33
    History::Manager *mManager;
33
34
};
43
44
    QSignalSpy fetchMoreChanged(&model, SIGNAL(canFetchMoreChanged()));
44
45
 
45
46
    // create a temporary thread to populate the model
46
 
    History::Thread textThread = mManager->threadForParticipants("accountId",
 
47
    History::Thread textThread = mManager->threadForParticipants("accountId0",
47
48
                                                             History::EventTypeText,
48
49
                                                             QStringList() << QString("textParticipant"),
49
50
                                                             History::MatchCaseSensitive, true);
52
53
    QVERIFY(!model.canFetchMore());
53
54
 
54
55
    HistoryQmlFilter *filter = new HistoryQmlFilter(this);
55
 
    filter->setFilterProperty(History::FieldAccountId);
56
 
    filter->setFilterValue("accountId");
57
 
 
58
56
    model.setFilter(filter);
 
57
    model.setGroupingProperty(History::FieldParticipants);
 
58
 
 
59
    HistoryQmlSort *sort = new HistoryQmlSort(this);
 
60
    sort->setSortOrder(HistoryQmlSort::DescendingOrder);
 
61
    sort->setSortField("lastEventTimestamp");
 
62
    model.setSort(sort);
 
63
 
59
64
    // force updateQuery() to be called
60
65
    model.componentComplete();
61
66
 
63
68
    model.fetchMore();
64
69
    QTRY_VERIFY(fetchMoreChanged.count() >= 1);
65
70
    QVERIFY(!model.canFetchMore());
 
71
 
 
72
    QTRY_COMPARE(model.rowCount(), 1);
 
73
    mManager->removeThreads(History::Threads() << textThread);
 
74
    QTRY_COMPARE(model.rowCount(), 0);
 
75
}
 
76
 
 
77
void HistoryGroupedThreadsModelTest::testThreadsUpdated()
 
78
{
 
79
    HistoryGroupedThreadsModel model;
 
80
    QSignalSpy dataChanged(&model, SIGNAL(dataChanged(QModelIndex, QModelIndex)));
 
81
    QSignalSpy rowsRemoved(&model, SIGNAL(rowsRemoved(QModelIndex, int, int)));
 
82
 
 
83
    HistoryQmlFilter *filter = new HistoryQmlFilter(this);
 
84
    model.setFilter(filter);
 
85
    model.setGroupingProperty(History::FieldParticipants);
 
86
 
 
87
    HistoryQmlSort *sort = new HistoryQmlSort(this);
 
88
    sort->setSortOrder(HistoryQmlSort::DescendingOrder);
 
89
    sort->setSortField("lastEventTimestamp");
 
90
    model.setSort(sort);
 
91
 
 
92
    // force updateQuery() to be called
 
93
    model.componentComplete();
 
94
 
 
95
    // create first thread
 
96
    History::Thread textThread = mManager->threadForParticipants("ofono/ofono/account0",
 
97
                                                             History::EventTypeText,
 
98
                                                             QStringList() << QString("1234567"),
 
99
                                                             History::MatchPhoneNumber, true);
 
100
 
 
101
    // insert one event
 
102
    History::TextEvent firstEvent = History::TextEvent(textThread.accountId(), textThread.threadId(), QString("eventId1%1").arg(QString::number(qrand() % 1024)),
 
103
                                   QString("1234567"), QDateTime::currentDateTime(), false, "Random Message",
 
104
                                   History::MessageTypeText);
 
105
    mManager->writeEvents(History::Events() << firstEvent);
 
106
    QTRY_COMPARE(dataChanged.count(), 1);
 
107
 
 
108
    QModelIndex firstIndex = dataChanged.first().first().value<QModelIndex>();
 
109
    QString lastEventMessage = model.data(firstIndex, HistoryThreadModel::LastEventTextMessageRole).toString();
 
110
    QCOMPARE(QString("Random Message"), lastEventMessage);
 
111
    dataChanged.clear();
 
112
 
 
113
    // create another thread to be grouped, but using another kind of account
 
114
    textThread = mManager->threadForParticipants("multimedia/multimedia/account1",
 
115
                                                 History::EventTypeText,
 
116
                                                 QStringList() << QString("1234567"),
 
117
                                                 History::MatchPhoneNumber, true);
 
118
 
 
119
    QTRY_COMPARE(dataChanged.count(), 1);
 
120
    QModelIndex index = dataChanged.first().first().value<QModelIndex>();
 
121
    QCOMPARE(firstIndex, index);
 
122
    dataChanged.clear();
 
123
 
 
124
    // insert another event in second thread
 
125
    History::TextEvent secondEvent = History::TextEvent(textThread.accountId(), textThread.threadId(), QString("eventId2%1").arg(QString::number(qrand() % 1024)),
 
126
                               QString("1234567"), QDateTime::currentDateTime(), false, "Random Message2",
 
127
                               History::MessageTypeText);
 
128
    mManager->writeEvents(History::Events() << secondEvent);
 
129
    QTRY_COMPARE(dataChanged.count(), 1);
 
130
 
 
131
    // make sure the index is the same, meaning both threads are grouped
 
132
    index = dataChanged.first().first().value<QModelIndex>();
 
133
    QCOMPARE(firstIndex, index);
 
134
 
 
135
    // check if latest message is from the second event
 
136
    lastEventMessage = model.data(index, HistoryThreadModel::LastEventTextMessageRole).toString();
 
137
 
 
138
    // check if count is correct given that we have two threads grouped with one message in each
 
139
    QCOMPARE(model.data(index, HistoryThreadModel::CountRole).toInt(), 2);
 
140
    QCOMPARE(QString("Random Message2"), lastEventMessage);
 
141
    dataChanged.clear();
 
142
 
 
143
    // delete latest event and make sure the text displayed is from the first thread again
 
144
    mManager->removeEvents(History::Events() << secondEvent);
 
145
    QTRY_COMPARE(dataChanged.count(), 1);
 
146
    index = dataChanged.first().first().value<QModelIndex>();
 
147
    QCOMPARE(firstIndex, index);
 
148
    lastEventMessage = model.data(index, HistoryThreadModel::LastEventTextMessageRole).toString();
 
149
    QCOMPARE(QString("Random Message"), lastEventMessage);
 
150
    // check if count is correct given that we have only one thread now
 
151
    QCOMPARE(model.data(index, HistoryThreadModel::CountRole).toInt(), 1);
 
152
 
 
153
    // delete first event and make sure the model is cleared
 
154
    mManager->removeEvents(History::Events() << firstEvent);
 
155
    QTRY_COMPARE(rowsRemoved.count(), 1);
 
156
    QTRY_COMPARE(model.rowCount(), 0);
66
157
}
67
158
 
68
159
QTEST_MAIN(HistoryGroupedThreadsModelTest)