092c9cb3308188f4c271fbc9b6800750d0831c18
[WebKit-https.git] / Tools / WebKitTestRunner / TestController.h
1 /*
2  * Copyright (C) 2010 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 "WebNotificationProvider.h"
30 #include "WorkQueueManager.h"
31 #include <GeolocationProviderMock.h>
32 #include <WebKit2/WKRetainPtr.h>
33 #include <string>
34 #include <vector>
35 #include <wtf/OwnPtr.h>
36 #include <wtf/Vector.h>
37
38 namespace WTR {
39
40 class TestInvocation;
41 class PlatformWebView;
42 class EventSenderProxy;
43
44 // FIXME: Rename this TestRunner?
45 class TestController {
46 public:
47     static TestController& shared();
48
49     TestController(int argc, const char* argv[]);
50     ~TestController();
51
52     bool verbose() const { return m_verbose; }
53
54     WKStringRef injectedBundlePath() { return m_injectedBundlePath.get(); }
55     WKStringRef testPluginDirectory() { return m_testPluginDirectory.get(); }
56
57     PlatformWebView* mainWebView() { return m_mainWebView.get(); }
58     WKContextRef context() { return m_context.get(); }
59
60     void ensureViewSupportsOptions(WKDictionaryRef options);
61     
62     // Runs the run loop until `done` is true or the timeout elapses.
63     enum TimeoutDuration { ShortTimeout, LongTimeout, NoTimeout };
64     bool useWaitToDumpWatchdogTimer() { return m_useWaitToDumpWatchdogTimer; }
65     void runUntil(bool& done, TimeoutDuration);
66     void notifyDone();
67     
68     bool beforeUnloadReturnValue() const { return m_beforeUnloadReturnValue; }
69     void setBeforeUnloadReturnValue(bool value) { m_beforeUnloadReturnValue = value; }
70
71     void simulateWebNotificationClick(uint64_t notificationID);
72
73     // Geolocation.
74     void setGeolocationPermission(bool);
75     void setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed);
76     void setMockGeolocationPositionUnavailableError(WKStringRef errorMessage);
77     void handleGeolocationPermissionRequest(WKGeolocationPermissionRequestRef);
78
79     // Policy delegate.
80     void setCustomPolicyDelegate(bool enabled, bool permissive);
81
82     bool resetStateToConsistentValues();
83
84     WorkQueueManager& workQueueManager() { return m_workQueueManager; }
85
86 private:
87     void initialize(int argc, const char* argv[]);
88     void createWebViewWithOptions(WKDictionaryRef);
89     void run();
90
91     void runTestingServerLoop();
92     bool runTest(const char* pathOrURL);
93
94     void platformInitialize();
95     void platformDestroy();
96     void platformInitializeContext();
97     void platformRunUntil(bool& done, double timeout);
98     void platformDidCommitLoadForFrame(WKPageRef, WKFrameRef);
99     void initializeInjectedBundlePath();
100     void initializeTestPluginDirectory();
101
102     void decidePolicyForGeolocationPermissionRequestIfPossible();
103
104     // WKContextInjectedBundleClient
105     static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*);
106     static void didReceiveSynchronousMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void*);
107     void didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
108     WKRetainPtr<WKTypeRef> didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody);
109
110     // WKPageLoaderClient
111     static void didCommitLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef userData, const void*);
112     void didCommitLoadForFrame(WKPageRef, WKFrameRef);
113
114     static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef userData, const void*);
115     void didFinishLoadForFrame(WKPageRef, WKFrameRef);
116
117     static void processDidCrash(WKPageRef, const void* clientInfo);
118     void processDidCrash();
119
120     static void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef, const void*);
121     void decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef);
122
123     static void unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKStringRef, WKStringRef, WKStringRef, const void*);
124
125     // WKPagePolicyClient
126     static void decidePolicyForNavigationAction(WKPageRef, WKFrameRef, WKFrameNavigationType, WKEventModifiers, WKEventMouseButton, WKURLRequestRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
127     void decidePolicyForNavigationAction(WKFramePolicyListenerRef);
128
129     static void decidePolicyForResponse(WKPageRef, WKFrameRef, WKURLResponseRef, WKURLRequestRef, WKFramePolicyListenerRef, WKTypeRef, const void*);
130     void decidePolicyForResponse(WKFrameRef, WKURLResponseRef, WKFramePolicyListenerRef);
131
132     static WKPageRef createOtherPage(WKPageRef oldPage, WKURLRequestRef, WKDictionaryRef, WKEventModifiers, WKEventMouseButton, const void*);
133
134     static void runModal(WKPageRef, const void* clientInfo);
135     static void runModal(PlatformWebView*);
136
137     static const char* libraryPathForTesting();
138     static const char* platformLibraryPathForTesting();
139
140     OwnPtr<TestInvocation> m_currentInvocation;
141
142     bool m_verbose;
143     bool m_printSeparators;
144     bool m_usingServerMode;
145     bool m_gcBetweenTests;
146     bool m_shouldDumpPixelsForAllTests;
147     std::vector<std::string> m_paths;
148     WKRetainPtr<WKStringRef> m_injectedBundlePath;
149     WKRetainPtr<WKStringRef> m_testPluginDirectory;
150
151     WebNotificationProvider m_webNotificationProvider;
152
153     OwnPtr<PlatformWebView> m_mainWebView;
154     WKRetainPtr<WKContextRef> m_context;
155     WKRetainPtr<WKPageGroupRef> m_pageGroup;
156
157     enum State {
158         Initial,
159         Resetting,
160         RunningTest
161     };
162     State m_state;
163     bool m_doneResetting;
164
165     double m_longTimeout;
166     double m_shortTimeout;
167     double m_noTimeout;
168     bool m_useWaitToDumpWatchdogTimer;
169     bool m_forceNoTimeout;
170
171     bool m_didPrintWebProcessCrashedMessage;
172     bool m_shouldExitWhenWebProcessCrashes;
173     
174     bool m_beforeUnloadReturnValue;
175
176     OwnPtr<GeolocationProviderMock> m_geolocationProvider;
177     Vector<WKRetainPtr<WKGeolocationPermissionRequestRef> > m_geolocationPermissionRequests;
178     bool m_isGeolocationPermissionSet;
179     bool m_isGeolocationPermissionAllowed;
180
181     bool m_policyDelegateEnabled;
182     bool m_policyDelegatePermissive;
183
184 #if PLATFORM(MAC) || PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
185     OwnPtr<EventSenderProxy> m_eventSenderProxy;
186 #endif
187
188 #if PLATFORM(QT)
189     class RunLoop;
190     RunLoop* m_runLoop;
191 #endif
192
193     WorkQueueManager m_workQueueManager;
194 };
195
196 } // namespace WTR
197
198 #endif // TestController_h