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/WebBackForwardList.h>
40 #import <WebKit/WebFrame.h>
41 #import <WebKit/WebHTMLViewPrivate.h>
42 #import <WebKit/WebHistory.h>
43 #import <WebKit/WebNSURLExtras.h>
44 #import <WebKit/WebPreferences.h>
45 #import <WebKit/WebPreferencesPrivate.h>
46 #import <WebKit/WebView.h>
47 #import <WebKit/WebViewPrivate.h>
48 #import <wtf/RetainPtr.h>
50 LayoutTestController::~LayoutTestController()
54 void LayoutTestController::addDisallowedURL(JSStringRef url)
56 RetainPtr<CFStringRef> urlCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, url));
59 disallowedURLs = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
61 // Canonicalize the URL
62 NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
63 request = [NSURLProtocol canonicalRequestForRequest:request];
65 CFSetAddValue(disallowedURLs, [request URL]);
68 void LayoutTestController::clearBackForwardList()
70 WebBackForwardList *backForwardList = [[mainFrame webView] backForwardList];
71 WebHistoryItem *item = [[backForwardList currentItem] retain];
73 // We clear the history by setting the back/forward list's capacity to 0
74 // then restoring it back and adding back the current item.
75 int capacity = [backForwardList capacity];
76 [backForwardList setCapacity:0];
77 [backForwardList setCapacity:capacity];
78 [backForwardList addItem:item];
79 [backForwardList goToItem:item];
83 JSStringRef LayoutTestController::copyDecodedHostName(JSStringRef name)
85 RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, name));
86 NSString *nameNS = (NSString *)nameCF.get();
87 return JSStringCreateWithCFString((CFStringRef)[nameNS _web_decodeHostName]);
90 JSStringRef LayoutTestController::copyEncodedHostName(JSStringRef name)
92 RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, name));
93 NSString *nameNS = (NSString *)nameCF.get();
94 return JSStringCreateWithCFString((CFStringRef)[nameNS _web_encodeHostName]);
97 void LayoutTestController::display()
102 void LayoutTestController::keepWebHistory()
104 if (![WebHistory optionalSharedHistory]) {
105 WebHistory *history = [[WebHistory alloc] init];
106 [WebHistory setOptionalSharedHistory:history];
111 void LayoutTestController::notifyDone()
113 if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
115 m_waitToDump = false;
118 JSStringRef LayoutTestController::pathToLocalResource(JSContextRef context, JSStringRef url)
120 return url; // Do nothing on mac.
123 void LayoutTestController::queueBackNavigation(int howFarBack)
125 WorkQueue::shared()->queue(new BackItem(howFarBack));
128 void LayoutTestController::queueForwardNavigation(int howFarForward)
130 WorkQueue::shared()->queue(new ForwardItem(howFarForward));
133 void LayoutTestController::queueLoad(JSStringRef url, JSStringRef target)
135 RetainPtr<CFStringRef> urlCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, url));
136 NSString *urlNS = (NSString *)urlCF.get();
138 NSURL *nsurl = [NSURL URLWithString:urlNS relativeToURL:[[[mainFrame dataSource] response] URL]];
139 NSString* nsurlString = [nsurl absoluteString];
141 JSRetainPtr<JSStringRef> absoluteURL(Adopt, JSStringCreateWithUTF8CString([nsurlString UTF8String]));
142 WorkQueue::shared()->queue(new LoadItem(absoluteURL.get(), target));
145 void LayoutTestController::queueReload()
147 WorkQueue::shared()->queue(new ReloadItem);
150 void LayoutTestController::queueScript(JSStringRef script)
152 WorkQueue::shared()->queue(new ScriptItem(script));
155 void LayoutTestController::setAcceptsEditing(bool newAcceptsEditing)
157 [(EditingDelegate *)[[mainFrame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
160 void LayoutTestController::setAuthorAndUserStylesEnabled(bool flag)
162 [[[mainFrame webView] preferences] setAuthorAndUserStylesEnabled:flag];
165 void LayoutTestController::setCustomPolicyDelegate(bool setDelegate)
168 [[mainFrame webView] setPolicyDelegate:policyDelegate];
170 [[mainFrame webView] setPolicyDelegate:nil];
173 void LayoutTestController::setMainFrameIsFirstResponder(bool flag)
175 NSView *documentView = [[mainFrame frameView] documentView];
177 NSResponder *firstResponder = flag ? documentView : nil;
178 [[[mainFrame webView] window] makeFirstResponder:firstResponder];
180 if ([documentView isKindOfClass:[WebHTMLView class]])
181 [(WebHTMLView *)documentView _updateActiveState];
184 void LayoutTestController::setPrivateBrowsingEnabled(bool privateBrowsingEnabled)
186 [[[mainFrame webView] preferences] setPrivateBrowsingEnabled:privateBrowsingEnabled];
189 void LayoutTestController::setTabKeyCyclesThroughElements(bool cycles)
191 [[mainFrame webView] setTabKeyCyclesThroughElements:cycles];
194 void LayoutTestController::setUseDashboardCompatibilityMode(bool flag)
196 [[mainFrame webView] _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:flag];
199 void LayoutTestController::setUserStyleSheetEnabled(bool flag)
201 [[WebPreferences standardPreferences] setUserStyleSheetEnabled:flag];
204 void LayoutTestController::setUserStyleSheetLocation(JSStringRef path)
206 RetainPtr<CFStringRef> pathCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, path));
207 NSURL *url = [NSURL URLWithString:(NSString *)pathCF.get()];
208 [[WebPreferences standardPreferences] setUserStyleSheetLocation:url];
211 void LayoutTestController::setWindowIsKey(bool windowIsKey)
213 m_windowIsKey = windowIsKey;
214 NSView *documentView = [[mainFrame frameView] documentView];
215 if ([documentView isKindOfClass:[WebHTMLView class]])
216 [(WebHTMLView *)documentView _updateActiveState];
219 static const CFTimeInterval waitToDumpWatchdogInterval = 10.0;
221 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
223 const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
224 fprintf(stderr, message);
225 fprintf(stdout, message);
229 void LayoutTestController::setWaitToDump(bool waitUntilDone)
231 m_waitToDump = waitUntilDone;
232 if (m_waitToDump && !waitToDumpWatchdog)
233 waitToDumpWatchdog = CFRunLoopTimerCreate(kCFAllocatorDefault, 0, waitToDumpWatchdogInterval, 0, 0, waitUntilDoneWatchdogFired, NULL);
236 int LayoutTestController::windowCount()
238 return CFArrayGetCount(allWindowsRef);