Commit b8bea136 authored by Claudio Cambra's avatar Claudio Cambra
Browse files

Reduce code duplication in incidenceoccurrencemodel test, add testing for...


Reduce code duplication in incidenceoccurrencemodel test, add testing for correct model row removal and change

Signed-off-by: Claudio Cambra's avatarClaudio Cambra <claudio.cambra@gmail.com>
parent 18d965ea
Pipeline #248462 failed with stage
in 2 minutes and 8 seconds
......@@ -21,6 +21,33 @@ public:
IncidenceOccurrenceModelTest() = default;
~IncidenceOccurrenceModelTest() override = default;
bool standardSetupModel(IncidenceOccurrenceModel &model)
{
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
model.setStart(m_now.date());
model.setLength(m_testModelLength);
model.setCalendar(m_calendar);
return loadingChanged.wait(3000) &&
!model.loading() &&
model.rowCount() == m_expectedIncidenceCount;
}
bool addTestTodo(const IncidenceOccurrenceModel &model)
{
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
QSignalSpy createFinished(m_calendar->incidenceChanger(), &Akonadi::IncidenceChanger::createFinished);
const auto createResult = m_calendar->incidenceChanger()->createIncidence(m_testTodo, m_testCollection);
return createResult != -1 &&
createFinished.wait(3000) &&
loadingChanged.wait(3000) &&
!model.loading() &&
model.rowCount() == m_expectedIncidenceCount + 1;
}
public Q_SLOTS:
void checkAllItems(KCheckableProxyModel *model, const QModelIndex &parent = QModelIndex())
{
......@@ -49,13 +76,19 @@ private:
Akonadi::ETMCalendar::Ptr m_calendar;
KCalendarCore::Todo::Ptr m_testTodo;
Akonadi::Collection m_testCollection;
QString m_testTag;
QDateTime m_now = QDate(2022, 01, 10).startOfDay();
Filter m_testFilter;
const QString m_testTag = QStringLiteral("Tag 2");
const QDateTime m_now = QDate(2022, 01, 10).startOfDay();
static constexpr auto m_testModelLength = 7;
// Our test calendar file has an event that recurs every day.
// This event is an all-day event that covers two full days.
// Since we are checking for 7 days, we should have an instance of this event m_expectedIncidenceCount times.
const int m_expectedIncidenceCount = 8;
//
// That means we will always have one more occurrence than the model length
// as we will get overlap from the occurrence that starts a day before the
// model's start date and ends on the day the model starts.
static constexpr auto m_expectedIncidenceCount = m_testModelLength + 1;
private Q_SLOTS:
void initTestCase()
......@@ -79,8 +112,6 @@ private Q_SLOTS:
m_testCollection = collectionsList.first();
QVERIFY(m_testCollection.isValid());
m_testTag = QStringLiteral("Tag 2");
m_testTodo.reset(new KCalendarCore::Todo);
m_testTodo->setSummary(QStringLiteral("Test todo"));
m_testTodo->setCompleted(true);
......@@ -88,9 +119,11 @@ private Q_SLOTS:
m_testTodo->setDtDue(m_now.addDays(1));
m_testTodo->setPriority(1);
m_testTodo->setCategories(m_testTag);
m_testFilter.setTags({m_testTag});
}
void testModelProperties()
void testModelSettableProperties()
{
resetCalendar();
......@@ -98,18 +131,29 @@ private Q_SLOTS:
QSignalSpy startChanged(&model, &IncidenceOccurrenceModel::startChanged);
QSignalSpy lengthChanged(&model, &IncidenceOccurrenceModel::lengthChanged);
QSignalSpy calendarChanged(&model, &IncidenceOccurrenceModel::calendarChanged);
QSignalSpy filterChanged(&model, &IncidenceOccurrenceModel::filterChanged);
QSignalSpy resetThrottleIntervalChanged(&model, &IncidenceOccurrenceModel::resetThrottleIntervalChanged);
static constexpr auto testRefreshIntervalThrottle = 200;
model.setStart(m_now.date());
QCOMPARE(model.start(), m_now.date());
QCOMPARE(startChanged.count(), 1);
model.setLength(7);
QCOMPARE(model.length(), 7);
model.setLength(m_testModelLength);
QCOMPARE(model.length(), m_testModelLength);
QCOMPARE(lengthChanged.count(), 1);
model.setCalendar(m_calendar);
QCOMPARE(model.calendar(), m_calendar);
QCOMPARE(calendarChanged.count(), 1);
model.setFilter(&m_testFilter);
QCOMPARE(model.filter(), &m_testFilter);
QCOMPARE(filterChanged.count(), 1);
model.setResetThrottleInterval(testRefreshIntervalThrottle);
QCOMPARE(model.resetThrottleInterval(), testRefreshIntervalThrottle);
QCOMPARE(resetThrottleIntervalChanged.count(), 1);
}
void testLoadingSignalling()
......@@ -126,7 +170,7 @@ private Q_SLOTS:
// Wait for at least the time it takes for the throttler to let a reset happen
const auto signalWaitTime = model.resetThrottleInterval() + 1; // To be safe
model.setStart(m_now.date());
model.setLength(7);
model.setLength(m_testModelLength);
QVERIFY(!loadingChanged.wait(signalWaitTime));
QCOMPARE(loadingChanged.count(), 0);
QVERIFY(!model.loading());
......@@ -150,9 +194,10 @@ private Q_SLOTS:
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
model.setStart(m_now.date());
model.setLength(7);
model.setLength(m_testModelLength);
model.setCalendar(m_calendar);
// Don't use standardSetupModel -- let's see where exactly things go wrong
QVERIFY(loadingChanged.wait(3000));
QCOMPARE(loadingChanged.count(), 2);
QVERIFY(!model.loading());
......@@ -166,14 +211,7 @@ private Q_SLOTS:
IncidenceOccurrenceModel model;
QAbstractItemModelTester modelTester(&model);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
model.setStart(m_now.date());
model.setLength(7);
model.setCalendar(m_calendar);
QVERIFY(loadingChanged.wait(3000));
QCOMPARE(model.rowCount(), m_expectedIncidenceCount);
QVERIFY(standardSetupModel(model));
// Test that the data function gives us the event info we have in our calendar file
const auto index = model.index(0, 0);
......@@ -212,49 +250,98 @@ private Q_SLOTS:
QVERIFY(index.data(IncidenceOccurrenceModel::IncidenceOccurrence).canConvert<IncidenceOccurrenceModel::Occurrence>());
}
void testNewIncidenceAdded()
void testIncidenceAdded()
{
resetCalendar();
IncidenceOccurrenceModel model;
QAbstractItemModelTester modelTester(&model);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
model.setStart(m_now.date());
model.setLength(7);
model.setCalendar(m_calendar);
QVERIFY(loadingChanged.wait(3000));
QCOMPARE(model.rowCount(), m_expectedIncidenceCount);
QVERIFY(standardSetupModel(model));
// Let's not use the addTestTodo helper so we get information on where exactly things broke
// We want to make sure that we get both the final loadingChanged signal as well as the model's
// row inserted signal.
QSignalSpy createFinished(m_calendar->incidenceChanger(), &Akonadi::IncidenceChanger::createFinished);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
QSignalSpy rowsInserted(&model, &IncidenceOccurrenceModel::rowsInserted);
QVERIFY(m_calendar->incidenceChanger()->createIncidence(m_testTodo, m_testCollection) != -1);
QVERIFY(createFinished.wait(5000));
QVERIFY(loadingChanged.wait(3000));
QVERIFY(!model.loading());
QCOMPARE(rowsInserted.count(), 1);
QCOMPARE(model.rowCount(), m_expectedIncidenceCount + 1);
}
void testTodoData()
void testIncidenceRemoved()
{
resetCalendar();
IncidenceOccurrenceModel model;
QAbstractItemModelTester modelTester(&model);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
QVERIFY(standardSetupModel(model));
model.setStart(m_now.date());
model.setLength(7);
model.setCalendar(m_calendar);
// First add the test todo...
QVERIFY(addTestTodo(model));
// Then remove it and verify the incidence occurrence model reflects the change
QSignalSpy deleteFinished(m_calendar.data(), &Akonadi::ETMCalendar::deleteFinished);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
m_calendar->deleteIncidence(m_testTodo);
QVERIFY(deleteFinished.wait(5000));
QVERIFY(loadingChanged.wait(3000));
QVERIFY(!model.loading());
QCOMPARE(model.rowCount(), m_expectedIncidenceCount);
}
void testIncidenceDataChanged()
{
resetCalendar();
IncidenceOccurrenceModel model;
QAbstractItemModelTester modelTester(&model);
QVERIFY(standardSetupModel(model));
// First add the test todo...
QVERIFY(addTestTodo(model));
// Now let's create a copy and modify it
const KCalendarCore::Todo::Ptr todoClone(m_testTodo->clone());
const auto newSummary = QStringLiteral("New summary!");
todoClone->setSummary(newSummary);
// Then try modifying it
QSignalSpy modifyFinished(m_calendar.data(), &Akonadi::ETMCalendar::modifyFinished);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
QSignalSpy dataChanged(&model, &IncidenceOccurrenceModel::dataChanged);
m_calendar->modifyIncidence(todoClone);
QVERIFY(modifyFinished.wait(3000));
QSignalSpy createFinished(m_calendar->incidenceChanger(), &Akonadi::IncidenceChanger::createFinished);
QVERIFY(m_calendar->incidenceChanger()->createIncidence(m_testTodo, m_testCollection) != -1);
QVERIFY(createFinished.wait(5000));
QVERIFY(loadingChanged.wait(3000));
QCOMPARE(model.rowCount(), m_expectedIncidenceCount + 1);
QVERIFY(!model.loading());
// FIXME: Currently the model emits more dataChanged signals than needed
QVERIFY(dataChanged.count() > 0);
const auto dataChangedSignalEmitted = dataChanged.first();
const auto dataChangedIndex = dataChangedSignalEmitted.first().toModelIndex();
QVERIFY(dataChangedIndex.isValid());
QCOMPARE(dataChangedIndex.data(IncidenceOccurrenceModel::Summary).toString(), newSummary);
}
void testTodoData()
{
resetCalendar();
IncidenceOccurrenceModel model;
QAbstractItemModelTester modelTester(&model);
QVERIFY(standardSetupModel(model));
QVERIFY(addTestTodo(model));
const auto todoIndex = model.index(model.rowCount() - 1, 0);
QVERIFY(todoIndex.data(IncidenceOccurrenceModel::TodoCompleted).toBool());
......@@ -264,39 +351,27 @@ private Q_SLOTS:
QCOMPARE(todoIndex.data(IncidenceOccurrenceModel::Priority).toInt(), 1);
}
void testFilter()
void testFiltering()
{
resetCalendar();
IncidenceOccurrenceModel model;
QAbstractItemModelTester modelTester(&model);
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
QVERIFY(standardSetupModel(model));
model.setStart(m_now.date());
model.setLength(7);
model.setCalendar(m_calendar);
QVERIFY(loadingChanged.wait(3000));
QCOMPARE(model.rowCount(), m_expectedIncidenceCount);
QVERIFY(addTestTodo(model));
QSignalSpy createFinished(m_calendar->incidenceChanger(), &Akonadi::IncidenceChanger::createFinished);
QVERIFY(m_calendar->incidenceChanger()->createIncidence(m_testTodo, m_testCollection) != -1);
QVERIFY(createFinished.wait(5000));
QSignalSpy loadingChanged(&model, &IncidenceOccurrenceModel::loadingChanged);
model.setFilter(&m_testFilter);
QCOMPARE(model.filter(), &m_testFilter);
QVERIFY(loadingChanged.wait(3000));
QCOMPARE(model.rowCount(), m_expectedIncidenceCount + 1);
Filter filter;
filter.setTags({m_testTag});
QSignalSpy fetchFinished(&model, &QAbstractItemModel::modelReset);
model.setFilter(&filter);
QCOMPARE(model.filter(), &filter);
fetchFinished.wait(10000);
QVERIFY(!model.loading());
QCOMPARE(model.rowCount(), 1);
model.setFilter({});
fetchFinished.wait(10000);
QVERIFY(loadingChanged.wait(3000));
QVERIFY(!model.loading());
QCOMPARE(model.rowCount(), m_expectedIncidenceCount + 1);
}
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment