fa447744a8f08241c4f7f2fc5f7f268c1e82da8d
[WebKit.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
137     void setBlockAllPlugins(bool shouldBlock) { m_shouldBlockAllPlugins = shouldBlock; }
138
139     void setShouldLogHistoryClientCallbacks(bool shouldLog) { m_shouldLogHistoryClientCallbacks = shouldLog; }
140     void setShouldLogCanAuthenticateAgainstProtectionSpace(bool shouldLog) { m_shouldLogCanAuthenticateAgainstProtectionSpace = shouldLog; }
141
142     bool isCurrentInvocation(TestInvocation* invocation) const { return invocation == m_currentInvocation.get(); }
143
144     void setShouldDecideNavigationPolicyAfterDelay(bool value) { m_shouldDecideNavigationPolicyAfterDelay = value; }
145
146     void setNavigationGesturesEnabled(bool value);
147     void setIgnoresViewportScaleLimits(bool);
148
149     void setShouldDownloadUndisplayableMIMETypes(bool value) { m_shouldDownloadUndisplayableMIMETypes = value; }
150
151     void setStatisticsPrevalentResource(WKStringRef hostName, bool value);
152     bool isStatisticsPrevalentResource(WKStringRef hostName);
153     void setStatisticsHasHadUserInteraction(WKStringRef hostName, bool value);
154     bool isStatisticsHasHadUserInteraction(WKStringRef hostName);
155     void setStatisticsSubframeUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
156     void setStatisticsSubresourceUnderTopFrameOrigin(WKStringRef hostName, WKStringRef topFrameHostName);
157     void setStatisticsSubresourceUniqueRedirectTo(WKStringRef hostName, WKStringRef hostNameRedirectedTo);
158     void setStatisticsTimeToLiveUserInteraction(double seconds);
159     void statisticsFireDataModificationHandler();
160     void setStatisticsNotifyPagesWhenDataRecordsWereScanned(bool);
161     void setStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(bool);
162     void setStatisticsMinimumTimeBetweeenDataRecordsRemoval(double);
163     void statisticsResetToConsistentState();
164
165     WKArrayRef openPanelFileURLs() const { return m_openPanelFileURLs.get(); }
166     void setOpenPanelFileURLs(WKArrayRef fileURLs) { m_openPanelFileURLs = fileURLs; }
167
168 private:
169     WKRetainPtr<WKPageConfigurationRef> generatePageConfiguration(WKContextConfigurationRef);
170     WKRetainPtr<WKContextConfigurationRef> generateContextConfiguration() const;
171     void initialize(int argc, const char* argv[]);
172     void createWebViewWithOptions(const TestOptions&);
173     void run();
174
175     void runTestingServerLoop();
176     bool runTest(const char* pathOrURL);
177
178     void platformInitialize();
179     void platformDestroy();
180     WKContextRef platformAdjustContext(WKContextRef, WKContextConfigurationRef);
181     void platformInitializeContext();
182     void platformCreateWebView(WKPageConfigurationRef, const TestOptions&);
183     static PlatformWebView* platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef, const TestOptions&);
184     void platformResetPreferencesToConsistentValues();
185     void platformResetStateToConsistentValues();
186 #if PLATFORM(COCOA)
187     void cocoaResetStateToConsistentValues();
188 #endif
189     void platformConfigureViewForTest(const TestInvocation&);
190     void platformWillRunTest(const TestInvocation&);
191     void platformRunUntil(bool& done, double timeout);
192     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
193     WKPreferencesRef platformPreferences();
194     void initializeInjectedBundlePath();
195     void initializeTestPluginDirectory();
196
197     void ensureViewSupportsOptionsForTest(const TestInvocation&);
198     TestOptions testOptionsForTest(const TestCommand&) const;
199     void updatePlatformSpecificTestOptionsForTest(TestOptions&, const std::string& pathOrURL) const;
200
201     void updateWebViewSizeForTest(const TestInvocation&);
202     void updateWindowScaleForTest(PlatformWebView*, const TestInvocation&);
203
204     void decidePolicyForGeolocationPermissionRequestIfPossible();
205     void decidePolicyForUserMediaPermissionRequestIfPossible();
206
207     // WKContextInjectedBundleClient
208     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
209     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
210     static WKTypeRef getInjectedBundleInitializationUserData(WKContextRef, const void *clientInfo);
211
212     // WKPageInjectedBundleClient
213     static void didReceivePageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
214     static void didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
215     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
216     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
217     WKRetainPtr<WKTypeRef> getInjectedBundleInitializationUserData();
218
219     void didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous);
220
221     // WKContextClient
222     static void networkProcessDidCrash(WKContextRef, const void*);
223     void networkProcessDidCrash();
224     static void databaseProcessDidCrash(WKContextRef, const void*);
225     void databaseProcessDidCrash();
226
227     // WKPageNavigationClient
228     static void didCommitNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
229     void didCommitNavigation(WKPageRef, WKNavigationRef);
230
231     static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef userData, const void*);
232     void didFinishNavigation(WKPageRef, WKNavigationRef);
233
234     
235     // WKContextDownloadClient
236     static void downloadDidStart(WKContextRef, WKDownloadRef, const void*);
237     void downloadDidStart(WKContextRef, WKDownloadRef);
238     static WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool* allowOverwrite, const void *clientInfo);
239     WKStringRef decideDestinationWithSuggestedFilename(WKContextRef, WKDownloadRef, WKStringRef filename, bool*& allowOverwrite);
240     static void downloadDidFinish(WKContextRef, WKDownloadRef, const void*);
241     void downloadDidFinish(WKContextRef, WKDownloadRef);
242     static void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef, const void*);
243     void downloadDidFail(WKContextRef, WKDownloadRef, WKErrorRef);
244     static void downloadDidCancel(WKContextRef, WKDownloadRef, const void*);
245     void downloadDidCancel(WKContextRef, WKDownloadRef);
246     
247     static void processDidCrash(WKPageRef, const void* clientInfo);
248     void processDidCrash();
249
250     static void didBeginNavigationGesture(WKPageRef, const void*);
251     static void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
252     static void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef, const void*);
253     static void didRemoveNavigationGestureSnapshot(WKPageRef, const void*);
254     void didBeginNavigationGesture(WKPageRef);
255     void willEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
256     void didEndNavigationGesture(WKPageRef, WKBackForwardListItemRef);
257     void didRemoveNavigationGestureSnapshot(WKPageRef);
258
259     static WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo);
260     WKPluginLoadPolicy decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription);
261     
262
263     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
264     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
265
266     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*);
267
268     static bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef, const void*);
269     bool canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef);
270
271     static void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef, const void*);
272     void didReceiveAuthenticationChallenge(WKPageRef, WKAuthenticationChallengeRef);
273
274     static void decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
275     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
276
277     static void decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
278     void decidePolicyForNavigationResponse(WKNavigationResponseRef, WKFramePolicyListenerRef);
279
280     // WKContextHistoryClient
281     static void didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef, WKFrameRef, const void*);
282     void didNavigateWithNavigationData(WKNavigationDataRef, WKFrameRef);
283
284     static void didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
285     void didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
286
287     static void didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef, const void*);
288     void didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef);
289
290     static void didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef, WKFrameRef, const void*);
291     void didUpdateHistoryTitle(WKStringRef title, WKURLRef, WKFrameRef);
292
293     static WKPageRef createOtherPage(WKPageRef, WKPageConfigurationRef, WKNavigationActionRef, WKWindowFeaturesRef, const void*);
294
295     static void runModal(WKPageRef, const void* clientInfo);
296     static void runModal(PlatformWebView*);
297
298     static const char* libraryPathForTesting();
299     static const char* platformLibraryPathForTesting();
300
301     std::unique_ptr<TestInvocation> m_currentInvocation;
302
303     bool m_verbose { false };
304     bool m_printSeparators { false };
305     bool m_usingServerMode { false };
306     bool m_gcBetweenTests { false };
307     bool m_shouldDumpPixelsForAllTests { false };
308     std::vector<std::string> m_paths;
309     std::vector<std::string> m_allowedHosts;
310     WKRetainPtr<WKStringRef> m_injectedBundlePath;
311     WKRetainPtr<WKStringRef> m_testPluginDirectory;
312
313     WebNotificationProvider m_webNotificationProvider;
314
315     std::unique_ptr<PlatformWebView> m_mainWebView;
316     WKRetainPtr<WKContextRef> m_context;
317     WKRetainPtr<WKPageGroupRef> m_pageGroup;
318
319     enum State {
320         Initial,
321         Resetting,
322         RunningTest
323     };
324     State m_state { Initial };
325     bool m_doneResetting { false };
326
327     bool m_useWaitToDumpWatchdogTimer { true };
328     bool m_forceNoTimeout { false };
329
330     bool m_didPrintWebProcessCrashedMessage { false };
331     bool m_shouldExitWhenWebProcessCrashes { true };
332     
333     bool m_beforeUnloadReturnValue { true };
334
335     std::unique_ptr<GeolocationProviderMock> m_geolocationProvider;
336     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
337     bool m_isGeolocationPermissionSet { false };
338     bool m_isGeolocationPermissionAllowed { false };
339
340     HashMap<String, RefPtr<OriginSettings>> m_cachedUserMediaPermissions;
341
342     typedef Vector<std::pair<String, WKRetainPtr<WKUserMediaPermissionRequestRef>>> PermissionRequestList;
343     PermissionRequestList m_userMediaPermissionRequests;
344
345     bool m_isUserMediaPermissionSet { false };
346     bool m_isUserMediaPermissionAllowed { false };
347
348     bool m_policyDelegateEnabled { false };
349     bool m_policyDelegatePermissive { false };
350     bool m_shouldDownloadUndisplayableMIMETypes { false };
351
352     bool m_rejectsProtectionSpaceAndContinueForAuthenticationChallenges { false };
353     bool m_handlesAuthenticationChallenges { false };
354     String m_authenticationUsername;
355     String m_authenticationPassword;
356
357     bool m_shouldBlockAllPlugins { false };
358
359     bool m_forceComplexText { false };
360     bool m_shouldUseAcceleratedDrawing { false };
361     bool m_shouldUseRemoteLayerTree { false };
362
363     bool m_shouldLogCanAuthenticateAgainstProtectionSpace { false };
364     bool m_shouldLogHistoryClientCallbacks { false };
365     bool m_shouldShowWebView { false };
366     
367     bool m_shouldDecideNavigationPolicyAfterDelay { false };
368
369     WKRetainPtr<WKArrayRef> m_openPanelFileURLs;
370
371     std::unique_ptr<EventSenderProxy> m_eventSenderProxy;
372
373     WorkQueueManager m_workQueueManager;
374 };
375
376 struct TestCommand {
377     std::string pathOrURL;
378     std::string absolutePath;
379     bool shouldDumpPixels { false };
380     std::string expectedPixelHash;
381     int timeout { 0 };
382     bool dumpJSConsoleLogInStdErr { false };
383 };
384
385 } // namespace WTR
386
387 #endif // TestController_h