remove remaining incorrect use of hidesOverflow - use hasOverflowClip
[kdelibs.git] / kparts / tests / parttest.cpp
blobe48b5782c80d7281fc0747dc4662e37e552d2516
1 /*
2 Copyright (c) 2007 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
19 #include "parttest.h"
20 #include "qtest_kde.h"
22 #include <kparts/part.h>
23 #include <QWidget>
25 QTEST_KDEMAIN( PartTest, GUI )
27 class TestPart : public KParts::ReadOnlyPart
29 public:
30 TestPart(QObject* parent, QWidget* parentWidget)
31 : KParts::ReadOnlyPart(parent),
32 m_openFileCalled(false) {
33 setWidget(new QWidget(parentWidget));
36 bool openFileCalled() const { return m_openFileCalled; }
37 protected:
38 /*reimp*/ bool openFile() {
39 m_openFileCalled = true;
40 return true;
42 private:
43 bool m_openFileCalled;
46 void PartTest::testAutoDeletePart()
48 KParts::Part* part = new TestPart(0, 0);
49 QPointer<KParts::Part> partPointer(part);
50 delete part->widget();
51 QVERIFY(partPointer.isNull());
54 void PartTest::testAutoDeleteWidget()
56 KParts::Part* part = new TestPart(0, 0);
57 QPointer<KParts::Part> partPointer(part);
58 QPointer<QWidget> widgetPointer(part->widget());
59 delete part;
60 QVERIFY(widgetPointer.isNull());
63 void PartTest::testNoAutoDeletePart()
65 KParts::Part* part = new TestPart(0, 0);
66 part->setAutoDeletePart(false);
67 QPointer<KParts::Part> partPointer(part);
68 delete part->widget();
69 QVERIFY(part->widget() == 0);
70 QCOMPARE(static_cast<KParts::Part*>(partPointer), part);
71 delete part;
74 void PartTest::testNoAutoDeleteWidget()
76 KParts::Part* part = new TestPart(0, 0);
77 part->setAutoDeleteWidget(false);
78 QWidget* widget = part->widget();
79 QVERIFY(widget);
80 QPointer<QWidget> widgetPointer(part->widget());
81 delete part;
82 QCOMPARE(static_cast<QWidget*>(widgetPointer), widget);
83 delete widget;
86 // There is no operator== in OpenUrlArguments because it's only useful in unit tests
87 static bool compareArgs(const KParts::OpenUrlArguments& arg1,
88 const KParts::OpenUrlArguments& arg2)
90 return arg1.mimeType() == arg2.mimeType() &&
91 arg1.xOffset() == arg2.xOffset() &&
92 arg1.yOffset() == arg2.yOffset() &&
93 arg1.reload() == arg2.reload();
96 void PartTest::testOpenUrlArguments()
98 TestPart* part = new TestPart(0, 0);
99 QVERIFY(part->closeUrl()); // nothing to do, no error
100 QVERIFY(part->arguments().mimeType().isEmpty());
101 KParts::OpenUrlArguments args;
102 args.setMimeType("application/xml");
103 args.setXOffset(50);
104 args.setYOffset(10);
105 args.setReload(true);
106 part->setArguments(args);
107 QVERIFY(compareArgs(args, part->arguments()));
108 part->openUrl(KUrl(KDESRCDIR "/parttest.cpp"));
109 QVERIFY(part->openFileCalled());
110 QVERIFY(compareArgs(args, part->arguments()));
112 // Explicit call to closeUrl: arguments are cleared
113 part->closeUrl();
114 QVERIFY(part->arguments().mimeType().isEmpty());
116 // Calling openUrl with local file: mimetype is determined
117 part->openUrl(KUrl(KDESRCDIR "/parttest.cpp"));
118 QCOMPARE(part->arguments().mimeType(), QString("text/x-c++src"));
119 // (for a remote url it would be determined during downloading)
121 delete part;
124 void PartTest::testAutomaticMimeType()
126 TestPart* part = new TestPart(0, 0);
127 QVERIFY(part->closeUrl()); // nothing to do, no error
128 QVERIFY(part->arguments().mimeType().isEmpty());
129 // open a file, and test the detected mimetype
130 part->openUrl(KUrl(KDESRCDIR "/notepad.desktop"));
131 QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/x-desktop"));
133 // manually closing, no mimetype should be stored now
134 part->closeUrl();
135 QVERIFY(part->arguments().mimeType().isEmpty());
137 // open a new file, and test again its (autdetected) mimetype
138 part->openUrl(KUrl(KDESRCDIR "/parttest.cpp"));
139 QCOMPARE(part->arguments().mimeType(), QString("text/x-c++src"));
141 // open a new file, but without explicitely close the first
142 part->openUrl(KUrl(KDESRCDIR "/notepad.desktop"));
143 // test again its (autdetected) mimetype
144 QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/x-desktop"));
146 // open a new file, but manually forcing a mimetype
147 KParts::OpenUrlArguments args;
148 args.setMimeType("application/xml");
149 part->setArguments(args);
150 QVERIFY(compareArgs(args, part->arguments()));
151 part->openUrl(KUrl(KDESRCDIR "/parttest.cpp"));
152 QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/xml"));
154 // clear the args and open a new file, reactivating the automatic mimetype detection again
155 part->setArguments(KParts::OpenUrlArguments());
156 part->openUrl(KUrl(KDESRCDIR "/notepad.desktop"));
157 // test again its (autdetected) mimetype
158 QCOMPARE(part->arguments().mimeType(), QString::fromLatin1("application/x-desktop"));
160 delete part;
163 #include <kparts/mainwindow.h>
164 #include <ktoolbar.h>
165 #include <kconfiggroup.h>
166 #include <ktoggletoolbaraction.h>
167 class MyMainWindow : public KParts::MainWindow
169 public:
170 MyMainWindow() : KParts::MainWindow() {
171 tb = new KToolBar(this);
172 tb->setObjectName("testtbvisibility");
175 // createGUI and saveAutoSaveSettings are protected, so the whole test is here:
176 void testToolbarVisibility()
178 QVERIFY(tb->isVisible());
180 TestPart* part = new TestPart(0, 0);
181 // TODO define xml with a toolbar for the part
182 // and put some saved settings into qttestrc in order to test
183 // r347935+r348051, i.e. the fact that KParts::MainWindow::createGUI
184 // will apply the toolbar settings (and that they won't have been
185 // erased by the previous call to saveMainWindowSettings...)
186 this->createGUI(part);
188 QVERIFY(tb->isVisible());
189 this->saveAutoSaveSettings();
191 // Hide the toolbar using the action (so that setSettingsDirty is called, too)
192 KToggleToolBarAction action(tb, QString(), 0);
193 action.trigger();
194 QVERIFY(!tb->isVisible());
196 // Switch the active part, and check that
197 // the toolbar doesn't magically reappear,
198 // as it did when createGUI was calling applyMainWindowSettings
199 this->createGUI(0);
200 QVERIFY(!tb->isVisible());
201 this->createGUI(part);
202 QVERIFY(!tb->isVisible());
204 // All ok, show it again so that test can be run again :)
205 action.trigger();
206 QVERIFY(tb->isVisible());
207 close();
209 private:
210 KToolBar* tb;
213 // A KParts::MainWindow unit test
214 void PartTest::testToolbarVisibility()
216 // The bug was: hide a toolbar in konqueror,
217 // then switch tabs -> the toolbar shows again
218 // (unless you waited for the autosave timer to kick in)
219 MyMainWindow window;
220 KConfigGroup cg(KGlobal::config(), "kxmlgui_unittest");
221 window.setAutoSaveSettings(cg.name());
222 window.show();
223 window.testToolbarVisibility();
226 #include "parttest.moc"