]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testbase.cpp
1. Add method TestBase::reloadViewAndWait().
[dolphin.git] / src / tests / testbase.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "testbase.h"
21
22 #include <qtest_kde.h>
23
24 #include "views/dolphinview.h"
25 #include "views/dolphinmodel.h"
26 #include "views/dolphindirlister.h"
27 #include "views/dolphinsortfilterproxymodel.h"
28
29 #include <KTempDir>
30
31 #include <QtCore/QDir>
32 #include <QtGui/QAbstractItemView>
33
34 #include <kdebug.h>
35
36 TestBase::TestBase()
37 {
38 m_tempDir = new KTempDir;
39 Q_ASSERT(m_tempDir->exists());
40 m_path = m_tempDir->name();
41 m_dir = new QDir(m_path);
42 m_dirLister = new DolphinDirLister();
43 m_dirLister->setAutoUpdate(true);
44 m_dolphinModel = new DolphinModel();
45 m_dolphinModel->setDirLister(m_dirLister);
46 m_proxyModel = new DolphinSortFilterProxyModel(0);
47 m_proxyModel->setSourceModel(m_dolphinModel);
48 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
49 m_view = new DolphinView(0, KUrl(m_path), m_proxyModel);
50 }
51
52 TestBase::~TestBase()
53 {
54 delete m_view;
55 delete m_proxyModel;
56 // m_dolphinModel owns m_dirLister -> do not delete it here!
57 delete m_dolphinModel;
58 delete m_dir;
59 delete m_tempDir;
60 }
61
62 QAbstractItemView* TestBase::itemView () const
63 {
64 return m_view->m_viewAccessor.itemView();
65 }
66
67 void TestBase::reloadViewAndWait()
68 {
69 kDebug() << "Reloading view and waiting for the finishedPathLoading(const KUrl&) signal...";
70 QSignalSpy finished(m_view, SIGNAL(finishedPathLoading(const KUrl&)));
71 m_view->reload();
72 while (finished.count() != 1) {
73 QTest::qWait(50);
74 }
75 kDebug() << "...signal received, continuing";
76 }
77
78 KUrl TestBase::testDirUrl() const
79 {
80 return KUrl(m_path);
81 }
82
83 void TestBase::createFile(const QString& path, const QByteArray& data)
84 {
85 QString absolutePath = path;
86 makePathAbsoluteAndCreateParents(absolutePath);
87
88 QFile f(absolutePath);
89 f.open(QIODevice::WriteOnly);
90 f.write(data);
91 f.close();
92
93 Q_ASSERT(QFile::exists(absolutePath));
94 }
95
96 void TestBase::createFiles(const QStringList& files)
97 {
98 foreach(const QString& path, files) {
99 createFile(path);
100 }
101 }
102
103 void TestBase::createDir(const QString& path)
104 {
105 QString absolutePath = path;
106 makePathAbsoluteAndCreateParents(absolutePath);
107 m_dir->mkdir(absolutePath);
108
109 Q_ASSERT(QFile::exists(absolutePath));
110 }
111
112 void TestBase::makePathAbsoluteAndCreateParents(QString& path)
113 {
114 QFileInfo fileInfo(path);
115 if (!fileInfo.isAbsolute()) {
116 path = m_path + path;
117 fileInfo.setFile(path);
118 }
119
120 const QDir dir = fileInfo.dir();
121 if (!dir.exists()) {
122 createDir(dir.absolutePath());
123 }
124
125 Q_ASSERT(dir.exists());
126 }
127
128 void TestBase::cleanupTestDir()
129 {
130 delete m_tempDir;
131 m_tempDir = new KTempDir;
132 Q_ASSERT(m_tempDir->exists());
133 m_path = m_tempDir->name();
134 m_dir->setPath(m_path);
135 m_view->setUrl(m_path);
136 }