1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 University of Szeged. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "TestController.h"
29
30#include "WKStringQt.h"
31
32#include <cstdlib>
33#include <QCoreApplication>
34#include <QEventLoop>
35#include <QFileInfo>
36#include <QLibrary>
37#include <QObject>
38#include <QtGlobal>
39#include <wtf/Platform.h>
40#include <wtf/text/WTFString.h>
41
42namespace WTR {
43
44class TestControllerRunLoop : public QObject {
45    Q_OBJECT
46public:
47    static TestControllerRunLoop* instance()
48    {
49        static TestControllerRunLoop* result = new TestControllerRunLoop;
50        return result;
51    }
52
53    void start(int msec)
54    {
55        m_timerID = startTimer(msec);
56        ASSERT(m_timerID);
57        m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
58    }
59
60    void stop()
61    {
62        killTimer(m_timerID);
63        m_eventLoop.quit();
64    }
65private:
66    TestControllerRunLoop() {}
67
68    void timerEvent(QTimerEvent*)
69    {
70        fprintf(stderr, "FAIL: TestControllerRunLoop timed out.\n");
71        stop();
72    }
73
74    QEventLoop m_eventLoop;
75    int m_timerID;
76};
77
78void TestController::notifyDone()
79{
80    TestControllerRunLoop::instance()->stop();
81}
82
83void TestController::platformInitialize()
84{
85}
86
87void TestController::platformRunUntil(bool&, double timeout)
88{
89    TestControllerRunLoop::instance()->start(static_cast<int>(timeout * 1000));
90}
91
92static bool isExistingLibrary(const QString& path)
93{
94#if OS(WINDOWS) || OS(SYMBIAN)
95    const char* librarySuffixes[] = { ".dll" };
96#elif OS(MAC_OS_X)
97    const char* librarySuffixes[] = { ".bundle", ".dylib", ".so" };
98#elif OS(UNIX)
99    const char* librarySuffixes[] = { ".so" };
100#else
101#error Library path suffix should be specified for this platform
102#endif
103    for (unsigned i = 0; i < sizeof(librarySuffixes) / sizeof(const char*); ++i) {
104        if (QLibrary::isLibrary(path + librarySuffixes[i]))
105            return true;
106    }
107
108    return false;
109}
110
111void TestController::initializeInjectedBundlePath()
112{
113    QString path = QLatin1String(getenv("WTR_INJECTEDBUNDLE_PATH"));
114    if (path.isEmpty())
115        path = QFileInfo(QCoreApplication::applicationDirPath() + "/../lib/libWTRInjectedBundle").absoluteFilePath();
116    if (!isExistingLibrary(path))
117        qFatal("Cannot find the injected bundle at %s\n", qPrintable(path));
118
119    m_injectedBundlePath = WKStringCreateWithQString(path);
120}
121
122void TestController::initializeTestPluginDirectory()
123{
124    // This is called after initializeInjectedBundlePath.
125    m_testPluginDirectory = m_injectedBundlePath;
126}
127
128void TestController::platformInitializeContext()
129{
130}
131
132void TestController::runModal(PlatformWebView*)
133{
134    // FIXME: Need to implement this to test showModalDialog.
135}
136
137const char* TestController::platformLibraryPathForTesting()
138{
139    return 0;
140}
141
142#include "TestControllerQt.moc"
143
144} // namespace WTR
145