79e37279c73391ca6089bf470339c2941e3454c8
[WebKit-https.git] / WebKitTools / DumpRenderTree / win / LayoutTestControllerWin.cpp
1 /*
2  * Copyright (C) 2006, 2007 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "DumpRenderTree.h"
30 #include "LayoutTestController.h"
31
32 #include "EditingDelegate.h"
33 #include "PolicyDelegate.h"
34 #include "WorkQueue.h"
35 #include "WorkQueueItem.h"
36 #include <WebCore/COMPtr.h>
37 #include <wtf/Platform.h>
38 #include <wtf/RetainPtr.h>
39 #include <wtf/Vector.h>
40 #include <JavaScriptCore/Assertions.h>
41 #include <JavaScriptCore/JavaScriptCore.h>
42 #include <JavaScriptCore/JSRetainPtr.h>
43 #include <WebKit/IWebViewPrivate.h>
44 #include <string>
45 #include <CoreFoundation/CoreFoundation.h>
46
47 using std::string;
48 using std::wstring;
49
50 LayoutTestController::~LayoutTestController()
51 {
52     COMPtr<IWebView> webView;
53     if (FAILED(frame->webView(&webView)))
54         return;
55
56     // reset webview-related states back to default values in preparation for next test
57
58     COMPtr<IWebViewPrivate> viewPrivate;
59     if (SUCCEEDED(webView->QueryInterface(&viewPrivate)))
60         viewPrivate->setTabKeyCyclesThroughElements(TRUE);
61
62     COMPtr<IWebViewEditing> viewEditing;
63     if (FAILED(webView->QueryInterface(&viewEditing)))
64         return;
65     COMPtr<IWebEditingDelegate> delegate;
66     if (FAILED(viewEditing->editingDelegate(&delegate)))
67         return;
68     COMPtr<EditingDelegate> editingDelegate(Query, viewEditing.get());
69     if (editingDelegate)
70         editingDelegate->setAcceptsEditing(TRUE);
71 }
72
73 void LayoutTestController::addDisallowedURL(JSStringRef url)
74 {
75     // FIXME: Implement!
76 }
77
78 void LayoutTestController::clearBackForwardList()
79 {
80     COMPtr<IWebView> webView;
81     if (FAILED(frame->webView(&webView)))
82         return;
83
84     COMPtr<IWebBackForwardList> backForwardList;
85     if (FAILED(webView->backForwardList(&backForwardList)))
86         return;
87
88     COMPtr<IWebHistoryItem> item;
89     if (FAILED(backForwardList->currentItem(&item)))
90         return;
91
92     // We clear the history by setting the back/forward list's capacity to 0
93     // then restoring it back and adding back the current item.
94     int capacity;
95     if (FAILED(backForwardList->capacity(&capacity)))
96         return;
97
98     backForwardList->setCapacity(0);
99     backForwardList->setCapacity(capacity);
100     backForwardList->addItem(item.get());
101     backForwardList->goToItem(item.get());
102 }
103
104 JSStringRef LayoutTestController::copyDecodedHostName(JSStringRef name)
105 {
106     // FIXME: Implement!
107     return 0;
108 }
109
110 JSStringRef LayoutTestController::copyEncodedHostName(JSStringRef name)
111 {
112     // FIXME: Implement!
113     return 0;
114 }
115
116 void LayoutTestController::display()
117 {
118     displayWebView();
119 }
120
121 void LayoutTestController::keepWebHistory()
122 {
123     // FIXME: Implement!
124 }
125
126 void LayoutTestController::notifyDone()
127 {
128     // Same as on mac.  This can be shared.
129     if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
130         dump();
131     m_waitToDump = false;
132 }
133
134 void LayoutTestController::queueBackNavigation(int howFarBack)
135 {
136     // Same as on mac.  This can be shared.
137     WorkQueue::shared()->queue(new BackItem(howFarBack));
138 }
139
140 void LayoutTestController::queueForwardNavigation(int howFarForward)
141 {
142     // Same as on mac.  This can be shared.
143     WorkQueue::shared()->queue(new ForwardItem(howFarForward));
144 }
145
146 static wstring jsStringRefToWString(JSStringRef jsStr)
147 {
148     size_t length = JSStringGetLength(jsStr);
149     Vector<WCHAR> buffer(length + 1);
150     memcpy(buffer.data(), JSStringGetCharactersPtr(jsStr), length * sizeof(WCHAR));
151     buffer[length] = '\0';
152
153     return buffer.data();
154 }
155
156 void LayoutTestController::queueLoad(JSStringRef url, JSStringRef target)
157 {
158     COMPtr<IWebDataSource> dataSource;
159     if (FAILED(frame->dataSource(&dataSource)))
160         return;
161
162     COMPtr<IWebURLResponse> response;
163     if (FAILED(dataSource->response(&response)) || !response)
164         return;
165
166     BSTR responseURLBSTR;
167     if (FAILED(response->URL(&responseURLBSTR)))
168         return;
169     wstring responseURL(responseURLBSTR, SysStringLen(responseURLBSTR));
170     SysFreeString(responseURLBSTR);
171
172     // FIXME: We should do real relative URL resolution here.
173     int lastSlash = responseURL.rfind('/');
174     if (lastSlash != -1)
175         responseURL = responseURL.substr(0, lastSlash);
176
177     wstring wURL = jsStringRefToWString(url);
178     wstring wAbosuluteURL = responseURL + TEXT("/") + wURL;
179     JSRetainPtr<JSStringRef> jsAbsoluteURL(Adopt, JSStringCreateWithCharacters(wAbosuluteURL.data(), wAbosuluteURL.length()));
180
181     WorkQueue::shared()->queue(new LoadItem(jsAbsoluteURL.get(), target));
182 }
183
184 void LayoutTestController::queueReload()
185 {
186     WorkQueue::shared()->queue(new ReloadItem);
187 }
188
189 void LayoutTestController::queueScript(JSStringRef script)
190 {
191     WorkQueue::shared()->queue(new ScriptItem(script));
192 }
193
194 void LayoutTestController::setAcceptsEditing(bool acceptsEditing)
195 {
196     COMPtr<IWebView> webView;
197     if (FAILED(frame->webView(&webView)))
198         return;
199
200     COMPtr<IWebViewEditing> viewEditing;
201     if (FAILED(webView->QueryInterface(&viewEditing)))
202         return;
203
204     COMPtr<IWebEditingDelegate> delegate;
205     if (FAILED(viewEditing->editingDelegate(&delegate)))
206         return;
207
208     EditingDelegate* editingDelegate = (EditingDelegate*)(IWebEditingDelegate*)delegate.get();
209     editingDelegate->setAcceptsEditing(acceptsEditing);
210 }
211
212 void LayoutTestController::setCustomPolicyDelegate(bool setDelegate)
213 {
214     COMPtr<IWebView> webView;
215     if (FAILED(frame->webView(&webView)))
216         return;
217
218     if (setDelegate)
219         webView->setPolicyDelegate(policyDelegate);
220     else
221         webView->setPolicyDelegate(NULL);
222 }
223
224 void LayoutTestController::setMainFrameIsFirstResponder(bool flag)
225 {
226     // FIXME: Implement!
227 }
228
229 void LayoutTestController::setPrivateBrowsingEnabled(bool /*privateBrowsingEnabled*/)
230 {
231     // FIXME: Implement!
232 }
233
234 void LayoutTestController::setTabKeyCyclesThroughElements(bool shouldCycle)
235 {
236     COMPtr<IWebView> webView;
237     if (FAILED(frame->webView(&webView)))
238         return;
239
240     COMPtr<IWebViewPrivate> viewPrivate;
241     if (FAILED(webView->QueryInterface(&viewPrivate)))
242         return;
243
244     viewPrivate->setTabKeyCyclesThroughElements(shouldCycle ? TRUE : FALSE);
245 }
246
247 void LayoutTestController::setUseDashboardCompatibilityMode(bool flag)
248 {
249     // FIXME: Implement!
250 }
251
252 void LayoutTestController::setUserStyleSheetEnabled(bool flag)
253 {
254     // FIXME: Implement!
255 }
256
257 void LayoutTestController::setUserStyleSheetLocation(JSStringRef path)
258 {
259     // FIXME: Implement!
260 }
261
262 void LayoutTestController::setWindowIsKey(bool flag)
263 {
264     // FIXME: Implement!
265 }
266
267 static const CFTimeInterval waitToDumpWatchdogInterval = 10.0;
268
269 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
270 {
271     const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
272     fprintf(stderr, message);
273     fprintf(stdout, message);
274     dump();
275 }
276
277 void LayoutTestController::setWaitToDump(bool waitUntilDone)
278 {
279     // Same as on mac.  This can be shared.
280     m_waitToDump = waitUntilDone;
281     if (m_waitToDump && !waitToDumpWatchdog)
282         ::waitToDumpWatchdog = CFRunLoopTimerCreate(0, 0, waitToDumpWatchdogInterval, 0, 0, waitUntilDoneWatchdogFired, NULL);
283 }
284
285 int LayoutTestController::windowCount()
286 {
287     // FIXME: Implement!
288     return 1;
289 }