diff options
| author | Frank Reininghaus <[email protected]> | 2011-04-03 15:31:27 +0200 |
|---|---|---|
| committer | Frank Reininghaus <[email protected]> | 2011-04-03 15:32:12 +0200 |
| commit | 95cb8ffe01e0252d3a26f244e8607254200d067f (patch) | |
| tree | d76499d9b454b8730e48a2f24a55c914f7f0230c /src/tests/dolphindetailsviewtest.cpp | |
| parent | 5ff682d852a07b5121e49d8ed2a239b0a1762b6b (diff) | |
Dolphin unit test refactoring
This commit changes mainly two things:
1. The functionality related to the handling of temporary files and
folders is moved to a new class TestDir.
2. The setup of a temporary folder and of DolphinView instances is
now done in each unit test function. A benefit of this change is that
there are no unwanted interactions between different unit test functions
any more. Moreover, different test functions could now share a test
folder more easily.
Additionally, the reference arguments of all functions are changed to
pointers for consistency.
Diffstat (limited to 'src/tests/dolphindetailsviewtest.cpp')
| -rw-r--r-- | src/tests/dolphindetailsviewtest.cpp | 154 |
1 files changed, 81 insertions, 73 deletions
diff --git a/src/tests/dolphindetailsviewtest.cpp b/src/tests/dolphindetailsviewtest.cpp index 49fbb629f..87a9d253d 100644 --- a/src/tests/dolphindetailsviewtest.cpp +++ b/src/tests/dolphindetailsviewtest.cpp @@ -20,6 +20,7 @@ #include <qtest_kde.h> #include "testbase.h" +#include "testdir.h" #include "views/dolphindetailsview.h" #include "views/dolphinview.h" @@ -36,21 +37,6 @@ class DolphinDetailsViewTest : public TestBase private slots: - void init() { - m_view->setMode(DolphinView::DetailsView); - m_detailsView = qobject_cast<DolphinDetailsView*>(itemView()); - QVERIFY(m_detailsView); - m_detailsView->setFoldersExpandable(true); - m_view->resize(400, 400); - m_view->show(); - QTest::qWaitForWindowShown(m_view); - } - - void cleanup() { - m_view->hide(); - cleanupTestDir(); - } - void testExpandedUrls(); void bug217447_shiftArrowSelection(); @@ -59,12 +45,28 @@ private slots: private: - QModelIndex proxyModelIndexForUrl(const KUrl& url) const { - const QModelIndex index = m_view->m_viewAccessor.m_dolphinModel->indexForUrl(url); - return m_view->m_viewAccessor.m_proxyModel->mapFromSource(index); + /** + * initView(DolphinView*) sets the correct view mode, shows the view on the screen, and waits + * until loading the folder in the view is finished. + * + * Many unit tests need access to the internal DolphinDetailsView in DolphinView. + * Therefore, a pointer to the details view is returned by initView(DolphinView*). + */ + DolphinDetailsView* initView(DolphinView* view) const { + view->setMode(DolphinView::DetailsView); + DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView(view)); + detailsView->setFoldersExpandable(true); + view->resize(400, 400); + view->show(); + QTest::qWaitForWindowShown(view); + reloadViewAndWait(view); + return detailsView; } - DolphinDetailsView* m_detailsView; + QModelIndex proxyModelIndexForUrl(const DolphinView* view, const KUrl& url) const { + const QModelIndex index = view->m_viewAccessor.m_dolphinModel->indexForUrl(url); + return view->m_viewAccessor.m_proxyModel->mapFromSource(index); + } }; /** @@ -75,8 +77,6 @@ private: void DolphinDetailsViewTest::testExpandedUrls() { - m_detailsView->setFoldersExpandable(true); - QStringList files; QStringList subFolderNames; subFolderNames << "a" << "b" << "c"; @@ -89,27 +89,29 @@ void DolphinDetailsViewTest::testExpandedUrls() } } - createFiles(files); - reloadViewAndWait(); + TestDir dir; + dir.createFiles(files); + DolphinView view(dir.url(), 0); + DolphinDetailsView* detailsView = initView(&view); // We start with an empty set of expanded URLs. QSet<KUrl> expectedExpandedUrls; - QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls); + QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls); // Every time we expand a folder, we have to wait until the view has finished loading // its contents before we can expand further subfolders. We keep track of the reloading // using a signal spy. - QSignalSpy spyFinishedPathLoading(m_view, SIGNAL(finishedPathLoading(const KUrl&))); + QSignalSpy spyFinishedPathLoading(&view, SIGNAL(finishedPathLoading(const KUrl&))); // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls() QStringList itemsToExpand; itemsToExpand << "b" << "b/a" << "b/a/c" << "b/c" << "c"; foreach(const QString& item, itemsToExpand) { - KUrl url(m_path + item); - m_detailsView->expand(proxyModelIndexForUrl(url)); + KUrl url(dir.name() + item); + detailsView->expand(proxyModelIndexForUrl(&view, url)); expectedExpandedUrls += url; - QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls); + QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls); // Before we proceed, we have to make sure that the view has finished // loading the contents of the expanded folder. @@ -124,10 +126,10 @@ void DolphinDetailsViewTest::testExpandedUrls() itemsToCollapse << "b/c" << "b/a/c" << "c" << "b/a" << "b"; foreach(const QString& item, itemsToCollapse) { - KUrl url(m_path + item); - m_detailsView->collapse(proxyModelIndexForUrl(url)); + KUrl url(dir.name() + item); + detailsView->collapse(proxyModelIndexForUrl(&view, url)); expectedExpandedUrls -= url; - QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls); + QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls); } } @@ -153,33 +155,35 @@ void DolphinDetailsViewTest::testExpandedUrls() void DolphinDetailsViewTest::bug217447_shiftArrowSelection() { + TestDir dir; for (int i = 0; i < 100; i++) { - createFile(QString("%1").arg(i)); + dir.createFile(QString("%1").arg(i)); } - reloadViewAndWait(); + DolphinView view(dir.url(), 0); + DolphinDetailsView* detailsView = initView(&view); // Select the first item - QModelIndex index0 = m_detailsView->model()->index(0, 0); - m_detailsView->setCurrentIndex(index0); - QCOMPARE(m_detailsView->currentIndex(), index0); + QModelIndex index0 = detailsView->model()->index(0, 0); + detailsView->setCurrentIndex(index0); + QCOMPARE(detailsView->currentIndex(), index0); // Before we test Shift-selection, we verify that the root cause is fixed a bit more // directly: we check that passing the corners or the center of an item's visualRect // to itemAt() returns the item (and not an invalid model index). - QRect rect = m_detailsView->visualRect(index0); - QCOMPARE(m_detailsView->indexAt(rect.center()), index0); - QCOMPARE(m_detailsView->indexAt(rect.topLeft()), index0); - QCOMPARE(m_detailsView->indexAt(rect.topRight()), index0); - QCOMPARE(m_detailsView->indexAt(rect.bottomLeft()), index0); - QCOMPARE(m_detailsView->indexAt(rect.bottomRight()), index0); + QRect rect = detailsView->visualRect(index0); + QCOMPARE(detailsView->indexAt(rect.center()), index0); + QCOMPARE(detailsView->indexAt(rect.topLeft()), index0); + QCOMPARE(detailsView->indexAt(rect.topRight()), index0); + QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0); + QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0); // Another way to test this is to Ctrl-click the center of the visualRect. // The selection state of the item should be toggled. - m_detailsView->clearSelection(); - QItemSelectionModel* selectionModel = m_detailsView->selectionModel(); + detailsView->clearSelection(); + QItemSelectionModel* selectionModel = detailsView->selectionModel(); QCOMPARE(selectionModel->selectedIndexes().count(), 0); - QTest::mouseClick(m_detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center()); + QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center()); QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); QCOMPARE(selectedIndexes.count(), 1); QVERIFY(selectedIndexes.contains(index0)); @@ -190,9 +194,9 @@ void DolphinDetailsViewTest::bug217447_shiftArrowSelection() int current = 1; while (current < 100) { - QTest::keyClick(m_detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier); - QModelIndex currentIndex = m_detailsView->model()->index(current, 0); - QCOMPARE(m_detailsView->currentIndex(), currentIndex); + QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier); + QModelIndex currentIndex = detailsView->model()->index(current, 0); + QCOMPARE(detailsView->currentIndex(), currentIndex); selectedIndexes = selectionModel->selectedIndexes(); QCOMPARE(selectedIndexes.count(), current + 1); @@ -216,34 +220,36 @@ void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming() QStringList files; files << "a" << "b" << "c" << "d"; - createFiles(files); - reloadViewAndWait(); + TestDir dir; + dir.createFiles(files); + DolphinView view(dir.url(), 0); + DolphinDetailsView* detailsView = initView(&view); - QModelIndex index0 = m_detailsView->model()->index(0, 0); - m_detailsView->setCurrentIndex(index0); - QCOMPARE(m_detailsView->currentIndex(), index0); + QModelIndex index0 = detailsView->model()->index(0, 0); + detailsView->setCurrentIndex(index0); + QCOMPARE(detailsView->currentIndex(), index0); // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...) // should make sure that the bug is triggered. - int zoomLevelBackup = m_view->zoomLevel(); + int zoomLevelBackup = view.zoomLevel(); int zoomLevel = ZoomLevelInfo::minimumLevel(); - m_view->setZoomLevel(zoomLevel); + view.setZoomLevel(zoomLevel); - QModelIndex index1 = m_detailsView->model()->index(1, 0); - m_detailsView->setCurrentIndex(index1); - QCOMPARE(m_detailsView->currentIndex(), index1); + QModelIndex index1 = detailsView->model()->index(1, 0); + detailsView->setCurrentIndex(index1); + QCOMPARE(detailsView->currentIndex(), index1); // Increase the zoom level successively to the maximum. while(zoomLevel < ZoomLevelInfo::maximumLevel()) { zoomLevel++; - m_view->setZoomLevel(zoomLevel); - QCOMPARE(m_view->zoomLevel(), zoomLevel); + view.setZoomLevel(zoomLevel); + QCOMPARE(view.zoomLevel(), zoomLevel); //Check for each zoom level that the height of each item is at least the icon size. - QVERIFY(m_detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel)); + QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel)); } - m_view->setZoomLevel(zoomLevelBackup); + view.setZoomLevel(zoomLevelBackup); } /** @@ -257,20 +263,22 @@ void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming() */ void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() { + TestDir dir; QString name; for (int i = 0; i < 20; i++) { name += "mmmmmmmmmm"; - createFile(name); + dir.createFile(name); } - reloadViewAndWait(); + DolphinView view(dir.url(), 0); + DolphinDetailsView* detailsView = initView(&view); // Select the first item - QModelIndex index0 = m_detailsView->model()->index(0, 0); - m_detailsView->setCurrentIndex(index0); - QCOMPARE(m_detailsView->currentIndex(), index0); - QVERIFY(m_detailsView->visualRect(index0).width() < m_detailsView->columnWidth(DolphinModel::Name)); + QModelIndex index0 = detailsView->model()->index(0, 0); + detailsView->setCurrentIndex(index0); + QCOMPARE(detailsView->currentIndex(), index0); + QVERIFY(detailsView->visualRect(index0).width() < detailsView->columnWidth(DolphinModel::Name)); - QItemSelectionModel* selectionModel = m_detailsView->selectionModel(); + QItemSelectionModel* selectionModel = detailsView->selectionModel(); QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); QCOMPARE(selectedIndexes.count(), 1); QVERIFY(selectedIndexes.contains(index0)); @@ -278,10 +286,10 @@ void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() { // Move down successively using the "Down" key and check that current item // and selection are as expected. for (int i = 0; i < 19; i++) { - QTest::keyClick(m_detailsView->viewport(), Qt::Key_Down, Qt::NoModifier); - QModelIndex currentIndex = m_detailsView->model()->index(i + 1, 0); - QCOMPARE(m_detailsView->currentIndex(), currentIndex); - QVERIFY(m_detailsView->visualRect(currentIndex).width() <= m_detailsView->columnWidth(DolphinModel::Name)); + QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::NoModifier); + QModelIndex currentIndex = detailsView->model()->index(i + 1, 0); + QCOMPARE(detailsView->currentIndex(), currentIndex); + QVERIFY(detailsView->visualRect(currentIndex).width() <= detailsView->columnWidth(DolphinModel::Name)); selectedIndexes = selectionModel->selectedIndexes(); QCOMPARE(selectedIndexes.count(), 1); QVERIFY(selectedIndexes.contains(currentIndex)); |
