2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #import "DumpRenderTree.h"
30 #import "LayoutTestController.h"
32 #import "EditingDelegate.h"
34 #import "WorkQueueItem.h"
35 #import <Foundation/Foundation.h>
36 #import <JavaScriptCore/JSRetainPtr.h>
37 #import <JavaScriptCore/JSStringRef.h>
38 #import <JavaScriptCore/JSStringRefCF.h>
39 #import <WebKit/DOMDocument.h>
40 #import <WebKit/WebBackForwardList.h>
41 #import <WebKit/WebDatabaseManagerPrivate.h>
42 #import <WebKit/WebDataSource.h>
43 #import <WebKit/WebFrame.h>
44 #import <WebKit/WebHTMLRepresentation.h>
45 #import <WebKit/WebHTMLViewPrivate.h>
46 #import <WebKit/WebHistory.h>
47 #import <WebKit/WebNSURLExtras.h>
48 #import <WebKit/WebPreferences.h>
49 #import <WebKit/WebPreferencesPrivate.h>
50 #import <WebKit/WebSecurityOriginPrivate.h>
51 #import <WebKit/WebView.h>
52 #import <WebKit/WebViewPrivate.h>
53 #import <wtf/RetainPtr.h>
55 LayoutTestController::~LayoutTestController()
59 void LayoutTestController::addDisallowedURL(JSStringRef url)
61 RetainPtr<CFStringRef> urlCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, url));
64 disallowedURLs = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
66 // Canonicalize the URL
67 NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
68 request = [NSURLProtocol canonicalRequestForRequest:request];
70 CFSetAddValue(disallowedURLs, [request URL]);
73 void LayoutTestController::clearAllDatabases()
75 [[WebDatabaseManager sharedWebDatabaseManager] deleteAllDatabases];
78 void LayoutTestController::clearBackForwardList()
80 WebBackForwardList *backForwardList = [[mainFrame webView] backForwardList];
81 WebHistoryItem *item = [[backForwardList currentItem] retain];
83 // We clear the history by setting the back/forward list's capacity to 0
84 // then restoring it back and adding back the current item.
85 int capacity = [backForwardList capacity];
86 [backForwardList setCapacity:0];
87 [backForwardList setCapacity:capacity];
88 [backForwardList addItem:item];
89 [backForwardList goToItem:item];
93 JSStringRef LayoutTestController::copyDecodedHostName(JSStringRef name)
95 RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, name));
96 NSString *nameNS = (NSString *)nameCF.get();
97 return JSStringCreateWithCFString((CFStringRef)[nameNS _web_decodeHostName]);
100 JSStringRef LayoutTestController::copyEncodedHostName(JSStringRef name)
102 RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, name));
103 NSString *nameNS = (NSString *)nameCF.get();
104 return JSStringCreateWithCFString((CFStringRef)[nameNS _web_encodeHostName]);
107 void LayoutTestController::display()
112 void LayoutTestController::keepWebHistory()
114 if (![WebHistory optionalSharedHistory]) {
115 WebHistory *history = [[WebHistory alloc] init];
116 [WebHistory setOptionalSharedHistory:history];
121 void LayoutTestController::notifyDone()
123 if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
125 m_waitToDump = false;
128 JSStringRef LayoutTestController::pathToLocalResource(JSContextRef context, JSStringRef url)
130 return JSStringRetain(url); // Do nothing on mac.
133 void LayoutTestController::queueBackNavigation(int howFarBack)
135 WorkQueue::shared()->queue(new BackItem(howFarBack));
138 void LayoutTestController::queueForwardNavigation(int howFarForward)
140 WorkQueue::shared()->queue(new ForwardItem(howFarForward));
143 void LayoutTestController::queueLoad(JSStringRef url, JSStringRef target)
145 RetainPtr<CFStringRef> urlCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, url));
146 NSString *urlNS = (NSString *)urlCF.get();
148 NSURL *nsurl = [NSURL URLWithString:urlNS relativeToURL:[[[mainFrame dataSource] response] URL]];
149 NSString* nsurlString = [nsurl absoluteString];
151 JSRetainPtr<JSStringRef> absoluteURL(Adopt, JSStringCreateWithUTF8CString([nsurlString UTF8String]));
152 WorkQueue::shared()->queue(new LoadItem(absoluteURL.get(), target));
155 void LayoutTestController::queueReload()
157 WorkQueue::shared()->queue(new ReloadItem);
160 void LayoutTestController::queueScript(JSStringRef script)
162 WorkQueue::shared()->queue(new ScriptItem(script));
165 void LayoutTestController::setAcceptsEditing(bool newAcceptsEditing)
167 [(EditingDelegate *)[[mainFrame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
170 void LayoutTestController::setAuthorAndUserStylesEnabled(bool flag)
172 [[[mainFrame webView] preferences] setAuthorAndUserStylesEnabled:flag];
175 void LayoutTestController::setCustomPolicyDelegate(bool setDelegate)
178 [[mainFrame webView] setPolicyDelegate:policyDelegate];
180 [[mainFrame webView] setPolicyDelegate:nil];
183 void LayoutTestController::setDatabaseQuota(unsigned long long quota)
185 WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:@"file:///"]];
186 [origin setQuota:quota];
190 void LayoutTestController::setMainFrameIsFirstResponder(bool flag)
192 NSView *documentView = [[mainFrame frameView] documentView];
194 NSResponder *firstResponder = flag ? documentView : nil;
195 [[[mainFrame webView] window] makeFirstResponder:firstResponder];
197 if ([documentView isKindOfClass:[WebHTMLView class]])
198 [(WebHTMLView *)documentView _updateFocusedAndActiveState];
201 void LayoutTestController::setPrivateBrowsingEnabled(bool privateBrowsingEnabled)
203 [[[mainFrame webView] preferences] setPrivateBrowsingEnabled:privateBrowsingEnabled];
206 void LayoutTestController::setPopupBlockingEnabled(bool popupBlockingEnabled)
208 [[[mainFrame webView] preferences] setJavaScriptCanOpenWindowsAutomatically:!popupBlockingEnabled];
211 void LayoutTestController::setTabKeyCyclesThroughElements(bool cycles)
213 [[mainFrame webView] setTabKeyCyclesThroughElements:cycles];
216 void LayoutTestController::setUseDashboardCompatibilityMode(bool flag)
218 [[mainFrame webView] _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:flag];
221 void LayoutTestController::setUserStyleSheetEnabled(bool flag)
223 [[WebPreferences standardPreferences] setUserStyleSheetEnabled:flag];
226 void LayoutTestController::setUserStyleSheetLocation(JSStringRef path)
228 RetainPtr<CFStringRef> pathCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, path));
229 NSURL *url = [NSURL URLWithString:(NSString *)pathCF.get()];
230 [[WebPreferences standardPreferences] setUserStyleSheetLocation:url];
233 void LayoutTestController::setPersistentUserStyleSheetLocation(JSStringRef jsURL)
235 RetainPtr<CFStringRef> urlString(AdoptCF, JSStringCopyCFString(0, jsURL));
236 ::setPersistentUserStyleSheetLocation(urlString.get());
239 void LayoutTestController::clearPersistentUserStyleSheet()
241 ::setPersistentUserStyleSheetLocation(0);
244 void LayoutTestController::setWindowIsKey(bool windowIsKey)
246 m_windowIsKey = windowIsKey;
247 NSView *documentView = [[mainFrame frameView] documentView];
248 if ([documentView isKindOfClass:[WebHTMLView class]])
249 [(WebHTMLView *)documentView _updateFocusedAndActiveState];
252 static const CFTimeInterval waitToDumpWatchdogInterval = 10.0;
254 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
256 const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
257 fprintf(stderr, message);
258 fprintf(stdout, message);
262 void LayoutTestController::setWaitToDump(bool waitUntilDone)
264 m_waitToDump = waitUntilDone;
265 if (m_waitToDump && !waitToDumpWatchdog) {
266 waitToDumpWatchdog = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + waitToDumpWatchdogInterval, 0, 0, 0, waitUntilDoneWatchdogFired, NULL);
267 CFRunLoopAddTimer(CFRunLoopGetCurrent(), waitToDumpWatchdog, kCFRunLoopCommonModes);
271 int LayoutTestController::windowCount()
273 return CFArrayGetCount(openWindowsRef);
276 bool LayoutTestController::elementDoesAutoCompleteForElementWithId(JSStringRef id)
278 RetainPtr<CFStringRef> idCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, id));
279 NSString *idNS = (NSString *)idCF.get();
281 DOMElement *element = [[mainFrame DOMDocument] getElementById:idNS];
282 id rep = [[mainFrame dataSource] representation];
284 if ([rep class] == [WebHTMLRepresentation class])
285 return [(WebHTMLRepresentation *)rep elementDoesAutoComplete:element];
290 void LayoutTestController::execCommand(JSStringRef name, JSStringRef value)
292 RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, name));
293 NSString *nameNS = (NSString *)nameCF.get();
295 RetainPtr<CFStringRef> valueCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, value));
296 NSString *valueNS = (NSString *)valueCF.get();
298 [[mainFrame webView] _executeCoreCommandByName:nameNS value:valueNS];