b46b6b18028f002a39496f98c474265bd7ea473f
[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 shouldShowTouches() const { return m_shouldShowTouches; }
89     
90     bool beforeUnloadReturnValue() const { return m_beforeUnloadReturnValue; }
91     void setBeforeUnloadReturnValue(bool value) { m_beforeUnloadReturnValue = value; }
92
93     void simulateWebNotificationClick(uint64_t notificationID);
94
95     // Geolocation.
96     void setGeolocationPermission(bool);
97     void setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, bool providesFloorLevel, double floorLevel);
98     void setMockGeolocationPositionUnavailableError(WKStringRef errorMessage);
99     void handleGeolocationPermissionRequest(WKGeolocationPermissionRequestRef);
100     bool isGeolocationProviderActive() const;
101
102     // MediaStream.
103     String saltForOrigin(WKFrameRef, String);
104     void getUserMediaInfoForOrigin(WKFrameRef, WKStringRef originKey, bool&, WKRetainPtr<WKStringRef>&);
105     WKStringRef getUserMediaSaltForOrigin(WKFrameRef, WKStringRef originKey);
106     void setUserMediaPermission(bool);
107     void resetUserMediaPermission();
108     void setUserMediaPersistentPermissionForOrigin(bool, WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
109     void handleUserMediaPermissionRequest(WKFrameRef, WKSecurityOriginRef, WKSecurityOriginRef, WKUserMediaPermissionRequestRef);
110     void handleCheckOfUserMediaPermissionForOrigin(WKFrameRef, WKSecurityOriginRef, WKSecurityOriginRef, const WKUserMediaPermissionCheckRef&);
111     OriginSettings& settingsForOrigin(const String&);
112     unsigned userMediaPermissionRequestCountForOrigin(WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
113     void resetUserMediaPermissionRequestCountForOrigin(WKStringRef userMediaDocumentOriginString, WKStringRef topLevelDocumentOriginString);
114
115     // Policy delegate.
116     void setCustomPolicyDelegate(bool enabled, bool permissive);
117
118     // Page Visibility.
119     void setHidden(bool);
120
121     unsigned imageCountInGeneralPasteboard() const;
122
123     bool resetStateToConsistentValues(const TestOptions&);
124     void resetPreferencesToConsistentValues(const TestOptions&);
125
126     void terminateWebContentProcess();
127     void reattachPageToWebProcess();
128
129     static const char* webProcessName();
130     static const char* networkProcessName();
131     static const char* databaseProcessName();
132
133     WorkQueueManager& workQueueManager() { return m_workQueueManager; }
134
135     void setRejectsProtectionSpaceAndContinueForAuthenticationChallenges(bool value) { m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges = value; }
136     void setHandlesAuthenticationChallenges(bool value) { m_handlesAuthenticationChallenges = value; }
137     void setAuthenticationUsername(String username) { m_authenticationUsername = username; }
138     void setAuthenticationPassword(String password) { m_authenticationPassword = password; }
139     void setAllowsAnySSLCertificate(bool);
140
141     void setBlockAllPlugins(bool shouldBlock) { m_shouldBlockAllPlugins = shouldBlock; }
142
143     void setShouldLogHistoryClientCallbacks(bool shouldLog) { m_shouldLogHistoryClientCallbacks = shouldLog; }
144     void setShouldLogCanAuthenticateAgainstProtectionSpace(bool shouldLog) { m_shouldLogCanAuthenticateAgainstProtectionSpace = shouldLog; }
145     void setShouldLogDownloadCallbacks(bool shouldLog) { m_shouldLogDownloadCallbacks = shouldLog; }
146
147     bool isCurrentInvocation(TestInvocation* invocation) const { return invocation == m_currentInvocation.get(); }
148
149     void setShouldDecideNavigationPolicyAfterDelay(bool value) { m_shouldDecideNavigationPolicyAfterDelay = value; }
150
151     void setNavigationGesturesEnabled(bool value);
152     void setIgnoresViewportScaleLimits(bool);
153
154     void setShouldDownloadUndisplayableMIMETypes(bool value) { m_shouldDownloadUndisplayableMIMETypes = value; }
155
156     void setStatisticsLastSeen(WKStringRef hostName, double seconds);
157     void setStatisticsPrevalentResource(WKStringRef hostName, bool value);
158     bool isStatisticsPrevalentResource(WKStringRef hostName);
159     bool isStatisticsRegisteredAsSubFrameUnder(WKStringRef subFrameHost, WKStringRef topFrameHost);
160     bool isStatisticsRegisteredAsRedirectingTo(WKStringRef hostRedirectedFrom, WKStringRef hostRedirectedTo);
161     void setStatisticsHasHadUserInteraction(WKStringRef hostName, bool value);
162     bool isStatisticsHasHadUserInteraction(WKStringRef hostName);
163     void setStatisticsGrandfathered(WKStringRef hostName, bool value);
164     bool isStatisticsGrandfathered(WKStringRef hostName);
165     void setStatisticsSubframeUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
166     void setStatisticsSubresourceUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
167     void setStatisticsSubresourceUniqueRedirectTo(WKStringRef hostName, WKStringRef hostNameRedirectedTo);
168     void setStatisticsTimeToLiveUserInteraction(double seconds);
169     void setStatisticsTimeToLiveCookiePartitionFree(double seconds);
170     void statisticsProcessStatisticsAndDataRecords();
171     void statisticsUpdateCookiePartitioning();
172     void statisticsSetShouldPartitionCookiesForHost(WKStringRef hostName, bool value);
173     void statisticsSubmitTelemetry();
174     void setStatisticsNotifyPagesWhenDataRecordsWereScanned(bool);
175     void setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(bool);
176     void setStatisticsNotifyPagesWhenTelemetryWasCaptured(bool value);
177     void setStatisticsMinimumTimeBetweenDataRecordsRemoval(double);
178     void setStatisticsGrandfatheringTime(double seconds);
179     void setStatisticsMaxStatisticsEntries(unsigned);
180     void setStatisticsPruneEntriesDownTo(unsigned);
181     void statisticsClearInMemoryAndPersistentStore();
182     void statisticsClearInMemoryAndPersistentStoreModifiedSinceHours(unsigned);
183     void statisticsClearThroughWebsiteDataRemoval();
184     void statisticsResetToConsistentState();
185
186     WKArrayRef openPanelFileURLs() const { return m_openPanelFileURLs.get(); }
187     void setOpenPanelFileURLs(WKArrayRef fileURLs) { m_openPanelFileURLs = fileURLs; }
188
189     void terminateNetworkProcess();
190
191     void removeAllSessionCredentials();
192
193     void clearDOMCache(WKStringRef origin);
194     bool hasDOMCache(WKStringRef origin);
195     uint64_t domCacheSize(WKStringRef origin);
196
197 private:
198     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
199     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;
200     void initialize(int argc, const char* argv[]);
201     void createWebViewWithOptions(const TestOptions&);
202     void run();
203
204     void runTestingServerLoop();
205     bool runTest(const char* pathOrURL);
206
207     void platformInitialize();
208     void platformDestroy();
209     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
210     void platformInitializeContext();
211     void platformCreateWebView(WKPageConfigurationRef, const TestOptions&);
212     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions&);
213     void platformResetPreferencesToConsistentValues();
214     void platformResetStateToConsistentValues();
215 #if PLATFORM(COCOA)
216     void cocoaPlatformInitialize();
217     void cocoaResetStateToConsistentValues();
218 #endif
219     void platformConfigureViewForTest(const TestInvocation&);
220     void platformWillRunTest(const TestInvocation&);
221     void platformRunUntil(bool& done, double timeout);
222     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
223     WKContextRef platformContext();
224     WKPreferencesRef platformPreferences();
225     void initializeInjectedBundlePath();
226     void initializeTestPluginDirectory();
227
228     void ensureViewSupportsOptionsForTest(const TestInvocation&);
229     TestOptions testOptionsForTest(const TestCommand&) const;
230     void updatePlatformSpecificTestOptionsForTest(TestOptions&, const std::string& pathOrURL) const;
231
232     void updateWebViewSizeForTest(const TestInvocation&);
233     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
234
235     void decidePolicyForGeolocationPermissionRequestIfPossible();
236     void decidePolicyForUserMediaPermissionRequestIfPossible();
237
238     // WKContextInjectedBundleClient
239     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
240     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
241     static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void *clientInfo);
242
243     // WKPageInjectedBundleClient
244     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
245     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
246     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
247     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
248     WKRetainPtr<WKTypeRef> getInjectedBundleInitializationUserData();
249
250     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
251
252     // WKContextClient
253     static void networkProcessDidCrash(WKContextRef, const void*);
254     void networkProcessDidCrash();
255     static void databaseProcessDidCrash(WKContextRef, const void*);
256     void databaseProcessDidCrash();
257
258     // WKPageNavigationClient
259     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
260     void didCommitNavigation(WKPageRef, WKNavigationRef);
261
262     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
263     void didFinishNavigation(WKPageRef, WKNavigationRef);
264
265     
266     // WKContextDownloadClient
267     static void downloadDidStart(WKContextRef, WKDownloadRef, const void*);
268     void downloadDidStart(WKContextRef, WKDownloadRef);
269     static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool* allowOverwrite, const void *clientInfo);
270     WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool*& allowOverwrite);
271     static void downloadDidFinish(WKContextRef, WKDownloadRef, const void*);
272     void downloadDidFinish(WKContextRef, WKDownloadRef);
273     static void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef, const void*);
274     void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef);
275     static void downloadDidCancel(WKContextRef, WKDownloadRef, const void*);
276     void downloadDidCancel(WKContextRef, WKDownloadRef);
277     static void downloadDidReceiveServerRedirectToURL(WKContextRef, WKDownloadRef, WKURLRef, const void*);
278     void downloadDidReceiveServerRedirectToURL(WKContextRef, WKDownloadRef, WKURLRef);
279     
280     static void processDidCrash(WKPageRef, const void* clientInfo);
281     void processDidCrash();
282
283     static void didBeginNavigationGesture(WKPageRef, const void*);
284     static void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
285     static void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
286     static void didRemoveNavigationGestureSnapshot(WKPageRef, const void*);
287     void didBeginNavigationGesture(WKPageRef);
288     void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
289     void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
290     void didRemoveNavigationGestureSnapshot(WKPageRef);
291
292     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
293     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
294     
295
296     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
297     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
298
299     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
300
301     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void*);
302     bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef);
303
304     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void*);
305     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
306
307     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
308     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
309
310     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
311     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
312
313     // WKContextHistoryClient
314     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
315     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
316
317     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
318     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
319
320     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
321     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
322
323     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
324     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
325
326     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
327
328     static void runModal(WKPageRef, const void* clientInfo);
329     static void runModal(PlatformWebView*);
330
331     static const char* libraryPathForTesting();
332     static const char* platformLibraryPathForTesting();
333
334     static void statisticsClearThroughWebsiteDataRemovalCallback(void*);
335
336     std::unique_ptr<TestInvocation> m_currentInvocation;
337
338     bool m_verbose { false };
339     bool m_printSeparators { false };
340     bool m_usingServerMode { false };
341     bool m_gcBetweenTests { false };
342     bool m_shouldDumpPixelsForAllTests { false };
343     std::vector<std::string> m_paths;
344     std::vector<std::string> m_allowedHosts;
345     WKRetainPtr<WKStringRef> m_injectedBundlePath;
346     WKRetainPtr<WKStringRef> m_testPluginDirectory;
347
348     WebNotificationProvider m_webNotificationProvider;
349
350     std::unique_ptr<PlatformWebView> m_mainWebView;
351     WKRetainPtr<WKContextRef> m_context;
352     WKRetainPtr<WKPageGroupRef> m_pageGroup;
353
354     enum State {
355         Initial,
356         Resetting,
357         RunningTest
358     };
359     State m_state { Initial };
360     bool m_doneResetting { false };
361
362     bool m_useWaitToDumpWatchdogTimer { true };
363     bool m_forceNoTimeout { false };
364
365     bool m_didPrintWebProcessCrashedMessage { false };
366     bool m_shouldExitWhenWebProcessCrashes { true };
367     
368     bool m_beforeUnloadReturnValue { true };
369
370     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
371     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
372     bool m_isGeolocationPermissionSet { false };
373     bool m_isGeolocationPermissionAllowed { false };
374
375     HashMap<String, RefPtr<OriginSettings>> m_cachedUserMediaPermissions;
376
377     typedef Vector<std::pair<String, WKRetainPtr<WKUserMediaPermissionRequestRef>>> PermissionRequestList;
378     PermissionRequestList m_userMediaPermissionRequests;
379
380     bool m_isUserMediaPermissionSet { false };
381     bool m_isUserMediaPermissionAllowed { false };
382
383     bool m_policyDelegateEnabled { false };
384     bool m_policyDelegatePermissive { false };
385     bool m_shouldDownloadUndisplayableMIMETypes { false };
386
387     bool m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges { false };
388     bool m_handlesAuthenticationChallenges { false };
389     String m_authenticationUsername;
390     String m_authenticationPassword;
391
392     bool m_shouldBlockAllPlugins { false };
393
394     bool m_forceComplexText { false };
395     bool m_shouldUseAcceleratedDrawing { false };
396     bool m_shouldUseRemoteLayerTree { false };
397
398     bool m_shouldLogCanAuthenticateAgainstProtectionSpace { false };
399     bool m_shouldLogDownloadCallbacks { false };
400     bool m_shouldLogHistoryClientCallbacks { false };
401     bool m_shouldShowWebView { false };
402     
403     bool m_shouldShowTouches { false };
404     
405     bool m_shouldDecideNavigationPolicyAfterDelay { false };
406
407     WKRetainPtr<WKArrayRef> m_openPanelFileURLs;
408
409     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
410
411     WorkQueueManager m_workQueueManager;
412 };
413
414 struct TestCommand {
415     std::string pathOrURL;
416     std::string absolutePath;
417     bool shouldDumpPixels { false };
418     std::string expectedPixelHash;
419     int timeout { 0 };
420     bool dumpJSConsoleLogInStdErr { false };
421 };
422
423 } // namespace WTR
424
425 #endif // TestController_h