]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testbase.cpp
2161c9314c4ca6b7a18d3850e31e538da121371a
[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 m_view->reload();
70 QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 5000));
71 }
72
73 KUrl TestBase::testDirUrl() const
74 {
75 return KUrl(m_path);
76 }
77
78 void TestBase::createFile(const QString& path, const QByteArray& data)
79 {
80 QString absolutePath = path;
81 makePathAbsoluteAndCreateParents(absolutePath);
82
83 QFile f(absolutePath);
84 f.open(QIODevice::WriteOnly);
85 f.write(data);
86 f.close();
87
88 Q_ASSERT(QFile::exists(absolutePath));
89 }
90
91 void TestBase::createFiles(const QStringList& files)
92 {
93 foreach(const QString& path, files) {
94 createFile(path);
95 }
96 }
97
98 void TestBase::createDir(const QString& path)
99 {
100 QString absolutePath = path;
101 makePathAbsoluteAndCreateParents(absolutePath);
102 m_dir->mkdir(absolutePath);
103
104 Q_ASSERT(QFile::exists(absolutePath));
105 }
106
107 void TestBase::makePathAbsoluteAndCreateParents(QString& path)
108 {
109 QFileInfo fileInfo(path);
110 if (!fileInfo.isAbsolute()) {
111 path = m_path + path;
112 fileInfo.setFile(path);
113 }
114
115 const QDir dir = fileInfo.dir();
116 if (!dir.exists()) {
117 createDir(dir.absolutePath());
118 }
119
120 Q_ASSERT(dir.exists());
121 }
122
123 void TestBase::cleanupTestDir()
124 {
125 delete m_tempDir;
126 m_tempDir = new KTempDir;
127 Q_ASSERT(m_tempDir->exists());
128 m_path = m_tempDir->name();
129 m_dir->setPath(m_path);
130 m_view->setUrl(m_path);
131 }