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