WebKitTools/QtTestBrowser/main.cpp
changeset 2 303757a437d3
parent 0 4f2f89ce4247
equal deleted inserted replaced
0:4f2f89ce4247 2:303757a437d3
     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 "launcherwindow.h"
       
    34 #include "urlloader.h"
       
    35 
       
    36 WindowOptions windowOptions;
       
    37 
       
    38 int launcherMain(const QApplication& app)
       
    39 {
       
    40 #ifndef NDEBUG
       
    41     int retVal = app.exec();
       
    42     DumpRenderTreeSupportQt::garbageCollectorCollect();
       
    43     QWebSettings::clearMemoryCaches();
       
    44     return retVal;
       
    45 #else
       
    46     return app.exec();
       
    47 #endif
       
    48 }
       
    49 
       
    50 class LauncherApplication : public QApplication {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     LauncherApplication(int& argc, char** argv);
       
    55     QStringList urls() const { return m_urls; }
       
    56     bool isRobotized() const { return m_isRobotized; }
       
    57 
       
    58 private:
       
    59     void handleUserOptions();
       
    60     void applyDefaultSettings();
       
    61 
       
    62 private:
       
    63     bool m_isRobotized;
       
    64     QStringList m_urls;
       
    65 };
       
    66 
       
    67 void LauncherApplication::applyDefaultSettings()
       
    68 {
       
    69     QWebSettings::setMaximumPagesInCache(4);
       
    70 
       
    71     QWebSettings::setObjectCacheCapacities((16*1024*1024) / 8, (16*1024*1024) / 8, 16*1024*1024);
       
    72 
       
    73     QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
       
    74     QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
       
    75     QWebSettings::enablePersistentStorage();
       
    76 }
       
    77 
       
    78 LauncherApplication::LauncherApplication(int& argc, char** argv)
       
    79     : QApplication(argc, argv, QApplication::GuiServer)
       
    80     , m_isRobotized(false)
       
    81 {
       
    82     // To allow QWebInspector's configuration persistence
       
    83     setOrganizationName("Nokia");
       
    84     setApplicationName("QtTestBrowser");
       
    85     setApplicationVersion("0.1");
       
    86 
       
    87     applyDefaultSettings();
       
    88 
       
    89     handleUserOptions();
       
    90 }
       
    91 
       
    92 static void requiresGraphicsView(const QString& option)
       
    93 {
       
    94     if (windowOptions.useGraphicsView)
       
    95         return;
       
    96     appQuit(1, QString("%1 only works in combination with the -graphicsbased option").arg(option));
       
    97 }
       
    98 
       
    99 void LauncherApplication::handleUserOptions()
       
   100 {
       
   101     QStringList args = arguments();
       
   102     QFileInfo program(args.at(0));
       
   103     QString programName("QtTestBrowser");
       
   104     if (program.exists())
       
   105         programName = program.baseName();
       
   106 
       
   107     QList<QString> updateModes(enumToKeys(QGraphicsView::staticMetaObject,
       
   108             "ViewportUpdateMode", "ViewportUpdate"));
       
   109 
       
   110     if (args.contains("-help")) {
       
   111         qDebug() << "Usage:" << programName.toLatin1().data()
       
   112              << "[-graphicsbased]"
       
   113              << "[-no-compositing]"
       
   114              << QString("[-viewport-update-mode %1]").arg(formatKeys(updateModes)).toLatin1().data()
       
   115              << "[-cache-webview]"
       
   116              << "[-show-fps]"
       
   117              << "[-r list]"
       
   118              << "[-inspector-url location]"
       
   119              << "[-tiled-backing-store]"
       
   120              << "[-resizes-to-contents]"
       
   121              << "URLs";
       
   122         appQuit(0);
       
   123     }
       
   124 
       
   125     if (args.contains("-graphicsbased"))
       
   126         windowOptions.useGraphicsView = true;
       
   127 
       
   128     if (args.contains("-no-compositing")) {
       
   129         requiresGraphicsView("-no-compositing");
       
   130         windowOptions.useCompositing = false;
       
   131     }
       
   132 
       
   133     if (args.contains("-show-fps")) {
       
   134         requiresGraphicsView("-show-fps");
       
   135         windowOptions.showFrameRate = true;
       
   136     }
       
   137 
       
   138     if (args.contains("-cache-webview")) {
       
   139         requiresGraphicsView("-cache-webview");
       
   140         windowOptions.cacheWebView = true;
       
   141     }
       
   142 
       
   143     if (args.contains("-tiled-backing-store")) {
       
   144         requiresGraphicsView("-tiled-backing-store");
       
   145         windowOptions.useTiledBackingStore = true;
       
   146     }
       
   147 
       
   148     if (args.contains("-resizes-to-contents")) {
       
   149         requiresGraphicsView("-resizes-to-contents");
       
   150         windowOptions.resizesToContents = true;
       
   151     }
       
   152 
       
   153     QString arg1("-viewport-update-mode");
       
   154     int modeIndex = args.indexOf(arg1);
       
   155     if (modeIndex != -1) {
       
   156         requiresGraphicsView(arg1);
       
   157 
       
   158         QString mode = takeOptionValue(&args, modeIndex);
       
   159         if (mode.isEmpty())
       
   160             appQuit(1, QString("%1 needs a value of one of [%2]").arg(arg1).arg(formatKeys(updateModes)));
       
   161         int idx = updateModes.indexOf(mode);
       
   162         if (idx == -1)
       
   163             appQuit(1, QString("%1 value has to be one of [%2]").arg(arg1).arg(formatKeys(updateModes)));
       
   164 
       
   165         windowOptions.viewportUpdateMode = static_cast<QGraphicsView::ViewportUpdateMode>(idx);
       
   166     }
       
   167 
       
   168     QString inspectorUrlArg("-inspector-url");
       
   169     int inspectorUrlIndex = args.indexOf(inspectorUrlArg);
       
   170     if (inspectorUrlIndex != -1)
       
   171        windowOptions.inspectorUrl = takeOptionValue(&args, inspectorUrlIndex);
       
   172 
       
   173     int robotIndex = args.indexOf("-r");
       
   174     if (robotIndex != -1) {
       
   175         QString listFile = takeOptionValue(&args, robotIndex);
       
   176         if (listFile.isEmpty())
       
   177             appQuit(1, "-r needs a list file to start in robotized mode");
       
   178         if (!QFile::exists(listFile))
       
   179             appQuit(1, "The list file supplied to -r does not exist.");
       
   180 
       
   181         m_isRobotized = true;
       
   182         m_urls = QStringList(listFile);
       
   183         return;
       
   184     }
       
   185 
       
   186     int lastArg = args.lastIndexOf(QRegExp("^-.*"));
       
   187     m_urls = (lastArg != -1) ? args.mid(++lastArg) : args.mid(1);
       
   188 }
       
   189 
       
   190 
       
   191 int main(int argc, char **argv)
       
   192 {
       
   193     LauncherApplication app(argc, argv);
       
   194 
       
   195     if (app.isRobotized()) {
       
   196         LauncherWindow* window = new LauncherWindow();
       
   197         UrlLoader loader(window->page()->mainFrame(), app.urls().at(0));
       
   198         QObject::connect(window->page()->mainFrame(), SIGNAL(loadFinished(bool)), &loader, SLOT(loadNext()));
       
   199         loader.loadNext();
       
   200         window->show();
       
   201         return launcherMain(app);
       
   202     }
       
   203 
       
   204     QStringList urls = app.urls();
       
   205 
       
   206     if (urls.isEmpty()) {
       
   207         QString defaultUrl = QString("file://%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html"));
       
   208         if (QDir(defaultUrl).exists())
       
   209             urls.append(defaultUrl);
       
   210         else
       
   211             urls.append("");
       
   212     }
       
   213 
       
   214     LauncherWindow* window = 0;
       
   215     foreach (QString url, urls) {
       
   216         LauncherWindow* newWindow;
       
   217         if (!window)
       
   218             newWindow = window = new LauncherWindow(&windowOptions);
       
   219         else
       
   220             newWindow = window->newWindow();
       
   221 
       
   222         newWindow->load(url);
       
   223     }
       
   224 
       
   225     window->show();
       
   226     return launcherMain(app);
       
   227 }
       
   228 
       
   229 #include "main.moc"