b7404873eaca496b4157807156fbdc9e70b2f3c5
[WebKit-https.git] / Tools / WebKitTestRunner / TestController.h
1 /*
2  * Copyright (C) 2010, 2015-2017 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/HashMap.h>
36 #include <wtf/Vector.h>
37 #include <wtf/text/StringHash.h>
38
39 OBJC_CLASS WKWebViewConfiguration;
40
41 namespace WTR {
42
43 class TestInvocation;
44 class OriginSettings;
45 class PlatformWebView;
46 class EventSenderProxy;
47 struct TestCommand;
48 struct TestOptions;
49
50 // FIXME: Rename this TestRunner?
51 class TestController {
52 public:
53     static TestController& singleton();
54
55     static const unsigned viewWidth;
56     static const unsigned viewHeight;
57
58     static const unsigned w3cSVGViewWidth;
59     static const unsigned w3cSVGViewHeight;
60
61     static const double defaultShortTimeout;
62     static const double noTimeout;
63
64     TestController(int argc, const char* argv[]);
65     ~TestController();
66
67     bool verbose() const { return m_verbose; }
68
69     WKStringRef injectedBundlePath() const { return m_injectedBundlePath.get(); }
70     WKStringRef testPluginDirectory() const { return m_testPluginDirectory.get(); }
71
72     PlatformWebView* mainWebView() { return m_mainWebView.get(); }
73     WKContextRef context() { return m_context.get(); }
74
75     EventSenderProxy* eventSenderProxy() { return m_eventSenderProxy.get(); }
76
77     bool shouldUseRemoteLayerTree() const { return m_shouldUseRemoteLayerTree; }
78     
79     // Runs the run loop until `done` is true or the timeout elapses.
80     bool useWaitToDumpWatchdogTimer() { return m_useWaitToDumpWatchdogTimer; }
81     void runUntil(bool& done, double timeoutSeconds);
82     void notifyDone();
83
84     bool shouldShowWebView() const { return m_shouldShowWebView; }
85     bool usingServerMode() const { return m_usingServerMode; }
86     void configureViewForTest(const TestInvocation&);
87     
88     bool beforeUnloadReturnValue() const { return m_beforeUnloadReturnValue; }
89     void setBeforeUnloadReturnValue(bool value) { m_beforeUnloadReturnValue = value; }
90
91     void simulateWebNotificationClick(uint64_t notificationID);
92
93     // Geolocation.
94     void setGeolocationPermission(bool);
95     void setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed);
96     void setMockGeolocationPositionUnavailableError(WKStringRef errorMessage);
97     void handleGeolocationPermissionRequest(WKGeolocationPermissionRequestRef);
98     bool isGeolocationProviderActive() const;
99
100     // MediaStream.
101     String saltForOrigin(WKFrameRef, String);
102     void getUserMediaInfoForOrigin(WKFrameRef, WKStringRef originKey, bool&, WKRetainPtr<WKStringRef>&);
103     WKStringRef getUserMediaSaltForOrigin(WKFrameRef, WKStringRef originKey);
104     void setUserMediaPermission(bool);
105     void setUserMediaPersistentPermissionForOrigin(bool, WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
106     void handleUserMediaPermissionRequest(WKFrameRef, WKSecurityOriginRef, WKSecurityOriginRef, WKUserMediaPermissionRequestRef);
107     void handleCheckOfUserMediaPermissionForOrigin(WKFrameRef, WKSecurityOriginRef, WKSecurityOriginRef, const WKUserMediaPermissionCheckRef&);
108     OriginSettings& settingsForOrigin(const String&);
109     unsigned userMediaPermissionRequestCountForOrigin(WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
110     void resetUserMediaPermissionRequestCountForOrigin(WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
111
112     // Policy delegate.
113     void setCustomPolicyDelegate(bool enabled, bool permissive);
114
115     // Page Visibility.
116     void setHidden(bool);
117
118     unsigned imageCountInGeneralPasteboard() const;
119
120     bool resetStateToConsistentValues(const TestOptions&);
121     void resetPreferencesToConsistentValues(const TestOptions&);
122
123     void terminateWebContentProcess();
124     void reattachPageToWebProcess();
125
126     static const char* webProcessName();
127     static const char* networkProcessName();
128     static const char* databaseProcessName();
129
130     WorkQueueManager& workQueueManager() { return m_workQueueManager; }
131
132     void setRejectsProtectionSpaceAndContinueForAuthenticationChallenges(bool value) { m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges = value; }
133     void setHandlesAuthenticationChallenges(bool value) { m_handlesAuthenticationChallenges = value; }
134     void setAuthenticationUsername(String username) { m_authenticationUsername = username; }
135     void setAuthenticationPassword(String password) { m_authenticationPassword = password; }
136     void setAllowsAnySSLCertificate(bool);
137
138     void setBlockAllPlugins(bool shouldBlock) { m_shouldBlockAllPlugins = shouldBlock; }
139
140     void setShouldLogHistoryClientCallbacks(bool shouldLog) { m_shouldLogHistoryClientCallbacks = shouldLog; }
141     void setShouldLogCanAuthenticateAgainstProtectionSpace(bool shouldLog) { m_shouldLogCanAuthenticateAgainstProtectionSpace = shouldLog; }
142
143     bool isCurrentInvocation(TestInvocation* invocation) const { return invocation == m_currentInvocation.get(); }
144
145     void setShouldDecideNavigationPolicyAfterDelay(bool value) { m_shouldDecideNavigationPolicyAfterDelay = value; }
146
147     void setNavigationGesturesEnabled(bool value);
148     void setIgnoresViewportScaleLimits(bool);
149
150     void setShouldDownloadUndisplayableMIMETypes(bool value) { m_shouldDownloadUndisplayableMIMETypes = value; }
151
152     void setStatisticsPrevalentResource(WKStringRef hostName, bool value);
153     bool isStatisticsPrevalentResource(WKStringRef hostName);
154     void setStatisticsHasHadUserInteraction(WKStringRef hostName, bool value);
155     bool isStatisticsHasHadUserInteraction(WKStringRef hostName);
156     void setStatisticsSubframeUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
157     void setStatisticsSubresourceUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
158     void setStatisticsSubresourceUniqueRedirectTo(WKStringRef hostName, WKStringRef hostNameRedirectedTo);
159     void setStatisticsTimeToLiveUserInteraction(double seconds);
160     void setStatisticsTimeToLiveCookiePartitionFree(double seconds);
161     void statisticsFireDataModificationHandler();
162     void statisticsFireShouldPartitionCookiesHandler();
163     void statisticsFireShouldPartitionCookiesHandlerForOneDomain(WKStringRef hostName, bool value);
164     void setStatisticsNotifyPagesWhenDataRecordsWereScanned(bool);
165     void setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(bool);
166     void setStatisticsMinimumTimeBetweeenDataRecordsRemoval(double);
167     void statisticsClearInMemoryAndPersistentStore();
168     void statisticsClearInMemoryAndPersistentStoreModifiedSinceHours(unsigned);
169     void statisticsResetToConsistentState();
170
171     WKArrayRef openPanelFileURLs() const { return m_openPanelFileURLs.get(); }
172     void setOpenPanelFileURLs(WKArrayRef fileURLs) { m_openPanelFileURLs = fileURLs; }
173
174     void terminateNetworkProcess();
175     
176 private:
177     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
178     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;
179     void initialize(int argc, const char* argv[]);
180     void createWebViewWithOptions(const TestOptions&);
181     void run();
182
183     void runTestingServerLoop();
184     bool runTest(const char* pathOrURL);
185
186     void platformInitialize();
187     void platformDestroy();
188     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
189     void platformInitializeContext();
190     void platformCreateWebView(WKPageConfigurationRef, const TestOptions&);
191     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions&);
192     void platformResetPreferencesToConsistentValues();
193     void platformResetStateToConsistentValues();
194 #if PLATFORM(COCOA)
195     void cocoaResetStateToConsistentValues();
196 #endif
197     void platformConfigureViewForTest(const TestInvocation&);
198     void platformWillRunTest(const TestInvocation&);
199     void platformRunUntil(bool& done, double timeout);
200     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
201     WKContextRef platformContext();
202     WKPreferencesRef platformPreferences();
203     void initializeInjectedBundlePath();
204     void initializeTestPluginDirectory();
205
206     void ensureViewSupportsOptionsForTest(const TestInvocation&);
207     TestOptions testOptionsForTest(const TestCommand&) const;
208     void updatePlatformSpecificTestOptionsForTest(TestOptions&, const std::string& pathOrURL) const;
209
210     void updateWebViewSizeForTest(const TestInvocation&);
211     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
212
213     void decidePolicyForGeolocationPermissionRequestIfPossible();
214     void decidePolicyForUserMediaPermissionRequestIfPossible();
215
216     // WKContextInjectedBundleClient
217     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
218     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
219     static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void *clientInfo);
220
221     // WKPageInjectedBundleClient
222     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
223     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
224     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
225     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
226     WKRetainPtr<WKTypeRef> getInjectedBundleInitializationUserData();
227
228     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
229
230     // WKContextClient
231     static void networkProcessDidCrash(WKContextRef, const void*);
232     void networkProcessDidCrash();
233     static void databaseProcessDidCrash(WKContextRef, const void*);
234     void databaseProcessDidCrash();
235
236     // WKPageNavigationClient
237     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
238     void didCommitNavigation(WKPageRef, WKNavigationRef);
239
240     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
241     void didFinishNavigation(WKPageRef, WKNavigationRef);
242
243     
244     // WKContextDownloadClient
245     static void downloadDidStart(WKContextRef, WKDownloadRef, const void*);
246     void downloadDidStart(WKContextRef, WKDownloadRef);
247     static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool* allowOverwrite, const void *clientInfo);
248     WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool*& allowOverwrite);
249     static void downloadDidFinish(WKContextRef, WKDownloadRef, const void*);
250     void downloadDidFinish(WKContextRef, WKDownloadRef);
251     static void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef, const void*);
252     void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef);
253     static void downloadDidCancel(WKContextRef, WKDownloadRef, const void*);
254     void downloadDidCancel(WKContextRef, WKDownloadRef);
255     
256     static void processDidCrash(WKPageRef, const void* clientInfo);
257     void processDidCrash();
258
259     static void didBeginNavigationGesture(WKPageRef, const void*);
260     static void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
261     static void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
262     static void didRemoveNavigationGestureSnapshot(WKPageRef, const void*);
263     void didBeginNavigationGesture(WKPageRef);
264     void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
265     void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
266     void didRemoveNavigationGestureSnapshot(WKPageRef);
267
268     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
269     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
270     
271
272     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
273     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
274
275     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
276
277     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void*);
278     bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef);
279
280     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void*);
281     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
282
283     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
284     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
285
286     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
287     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
288
289     // WKContextHistoryClient
290     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
291     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
292
293     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
294     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
295
296     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
297     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
298
299     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
300     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
301
302     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
303
304     static void runModal(WKPageRef, const void* clientInfo);
305     static void runModal(PlatformWebView*);
306
307     static const char* libraryPathForTesting();
308     static const char* platformLibraryPathForTesting();
309
310     std::unique_ptr<TestInvocation> m_currentInvocation;
311
312     bool m_verbose { false };
313     bool m_printSeparators { false };
314     bool m_usingServerMode { false };
315     bool m_gcBetweenTests { false };
316     bool m_shouldDumpPixelsForAllTests { false };
317     std::vector<std::string> m_paths;
318     std::vector<std::string> m_allowedHosts;
319     WKRetainPtr<WKStringRef> m_injectedBundlePath;
320     WKRetainPtr<WKStringRef> m_testPluginDirectory;
321
322     WebNotificationProvider m_webNotificationProvider;
323
324     std::unique_ptr<PlatformWebView> m_mainWebView;
325     WKRetainPtr<WKContextRef> m_context;
326     WKRetainPtr<WKPageGroupRef> m_pageGroup;
327
328     enum State {
329         Initial,
330         Resetting,
331         RunningTest
332     };
333     State m_state { Initial };
334     bool m_doneResetting { false };
335
336     bool m_useWaitToDumpWatchdogTimer { true };
337     bool m_forceNoTimeout { false };
338
339     bool m_didPrintWebProcessCrashedMessage { false };
340     bool m_shouldExitWhenWebProcessCrashes { true };
341     
342     bool m_beforeUnloadReturnValue { true };
343
344     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
345     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
346     bool m_isGeolocationPermissionSet { false };
347     bool m_isGeolocationPermissionAllowed { false };
348
349     HashMap<String, RefPtr<OriginSettings>> m_cachedUserMediaPermissions;
350
351     typedef Vector<std::pair<String, WKRetainPtr<WKUserMediaPermissionRequestRef>>> PermissionRequestList;
352     PermissionRequestList m_userMediaPermissionRequests;
353
354     bool m_isUserMediaPermissionSet { false };
355     bool m_isUserMediaPermissionAllowed { false };
356
357     bool m_policyDelegateEnabled { false };
358     bool m_policyDelegatePermissive { false };
359     bool m_shouldDownloadUndisplayableMIMETypes { false };
360
361     bool m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges { false };
362     bool m_handlesAuthenticationChallenges { false };
363     String m_authenticationUsername;
364     String m_authenticationPassword;
365
366     bool m_shouldBlockAllPlugins { false };
367
368     bool m_forceComplexText { false };
369     bool m_shouldUseAcceleratedDrawing { false };
370     bool m_shouldUseRemoteLayerTree { false };
371
372     bool m_shouldLogCanAuthenticateAgainstProtectionSpace { false };
373     bool m_shouldLogHistoryClientCallbacks { false };
374     bool m_shouldShowWebView { false };
375     
376     bool m_shouldDecideNavigationPolicyAfterDelay { false };
377
378     WKRetainPtr<WKArrayRef> m_openPanelFileURLs;
379
380     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
381
382     WorkQueueManager m_workQueueManager;
383 };
384
385 struct TestCommand {
386     std::string pathOrURL;
387     std::string absolutePath;
388     bool shouldDumpPixels { false };
389     std::string expectedPixelHash;
390     int timeout { 0 };
391     bool dumpJSConsoleLogInStdErr { false };
392 };
393
394 } // namespace WTR
395
396 #endif // TestController_h