[WKTR] Allow changing the WKContextConfiguration between successive tests
[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 TestOptions;
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() const { return m_injectedBundlePath.get(); }
66     WKStringRef testPluginDirectory() const { 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     void setShouldDecideNavigationPolicyAfterDelay(bool value) { m_shouldDecideNavigationPolicyAfterDelay = value; }
128
129     void setNavigationGesturesEnabled(bool value);
130
131 private:
132     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
133     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;
134     void initialize(int argc, const char* argv[]);
135     void createWebViewWithOptions(const TestOptions&);
136     void run();
137
138     void runTestingServerLoop();
139     bool runTest(const char* pathOrURL);
140
141     void platformInitialize();
142     void platformDestroy();
143     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
144     void platformInitializeContext();
145     void platformCreateWebView(WKPageConfigurationRef, const TestOptions&);
146     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions&);
147     void platformResetPreferencesToConsistentValues();
148     void platformResetStateToConsistentValues();
149 #if PLATFORM(COCOA)
150     void cocoaResetStateToConsistentValues();
151 #endif
152     void platformConfigureViewForTest(const TestInvocation&);
153     void platformWillRunTest(const TestInvocation&);
154     void platformRunUntil(bool& done, double timeout);
155     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
156     WKPreferencesRef platformPreferences();
157     void initializeInjectedBundlePath();
158     void initializeTestPluginDirectory();
159
160     void ensureViewSupportsOptionsForTest(const TestInvocation&);
161     TestOptions testOptionsForTest(const TestInvocation&) const;
162     void updatePlatformSpecificTestOptionsForTest(TestOptions&, const TestInvocation&) const;
163
164     void updateWebViewSizeForTest(const TestInvocation&);
165     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
166
167     void decidePolicyForGeolocationPermissionRequestIfPossible();
168     void decidePolicyForUserMediaPermissionRequestIfPossible();
169
170     // WKContextInjectedBundleClient
171     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
172     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
173
174     // WKPageInjectedBundleClient
175     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
176     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
177     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
178     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
179
180     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
181
182     // WKContextClient
183     static void networkProcessDidCrash(WKContextRef, const void*);
184     void networkProcessDidCrash();
185
186     // WKPageNavigationClient
187     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
188     void didCommitNavigation(WKPageRef, WKNavigationRef);
189
190     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
191     void didFinishNavigation(WKPageRef, WKNavigationRef);
192
193     static void processDidCrash(WKPageRef, const void* clientInfo);
194     void processDidCrash();
195
196     static void didBeginNavigationGesture(WKPageRef, const void*);
197     static void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
198     static void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
199     static void didRemoveNavigationGestureSnapshot(WKPageRef, const void*);
200     void didBeginNavigationGesture(WKPageRef);
201     void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
202     void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
203     void didRemoveNavigationGestureSnapshot(WKPageRef);
204
205     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
206     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
207     
208
209     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
210     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
211
212     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
213
214     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void *clientInfo);
215
216     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void *clientInfo);
217     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
218
219     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
220     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
221
222     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
223     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
224
225     // WKContextHistoryClient
226     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
227     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
228
229     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
230     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
231
232     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
233     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
234
235     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
236     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
237
238     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
239
240     static void runModal(WKPageRef, const void* clientInfo);
241     static void runModal(PlatformWebView*);
242
243     static const char* libraryPathForTesting();
244     static const char* platformLibraryPathForTesting();
245
246     std::unique_ptr<TestInvocation> m_currentInvocation;
247
248     bool m_verbose;
249     bool m_printSeparators;
250     bool m_usingServerMode;
251     bool m_gcBetweenTests;
252     bool m_shouldDumpPixelsForAllTests;
253     std::vector<std::string> m_paths;
254     std::vector<std::string> m_allowedHosts;
255     WKRetainPtr<WKStringRef> m_injectedBundlePath;
256     WKRetainPtr<WKStringRef> m_testPluginDirectory;
257
258     WebNotificationProvider m_webNotificationProvider;
259
260     std::unique_ptr<PlatformWebView> m_mainWebView;
261     WKRetainPtr<WKContextRef> m_context;
262     WKRetainPtr<WKPageGroupRef> m_pageGroup;
263
264     enum State {
265         Initial,
266         Resetting,
267         RunningTest
268     };
269     State m_state;
270     bool m_doneResetting;
271
272     bool m_useWaitToDumpWatchdogTimer;
273     bool m_forceNoTimeout;
274
275     bool m_didPrintWebProcessCrashedMessage;
276     bool m_shouldExitWhenWebProcessCrashes;
277     
278     bool m_beforeUnloadReturnValue;
279
280     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
281     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
282     bool m_isGeolocationPermissionSet;
283     bool m_isGeolocationPermissionAllowed;
284
285     Vector<WKRetainPtr<WKUserMediaPermissionRequestRef>> m_userMediaPermissionRequests;
286     bool m_isUserMediaPermissionSet;
287     bool m_isUserMediaPermissionAllowed;
288
289     bool m_policyDelegateEnabled;
290     bool m_policyDelegatePermissive;
291
292     bool m_handlesAuthenticationChallenges;
293     String m_authenticationUsername;
294     String m_authenticationPassword;
295
296     bool m_shouldBlockAllPlugins;
297
298     bool m_forceComplexText;
299     bool m_shouldUseAcceleratedDrawing;
300     bool m_shouldUseRemoteLayerTree;
301
302     bool m_shouldLogHistoryClientCallbacks;
303     bool m_shouldShowWebView;
304
305     bool m_shouldDecideNavigationPolicyAfterDelay { false };
306
307     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
308
309     WorkQueueManager m_workQueueManager;
310 };
311
312 } // namespace WTR
313
314 #endif // TestController_h