dd7f808d5064f80ae1a4ab8e1cedc46a912a63a5
[WebKit-https.git] / Tools / WebKitTestRunner / TestController.h
1 /*
2  * Copyright (C) 2010, 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef TestController_h
27 #define TestController_h
28
29 #include "GeolocationProviderMock.h"
30 #include "WebNotificationProvider.h"
31 #include "WorkQueueManager.h"
32 #include <WebKit/WKRetainPtr.h>
33 #include <string>
34 #include <vector>
35 #include <wtf/Vector.h>
36
37 OBJC_CLASS WKWebViewConfiguration;
38
39 namespace WTR {
40
41 class TestInvocation;
42 class PlatformWebView;
43 class EventSenderProxy;
44 struct ViewOptions;
45
46 // FIXME: Rename this TestRunner?
47 class TestController {
48 public:
49     static TestController& singleton();
50
51     static const unsigned viewWidth;
52     static const unsigned viewHeight;
53
54     static const unsigned w3cSVGViewWidth;
55     static const unsigned w3cSVGViewHeight;
56
57     static const double shortTimeout;
58     static const double noTimeout;
59
60     TestController(int argc, const char* argv[]);
61     ~TestController();
62
63     bool verbose() const { return m_verbose; }
64
65     WKStringRef injectedBundlePath() { return m_injectedBundlePath.get(); }
66     WKStringRef testPluginDirectory() { return m_testPluginDirectory.get(); }
67
68     PlatformWebView* mainWebView() { return m_mainWebView.get(); }
69     WKContextRef context() { return m_context.get(); }
70
71     EventSenderProxy* eventSenderProxy() { return m_eventSenderProxy.get(); }
72
73     bool shouldUseRemoteLayerTree() const { return m_shouldUseRemoteLayerTree; }
74     
75     // Runs the run loop until `done` is true or the timeout elapses.
76     bool useWaitToDumpWatchdogTimer() { return m_useWaitToDumpWatchdogTimer; }
77     void runUntil(bool& done, double timeoutSeconds);
78     void notifyDone();
79
80     bool shouldShowWebView() const { return m_shouldShowWebView; }
81
82     void configureViewForTest(const TestInvocation&);
83     
84     bool beforeUnloadReturnValue() const { return m_beforeUnloadReturnValue; }
85     void setBeforeUnloadReturnValue(bool value) { m_beforeUnloadReturnValue = value; }
86
87     void simulateWebNotificationClick(uint64_t notificationID);
88
89     // Geolocation.
90     void setGeolocationPermission(bool);
91     void setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed);
92     void setMockGeolocationPositionUnavailableError(WKStringRef errorMessage);
93     void handleGeolocationPermissionRequest(WKGeolocationPermissionRequestRef);
94     bool isGeolocationProviderActive() const;
95
96     // MediaStream.
97     void setUserMediaPermission(bool);
98     void handleUserMediaPermissionRequest(WKUserMediaPermissionRequestRef);
99
100     // Policy delegate.
101     void setCustomPolicyDelegate(bool enabled, bool permissive);
102
103     // Page Visibility.
104     void setHidden(bool);
105
106     bool resetStateToConsistentValues();
107     void resetPreferencesToConsistentValues();
108
109     void terminateWebContentProcess();
110     void reattachPageToWebProcess();
111
112     static const char* webProcessName();
113     static const char* networkProcessName();
114
115     WorkQueueManager& workQueueManager() { return m_workQueueManager; }
116
117     void setHandlesAuthenticationChallenges(bool value) { m_handlesAuthenticationChallenges = value; }
118     void setAuthenticationUsername(String username) { m_authenticationUsername = username; }
119     void setAuthenticationPassword(String password) { m_authenticationPassword = password; }
120
121     void setBlockAllPlugins(bool shouldBlock) { m_shouldBlockAllPlugins = shouldBlock; }
122
123     void setShouldLogHistoryClientCallbacks(bool shouldLog) { m_shouldLogHistoryClientCallbacks = shouldLog; }
124
125     bool isCurrentInvocation(TestInvocation* invocation) const { return invocation == m_currentInvocation.get(); }
126
127 private:
128     void initialize(int argc, const char* argv[]);
129     void createWebViewWithOptions(const ViewOptions&);
130     void run();
131
132     void runTestingServerLoop();
133     bool runTest(const char* pathOrURL);
134
135     void platformInitialize();
136     void platformDestroy();
137     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
138     void platformInitializeContext();
139     void platformCreateWebView(WKPageConfigurationRef, const ViewOptions&);
140     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const ViewOptions&);
141     void platformResetPreferencesToConsistentValues();
142     void platformResetStateToConsistentValues();
143     void platformConfigureViewForTest(const TestInvocation&);
144     void platformWillRunTest(const TestInvocation&);
145     void platformRunUntil(bool& done, double timeout);
146     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
147     WKPreferencesRef platformPreferences();
148     void initializeInjectedBundlePath();
149     void initializeTestPluginDirectory();
150
151     void ensureViewSupportsOptionsForTest(const TestInvocation&);
152     ViewOptions viewOptionsForTest(const TestInvocation&) const;
153     void updatePlatformSpecificViewOptionsForTest(ViewOptions&, const TestInvocation&) const;
154
155     void updateWebViewSizeForTest(const TestInvocation&);
156     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
157
158     void decidePolicyForGeolocationPermissionRequestIfPossible();
159     void decidePolicyForUserMediaPermissionRequestIfPossible();
160
161     // WKContextInjectedBundleClient
162     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
163     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
164
165     // WKPageInjectedBundleClient
166     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
167     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
168     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
169     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
170
171     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
172
173     // WKContextClient
174     static void networkProcessDidCrash(WKContextRef, const void*);
175     void networkProcessDidCrash();
176
177     // WKPageNavigationClient
178     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
179     void didCommitNavigation(WKPageRef, WKNavigationRef);
180
181     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
182     void didFinishNavigation(WKPageRef, WKNavigationRef);
183
184     static void processDidCrash(WKPageRef, const void* clientInfo);
185     void processDidCrash();
186
187     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
188     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
189     
190
191     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
192     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
193
194     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
195
196     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void *clientInfo);
197
198     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void *clientInfo);
199     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
200
201     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
202     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
203
204     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
205     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
206
207     // WKContextHistoryClient
208     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
209     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
210
211     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
212     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
213
214     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
215     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
216
217     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
218     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
219
220     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
221
222     static void runModal(WKPageRef, const void* clientInfo);
223     static void runModal(PlatformWebView*);
224
225     static const char* libraryPathForTesting();
226     static const char* platformLibraryPathForTesting();
227
228     std::unique_ptr<TestInvocation> m_currentInvocation;
229
230     bool m_verbose;
231     bool m_printSeparators;
232     bool m_usingServerMode;
233     bool m_gcBetweenTests;
234     bool m_shouldDumpPixelsForAllTests;
235     std::vector<std::string> m_paths;
236     std::vector<std::string> m_allowedHosts;
237     WKRetainPtr<WKStringRef> m_injectedBundlePath;
238     WKRetainPtr<WKStringRef> m_testPluginDirectory;
239
240     WebNotificationProvider m_webNotificationProvider;
241
242     std::unique_ptr<PlatformWebView> m_mainWebView;
243     WKRetainPtr<WKContextRef> m_context;
244     WKRetainPtr<WKPageGroupRef> m_pageGroup;
245     WKRetainPtr<WKPageConfigurationRef> m_configuration;
246
247     enum State {
248         Initial,
249         Resetting,
250         RunningTest
251     };
252     State m_state;
253     bool m_doneResetting;
254
255     bool m_useWaitToDumpWatchdogTimer;
256     bool m_forceNoTimeout;
257
258     bool m_didPrintWebProcessCrashedMessage;
259     bool m_shouldExitWhenWebProcessCrashes;
260     
261     bool m_beforeUnloadReturnValue;
262
263     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
264     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
265     bool m_isGeolocationPermissionSet;
266     bool m_isGeolocationPermissionAllowed;
267
268     Vector<WKRetainPtr<WKUserMediaPermissionRequestRef>> m_userMediaPermissionRequests;
269     bool m_isUserMediaPermissionSet;
270     bool m_isUserMediaPermissionAllowed;
271
272     bool m_policyDelegateEnabled;
273     bool m_policyDelegatePermissive;
274
275     bool m_handlesAuthenticationChallenges;
276     String m_authenticationUsername;
277     String m_authenticationPassword;
278
279     bool m_shouldBlockAllPlugins;
280
281     bool m_forceComplexText;
282     bool m_shouldUseAcceleratedDrawing;
283     bool m_shouldUseRemoteLayerTree;
284
285     bool m_shouldLogHistoryClientCallbacks;
286     bool m_shouldShowWebView;
287
288     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
289
290     WorkQueueManager m_workQueueManager;
291 };
292
293 } // namespace WTR
294
295 #endif // TestController_h