Changeset View
Changeset View
Standalone View
Standalone View
tests/units/presentation/availablesourcesmodeltest.cpp
Show First 20 Lines • Show All 206 Lines • ▼ Show 20 Line(s) | 67 | { | |||
---|---|---|---|---|---|
207 | QCOMPARE(spy.at(2).at(0).toModelIndex(), source4Index); | 207 | QCOMPARE(spy.at(2).at(0).toModelIndex(), source4Index); | ||
208 | QCOMPARE(spy.at(2).at(1).toModelIndex(), source4Index); | 208 | QCOMPARE(spy.at(2).at(1).toModelIndex(), source4Index); | ||
209 | QCOMPARE(spy.at(3).at(0).toModelIndex(), source2Index); | 209 | QCOMPARE(spy.at(3).at(0).toModelIndex(), source2Index); | ||
210 | QCOMPARE(spy.at(3).at(1).toModelIndex(), source2Index); | 210 | QCOMPARE(spy.at(3).at(1).toModelIndex(), source2Index); | ||
211 | 211 | | |||
212 | QVERIFY(sourceQueriesMock(&Domain::DataSourceQueries::changeDefaultSource).when(source4).exactly(1)); | 212 | QVERIFY(sourceQueriesMock(&Domain::DataSourceQueries::changeDefaultSource).when(source4).exactly(1)); | ||
213 | } | 213 | } | ||
214 | 214 | | |||
215 | void shouldListAvailableSearchSources() | | |||
216 | { | | |||
217 | // GIVEN | | |||
218 | | ||||
219 | // Two top level sources | | |||
220 | auto source1 = Domain::DataSource::Ptr::create(); | | |||
221 | source1->setName(QStringLiteral("Source 1")); | | |||
222 | source1->setIconName(QStringLiteral("foo-icon")); | | |||
223 | source1->setSelected(true); | | |||
224 | auto source2 = Domain::DataSource::Ptr::create(); | | |||
225 | source2->setName(QStringLiteral("Source 2")); | | |||
226 | source2->setSelected(false); | | |||
227 | source2->setContentTypes(Domain::DataSource::Tasks); | | |||
228 | auto topLevelProvider = Domain::QueryResultProvider<Domain::DataSource::Ptr>::Ptr::create(); | | |||
229 | auto topLevelResult = Domain::QueryResult<Domain::DataSource::Ptr>::create(topLevelProvider); | | |||
230 | topLevelProvider->append(source1); | | |||
231 | topLevelProvider->append(source2); | | |||
232 | | ||||
233 | // Two other sources under source1 | | |||
234 | auto source3 = Domain::DataSource::Ptr::create(); | | |||
235 | source3->setName(QStringLiteral("Source 3")); | | |||
236 | source3->setSelected(false); | | |||
237 | source3->setContentTypes(Domain::DataSource::Notes); | | |||
238 | auto source4 = Domain::DataSource::Ptr::create(); | | |||
239 | source4->setSelected(true); | | |||
240 | source4->setName(QStringLiteral("Source 4")); | | |||
241 | source4->setContentTypes(Domain::DataSource::Notes | Domain::DataSource::Tasks); | | |||
242 | auto source1Provider = Domain::QueryResultProvider<Domain::DataSource::Ptr>::Ptr::create(); | | |||
243 | auto source1Result = Domain::QueryResult<Domain::DataSource::Ptr>::create(source1Provider); | | |||
244 | source1Provider->append(source3); | | |||
245 | source1Provider->append(source4); | | |||
246 | | ||||
247 | // Nothing under source2, source3 or source4 | | |||
248 | auto source2Provider = Domain::QueryResultProvider<Domain::DataSource::Ptr>::Ptr::create(); | | |||
249 | auto source2Result = Domain::QueryResult<Domain::DataSource::Ptr>::create(source2Provider); | | |||
250 | auto source3Provider = Domain::QueryResultProvider<Domain::DataSource::Ptr>::Ptr::create(); | | |||
251 | auto source3Result = Domain::QueryResult<Domain::DataSource::Ptr>::create(source3Provider); | | |||
252 | auto source4Provider = Domain::QueryResultProvider<Domain::DataSource::Ptr>::Ptr::create(); | | |||
253 | auto source4Result = Domain::QueryResult<Domain::DataSource::Ptr>::create(source4Provider); | | |||
254 | | ||||
255 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
256 | sourceQueriesMock(&Domain::DataSourceQueries::findSearchTopLevel).when().thenReturn(topLevelResult); | | |||
257 | sourceQueriesMock(&Domain::DataSourceQueries::findSearchChildren).when(source1).thenReturn(source1Result); | | |||
258 | sourceQueriesMock(&Domain::DataSourceQueries::findSearchChildren).when(source2).thenReturn(source2Result); | | |||
259 | sourceQueriesMock(&Domain::DataSourceQueries::findSearchChildren).when(source3).thenReturn(source3Result); | | |||
260 | sourceQueriesMock(&Domain::DataSourceQueries::findSearchChildren).when(source4).thenReturn(source4Result); | | |||
261 | | ||||
262 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
263 | | ||||
264 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
265 | sourceRepositoryMock.getInstance(), | | |||
266 | Q_NULLPTR); | | |||
267 | | ||||
268 | // WHEN | | |||
269 | QAbstractItemModel *model = sources.searchListModel(); | | |||
270 | | ||||
271 | // THEN | | |||
272 | const QModelIndex source1Index = model->index(0, 0); | | |||
273 | const QModelIndex source2Index = model->index(1, 0); | | |||
274 | const QModelIndex source3Index = model->index(0, 0, source1Index); | | |||
275 | const QModelIndex source4Index = model->index(1, 0, source1Index); | | |||
276 | | ||||
277 | QCOMPARE(model->rowCount(), 2); | | |||
278 | QCOMPARE(model->rowCount(source1Index), 2); | | |||
279 | QCOMPARE(model->rowCount(source2Index), 0); | | |||
280 | QCOMPARE(model->rowCount(source3Index), 0); | | |||
281 | QCOMPARE(model->rowCount(source4Index), 0); | | |||
282 | | ||||
283 | const Qt::ItemFlags defaultFlags = Qt::ItemIsSelectable | | |||
284 | | Qt::ItemIsEnabled; | | |||
285 | QCOMPARE(model->flags(source1Index), defaultFlags); | | |||
286 | QCOMPARE(model->flags(source2Index), defaultFlags); | | |||
287 | QCOMPARE(model->flags(source3Index), defaultFlags); | | |||
288 | QCOMPARE(model->flags(source4Index), defaultFlags); | | |||
289 | | ||||
290 | QCOMPARE(model->data(source1Index).toString(), source1->name()); | | |||
291 | QCOMPARE(model->data(source2Index).toString(), source2->name()); | | |||
292 | QCOMPARE(model->data(source3Index).toString(), source3->name()); | | |||
293 | QCOMPARE(model->data(source4Index).toString(), source4->name()); | | |||
294 | | ||||
295 | QCOMPARE(model->data(source1Index, Qt::EditRole).toString(), source1->name()); | | |||
296 | QCOMPARE(model->data(source2Index, Qt::EditRole).toString(), source2->name()); | | |||
297 | QCOMPARE(model->data(source3Index, Qt::EditRole).toString(), source3->name()); | | |||
298 | QCOMPARE(model->data(source4Index, Qt::EditRole).toString(), source4->name()); | | |||
299 | | ||||
300 | QVERIFY(!model->data(source1Index, Qt::CheckStateRole).isValid()); | | |||
301 | QVERIFY(!model->data(source2Index, Qt::CheckStateRole).isValid()); | | |||
302 | QVERIFY(!model->data(source3Index, Qt::CheckStateRole).isValid()); | | |||
303 | QVERIFY(!model->data(source4Index, Qt::CheckStateRole).isValid()); | | |||
304 | | ||||
305 | QCOMPARE(model->data(source1Index, Presentation::QueryTreeModelBase::IconNameRole).toString(), source1->iconName()); | | |||
306 | QCOMPARE(model->data(source2Index, Presentation::QueryTreeModelBase::IconNameRole).toString(), QStringLiteral("folder")); | | |||
307 | QCOMPARE(model->data(source3Index, Presentation::QueryTreeModelBase::IconNameRole).toString(), QStringLiteral("folder")); | | |||
308 | QCOMPARE(model->data(source4Index, Presentation::QueryTreeModelBase::IconNameRole).toString(), QStringLiteral("folder")); | | |||
309 | | ||||
310 | QVERIFY(!model->setData(source1Index, Qt::Unchecked, Qt::CheckStateRole)); | | |||
311 | QVERIFY(!model->setData(source2Index, Qt::Checked, Qt::CheckStateRole)); | | |||
312 | QVERIFY(!model->setData(source4Index, Qt::Unchecked, Qt::CheckStateRole)); | | |||
313 | } | | |||
314 | | ||||
315 | void shouldChangeSourceToListed() | | |||
316 | { | | |||
317 | // GIVEN | | |||
318 | | ||||
319 | auto source = Domain::DataSource::Ptr::create(); | | |||
320 | source->setName(QStringLiteral("Source")); | | |||
321 | source->setIconName(QStringLiteral("folder")); | | |||
322 | source->setContentTypes(Domain::DataSource::Tasks); | | |||
323 | source->setSelected(false); | | |||
324 | source->setListStatus(Domain::DataSource::Unlisted); | | |||
325 | | ||||
326 | | ||||
327 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
328 | | ||||
329 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
330 | sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).thenReturn(new FakeJob(this)); | | |||
331 | | ||||
332 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
333 | sourceRepositoryMock.getInstance()); | | |||
334 | | ||||
335 | // WHEN | | |||
336 | sources.listSource(source); | | |||
337 | | ||||
338 | // THEN | | |||
339 | QVERIFY(sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).exactly(1)); | | |||
340 | QVERIFY(source->isSelected()); | | |||
341 | QCOMPARE(source->listStatus(), Domain::DataSource::Listed); | | |||
342 | } | | |||
343 | | ||||
344 | void shouldChangeSourceToUnlisted() | | |||
345 | { | | |||
346 | // GIVEN | | |||
347 | | ||||
348 | auto source = Domain::DataSource::Ptr::create(); | | |||
349 | source->setName(QStringLiteral("Source")); | | |||
350 | source->setIconName(QStringLiteral("folder")); | | |||
351 | source->setContentTypes(Domain::DataSource::Tasks); | | |||
352 | source->setSelected(true); | | |||
353 | source->setListStatus(Domain::DataSource::Bookmarked); | | |||
354 | | ||||
355 | | ||||
356 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
357 | | ||||
358 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
359 | sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).thenReturn(new FakeJob(this)); | | |||
360 | | ||||
361 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
362 | sourceRepositoryMock.getInstance()); | | |||
363 | | ||||
364 | // WHEN | | |||
365 | sources.unlistSource(source); | | |||
366 | | ||||
367 | // THEN | | |||
368 | QVERIFY(sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).exactly(1)); | | |||
369 | QVERIFY(!source->isSelected()); | | |||
370 | QCOMPARE(source->listStatus(), Domain::DataSource::Unlisted); | | |||
371 | } | | |||
372 | | ||||
373 | void shouldToggleSourceToBookmarkStatus_data() | | |||
374 | { | | |||
375 | QTest::addColumn<bool>("wasSelected"); | | |||
376 | QTest::addColumn<bool>("wasBookmarked"); | | |||
377 | QTest::newRow("unselected, not bookmarked") << false << false; | | |||
378 | QTest::newRow("selected, not bookmarked") << true << false; | | |||
379 | QTest::newRow("unselected, bookmarked") << false << true; | | |||
380 | QTest::newRow("selected, bookmarked") << true << true; | | |||
381 | } | | |||
382 | | ||||
383 | void shouldToggleSourceToBookmarkStatus() | | |||
384 | { | | |||
385 | // GIVEN | | |||
386 | QFETCH(bool, wasSelected); | | |||
387 | QFETCH(bool, wasBookmarked); | | |||
388 | | ||||
389 | auto source = Domain::DataSource::Ptr::create(); | | |||
390 | source->setName(QStringLiteral("Source")); | | |||
391 | source->setIconName(QStringLiteral("folder")); | | |||
392 | source->setContentTypes(Domain::DataSource::Tasks); | | |||
393 | source->setSelected(wasSelected); | | |||
394 | if (wasBookmarked) | | |||
395 | source->setListStatus(Domain::DataSource::Bookmarked); | | |||
396 | else | | |||
397 | source->setListStatus(Domain::DataSource::Listed); | | |||
398 | | ||||
399 | | ||||
400 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
401 | | ||||
402 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
403 | sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).thenReturn(new FakeJob(this)); | | |||
404 | | ||||
405 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
406 | sourceRepositoryMock.getInstance()); | | |||
407 | | ||||
408 | // WHEN | | |||
409 | sources.bookmarkSource(source); | | |||
410 | | ||||
411 | // THEN | | |||
412 | QVERIFY(sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).exactly(1)); | | |||
413 | QCOMPARE(source->isSelected(), wasSelected); | | |||
414 | if (wasBookmarked) | | |||
415 | QCOMPARE(source->listStatus(), Domain::DataSource::Listed); | | |||
416 | else | | |||
417 | QCOMPARE(source->listStatus(), Domain::DataSource::Bookmarked); | | |||
418 | } | | |||
419 | | ||||
420 | void shouldGetAnErrorMessageWhenListSourceFailed() | | |||
421 | { | | |||
422 | // GIVEN | | |||
423 | | ||||
424 | auto source = Domain::DataSource::Ptr::create(); | | |||
425 | source->setName(QStringLiteral("Source")); | | |||
426 | source->setIconName(QStringLiteral("folder")); | | |||
427 | source->setContentTypes(Domain::DataSource::Tasks); | | |||
428 | source->setSelected(false); | | |||
429 | source->setListStatus(Domain::DataSource::Unlisted); | | |||
430 | | ||||
431 | | ||||
432 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
433 | | ||||
434 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
435 | | ||||
436 | auto job = new FakeJob(this); | | |||
437 | job->setExpectedError(KJob::KilledJobError, QStringLiteral("Foo")); | | |||
438 | sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).thenReturn(job); | | |||
439 | | ||||
440 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
441 | sourceRepositoryMock.getInstance()); | | |||
442 | FakeErrorHandler errorHandler; | | |||
443 | sources.setErrorHandler(&errorHandler); | | |||
444 | | ||||
445 | // WHEN | | |||
446 | sources.listSource(source); | | |||
447 | | ||||
448 | // THEN | | |||
449 | QTest::qWait(150); | | |||
450 | QCOMPARE(errorHandler.m_message, QStringLiteral("Cannot modify source Source: Foo")); | | |||
451 | } | | |||
452 | | ||||
453 | void shouldGetAnErrorMessageWhenUnlistSourceFailed() | | |||
454 | { | | |||
455 | // GIVEN | | |||
456 | | ||||
457 | auto source = Domain::DataSource::Ptr::create(); | | |||
458 | source->setName(QStringLiteral("Source")); | | |||
459 | source->setIconName(QStringLiteral("folder")); | | |||
460 | source->setContentTypes(Domain::DataSource::Tasks); | | |||
461 | source->setSelected(false); | | |||
462 | source->setListStatus(Domain::DataSource::Unlisted); | | |||
463 | | ||||
464 | | ||||
465 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
466 | | ||||
467 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
468 | | ||||
469 | auto job = new FakeJob(this); | | |||
470 | job->setExpectedError(KJob::KilledJobError, QStringLiteral("Foo")); | | |||
471 | sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).thenReturn(job); | | |||
472 | | ||||
473 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
474 | sourceRepositoryMock.getInstance()); | | |||
475 | FakeErrorHandler errorHandler; | | |||
476 | sources.setErrorHandler(&errorHandler); | | |||
477 | | ||||
478 | // WHEN | | |||
479 | sources.unlistSource(source); | | |||
480 | | ||||
481 | // THEN | | |||
482 | QTest::qWait(150); | | |||
483 | QCOMPARE(errorHandler.m_message, QStringLiteral("Cannot modify source Source: Foo")); | | |||
484 | } | | |||
485 | | ||||
486 | void shouldGetAnErrorMessageWhenBookmarkSourceFailed() | | |||
487 | { | | |||
488 | // GIVEN | | |||
489 | | ||||
490 | auto source = Domain::DataSource::Ptr::create(); | | |||
491 | source->setName(QStringLiteral("Source")); | | |||
492 | source->setIconName(QStringLiteral("folder")); | | |||
493 | source->setContentTypes(Domain::DataSource::Tasks); | | |||
494 | source->setSelected(false); | | |||
495 | source->setListStatus(Domain::DataSource::Unlisted); | | |||
496 | | ||||
497 | | ||||
498 | Utils::MockObject<Domain::DataSourceQueries> sourceQueriesMock; | | |||
499 | | ||||
500 | Utils::MockObject<Domain::DataSourceRepository> sourceRepositoryMock; | | |||
501 | | ||||
502 | auto job = new FakeJob(this); | | |||
503 | job->setExpectedError(KJob::KilledJobError, QStringLiteral("Foo")); | | |||
504 | sourceRepositoryMock(&Domain::DataSourceRepository::update).when(source).thenReturn(job); | | |||
505 | | ||||
506 | Presentation::AvailableSourcesModel sources(sourceQueriesMock.getInstance(), | | |||
507 | sourceRepositoryMock.getInstance()); | | |||
508 | FakeErrorHandler errorHandler; | | |||
509 | sources.setErrorHandler(&errorHandler); | | |||
510 | | ||||
511 | // WHEN | | |||
512 | sources.bookmarkSource(source); | | |||
513 | | ||||
514 | // THEN | | |||
515 | QTest::qWait(150); | | |||
516 | QCOMPARE(errorHandler.m_message, QStringLiteral("Cannot modify source Source: Foo")); | | |||
517 | } | | |||
518 | | ||||
519 | void shouldGetAnErrorMessageWhenSetDataSourceFailed() | 215 | void shouldGetAnErrorMessageWhenSetDataSourceFailed() | ||
520 | { | 216 | { | ||
521 | // GIVEN | 217 | // GIVEN | ||
522 | 218 | | |||
523 | // Two top level sources | 219 | // Two top level sources | ||
524 | auto source1 = Domain::DataSource::Ptr::create(); | 220 | auto source1 = Domain::DataSource::Ptr::create(); | ||
525 | source1->setName(QStringLiteral("Source 1")); | 221 | source1->setName(QStringLiteral("Source 1")); | ||
526 | source1->setIconName(QStringLiteral("foo-icon")); | 222 | source1->setIconName(QStringLiteral("foo-icon")); | ||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |