qttestbrowser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "DumpRenderTreeSupportQt.h"
  33. #include "QtTestSupport.h"
  34. #include "launcherwindow.h"
  35. #include "urlloader.h"
  36. WindowOptions windowOptions;
  37. #include <QApplication>
  38. #include <QDir>
  39. #include <QFile>
  40. #include <QFileInfo>
  41. #include <QFontDatabase>
  42. int launcherMain(const QApplication& app)
  43. {
  44. #ifndef NDEBUG
  45. int retVal = app.exec();
  46. DumpRenderTreeSupportQt::garbageCollectorCollect();
  47. QWebSettings::clearMemoryCaches();
  48. return retVal;
  49. #else
  50. return app.exec();
  51. #endif
  52. }
  53. class LauncherApplication : public QApplication {
  54. Q_OBJECT
  55. public:
  56. LauncherApplication(int& argc, char** argv);
  57. QStringList urls() const { return m_urls; }
  58. bool isRobotized() const { return m_isRobotized; }
  59. int robotTimeout() const { return m_robotTimeoutSeconds; }
  60. int robotExtraTime() const { return m_robotExtraTimeSeconds; }
  61. private:
  62. void handleUserOptions();
  63. void applyDefaultSettings();
  64. private:
  65. bool m_isRobotized;
  66. int m_robotTimeoutSeconds;
  67. int m_robotExtraTimeSeconds;
  68. QStringList m_urls;
  69. };
  70. void LauncherApplication::applyDefaultSettings()
  71. {
  72. QWebSettings::setMaximumPagesInCache(4);
  73. QWebSettings::setObjectCacheCapacities((16*1024*1024) / 8, (16*1024*1024) / 8, 16*1024*1024);
  74. QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
  75. QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  76. QWebSettings::enablePersistentStorage();
  77. }
  78. LauncherApplication::LauncherApplication(int& argc, char** argv)
  79. : QApplication(argc, argv)
  80. , m_isRobotized(false)
  81. , m_robotTimeoutSeconds(0)
  82. , m_robotExtraTimeSeconds(0)
  83. {
  84. // To allow QWebInspector's configuration persistence
  85. setOrganizationName("Nokia");
  86. setApplicationName("QtTestBrowser");
  87. setApplicationVersion("0.1");
  88. applyDefaultSettings();
  89. handleUserOptions();
  90. }
  91. static void requiresGraphicsView(const QString& option)
  92. {
  93. if (windowOptions.useGraphicsView)
  94. return;
  95. appQuit(1, QString("%1 only works in combination with the -graphicsbased option").arg(option));
  96. }
  97. void LauncherApplication::handleUserOptions()
  98. {
  99. QStringList args = arguments();
  100. QFileInfo program(args.at(0));
  101. QString programName("QtTestBrowser");
  102. if (program.exists())
  103. programName = program.baseName();
  104. QList<QString> updateModes(enumToKeys(QGraphicsView::staticMetaObject,
  105. "ViewportUpdateMode", "ViewportUpdate"));
  106. if (args.contains("-help")) {
  107. qDebug() << "Usage:" << programName.toLatin1().data()
  108. << "[-graphicsbased]"
  109. << "[-no-compositing]"
  110. #if defined(QT_CONFIGURED_WITH_OPENGL)
  111. << "[-gl-viewport]"
  112. << "[-webgl]"
  113. #endif
  114. << QString("[-viewport-update-mode %1]").arg(formatKeys(updateModes)).toLatin1().data()
  115. #if !defined(QT_NO_NETWORKDISKCACHE) && !defined(QT_NO_DESKTOPSERVICES)
  116. << "[-disk-cache]"
  117. #endif
  118. << "[-cache-webview]"
  119. << "[-maximize]"
  120. << "[-show-fps]"
  121. << "[-r list]"
  122. << "[-robot-timeout seconds]"
  123. << "[-robot-extra-time seconds]"
  124. << "[-inspector-url location]"
  125. << "[-tiled-backing-store]"
  126. << "[-resizes-to-contents]"
  127. << "[-local-storage-enabled]"
  128. << "[-no-disk-cookies]"
  129. << "[-offline-storage-database-enabled]"
  130. << "[-offline-web-application-cache-enabled]"
  131. << "[-set-offline-storage-default-quota maxSize]"
  132. << "[-use-test-fonts]"
  133. << "[-print-loaded-urls]"
  134. << "URLs";
  135. appQuit(0);
  136. }
  137. const bool defaultForAnimations = args.contains("-default-animations");
  138. if (args.contains("-graphicsbased") || defaultForAnimations)
  139. windowOptions.useGraphicsView = true;
  140. if (args.contains("-no-compositing")) {
  141. requiresGraphicsView("-no-compositing");
  142. windowOptions.useCompositing = false;
  143. }
  144. if (args.contains("-show-fps")) {
  145. requiresGraphicsView("-show-fps");
  146. windowOptions.showFrameRate = true;
  147. }
  148. if (args.contains("-disk-cache")) {
  149. #if !defined(QT_NO_NETWORKDISKCACHE) && !defined(QT_NO_DESKTOPSERVICES)
  150. windowOptions.useDiskCache = true;
  151. #else
  152. appQuit(1, "-disk-cache only works if QNetworkDiskCache and QDesktopServices is enabled in your Qt build.");
  153. #endif
  154. }
  155. if (args.contains("-cache-webview") || defaultForAnimations) {
  156. requiresGraphicsView("-cache-webview");
  157. windowOptions.cacheWebView = true;
  158. }
  159. if (args.contains("-tiled-backing-store")) {
  160. requiresGraphicsView("-tiled-backing-store");
  161. windowOptions.useTiledBackingStore = true;
  162. }
  163. if (args.contains("-resizes-to-contents")) {
  164. requiresGraphicsView("-resizes-to-contents");
  165. windowOptions.resizesToContents = true;
  166. }
  167. if (args.contains("-local-storage-enabled"))
  168. windowOptions.useLocalStorage = true;
  169. if (args.contains("-no-disk-cookies"))
  170. windowOptions.useDiskCookies = false;
  171. if (args.contains("-maximize"))
  172. windowOptions.startMaximized = true;
  173. if (args.contains("-offline-storage-database-enabled"))
  174. windowOptions.useOfflineStorageDatabase = true;
  175. if (args.contains("-offline-web-application-cache-enabled"))
  176. windowOptions.useOfflineWebApplicationCache = true;
  177. int setOfflineStorageDefaultQuotaIndex = args.indexOf("-set-offline-storage-default-quota");
  178. if (setOfflineStorageDefaultQuotaIndex != -1) {
  179. unsigned int maxSize = takeOptionValue(&args, setOfflineStorageDefaultQuotaIndex).toUInt();
  180. windowOptions.offlineStorageDefaultQuotaSize = maxSize;
  181. }
  182. if (defaultForAnimations)
  183. windowOptions.viewportUpdateMode = QGraphicsView::BoundingRectViewportUpdate;
  184. QString arg1("-viewport-update-mode");
  185. int modeIndex = args.indexOf(arg1);
  186. if (modeIndex != -1) {
  187. requiresGraphicsView(arg1);
  188. QString mode = takeOptionValue(&args, modeIndex);
  189. if (mode.isEmpty())
  190. appQuit(1, QString("%1 needs a value of one of [%2]").arg(arg1).arg(formatKeys(updateModes)));
  191. int idx = updateModes.indexOf(mode);
  192. if (idx == -1)
  193. appQuit(1, QString("%1 value has to be one of [%2]").arg(arg1).arg(formatKeys(updateModes)));
  194. windowOptions.viewportUpdateMode = static_cast<QGraphicsView::ViewportUpdateMode>(idx);
  195. }
  196. #ifdef QT_CONFIGURED_WITH_OPENGL
  197. if (args.contains("-gl-viewport") || defaultForAnimations) {
  198. requiresGraphicsView("-gl-viewport");
  199. windowOptions.useQGLWidgetViewport = true;
  200. }
  201. if (args.contains("-webgl")) {
  202. requiresGraphicsView("-webgl");
  203. windowOptions.useWebGL = true;
  204. }
  205. #endif
  206. if (args.contains("-use-test-fonts"))
  207. WebKit::QtTestSupport::initializeTestFonts();
  208. if (args.contains("-print-loaded-urls"))
  209. windowOptions.printLoadedUrls = true;
  210. QString inspectorUrlArg("-inspector-url");
  211. int inspectorUrlIndex = args.indexOf(inspectorUrlArg);
  212. if (inspectorUrlIndex != -1)
  213. windowOptions.inspectorUrl = takeOptionValue(&args, inspectorUrlIndex);
  214. QString remoteInspectorPortArg("-remote-inspector-port");
  215. int remoteInspectorPortIndex = args.indexOf(remoteInspectorPortArg);
  216. if (remoteInspectorPortIndex != -1)
  217. windowOptions.remoteInspectorPort = takeOptionValue(&args, remoteInspectorPortIndex).toInt();
  218. int robotIndex = args.indexOf("-r");
  219. if (robotIndex != -1) {
  220. QString listFile = takeOptionValue(&args, robotIndex);
  221. if (listFile.isEmpty())
  222. appQuit(1, "-r needs a list file to start in robotized mode");
  223. if (!QFile::exists(listFile))
  224. appQuit(1, "The list file supplied to -r does not exist.");
  225. m_isRobotized = true;
  226. m_urls = QStringList(listFile);
  227. } else {
  228. int lastArg = args.lastIndexOf(QRegExp("^-.*"));
  229. m_urls = (lastArg != -1) ? args.mid(++lastArg) : args.mid(1);
  230. }
  231. int robotTimeoutIndex = args.indexOf("-robot-timeout");
  232. if (robotTimeoutIndex != -1)
  233. m_robotTimeoutSeconds = takeOptionValue(&args, robotTimeoutIndex).toInt();
  234. int robotExtraTimeIndex = args.indexOf("-robot-extra-time");
  235. if (robotExtraTimeIndex != -1)
  236. m_robotExtraTimeSeconds = takeOptionValue(&args, robotExtraTimeIndex).toInt();
  237. }
  238. int main(int argc, char **argv)
  239. {
  240. LauncherApplication app(argc, argv);
  241. if (app.isRobotized()) {
  242. LauncherWindow* window = new LauncherWindow();
  243. UrlLoader loader(window->page()->mainFrame(), app.urls().at(0), app.robotTimeout(), app.robotExtraTime());
  244. loader.loadNext();
  245. window->show();
  246. return launcherMain(app);
  247. }
  248. QStringList urls = app.urls();
  249. if (urls.isEmpty()) {
  250. QString defaultIndexFile = QString("%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html"));
  251. if (QFile(defaultIndexFile).exists())
  252. urls.append(QString("file://") + defaultIndexFile);
  253. else
  254. urls.append("http://www.google.com/");
  255. }
  256. LauncherWindow* window = 0;
  257. foreach (QString url, urls) {
  258. LauncherWindow* newWindow;
  259. if (!window)
  260. newWindow = window = new LauncherWindow(&windowOptions);
  261. else
  262. newWindow = window->newWindow();
  263. newWindow->load(url);
  264. }
  265. window->show();
  266. return launcherMain(app);
  267. }
  268. #include "qttestbrowser.moc"