1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Igalia S.L.
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 "PlatformWebView.h"
31#include <gtk/gtk.h>
32#include <wtf/Platform.h>
33#include <wtf/RunLoop.h>
34#include <wtf/glib/GRefPtr.h>
35#include <wtf/glib/GUniquePtr.h>
36#include <wtf/text/WTFString.h>
37
38namespace WTR {
39
40void TestController::notifyDone()
41{
42 RunLoop::main().stop();
43}
44
45void TestController::platformInitialize()
46{
47}
48
49WKPreferencesRef TestController::platformPreferences()
50{
51 return WKPageGroupGetPreferences(m_pageGroup.get());
52}
53
54void TestController::platformDestroy()
55{
56}
57
58void TestController::platformRunUntil(bool& done, WTF::Seconds timeout)
59{
60 struct TimeoutTimer {
61 TimeoutTimer()
62 : timer(RunLoop::main(), this, &TimeoutTimer::fired)
63 { }
64
65 void fired()
66 {
67 timedOut = true;
68 RunLoop::main().stop();
69 }
70
71 RunLoop::Timer<TimeoutTimer> timer;
72 bool timedOut { false };
73 } timeoutTimer;
74
75 timeoutTimer.timer.setPriority(G_PRIORITY_DEFAULT_IDLE);
76 if (timeout >= 0_s)
77 timeoutTimer.timer.startOneShot(timeout);
78
79 while (!done && !timeoutTimer.timedOut)
80 RunLoop::main().run();
81
82 timeoutTimer.timer.stop();
83}
84
85static char* getEnvironmentVariableAsUTF8String(const char* variableName)
86{
87 const char* value = g_getenv(variableName);
88 if (!value) {
89 fprintf(stderr, "%s environment variable not found\n", variableName);
90 exit(0);
91 }
92 gsize bytesWritten;
93 return g_filename_to_utf8(value, -1, 0, &bytesWritten, 0);
94}
95
96void TestController::initializeInjectedBundlePath()
97{
98 GUniquePtr<char> utf8BundlePath(getEnvironmentVariableAsUTF8String("TEST_RUNNER_INJECTED_BUNDLE_FILENAME"));
99 m_injectedBundlePath.adopt(WKStringCreateWithUTF8CString(utf8BundlePath.get()));
100}
101
102void TestController::initializeTestPluginDirectory()
103{
104 GUniquePtr<char> testPluginPath(getEnvironmentVariableAsUTF8String("TEST_RUNNER_TEST_PLUGIN_PATH"));
105 m_testPluginDirectory.adopt(WKStringCreateWithUTF8CString(testPluginPath.get()));
106}
107
108void TestController::platformInitializeContext()
109{
110}
111
112void TestController::setHidden(bool hidden)
113{
114 if (!m_mainWebView)
115 return;
116 if (hidden)
117 gtk_widget_unmap(GTK_WIDGET(m_mainWebView->platformView()));
118 else
119 gtk_widget_map(GTK_WIDGET(m_mainWebView->platformView()));
120}
121
122void TestController::runModal(PlatformWebView*)
123{
124 // FIXME: Need to implement this to test showModalDialog.
125}
126
127void TestController::abortModal()
128{
129}
130
131WKContextRef TestController::platformContext()
132{
133 return m_context.get();
134}
135
136const char* TestController::platformLibraryPathForTesting()
137{
138 return nullptr;
139}
140
141void TestController::platformConfigureViewForTest(const TestInvocation&)
142{
143 WKPageSetApplicationNameForUserAgent(mainWebView()->page(), WKStringCreateWithUTF8CString("WebKitTestRunnerGTK"));
144}
145
146void TestController::platformResetPreferencesToConsistentValues()
147{
148 if (!m_mainWebView)
149 return;
150 m_mainWebView->dismissAllPopupMenus();
151}
152
153void TestController::updatePlatformSpecificTestOptionsForTest(TestOptions& options, const std::string&) const
154{
155 options.enableModernMediaControls = false;
156}
157
158} // namespace WTR
159