TestController should clear all fetch caches when resetting its state
[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     void clearDOMCaches();
195     bool hasDOMCache(WKStringRef origin);
196     uint64_t domCacheSize(WKStringRef origin);
197
198 private:
199     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
200     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;
201     void initialize(int argc, const char* argv[]);
202     void createWebViewWithOptions(const TestOptions&);
203     void run();
204
205     void runTestingServerLoop();
206     bool runTest(const char* pathOrURL);
207
208     void platformInitialize();
209     void platformDestroy();
210     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
211     void platformInitializeContext();
212     void platformCreateWebView(WKPageConfigurationRef, const TestOptions&);
213     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions&);
214     void platformResetPreferencesToConsistentValues();
215     void platformResetStateToConsistentValues();
216 #if PLATFORM(COCOA)
217     void cocoaPlatformInitialize();
218     void cocoaResetStateToConsistentValues();
219 #endif
220     void platformConfigureViewForTest(const TestInvocation&);
221     void platformWillRunTest(const TestInvocation&);
222     void platformRunUntil(bool& done, double timeout);
223     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
224     WKContextRef platformContext();
225     WKPreferencesRef platformPreferences();
226     void initializeInjectedBundlePath();
227     void initializeTestPluginDirectory();
228
229     void ensureViewSupportsOptionsForTest(const TestInvocation&);
230     TestOptions testOptionsForTest(const TestCommand&) const;
231     void updatePlatformSpecificTestOptionsForTest(TestOptions&, const std::string& pathOrURL) const;
232
233     void updateWebViewSizeForTest(const TestInvocation&);
234     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
235
236     void decidePolicyForGeolocationPermissionRequestIfPossible();
237     void decidePolicyForUserMediaPermissionRequestIfPossible();
238
239     // WKContextInjectedBundleClient
240     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
241     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
242     static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void *clientInfo);
243
244     // WKPageInjectedBundleClient
245     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
246     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
247     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
248     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
249     WKRetainPtr<WKTypeRef> getInjectedBundleInitializationUserData();
250
251     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
252
253     // WKContextClient
254     static void networkProcessDidCrash(WKContextRef, const void*);
255     void networkProcessDidCrash();
256     static void databaseProcessDidCrash(WKContextRef, const void*);
257     void databaseProcessDidCrash();
258
259     // WKPageNavigationClient
260     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
261     void didCommitNavigation(WKPageRef, WKNavigationRef);
262
263     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
264     void didFinishNavigation(WKPageRef, WKNavigationRef);
265
266     
267     // WKContextDownloadClient
268     static void downloadDidStart(WKContextRef, WKDownloadRef, const void*);
269     void downloadDidStart(WKContextRef, WKDownloadRef);
270     static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool* allowOverwrite, const void *clientInfo);
271     WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool*& allowOverwrite);
272     static void downloadDidFinish(WKContextRef, WKDownloadRef, const void*);
273     void downloadDidFinish(WKContextRef, WKDownloadRef);
274     static void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef, const void*);
275     void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef);
276     static void downloadDidCancel(WKContextRef, WKDownloadRef, const void*);
277     void downloadDidCancel(WKContextRef, WKDownloadRef);
278     static void downloadDidReceiveServerRedirectToURL(WKContextRef, WKDownloadRef, WKURLRef, const void*);
279     void downloadDidReceiveServerRedirectToURL(WKContextRef, WKDownloadRef, WKURLRef);
280     
281     static void processDidCrash(WKPageRef, const void* clientInfo);
282     void processDidCrash();
283
284     static void didBeginNavigationGesture(WKPageRef, const void*);
285     static void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
286     static void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
287     static void didRemoveNavigationGestureSnapshot(WKPageRef, const void*);
288     void didBeginNavigationGesture(WKPageRef);
289     void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
290     void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
291     void didRemoveNavigationGestureSnapshot(WKPageRef);
292
293     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
294     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
295     
296
297     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
298     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
299
300     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
301
302     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void*);
303     bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef);
304
305     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void*);
306     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
307
308     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
309     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
310
311     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
312     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
313
314     // WKContextHistoryClient
315     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
316     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
317
318     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
319     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
320
321     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
322     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
323
324     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
325     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
326
327     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
328
329     static void runModal(WKPageRef, const void* clientInfo);
330     static void runModal(PlatformWebView*);
331
332     static const char* libraryPathForTesting();
333     static const char* platformLibraryPathForTesting();
334
335     static void statisticsClearThroughWebsiteDataRemovalCallback(void*);
336
337     std::unique_ptr<TestInvocation> m_currentInvocation;
338
339     bool m_verbose { false };
340     bool m_printSeparators { false };
341     bool m_usingServerMode { false };
342     bool m_gcBetweenTests { false };
343     bool m_shouldDumpPixelsForAllTests { false };
344     std::vector<std::string> m_paths;
345     std::vector<std::string> m_allowedHosts;
346     WKRetainPtr<WKStringRef> m_injectedBundlePath;
347     WKRetainPtr<WKStringRef> m_testPluginDirectory;
348
349     WebNotificationProvider m_webNotificationProvider;
350
351     std::unique_ptr<PlatformWebView> m_mainWebView;
352     WKRetainPtr<WKContextRef> m_context;
353     WKRetainPtr<WKPageGroupRef> m_pageGroup;
354
355     enum State {
356         Initial,
357         Resetting,
358         RunningTest
359     };
360     State m_state { Initial };
361     bool m_doneResetting { false };
362
363     bool m_useWaitToDumpWatchdogTimer { true };
364     bool m_forceNoTimeout { false };
365
366     bool m_didPrintWebProcessCrashedMessage { false };
367     bool m_shouldExitWhenWebProcessCrashes { true };
368     
369     bool m_beforeUnloadReturnValue { true };
370
371     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
372     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
373     bool m_isGeolocationPermissionSet { false };
374     bool m_isGeolocationPermissionAllowed { false };
375
376     HashMap<String, RefPtr<OriginSettings>> m_cachedUserMediaPermissions;
377
378     typedef Vector<std::pair<String, WKRetainPtr<WKUserMediaPermissionRequestRef>>> PermissionRequestList;
379     PermissionRequestList m_userMediaPermissionRequests;
380
381     bool m_isUserMediaPermissionSet { false };
382     bool m_isUserMediaPermissionAllowed { false };
383
384     bool m_policyDelegateEnabled { false };
385     bool m_policyDelegatePermissive { false };
386     bool m_shouldDownloadUndisplayableMIMETypes { false };
387
388     bool m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges { false };
389     bool m_handlesAuthenticationChallenges { false };
390     String m_authenticationUsername;
391     String m_authenticationPassword;
392
393     bool m_shouldBlockAllPlugins { false };
394
395     bool m_forceComplexText { false };
396     bool m_shouldUseAcceleratedDrawing { false };
397     bool m_shouldUseRemoteLayerTree { false };
398
399     bool m_shouldLogCanAuthenticateAgainstProtectionSpace { false };
400     bool m_shouldLogDownloadCallbacks { false };
401     bool m_shouldLogHistoryClientCallbacks { false };
402     bool m_shouldShowWebView { false };
403     
404     bool m_shouldShowTouches { false };
405     
406     bool m_shouldDecideNavigationPolicyAfterDelay { false };
407
408     WKRetainPtr<WKArrayRef> m_openPanelFileURLs;
409
410     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
411
412     WorkQueueManager m_workQueueManager;
413 };
414
415 struct TestCommand {
416     std::string pathOrURL;
417     std::string absolutePath;
418     bool shouldDumpPixels { false };
419     std::string expectedPixelHash;
420     int timeout { 0 };
421     bool dumpJSConsoleLogInStdErr { false };
422 };
423
424 } // namespace WTR
425
426 #endif // TestController_h