TestRunnerMac.mm 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (C) 2007, 2008, 2009, 2012 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. #import "config.h"
  29. #import "DumpRenderTree.h"
  30. #import "TestRunner.h"
  31. #import "DefaultPolicyDelegate.h"
  32. #import "EditingDelegate.h"
  33. #import "MockGeolocationProvider.h"
  34. #import "MockWebNotificationProvider.h"
  35. #import "PolicyDelegate.h"
  36. #import "StorageTrackerDelegate.h"
  37. #import "UIDelegate.h"
  38. #import "WorkQueue.h"
  39. #import "WorkQueueItem.h"
  40. #import <Foundation/Foundation.h>
  41. #import <JavaScriptCore/JSRetainPtr.h>
  42. #import <JavaScriptCore/JSStringRef.h>
  43. #import <JavaScriptCore/JSStringRefCF.h>
  44. #import <WebCore/GeolocationPosition.h>
  45. #import <WebKit/DOMDocument.h>
  46. #import <WebKit/DOMElement.h>
  47. #import <WebKit/DOMHTMLInputElementPrivate.h>
  48. #import <WebKit/WebApplicationCache.h>
  49. #import <WebKit/WebBackForwardList.h>
  50. #import <WebKit/WebCoreStatistics.h>
  51. #import <WebKit/WebDOMOperationsPrivate.h>
  52. #import <WebKit/WebDataSource.h>
  53. #import <WebKit/WebDatabaseManagerPrivate.h>
  54. #import <WebKit/WebDeviceOrientation.h>
  55. #import <WebKit/WebDeviceOrientationProviderMock.h>
  56. #import <WebKit/WebFrame.h>
  57. #import <WebKit/WebFrameViewPrivate.h>
  58. #import <WebKit/WebGeolocationPosition.h>
  59. #import <WebKit/WebHTMLRepresentation.h>
  60. #import <WebKit/WebHTMLViewPrivate.h>
  61. #import <WebKit/WebHistory.h>
  62. #import <WebKit/WebHistoryPrivate.h>
  63. #import <WebKit/WebIconDatabasePrivate.h>
  64. #import <WebKit/WebInspectorPrivate.h>
  65. #import <WebKit/WebNSURLExtras.h>
  66. #import <WebKit/WebKitErrors.h>
  67. #import <WebKit/WebPreferences.h>
  68. #import <WebKit/WebPreferencesPrivate.h>
  69. #import <WebKit/WebQuotaManager.h>
  70. #import <WebKit/WebScriptWorld.h>
  71. #import <WebKit/WebSecurityOriginPrivate.h>
  72. #import <WebKit/WebStorageManagerPrivate.h>
  73. #import <WebKit/WebTypesInternal.h>
  74. #import <WebKit/WebView.h>
  75. #import <WebKit/WebViewPrivate.h>
  76. #import <wtf/CurrentTime.h>
  77. #import <wtf/HashMap.h>
  78. #import <wtf/RetainPtr.h>
  79. @interface CommandValidationTarget : NSObject <NSValidatedUserInterfaceItem>
  80. {
  81. SEL _action;
  82. }
  83. - (id)initWithAction:(SEL)action;
  84. @end
  85. @implementation CommandValidationTarget
  86. - (id)initWithAction:(SEL)action
  87. {
  88. self = [super init];
  89. if (!self)
  90. return nil;
  91. _action = action;
  92. return self;
  93. }
  94. - (SEL)action
  95. {
  96. return _action;
  97. }
  98. - (NSInteger)tag
  99. {
  100. return 0;
  101. }
  102. @end
  103. @interface WebGeolocationPosition (Internal)
  104. - (id)initWithGeolocationPosition:(PassRefPtr<WebCore::GeolocationPosition>)coreGeolocationPosition;
  105. @end
  106. TestRunner::~TestRunner()
  107. {
  108. }
  109. void TestRunner::addDisallowedURL(JSStringRef url)
  110. {
  111. RetainPtr<CFStringRef> urlCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, url));
  112. if (!disallowedURLs)
  113. disallowedURLs = CFSetCreateMutable(kCFAllocatorDefault, 0, NULL);
  114. // Canonicalize the URL
  115. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
  116. request = [NSURLProtocol canonicalRequestForRequest:request];
  117. CFSetAddValue(disallowedURLs, [request URL]);
  118. }
  119. bool TestRunner::callShouldCloseOnWebView()
  120. {
  121. return [[mainFrame webView] shouldClose];
  122. }
  123. void TestRunner::clearAllApplicationCaches()
  124. {
  125. [WebApplicationCache deleteAllApplicationCaches];
  126. }
  127. long long TestRunner::applicationCacheDiskUsageForOrigin(JSStringRef url)
  128. {
  129. RetainPtr<CFStringRef> urlCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, url));
  130. WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
  131. long long usage = [WebApplicationCache diskUsageForOrigin:origin];
  132. [origin release];
  133. return usage;
  134. }
  135. void TestRunner::syncLocalStorage()
  136. {
  137. [[WebStorageManager sharedWebStorageManager] syncLocalStorage];
  138. }
  139. long long TestRunner::localStorageDiskUsageForOrigin(JSStringRef url)
  140. {
  141. RetainPtr<CFStringRef> urlCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, url));
  142. WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
  143. long long usage = [[WebStorageManager sharedWebStorageManager] diskUsageForOrigin:origin];
  144. [origin release];
  145. return usage;
  146. }
  147. void TestRunner::observeStorageTrackerNotifications(unsigned number)
  148. {
  149. [storageDelegate logNotifications:number controller:this];
  150. }
  151. void TestRunner::clearApplicationCacheForOrigin(JSStringRef url)
  152. {
  153. RetainPtr<CFStringRef> urlCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, url));
  154. WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
  155. [WebApplicationCache deleteCacheForOrigin:origin];
  156. [origin release];
  157. }
  158. JSValueRef originsArrayToJS(JSContextRef context, NSArray *origins)
  159. {
  160. NSUInteger count = [origins count];
  161. JSValueRef jsOriginsArray[count];
  162. for (NSUInteger i = 0; i < count; i++) {
  163. NSString *origin = [[origins objectAtIndex:i] databaseIdentifier];
  164. JSRetainPtr<JSStringRef> originJS(Adopt, JSStringCreateWithCFString((CFStringRef)origin));
  165. jsOriginsArray[i] = JSValueMakeString(context, originJS.get());
  166. }
  167. return JSObjectMakeArray(context, count, jsOriginsArray, NULL);
  168. }
  169. JSValueRef TestRunner::originsWithApplicationCache(JSContextRef context)
  170. {
  171. return originsArrayToJS(context, [WebApplicationCache originsWithCache]);
  172. }
  173. void TestRunner::clearAllDatabases()
  174. {
  175. [[WebDatabaseManager sharedWebDatabaseManager] deleteAllDatabases];
  176. }
  177. void TestRunner::deleteAllLocalStorage()
  178. {
  179. [[WebStorageManager sharedWebStorageManager] deleteAllOrigins];
  180. }
  181. void TestRunner::setStorageDatabaseIdleInterval(double interval)
  182. {
  183. [WebStorageManager setStorageDatabaseIdleInterval:interval];
  184. }
  185. void TestRunner::closeIdleLocalStorageDatabases()
  186. {
  187. [WebStorageManager closeIdleLocalStorageDatabases];
  188. }
  189. JSValueRef TestRunner::originsWithLocalStorage(JSContextRef context)
  190. {
  191. return originsArrayToJS(context, [[WebStorageManager sharedWebStorageManager] origins]);
  192. }
  193. void TestRunner::deleteLocalStorageForOrigin(JSStringRef URL)
  194. {
  195. RetainPtr<CFStringRef> urlCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, URL));
  196. WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:(NSString *)urlCF.get()]];
  197. [[WebStorageManager sharedWebStorageManager] deleteOrigin:origin];
  198. [origin release];
  199. }
  200. void TestRunner::clearBackForwardList()
  201. {
  202. WebBackForwardList *backForwardList = [[mainFrame webView] backForwardList];
  203. WebHistoryItem *item = [[backForwardList currentItem] retain];
  204. // We clear the history by setting the back/forward list's capacity to 0
  205. // then restoring it back and adding back the current item.
  206. int capacity = [backForwardList capacity];
  207. [backForwardList setCapacity:0];
  208. [backForwardList setCapacity:capacity];
  209. [backForwardList addItem:item];
  210. [backForwardList goToItem:item];
  211. [item release];
  212. }
  213. JSStringRef TestRunner::copyDecodedHostName(JSStringRef name)
  214. {
  215. RetainPtr<CFStringRef> nameCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, name));
  216. NSString *nameNS = (NSString *)nameCF.get();
  217. return JSStringCreateWithCFString((CFStringRef)[nameNS _web_decodeHostName]);
  218. }
  219. JSStringRef TestRunner::copyEncodedHostName(JSStringRef name)
  220. {
  221. RetainPtr<CFStringRef> nameCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, name));
  222. NSString *nameNS = (NSString *)nameCF.get();
  223. return JSStringCreateWithCFString((CFStringRef)[nameNS _web_encodeHostName]);
  224. }
  225. void TestRunner::display()
  226. {
  227. displayWebView();
  228. }
  229. void TestRunner::keepWebHistory()
  230. {
  231. if (![WebHistory optionalSharedHistory]) {
  232. WebHistory *history = [[WebHistory alloc] init];
  233. [WebHistory setOptionalSharedHistory:history];
  234. [history release];
  235. }
  236. }
  237. int TestRunner::numberOfPendingGeolocationPermissionRequests()
  238. {
  239. return [[[mainFrame webView] UIDelegate] numberOfPendingGeolocationPermissionRequests];
  240. }
  241. size_t TestRunner::webHistoryItemCount()
  242. {
  243. return [[[WebHistory optionalSharedHistory] allItems] count];
  244. }
  245. JSRetainPtr<JSStringRef> TestRunner::platformName() const
  246. {
  247. JSRetainPtr<JSStringRef> platformName(Adopt, JSStringCreateWithUTF8CString("mac"));
  248. return platformName;
  249. }
  250. void TestRunner::notifyDone()
  251. {
  252. if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
  253. dump();
  254. m_waitToDump = false;
  255. }
  256. static inline std::string stringFromJSString(JSStringRef jsString)
  257. {
  258. size_t maxBufferSize = JSStringGetMaximumUTF8CStringSize(jsString);
  259. char* utf8Buffer = new char[maxBufferSize];
  260. size_t bytesWrittenToUTF8Buffer = JSStringGetUTF8CString(jsString, utf8Buffer, maxBufferSize);
  261. std::string stdString(utf8Buffer, bytesWrittenToUTF8Buffer - 1); // bytesWrittenToUTF8Buffer includes a trailing \0 which std::string doesn't need.
  262. delete[] utf8Buffer;
  263. return stdString;
  264. }
  265. static inline size_t indexOfSeparatorAfterDirectoryName(const std::string& directoryName, const std::string& fullPath)
  266. {
  267. std::string searchKey = "/" + directoryName + "/";
  268. size_t indexOfSearchKeyStart = fullPath.rfind(searchKey);
  269. if (indexOfSearchKeyStart == std::string::npos) {
  270. ASSERT_NOT_REACHED();
  271. return 0;
  272. }
  273. // Callers expect the return value not to end in "/", so searchKey.length() - 1.
  274. return indexOfSearchKeyStart + searchKey.length() - 1;
  275. }
  276. static inline std::string resourceRootAbsolutePath(const std::string& testPathOrURL, const std::string& expectedRootName)
  277. {
  278. char* localResourceRootEnv = getenv("LOCAL_RESOURCE_ROOT");
  279. if (localResourceRootEnv)
  280. return std::string(localResourceRootEnv);
  281. // This fallback approach works for non-http tests and is useful
  282. // in the case when we're running DRT directly from the command line.
  283. return testPathOrURL.substr(0, indexOfSeparatorAfterDirectoryName(expectedRootName, testPathOrURL));
  284. }
  285. JSStringRef TestRunner::pathToLocalResource(JSContextRef context, JSStringRef localResourceJSString)
  286. {
  287. // The passed in path will be an absolute path to the resource starting
  288. // with "/tmp" or "/tmp/LayoutTests", optionally starting with the explicit file:// protocol.
  289. // /tmp maps to DUMPRENDERTREE_TEMP, and /tmp/LayoutTests maps to LOCAL_RESOURCE_ROOT.
  290. // FIXME: This code should work on all *nix platforms and can be moved into TestRunner.cpp.
  291. std::string expectedRootName;
  292. std::string absolutePathToResourceRoot;
  293. std::string localResourceString = stringFromJSString(localResourceJSString);
  294. if (localResourceString.find("LayoutTests") != std::string::npos) {
  295. expectedRootName = "LayoutTests";
  296. absolutePathToResourceRoot = resourceRootAbsolutePath(m_testPathOrURL, expectedRootName);
  297. } else if (localResourceString.find("tmp") != std::string::npos) {
  298. expectedRootName = "tmp";
  299. absolutePathToResourceRoot = getenv("DUMPRENDERTREE_TEMP");
  300. } else {
  301. ASSERT_NOT_REACHED(); // pathToLocalResource was passed a path it doesn't know how to map.
  302. }
  303. ASSERT(!absolutePathToResourceRoot.empty());
  304. size_t indexOfSeparatorAfterRootName = indexOfSeparatorAfterDirectoryName(expectedRootName, localResourceString);
  305. std::string absolutePathToLocalResource = absolutePathToResourceRoot + localResourceString.substr(indexOfSeparatorAfterRootName);
  306. // Note: It's important that we keep the file:// or http tests will get confused.
  307. if (localResourceString.find("file://") != std::string::npos) {
  308. ASSERT(absolutePathToLocalResource[0] == '/');
  309. absolutePathToLocalResource = std::string("file://") + absolutePathToLocalResource;
  310. }
  311. return JSStringCreateWithUTF8CString(absolutePathToLocalResource.c_str());
  312. }
  313. void TestRunner::queueLoad(JSStringRef url, JSStringRef target)
  314. {
  315. RetainPtr<CFStringRef> urlCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, url));
  316. NSString *urlNS = (NSString *)urlCF.get();
  317. NSURL *nsurl = [NSURL URLWithString:urlNS relativeToURL:[[[mainFrame dataSource] response] URL]];
  318. NSString *nsurlString = [nsurl absoluteString];
  319. JSRetainPtr<JSStringRef> absoluteURL(Adopt, JSStringCreateWithUTF8CString([nsurlString UTF8String]));
  320. WorkQueue::shared()->queue(new LoadItem(absoluteURL.get(), target));
  321. }
  322. void TestRunner::setAcceptsEditing(bool newAcceptsEditing)
  323. {
  324. [(EditingDelegate *)[[mainFrame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
  325. }
  326. void TestRunner::setAlwaysAcceptCookies(bool alwaysAcceptCookies)
  327. {
  328. if (alwaysAcceptCookies == m_alwaysAcceptCookies)
  329. return;
  330. m_alwaysAcceptCookies = alwaysAcceptCookies;
  331. NSHTTPCookieAcceptPolicy cookieAcceptPolicy = alwaysAcceptCookies ? NSHTTPCookieAcceptPolicyAlways : NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
  332. [WebPreferences _setCurrentNetworkLoaderSessionCookieAcceptPolicy:cookieAcceptPolicy];
  333. }
  334. void TestRunner::setAppCacheMaximumSize(unsigned long long size)
  335. {
  336. [WebApplicationCache setMaximumSize:size];
  337. }
  338. void TestRunner::setApplicationCacheOriginQuota(unsigned long long quota)
  339. {
  340. WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:@"http://127.0.0.1:8000"]];
  341. [[origin applicationCacheQuotaManager] setQuota:quota];
  342. [origin release];
  343. }
  344. void TestRunner::setAuthorAndUserStylesEnabled(bool flag)
  345. {
  346. [[[mainFrame webView] preferences] setAuthorAndUserStylesEnabled:flag];
  347. }
  348. void TestRunner::setCustomPolicyDelegate(bool setDelegate, bool permissive)
  349. {
  350. if (!setDelegate) {
  351. [[mainFrame webView] setPolicyDelegate:defaultPolicyDelegate];
  352. return;
  353. }
  354. [policyDelegate setPermissive:permissive];
  355. [[mainFrame webView] setPolicyDelegate:policyDelegate];
  356. }
  357. void TestRunner::setDatabaseQuota(unsigned long long quota)
  358. {
  359. WebSecurityOrigin *origin = [[WebSecurityOrigin alloc] initWithURL:[NSURL URLWithString:@"file:///"]];
  360. [[origin databaseQuotaManager] setQuota:quota];
  361. [origin release];
  362. }
  363. void TestRunner::goBack()
  364. {
  365. [[mainFrame webView] goBack];
  366. }
  367. void TestRunner::setDefersLoading(bool defers)
  368. {
  369. [[mainFrame webView] setDefersCallbacks:defers];
  370. }
  371. void TestRunner::setDomainRelaxationForbiddenForURLScheme(bool forbidden, JSStringRef scheme)
  372. {
  373. RetainPtr<CFStringRef> schemeCFString = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, scheme));
  374. [WebView _setDomainRelaxationForbidden:forbidden forURLScheme:(NSString *)schemeCFString.get()];
  375. }
  376. void TestRunner::setMockDeviceOrientation(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
  377. {
  378. // DumpRenderTree configured the WebView to use WebDeviceOrientationProviderMock.
  379. id<WebDeviceOrientationProvider> provider = [[mainFrame webView] _deviceOrientationProvider];
  380. WebDeviceOrientationProviderMock *mockProvider = static_cast<WebDeviceOrientationProviderMock*>(provider);
  381. WebDeviceOrientation *orientation = [[WebDeviceOrientation alloc] initWithCanProvideAlpha:canProvideAlpha alpha:alpha canProvideBeta:canProvideBeta beta:beta canProvideGamma:canProvideGamma gamma:gamma];
  382. [mockProvider setOrientation:orientation];
  383. [orientation release];
  384. }
  385. void TestRunner::setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed)
  386. {
  387. WebGeolocationPosition *position = nil;
  388. if (!providesAltitude && !providesAltitudeAccuracy && !providesHeading && !providesSpeed) {
  389. // Test the exposed API.
  390. position = [[WebGeolocationPosition alloc] initWithTimestamp:currentTime() latitude:latitude longitude:longitude accuracy:accuracy];
  391. } else {
  392. RefPtr<WebCore::GeolocationPosition> coreGeolocationPosition = WebCore::GeolocationPosition::create(currentTime(), latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed);
  393. position = [[WebGeolocationPosition alloc] initWithGeolocationPosition:(coreGeolocationPosition.release())];
  394. }
  395. [[MockGeolocationProvider shared] setPosition:position];
  396. [position release];
  397. }
  398. void TestRunner::setMockGeolocationPositionUnavailableError(JSStringRef message)
  399. {
  400. RetainPtr<CFStringRef> messageCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, message));
  401. NSString *messageNS = (NSString *)messageCF.get();
  402. [[MockGeolocationProvider shared] setPositionUnavailableErrorWithMessage:messageNS];
  403. }
  404. void TestRunner::setGeolocationPermission(bool allow)
  405. {
  406. setGeolocationPermissionCommon(allow);
  407. [[[mainFrame webView] UIDelegate] didSetMockGeolocationPermission];
  408. }
  409. void TestRunner::addMockSpeechInputResult(JSStringRef result, double confidence, JSStringRef language)
  410. {
  411. // FIXME: Implement for speech input layout tests.
  412. // See https://bugs.webkit.org/show_bug.cgi?id=39485.
  413. }
  414. void TestRunner::setMockSpeechInputDumpRect(bool flag)
  415. {
  416. // FIXME: Implement for speech input layout tests.
  417. // See https://bugs.webkit.org/show_bug.cgi?id=39485.
  418. }
  419. void TestRunner::startSpeechInput(JSContextRef inputElement)
  420. {
  421. // FIXME: Implement for speech input layout tests.
  422. // See https://bugs.webkit.org/show_bug.cgi?id=39485.
  423. }
  424. void TestRunner::setIconDatabaseEnabled(bool iconDatabaseEnabled)
  425. {
  426. // FIXME: Workaround <rdar://problem/6480108>
  427. static WebIconDatabase *sharedWebIconDatabase = NULL;
  428. if (!sharedWebIconDatabase) {
  429. if (!iconDatabaseEnabled)
  430. return;
  431. sharedWebIconDatabase = [WebIconDatabase sharedIconDatabase];
  432. if ([sharedWebIconDatabase isEnabled] == iconDatabaseEnabled)
  433. return;
  434. }
  435. [sharedWebIconDatabase setEnabled:iconDatabaseEnabled];
  436. }
  437. void TestRunner::setMainFrameIsFirstResponder(bool flag)
  438. {
  439. NSView *documentView = [[mainFrame frameView] documentView];
  440. NSResponder *firstResponder = flag ? documentView : nil;
  441. [[[mainFrame webView] window] makeFirstResponder:firstResponder];
  442. }
  443. void TestRunner::setPrivateBrowsingEnabled(bool privateBrowsingEnabled)
  444. {
  445. [[[mainFrame webView] preferences] setPrivateBrowsingEnabled:privateBrowsingEnabled];
  446. }
  447. void TestRunner::setXSSAuditorEnabled(bool enabled)
  448. {
  449. [[[mainFrame webView] preferences] setXSSAuditorEnabled:enabled];
  450. }
  451. void TestRunner::setSpatialNavigationEnabled(bool enabled)
  452. {
  453. [[[mainFrame webView] preferences] setSpatialNavigationEnabled:enabled];
  454. }
  455. void TestRunner::setAllowUniversalAccessFromFileURLs(bool enabled)
  456. {
  457. [[[mainFrame webView] preferences] setAllowUniversalAccessFromFileURLs:enabled];
  458. }
  459. void TestRunner::setAllowFileAccessFromFileURLs(bool enabled)
  460. {
  461. [[[mainFrame webView] preferences] setAllowFileAccessFromFileURLs:enabled];
  462. }
  463. void TestRunner::setPopupBlockingEnabled(bool popupBlockingEnabled)
  464. {
  465. [[[mainFrame webView] preferences] setJavaScriptCanOpenWindowsAutomatically:!popupBlockingEnabled];
  466. }
  467. void TestRunner::setPluginsEnabled(bool pluginsEnabled)
  468. {
  469. [[[mainFrame webView] preferences] setPlugInsEnabled:pluginsEnabled];
  470. }
  471. void TestRunner::setJavaScriptCanAccessClipboard(bool enabled)
  472. {
  473. [[[mainFrame webView] preferences] setJavaScriptCanAccessClipboard:enabled];
  474. }
  475. void TestRunner::setAutomaticLinkDetectionEnabled(bool enabled)
  476. {
  477. [[mainFrame webView] setAutomaticLinkDetectionEnabled:enabled];
  478. }
  479. void TestRunner::setTabKeyCyclesThroughElements(bool cycles)
  480. {
  481. [[mainFrame webView] setTabKeyCyclesThroughElements:cycles];
  482. }
  483. void TestRunner::setUseDashboardCompatibilityMode(bool flag)
  484. {
  485. [[mainFrame webView] _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:flag];
  486. }
  487. void TestRunner::setUserStyleSheetEnabled(bool flag)
  488. {
  489. [[WebPreferences standardPreferences] setUserStyleSheetEnabled:flag];
  490. }
  491. void TestRunner::setUserStyleSheetLocation(JSStringRef path)
  492. {
  493. RetainPtr<CFStringRef> pathCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, path));
  494. NSURL *url = [NSURL URLWithString:(NSString *)pathCF.get()];
  495. [[WebPreferences standardPreferences] setUserStyleSheetLocation:url];
  496. }
  497. void TestRunner::setValueForUser(JSContextRef context, JSValueRef nodeObject, JSStringRef value)
  498. {
  499. DOMElement *element = [DOMElement _DOMElementFromJSContext:context value:nodeObject];
  500. if (!element || ![element isKindOfClass:[DOMHTMLInputElement class]])
  501. return;
  502. RetainPtr<CFStringRef> valueCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, value));
  503. [(DOMHTMLInputElement *)element setValueForUser:(NSString *)valueCF.get()];
  504. }
  505. void TestRunner::setViewModeMediaFeature(JSStringRef mode)
  506. {
  507. // FIXME: implement
  508. }
  509. void TestRunner::dispatchPendingLoadRequests()
  510. {
  511. [[mainFrame webView] _dispatchPendingLoadRequests];
  512. }
  513. void TestRunner::overridePreference(JSStringRef key, JSStringRef value)
  514. {
  515. RetainPtr<CFStringRef> keyCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, key));
  516. NSString *keyNS = (NSString *)keyCF.get();
  517. RetainPtr<CFStringRef> valueCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, value));
  518. NSString *valueNS = (NSString *)valueCF.get();
  519. [[WebPreferences standardPreferences] _setPreferenceForTestWithValue:valueNS forKey:keyNS];
  520. }
  521. void TestRunner::removeAllVisitedLinks()
  522. {
  523. [WebHistory _removeAllVisitedLinks];
  524. }
  525. void TestRunner::setPersistentUserStyleSheetLocation(JSStringRef jsURL)
  526. {
  527. RetainPtr<CFStringRef> urlString = adoptCF(JSStringCopyCFString(0, jsURL));
  528. ::setPersistentUserStyleSheetLocation(urlString.get());
  529. }
  530. void TestRunner::clearPersistentUserStyleSheet()
  531. {
  532. ::setPersistentUserStyleSheetLocation(0);
  533. }
  534. void TestRunner::setWindowIsKey(bool windowIsKey)
  535. {
  536. m_windowIsKey = windowIsKey;
  537. [[mainFrame webView] _updateActiveState];
  538. }
  539. static const CFTimeInterval waitToDumpWatchdogInterval = 30.0;
  540. static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
  541. {
  542. gTestRunner->waitToDumpWatchdogTimerFired();
  543. }
  544. void TestRunner::setWaitToDump(bool waitUntilDone)
  545. {
  546. m_waitToDump = waitUntilDone;
  547. if (m_waitToDump && shouldSetWaitToDumpWatchdog())
  548. setWaitToDumpWatchdog(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + waitToDumpWatchdogInterval, 0, 0, 0, waitUntilDoneWatchdogFired, NULL));
  549. }
  550. int TestRunner::windowCount()
  551. {
  552. return CFArrayGetCount(openWindowsRef);
  553. }
  554. void TestRunner::execCommand(JSStringRef name, JSStringRef value)
  555. {
  556. RetainPtr<CFStringRef> nameCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, name));
  557. NSString *nameNS = (NSString *)nameCF.get();
  558. RetainPtr<CFStringRef> valueCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, value));
  559. NSString *valueNS = (NSString *)valueCF.get();
  560. [[mainFrame webView] _executeCoreCommandByName:nameNS value:valueNS];
  561. }
  562. bool TestRunner::findString(JSContextRef context, JSStringRef target, JSObjectRef optionsArray)
  563. {
  564. WebFindOptions options = 0;
  565. JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length"));
  566. JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0);
  567. if (!JSValueIsNumber(context, lengthValue))
  568. return false;
  569. RetainPtr<CFStringRef> targetCFString = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, target));
  570. size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0));
  571. for (size_t i = 0; i < length; ++i) {
  572. JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0);
  573. if (!JSValueIsString(context, value))
  574. continue;
  575. JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0));
  576. if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive"))
  577. options |= WebFindOptionsCaseInsensitive;
  578. else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts"))
  579. options |= WebFindOptionsAtWordStarts;
  580. else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart"))
  581. options |= WebFindOptionsTreatMedialCapitalAsWordStart;
  582. else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards"))
  583. options |= WebFindOptionsBackwards;
  584. else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround"))
  585. options |= WebFindOptionsWrapAround;
  586. else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection"))
  587. options |= WebFindOptionsStartInSelection;
  588. }
  589. return [[mainFrame webView] findString:(NSString *)targetCFString.get() options:options];
  590. }
  591. void TestRunner::setCacheModel(int cacheModel)
  592. {
  593. [[WebPreferences standardPreferences] setCacheModel:cacheModel];
  594. }
  595. bool TestRunner::isCommandEnabled(JSStringRef name)
  596. {
  597. RetainPtr<CFStringRef> nameCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, name));
  598. NSString *nameNS = (NSString *)nameCF.get();
  599. // Accept command strings with capital letters for first letter without trailing colon.
  600. if (![nameNS hasSuffix:@":"] && [nameNS length]) {
  601. nameNS = [[[[nameNS substringToIndex:1] lowercaseString]
  602. stringByAppendingString:[nameNS substringFromIndex:1]]
  603. stringByAppendingString:@":"];
  604. }
  605. SEL selector = NSSelectorFromString(nameNS);
  606. RetainPtr<CommandValidationTarget> target = adoptNS([[CommandValidationTarget alloc] initWithAction:selector]);
  607. id validator = [NSApp targetForAction:selector to:[mainFrame webView] from:target.get()];
  608. if (!validator)
  609. return false;
  610. if (![validator respondsToSelector:selector])
  611. return false;
  612. if (![validator respondsToSelector:@selector(validateUserInterfaceItem:)])
  613. return true;
  614. return [validator validateUserInterfaceItem:target.get()];
  615. }
  616. void TestRunner::waitForPolicyDelegate()
  617. {
  618. setWaitToDump(true);
  619. [policyDelegate setControllerToNotifyDone:this];
  620. [[mainFrame webView] setPolicyDelegate:policyDelegate];
  621. }
  622. void TestRunner::addOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
  623. {
  624. RetainPtr<CFStringRef> sourceOriginCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, sourceOrigin));
  625. NSString *sourceOriginNS = (NSString *)sourceOriginCF.get();
  626. RetainPtr<CFStringRef> protocolCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, destinationProtocol));
  627. NSString *destinationProtocolNS = (NSString *)protocolCF.get();
  628. RetainPtr<CFStringRef> hostCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, destinationHost));
  629. NSString *destinationHostNS = (NSString *)hostCF.get();
  630. [WebView _addOriginAccessWhitelistEntryWithSourceOrigin:sourceOriginNS destinationProtocol:destinationProtocolNS destinationHost:destinationHostNS allowDestinationSubdomains:allowDestinationSubdomains];
  631. }
  632. void TestRunner::removeOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
  633. {
  634. RetainPtr<CFStringRef> sourceOriginCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, sourceOrigin));
  635. NSString *sourceOriginNS = (NSString *)sourceOriginCF.get();
  636. RetainPtr<CFStringRef> protocolCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, destinationProtocol));
  637. NSString *destinationProtocolNS = (NSString *)protocolCF.get();
  638. RetainPtr<CFStringRef> hostCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, destinationHost));
  639. NSString *destinationHostNS = (NSString *)hostCF.get();
  640. [WebView _removeOriginAccessWhitelistEntryWithSourceOrigin:sourceOriginNS destinationProtocol:destinationProtocolNS destinationHost:destinationHostNS allowDestinationSubdomains:allowDestinationSubdomains];
  641. }
  642. void TestRunner::setScrollbarPolicy(JSStringRef orientation, JSStringRef policy)
  643. {
  644. // FIXME: implement
  645. }
  646. void TestRunner::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
  647. {
  648. RetainPtr<CFStringRef> sourceCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, source));
  649. NSString *sourceNS = (NSString *)sourceCF.get();
  650. [WebView _addUserScriptToGroup:@"org.webkit.DumpRenderTree" world:[WebScriptWorld world] source:sourceNS url:nil whitelist:nil blacklist:nil injectionTime:(runAtStart ? WebInjectAtDocumentStart : WebInjectAtDocumentEnd) injectedFrames:(allFrames ? WebInjectInAllFrames : WebInjectInTopFrameOnly)];
  651. }
  652. void TestRunner::addUserStyleSheet(JSStringRef source, bool allFrames)
  653. {
  654. RetainPtr<CFStringRef> sourceCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, source));
  655. NSString *sourceNS = (NSString *)sourceCF.get();
  656. [WebView _addUserStyleSheetToGroup:@"org.webkit.DumpRenderTree" world:[WebScriptWorld world] source:sourceNS url:nil whitelist:nil blacklist:nil injectedFrames:(allFrames ? WebInjectInAllFrames : WebInjectInTopFrameOnly)];
  657. }
  658. void TestRunner::setDeveloperExtrasEnabled(bool enabled)
  659. {
  660. [[[mainFrame webView] preferences] setDeveloperExtrasEnabled:enabled];
  661. }
  662. void TestRunner::showWebInspector()
  663. {
  664. [[[mainFrame webView] inspector] show:nil];
  665. }
  666. void TestRunner::closeWebInspector()
  667. {
  668. [[[mainFrame webView] inspector] close:nil];
  669. }
  670. void TestRunner::evaluateInWebInspector(long callId, JSStringRef script)
  671. {
  672. RetainPtr<CFStringRef> scriptCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, script));
  673. NSString *scriptNS = (NSString *)scriptCF.get();
  674. [[[mainFrame webView] inspector] evaluateInFrontend:nil callId:callId script:scriptNS];
  675. }
  676. typedef HashMap<unsigned, RetainPtr<WebScriptWorld> > WorldMap;
  677. static WorldMap& worldMap()
  678. {
  679. static WorldMap& map = *new WorldMap;
  680. return map;
  681. }
  682. unsigned worldIDForWorld(WebScriptWorld *world)
  683. {
  684. WorldMap::const_iterator end = worldMap().end();
  685. for (WorldMap::const_iterator it = worldMap().begin(); it != end; ++it) {
  686. if (it->value == world)
  687. return it->key;
  688. }
  689. return 0;
  690. }
  691. void TestRunner::evaluateScriptInIsolatedWorldAndReturnValue(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
  692. {
  693. // FIXME: Implement this.
  694. }
  695. void TestRunner::evaluateScriptInIsolatedWorld(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
  696. {
  697. RetainPtr<CFStringRef> scriptCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, script));
  698. NSString *scriptNS = (NSString *)scriptCF.get();
  699. // A worldID of 0 always corresponds to a new world. Any other worldID corresponds to a world
  700. // that is created once and cached forever.
  701. WebScriptWorld *world;
  702. if (!worldID)
  703. world = [WebScriptWorld world];
  704. else {
  705. RetainPtr<WebScriptWorld>& worldSlot = worldMap().add(worldID, 0).iterator->value;
  706. if (!worldSlot)
  707. worldSlot = adoptNS([[WebScriptWorld alloc] init]);
  708. world = worldSlot.get();
  709. }
  710. [mainFrame _stringByEvaluatingJavaScriptFromString:scriptNS withGlobalObject:globalObject inScriptWorld:world];
  711. }
  712. @interface APITestDelegate : NSObject
  713. {
  714. bool* m_condition;
  715. }
  716. @end
  717. @implementation APITestDelegate
  718. - (id)initWithCompletionCondition:(bool*)condition
  719. {
  720. [super init];
  721. ASSERT(condition);
  722. m_condition = condition;
  723. *m_condition = false;
  724. return self;
  725. }
  726. - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
  727. {
  728. printf("API Test load failed\n");
  729. *m_condition = true;
  730. }
  731. - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
  732. {
  733. printf("API Test load failed provisional\n");
  734. *m_condition = true;
  735. }
  736. - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
  737. {
  738. printf("API Test load succeeded\n");
  739. *m_condition = true;
  740. }
  741. @end
  742. void TestRunner::apiTestNewWindowDataLoadBaseURL(JSStringRef utf8Data, JSStringRef baseURL)
  743. {
  744. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  745. RetainPtr<CFStringRef> utf8DataCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, utf8Data));
  746. RetainPtr<CFStringRef> baseURLCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, baseURL));
  747. WebView *webView = [[WebView alloc] initWithFrame:NSZeroRect frameName:@"" groupName:@""];
  748. bool done = false;
  749. APITestDelegate *delegate = [[APITestDelegate alloc] initWithCompletionCondition:&done];
  750. [webView setFrameLoadDelegate:delegate];
  751. [[webView mainFrame] loadData:[(NSString *)utf8DataCF.get() dataUsingEncoding:NSUTF8StringEncoding] MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL URLWithString:(NSString *)baseURLCF.get()]];
  752. while (!done) {
  753. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  754. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
  755. [pool release];
  756. }
  757. [webView close];
  758. [webView release];
  759. [delegate release];
  760. [pool release];
  761. }
  762. void TestRunner::apiTestGoToCurrentBackForwardItem()
  763. {
  764. WebView *view = [mainFrame webView];
  765. [view goToBackForwardItem:[[view backForwardList] currentItem]];
  766. }
  767. void TestRunner::setWebViewEditable(bool editable)
  768. {
  769. WebView *view = [mainFrame webView];
  770. [view setEditable:editable];
  771. }
  772. static NSString *SynchronousLoaderRunLoopMode = @"DumpRenderTreeSynchronousLoaderRunLoopMode";
  773. #if __MAC_OS_X_VERSION_MIN_REQUIRED == 1060
  774. @protocol NSURLConnectionDelegate <NSObject>
  775. @end
  776. #endif
  777. @interface SynchronousLoader : NSObject <NSURLConnectionDelegate>
  778. {
  779. NSString *m_username;
  780. NSString *m_password;
  781. BOOL m_isDone;
  782. }
  783. + (void)makeRequest:(NSURLRequest *)request withUsername:(NSString *)username password:(NSString *)password;
  784. @end
  785. @implementation SynchronousLoader : NSObject
  786. - (void)dealloc
  787. {
  788. [m_username release];
  789. [m_password release];
  790. [super dealloc];
  791. }
  792. - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
  793. {
  794. return YES;
  795. }
  796. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
  797. {
  798. if ([challenge previousFailureCount] == 0) {
  799. RetainPtr<NSURLCredential> credential = adoptNS([[NSURLCredential alloc] initWithUser:m_username password:m_password persistence:NSURLCredentialPersistenceForSession]);
  800. [[challenge sender] useCredential:credential.get() forAuthenticationChallenge:challenge];
  801. return;
  802. }
  803. [[challenge sender] cancelAuthenticationChallenge:challenge];
  804. }
  805. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  806. {
  807. printf("SynchronousLoader failed: %s\n", [[error description] UTF8String]);
  808. m_isDone = YES;
  809. }
  810. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  811. {
  812. m_isDone = YES;
  813. }
  814. + (void)makeRequest:(NSURLRequest *)request withUsername:(NSString *)username password:(NSString *)password
  815. {
  816. ASSERT(![[request URL] user]);
  817. ASSERT(![[request URL] password]);
  818. SynchronousLoader *delegate = [[SynchronousLoader alloc] init];
  819. delegate->m_username = [username copy];
  820. delegate->m_password = [password copy];
  821. NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO];
  822. [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:SynchronousLoaderRunLoopMode];
  823. [connection start];
  824. while (!delegate->m_isDone)
  825. [[NSRunLoop currentRunLoop] runMode:SynchronousLoaderRunLoopMode beforeDate:[NSDate distantFuture]];
  826. [connection cancel];
  827. [connection release];
  828. [delegate release];
  829. }
  830. @end
  831. void TestRunner::authenticateSession(JSStringRef url, JSStringRef username, JSStringRef password)
  832. {
  833. // See <rdar://problem/7880699>.
  834. RetainPtr<CFStringRef> urlStringCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, url));
  835. RetainPtr<CFStringRef> usernameCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, username));
  836. RetainPtr<CFStringRef> passwordCF = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, password));
  837. RetainPtr<NSURLRequest> request = adoptNS([[NSURLRequest alloc] initWithURL:[NSURL URLWithString:(NSString *)urlStringCF.get()]]);
  838. [SynchronousLoader makeRequest:request.get() withUsername:(NSString *)usernameCF.get() password:(NSString *)passwordCF.get()];
  839. }
  840. void TestRunner::abortModal()
  841. {
  842. [NSApp abortModal];
  843. }
  844. void TestRunner::setSerializeHTTPLoads(bool serialize)
  845. {
  846. [WebView _setLoadResourcesSerially:serialize];
  847. }
  848. void TestRunner::setTextDirection(JSStringRef directionName)
  849. {
  850. if (JSStringIsEqualToUTF8CString(directionName, "ltr"))
  851. [[mainFrame webView] makeBaseWritingDirectionLeftToRight:0];
  852. else if (JSStringIsEqualToUTF8CString(directionName, "rtl"))
  853. [[mainFrame webView] makeBaseWritingDirectionRightToLeft:0];
  854. else
  855. ASSERT_NOT_REACHED();
  856. }
  857. void TestRunner::addChromeInputField()
  858. {
  859. NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 20)];
  860. textField.tag = 1;
  861. [[[[mainFrame webView] window] contentView] addSubview:textField];
  862. [textField release];
  863. [textField setNextKeyView:[mainFrame webView]];
  864. [[mainFrame webView] setNextKeyView:textField];
  865. }
  866. void TestRunner::removeChromeInputField()
  867. {
  868. NSView* textField = [[[[mainFrame webView] window] contentView] viewWithTag:1];
  869. if (textField) {
  870. [textField removeFromSuperview];
  871. focusWebView();
  872. }
  873. }
  874. void TestRunner::focusWebView()
  875. {
  876. [[[mainFrame webView] window] makeFirstResponder:[mainFrame webView]];
  877. }
  878. void TestRunner::setBackingScaleFactor(double backingScaleFactor)
  879. {
  880. [[mainFrame webView] _setCustomBackingScaleFactor:backingScaleFactor];
  881. }
  882. void TestRunner::resetPageVisibility()
  883. {
  884. WebView *webView = [mainFrame webView];
  885. if ([webView respondsToSelector:@selector(_setVisibilityState:isInitialState:)])
  886. [webView _setVisibilityState:WebPageVisibilityStateVisible isInitialState:YES];
  887. }
  888. void TestRunner::setPageVisibility(const char* newVisibility)
  889. {
  890. if (!newVisibility)
  891. return;
  892. WebView *webView = [mainFrame webView];
  893. if (!strcmp(newVisibility, "visible"))
  894. [webView _setVisibilityState:WebPageVisibilityStateVisible isInitialState:NO];
  895. else if (!strcmp(newVisibility, "hidden"))
  896. [webView _setVisibilityState:WebPageVisibilityStateHidden isInitialState:NO];
  897. else if (!strcmp(newVisibility, "prerender"))
  898. [webView _setVisibilityState:WebPageVisibilityStatePrerender isInitialState:NO];
  899. else if (!strcmp(newVisibility, "unloaded"))
  900. [webView _setVisibilityState:WebPageVisibilityStateUnloaded isInitialState:NO];
  901. }
  902. void TestRunner::grantWebNotificationPermission(JSStringRef jsOrigin)
  903. {
  904. RetainPtr<CFStringRef> cfOrigin = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, jsOrigin));
  905. ASSERT([[mainFrame webView] _notificationProvider] == [MockWebNotificationProvider shared]);
  906. [[MockWebNotificationProvider shared] setWebNotificationOrigin:(NSString *)cfOrigin.get() permission:TRUE];
  907. }
  908. void TestRunner::denyWebNotificationPermission(JSStringRef jsOrigin)
  909. {
  910. RetainPtr<CFStringRef> cfOrigin = adoptCF(JSStringCopyCFString(kCFAllocatorDefault, jsOrigin));
  911. ASSERT([[mainFrame webView] _notificationProvider] == [MockWebNotificationProvider shared]);
  912. [[MockWebNotificationProvider shared] setWebNotificationOrigin:(NSString *)cfOrigin.get() permission:FALSE];
  913. }
  914. void TestRunner::removeAllWebNotificationPermissions()
  915. {
  916. [[MockWebNotificationProvider shared] removeAllWebNotificationPermissions];
  917. }
  918. void TestRunner::simulateWebNotificationClick(JSValueRef jsNotification)
  919. {
  920. uint64_t notificationID = [[mainFrame webView] _notificationIDForTesting:jsNotification];
  921. m_hasPendingWebNotificationClick = true;
  922. dispatch_async(dispatch_get_main_queue(), ^{
  923. if (!m_hasPendingWebNotificationClick)
  924. return;
  925. [[MockWebNotificationProvider shared] simulateWebNotificationClick:notificationID];
  926. m_hasPendingWebNotificationClick = false;
  927. });
  928. }
  929. void TestRunner::simulateLegacyWebNotificationClick(JSStringRef jsTitle)
  930. {
  931. }