]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testbase.cpp
1cb326766c71984a50ff19b92fef17f2fb76b2f2
[dolphin.git] / src / tests / testbase.cpp
1 /*****************************************************************************
2 * Copyright (C) 2010-2011 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 #ifdef Q_OS_UNIX
37 #include <utime.h>
38 #else
39 #include <sys/utime.h>
40 #endif
41
42 TestBase::TestBase()
43 {
44 m_tempDir = new KTempDir;
45 Q_ASSERT(m_tempDir->exists());
46 m_path = m_tempDir->name();
47 m_dir = new QDir(m_path);
48 m_view = new DolphinView(KUrl(m_path), 0);
49 }
50
51 TestBase::~TestBase()
52 {
53 delete m_view;
54 delete m_dir;
55 delete m_tempDir;
56 }
57
58 QAbstractItemView* TestBase::itemView() const
59 {
60 return m_view->m_viewAccessor.itemView();
61 }
62
63 void TestBase::reloadViewAndWait()
64 {
65 m_view->reload();
66 QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(finishedPathLoading(const KUrl&)), 2000));
67 }
68
69 KUrl TestBase::testDirUrl() const
70 {
71 return KUrl(m_path);
72 }
73
74 /** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
75
76 static void setTimeStamp(const QString& path, const QDateTime& mtime)
77 {
78 #ifdef Q_OS_UNIX
79 struct utimbuf utbuf;
80 utbuf.actime = mtime.toTime_t();
81 utbuf.modtime = utbuf.actime;
82 utime(QFile::encodeName(path), &utbuf);
83 #elif defined(Q_OS_WIN)
84 struct _utimbuf utbuf;
85 utbuf.actime = mtime.toTime_t();
86 utbuf.modtime = utbuf.actime;
87 _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
88 #endif
89 }
90
91 void TestBase::createFile(const QString& path, const QByteArray& data, const QDateTime& time)
92 {
93 QString absolutePath = path;
94 makePathAbsoluteAndCreateParents(absolutePath);
95
96 QFile f(absolutePath);
97 f.open(QIODevice::WriteOnly);
98 f.write(data);
99 f.close();
100
101 if (time.isValid()) {
102 setTimeStamp(absolutePath, time);
103 }
104
105 Q_ASSERT(QFile::exists(absolutePath));
106 }
107
108 void TestBase::createFiles(const QStringList& files)
109 {
110 foreach(const QString& path, files) {
111 createFile(path);
112 }
113 }
114
115 void TestBase::createDir(const QString& path, const QDateTime& time)
116 {
117 QString absolutePath = path;
118 makePathAbsoluteAndCreateParents(absolutePath);
119 m_dir->mkdir(absolutePath);
120
121 if (time.isValid()) {
122 setTimeStamp(absolutePath, time);
123 }
124
125 Q_ASSERT(QFile::exists(absolutePath));
126 }
127
128 QStringList TestBase::viewItems() const
129 {
130 QStringList itemList;
131 const QAbstractItemModel* model = itemView()->model();
132
133 for (int row = 0; row < model->rowCount(); row++) {
134 itemList << model->data(model->index(row, 0), Qt::DisplayRole).toString();
135 }
136
137 return itemList;
138 }
139
140 void TestBase::makePathAbsoluteAndCreateParents(QString& path)
141 {
142 QFileInfo fileInfo(path);
143 if (!fileInfo.isAbsolute()) {
144 path = m_path + path;
145 fileInfo.setFile(path);
146 }
147
148 const QDir dir = fileInfo.dir();
149 if (!dir.exists()) {
150 createDir(dir.absolutePath());
151 }
152
153 Q_ASSERT(dir.exists());
154 }
155
156 void TestBase::cleanupTestDir()
157 {
158 delete m_tempDir;
159 m_tempDir = new KTempDir;
160 Q_ASSERT(m_tempDir->exists());
161 m_path = m_tempDir->name();
162 m_dir->setPath(m_path);
163 m_view->setUrl(m_path);
164 }