Fix webgl crash issue.
[framework/web/webkit-efl.git] / Tools / QtTestBrowser / qttestbrowser.cpp
1 /*
2  * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
4  * Copyright (C) 2006 George Staikos <staikos@kde.org>
5  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
6  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
7  * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "DumpRenderTreeSupportQt.h"
34 #include "QtInitializeTestFonts.h"
35 #include "launcherwindow.h"
36 #include "urlloader.h"
37
38 WindowOptions windowOptions;
39
40 #include <QApplication>
41 #include <QDir>
42 #include <QFile>
43 #include <QFileInfo>
44 #include <QFontDatabase>
45
46 int launcherMain(const QApplication& app)
47 {
48 #ifndef NDEBUG
49     int retVal = app.exec();
50     DumpRenderTreeSupportQt::garbageCollectorCollect();
51     QWebSettings::clearMemoryCaches();
52     return retVal;
53 #else
54     return app.exec();
55 #endif
56 }
57
58 class LauncherApplication : public QApplication {
59     Q_OBJECT
60
61 public:
62     LauncherApplication(int& argc, char** argv);
63     QStringList urls() const { return m_urls; }
64     bool isRobotized() const { return m_isRobotized; }
65     int robotTimeout() const { return m_robotTimeoutSeconds; }
66     int robotExtraTime() const { return m_robotExtraTimeSeconds; }
67
68 private:
69     void handleUserOptions();
70     void applyDefaultSettings();
71
72 private:
73     bool m_isRobotized;
74     int m_robotTimeoutSeconds;
75     int m_robotExtraTimeSeconds;
76     QStringList m_urls;
77 };
78
79 void LauncherApplication::applyDefaultSettings()
80 {
81     QWebSettings::setMaximumPagesInCache(4);
82
83     QWebSettings::setObjectCacheCapacities((16*1024*1024) / 8, (16*1024*1024) / 8, 16*1024*1024);
84
85     QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
86     QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
87     QWebSettings::enablePersistentStorage();
88 }
89
90 LauncherApplication::LauncherApplication(int& argc, char** argv)
91     : QApplication(argc, argv, QApplication::GuiServer)
92     , m_isRobotized(false)
93     , m_robotTimeoutSeconds(0)
94     , m_robotExtraTimeSeconds(0)
95 {
96     // To allow QWebInspector's configuration persistence
97     setOrganizationName("Nokia");
98     setApplicationName("QtTestBrowser");
99     setApplicationVersion("0.1");
100
101     applyDefaultSettings();
102
103     handleUserOptions();
104 }
105
106 static void requiresGraphicsView(const QString& option)
107 {
108     if (windowOptions.useGraphicsView)
109         return;
110     appQuit(1, QString("%1 only works in combination with the -graphicsbased option").arg(option));
111 }
112
113 void LauncherApplication::handleUserOptions()
114 {
115     QStringList args = arguments();
116     QFileInfo program(args.at(0));
117     QString programName("QtTestBrowser");
118     if (program.exists())
119         programName = program.baseName();
120
121     QList<QString> updateModes(enumToKeys(QGraphicsView::staticMetaObject,
122             "ViewportUpdateMode", "ViewportUpdate"));
123
124     if (args.contains("-help")) {
125         qDebug() << "Usage:" << programName.toLatin1().data()
126              << "[-graphicsbased]"
127              << "[-no-compositing]"
128 #if defined(QT_CONFIGURED_WITH_OPENGL)
129              << "[-gl-viewport]"
130              << "[-webgl]"
131 #endif
132              << QString("[-viewport-update-mode %1]").arg(formatKeys(updateModes)).toLatin1().data()
133 #if !defined(QT_NO_NETWORKDISKCACHE) && !defined(QT_NO_DESKTOPSERVICES)
134              << "[-disk-cache]"
135 #endif
136              << "[-cache-webview]"
137              << "[-maximize]"
138              << "[-show-fps]"
139              << "[-r list]"
140              << "[-robot-timeout seconds]"
141              << "[-robot-extra-time seconds]"
142              << "[-inspector-url location]"
143              << "[-tiled-backing-store]"
144              << "[-resizes-to-contents]"
145              << "[-local-storage-enabled]"
146              << "[-no-disk-cookies]"
147              << "[-offline-storage-database-enabled]"
148              << "[-offline-web-application-cache-enabled]"
149              << "[-set-offline-storage-default-quota maxSize]"
150              << "[-use-test-fonts]"
151              << "[-print-loaded-urls]"
152              << "URLs";
153         appQuit(0);
154     }
155
156     const bool defaultForAnimations = args.contains("-default-animations");
157     if (args.contains("-graphicsbased") || defaultForAnimations)
158         windowOptions.useGraphicsView = true;
159
160     if (args.contains("-no-compositing")) {
161         requiresGraphicsView("-no-compositing");
162         windowOptions.useCompositing = false;
163     }
164
165     if (args.contains("-show-fps")) {
166         requiresGraphicsView("-show-fps");
167         windowOptions.showFrameRate = true;
168     }
169
170     if (args.contains("-disk-cache")) {
171 #if !defined(QT_NO_NETWORKDISKCACHE) && !defined(QT_NO_DESKTOPSERVICES)
172         windowOptions.useDiskCache = true;
173 #else
174     appQuit(1, "-disk-cache only works if QNetworkDiskCache and QDesktopServices is enabled in your Qt build.");
175 #endif
176     }
177
178     if (args.contains("-cache-webview") || defaultForAnimations) {
179         requiresGraphicsView("-cache-webview");
180         windowOptions.cacheWebView = true;
181     }
182
183     if (args.contains("-tiled-backing-store")) {
184         requiresGraphicsView("-tiled-backing-store");
185         windowOptions.useTiledBackingStore = true;
186     }
187
188     if (args.contains("-resizes-to-contents")) {
189         requiresGraphicsView("-resizes-to-contents");
190         windowOptions.resizesToContents = true;
191     }
192     
193     if (args.contains("-local-storage-enabled"))
194         windowOptions.useLocalStorage = true;
195
196     if (args.contains("-no-disk-cookies"))
197         windowOptions.useDiskCookies = false;
198
199     if (args.contains("-maximize"))
200         windowOptions.startMaximized = true;
201
202     if (args.contains("-offline-storage-database-enabled"))
203         windowOptions.useOfflineStorageDatabase = true;
204         
205     if (args.contains("-offline-web-application-cache-enabled"))   
206         windowOptions.useOfflineWebApplicationCache = true;
207     
208     int setOfflineStorageDefaultQuotaIndex = args.indexOf("-set-offline-storage-default-quota");
209     if (setOfflineStorageDefaultQuotaIndex != -1) {
210         unsigned int maxSize = takeOptionValue(&args, setOfflineStorageDefaultQuotaIndex).toUInt();
211         windowOptions.offlineStorageDefaultQuotaSize = maxSize;
212     }   
213     
214     if (defaultForAnimations)
215         windowOptions.viewportUpdateMode = QGraphicsView::BoundingRectViewportUpdate;
216
217     QString arg1("-viewport-update-mode");
218     int modeIndex = args.indexOf(arg1);
219     if (modeIndex != -1) {
220         requiresGraphicsView(arg1);
221
222         QString mode = takeOptionValue(&args, modeIndex);
223         if (mode.isEmpty())
224             appQuit(1, QString("%1 needs a value of one of [%2]").arg(arg1).arg(formatKeys(updateModes)));
225         int idx = updateModes.indexOf(mode);
226         if (idx == -1)
227             appQuit(1, QString("%1 value has to be one of [%2]").arg(arg1).arg(formatKeys(updateModes)));
228
229         windowOptions.viewportUpdateMode = static_cast<QGraphicsView::ViewportUpdateMode>(idx);
230     }
231 #ifdef QT_CONFIGURED_WITH_OPENGL
232     if (args.contains("-gl-viewport") || defaultForAnimations) {
233         requiresGraphicsView("-gl-viewport");
234         windowOptions.useQGLWidgetViewport = true;
235     }
236
237     if (args.contains("-webgl")) {
238         requiresGraphicsView("-webgl");
239         windowOptions.useWebGL = true;
240     }
241 #endif
242
243     if (args.contains("-use-test-fonts"))
244         WebKit::initializeTestFonts();
245
246     if (args.contains("-print-loaded-urls"))
247         windowOptions.printLoadedUrls = true;
248
249     QString inspectorUrlArg("-inspector-url");
250     int inspectorUrlIndex = args.indexOf(inspectorUrlArg);
251     if (inspectorUrlIndex != -1)
252        windowOptions.inspectorUrl = takeOptionValue(&args, inspectorUrlIndex);
253
254     QString remoteInspectorPortArg("-remote-inspector-port");
255     int remoteInspectorPortIndex = args.indexOf(remoteInspectorPortArg);
256     if (remoteInspectorPortIndex != -1)
257         windowOptions.remoteInspectorPort = takeOptionValue(&args, remoteInspectorPortIndex).toInt();
258
259     int robotIndex = args.indexOf("-r");
260     if (robotIndex != -1) {
261         QString listFile = takeOptionValue(&args, robotIndex);
262         if (listFile.isEmpty())
263             appQuit(1, "-r needs a list file to start in robotized mode");
264         if (!QFile::exists(listFile))
265             appQuit(1, "The list file supplied to -r does not exist.");
266
267         m_isRobotized = true;
268         m_urls = QStringList(listFile);
269     } else {
270         int lastArg = args.lastIndexOf(QRegExp("^-.*"));
271         m_urls = (lastArg != -1) ? args.mid(++lastArg) : args.mid(1);
272     }
273
274     int robotTimeoutIndex = args.indexOf("-robot-timeout");
275     if (robotTimeoutIndex != -1)
276         m_robotTimeoutSeconds = takeOptionValue(&args, robotTimeoutIndex).toInt();
277
278     int robotExtraTimeIndex = args.indexOf("-robot-extra-time");
279     if (robotExtraTimeIndex != -1)
280         m_robotExtraTimeSeconds = takeOptionValue(&args, robotExtraTimeIndex).toInt();
281 }
282
283
284 int main(int argc, char **argv)
285 {
286     LauncherApplication app(argc, argv);
287
288     if (app.isRobotized()) {
289         LauncherWindow* window = new LauncherWindow();
290         UrlLoader loader(window->page()->mainFrame(), app.urls().at(0), app.robotTimeout(), app.robotExtraTime());
291         loader.loadNext();
292         window->show();
293         return launcherMain(app);
294     }
295
296     QStringList urls = app.urls();
297
298     if (urls.isEmpty()) {
299         QString defaultIndexFile = QString("%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html"));
300         if (QFile(defaultIndexFile).exists())
301             urls.append(QString("file://") + defaultIndexFile);
302         else
303             urls.append("");
304     }
305
306     LauncherWindow* window = 0;
307     foreach (QString url, urls) {
308         LauncherWindow* newWindow;
309         if (!window)
310             newWindow = window = new LauncherWindow(&windowOptions);
311         else
312             newWindow = window->newWindow();
313
314         newWindow->load(url);
315     }
316
317     window->show();
318     return launcherMain(app);
319 }
320
321 #include "qttestbrowser.moc"