]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testbase.cpp
It's easier to put functionality that is used by many unit tests into
[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 "views/dolphinview.h"
23 #include "views/dolphinmodel.h"
24 #include "views/dolphindirlister.h"
25 #include "views/dolphinsortfilterproxymodel.h"
26
27 #include <KTempDir>
28
29 #include <QtCore/QDir>
30 #include <QtGui/QAbstractItemView>
31
32 TestBase::TestBase()
33 {
34 m_tempDir = new KTempDir;
35 Q_ASSERT(m_tempDir->exists());
36 m_path = m_tempDir->name();
37 m_dir = new QDir(m_path);
38 m_dirLister = new DolphinDirLister();
39 m_dirLister->setAutoUpdate(true);
40 m_dolphinModel = new DolphinModel();
41 m_dolphinModel->setDirLister(m_dirLister);
42 m_proxyModel = new DolphinSortFilterProxyModel(0);
43 m_proxyModel->setSourceModel(m_dolphinModel);
44 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
45 m_view = new DolphinView(0, KUrl(m_path), m_proxyModel);
46 }
47
48 TestBase::~TestBase()
49 {
50 delete m_view;
51 delete m_proxyModel;
52 // m_dolphinModel owns m_dirLister -> do not delete it here!
53 delete m_dolphinModel;
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
64 KUrl TestBase::testDirUrl() const
65 {
66 return KUrl(m_path);
67 }
68
69 void TestBase::createFile(const QString& path, const QByteArray& data)
70 {
71 QString absolutePath = path;
72 makePathAbsoluteAndCreateParents(absolutePath);
73
74 QFile f(absolutePath);
75 f.open(QIODevice::WriteOnly);
76 f.write(data);
77 f.close();
78
79 Q_ASSERT(QFile::exists(absolutePath));
80 }
81
82 void TestBase::createFiles(const QStringList& files)
83 {
84 foreach(const QString& path, files) {
85 createFile(path);
86 }
87 }
88
89 void TestBase::createDir(const QString& path)
90 {
91 QString absolutePath = path;
92 makePathAbsoluteAndCreateParents(absolutePath);
93 m_dir->mkdir(absolutePath);
94
95 Q_ASSERT(QFile::exists(absolutePath));
96 }
97
98 void TestBase::makePathAbsoluteAndCreateParents(QString& path)
99 {
100 QFileInfo fileInfo(path);
101 if (!fileInfo.isAbsolute()) {
102 path = m_path + path;
103 fileInfo.setFile(path);
104 }
105
106 const QDir dir = fileInfo.dir();
107 if (!dir.exists()) {
108 createDir(dir.absolutePath());
109 }
110
111 Q_ASSERT(dir.exists());
112 }
113
114 void TestBase::cleanupTestDir()
115 {
116 delete m_tempDir;
117 m_tempDir = new KTempDir;
118 Q_ASSERT(m_tempDir->exists());
119 m_path = m_tempDir->name();
120 m_dir->setPath(m_path);
121 m_view->setUrl(m_path);
122 }