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