TestRunner.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright (C) 2010, 2011, 2012, 2013 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. #include "config.h"
  26. #include "TestRunner.h"
  27. #include "InjectedBundle.h"
  28. #include "InjectedBundlePage.h"
  29. #include "JSTestRunner.h"
  30. #include "PlatformWebView.h"
  31. #include "StringFunctions.h"
  32. #include "TestController.h"
  33. #include <WebKit2/WKBundle.h>
  34. #include <WebKit2/WKBundleBackForwardList.h>
  35. #include <WebKit2/WKBundleFrame.h>
  36. #include <WebKit2/WKBundleFramePrivate.h>
  37. #include <WebKit2/WKBundleInspector.h>
  38. #include <WebKit2/WKBundleNodeHandlePrivate.h>
  39. #include <WebKit2/WKBundlePage.h>
  40. #include <WebKit2/WKBundlePagePrivate.h>
  41. #include <WebKit2/WKBundlePrivate.h>
  42. #include <WebKit2/WKBundleScriptWorld.h>
  43. #include <WebKit2/WKData.h>
  44. #include <WebKit2/WKRetainPtr.h>
  45. #include <WebKit2/WKSerializedScriptValue.h>
  46. #include <WebKit2/WebKit2_C.h>
  47. #include <wtf/CurrentTime.h>
  48. #include <wtf/HashMap.h>
  49. #include <wtf/OwnArrayPtr.h>
  50. #include <wtf/PassOwnArrayPtr.h>
  51. #include <wtf/text/CString.h>
  52. #include <wtf/text/StringBuilder.h>
  53. namespace WTR {
  54. const double TestRunner::waitToDumpWatchdogTimerInterval = 30;
  55. PassRefPtr<TestRunner> TestRunner::create()
  56. {
  57. return adoptRef(new TestRunner);
  58. }
  59. TestRunner::TestRunner()
  60. : m_whatToDump(RenderTree)
  61. , m_shouldDumpAllFrameScrollPositions(false)
  62. , m_shouldDumpBackForwardListsForAllWindows(false)
  63. , m_shouldAllowEditing(true)
  64. , m_shouldCloseExtraWindows(false)
  65. , m_dumpEditingCallbacks(false)
  66. , m_dumpStatusCallbacks(false)
  67. , m_dumpTitleChanges(false)
  68. , m_dumpPixels(true)
  69. , m_dumpSelectionRect(false)
  70. , m_dumpFullScreenCallbacks(false)
  71. , m_dumpFrameLoadCallbacks(false)
  72. , m_dumpProgressFinishedCallback(false)
  73. , m_dumpResourceLoadCallbacks(false)
  74. , m_dumpResourceResponseMIMETypes(false)
  75. , m_dumpWillCacheResponse(false)
  76. , m_dumpApplicationCacheDelegateCallbacks(false)
  77. , m_dumpDatabaseCallbacks(false)
  78. , m_disallowIncreaseForApplicationCacheQuota(false)
  79. , m_waitToDump(false)
  80. , m_testRepaint(false)
  81. , m_testRepaintSweepHorizontally(false)
  82. , m_isPrinting(false)
  83. , m_willSendRequestReturnsNull(false)
  84. , m_willSendRequestReturnsNullOnRedirect(false)
  85. , m_shouldStopProvisionalFrameLoads(false)
  86. , m_policyDelegateEnabled(false)
  87. , m_policyDelegatePermissive(false)
  88. , m_globalFlag(false)
  89. , m_customFullScreenBehavior(false)
  90. , m_userStyleSheetEnabled(false)
  91. , m_userStyleSheetLocation(adoptWK(WKStringCreateWithUTF8CString("")))
  92. {
  93. platformInitialize();
  94. }
  95. TestRunner::~TestRunner()
  96. {
  97. }
  98. JSClassRef TestRunner::wrapperClass()
  99. {
  100. return JSTestRunner::testRunnerClass();
  101. }
  102. void TestRunner::display()
  103. {
  104. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  105. WKBundlePageForceRepaint(page);
  106. WKBundlePageSetTracksRepaints(page, true);
  107. WKBundlePageResetTrackedRepaints(page);
  108. }
  109. void TestRunner::dumpAsText(bool dumpPixels)
  110. {
  111. if (m_whatToDump < MainFrameText)
  112. m_whatToDump = MainFrameText;
  113. m_dumpPixels = dumpPixels;
  114. }
  115. void TestRunner::setCustomPolicyDelegate(bool enabled, bool permissive)
  116. {
  117. m_policyDelegateEnabled = enabled;
  118. m_policyDelegatePermissive = permissive;
  119. InjectedBundle::shared().setCustomPolicyDelegate(enabled, permissive);
  120. }
  121. void TestRunner::waitForPolicyDelegate()
  122. {
  123. setCustomPolicyDelegate(true);
  124. waitUntilDone();
  125. }
  126. void TestRunner::waitUntilDone()
  127. {
  128. m_waitToDump = true;
  129. if (InjectedBundle::shared().useWaitToDumpWatchdogTimer())
  130. initializeWaitToDumpWatchdogTimerIfNeeded();
  131. }
  132. void TestRunner::waitToDumpWatchdogTimerFired()
  133. {
  134. invalidateWaitToDumpWatchdogTimer();
  135. InjectedBundle::shared().outputText("FAIL: Timed out waiting for notifyDone to be called\n\n");
  136. InjectedBundle::shared().done();
  137. }
  138. void TestRunner::notifyDone()
  139. {
  140. if (!InjectedBundle::shared().isTestRunning())
  141. return;
  142. if (m_waitToDump && !InjectedBundle::shared().topLoadingFrame())
  143. InjectedBundle::shared().page()->dump();
  144. m_waitToDump = false;
  145. }
  146. void TestRunner::setCustomTimeout(int timeout)
  147. {
  148. m_timeout = timeout;
  149. }
  150. void TestRunner::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
  151. {
  152. WKRetainPtr<WKStringRef> sourceWK = toWK(source);
  153. WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
  154. WKBundleAddUserScript(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
  155. (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
  156. (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
  157. }
  158. void TestRunner::addUserStyleSheet(JSStringRef source, bool allFrames)
  159. {
  160. WKRetainPtr<WKStringRef> sourceWK = toWK(source);
  161. WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
  162. WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
  163. (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
  164. }
  165. void TestRunner::keepWebHistory()
  166. {
  167. WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
  168. }
  169. void TestRunner::execCommand(JSStringRef name, JSStringRef argument)
  170. {
  171. WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
  172. }
  173. bool TestRunner::findString(JSStringRef target, JSValueRef optionsArrayAsValue)
  174. {
  175. WKFindOptions options = 0;
  176. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  177. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  178. JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length"));
  179. JSObjectRef optionsArray = JSValueToObject(context, optionsArrayAsValue, 0);
  180. JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0);
  181. if (!JSValueIsNumber(context, lengthValue))
  182. return false;
  183. size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0));
  184. for (size_t i = 0; i < length; ++i) {
  185. JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0);
  186. if (!JSValueIsString(context, value))
  187. continue;
  188. JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0));
  189. if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive"))
  190. options |= kWKFindOptionsCaseInsensitive;
  191. else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts"))
  192. options |= kWKFindOptionsAtWordStarts;
  193. else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart"))
  194. options |= kWKFindOptionsTreatMedialCapitalAsWordStart;
  195. else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards"))
  196. options |= kWKFindOptionsBackwards;
  197. else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround"))
  198. options |= kWKFindOptionsWrapAround;
  199. else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection")) {
  200. // FIXME: No kWKFindOptionsStartInSelection.
  201. }
  202. }
  203. return WKBundlePageFindString(InjectedBundle::shared().page()->page(), toWK(target).get(), options);
  204. }
  205. void TestRunner::clearAllDatabases()
  206. {
  207. WKBundleClearAllDatabases(InjectedBundle::shared().bundle());
  208. }
  209. void TestRunner::setDatabaseQuota(uint64_t quota)
  210. {
  211. return WKBundleSetDatabaseQuota(InjectedBundle::shared().bundle(), quota);
  212. }
  213. void TestRunner::clearAllApplicationCaches()
  214. {
  215. WKBundleClearApplicationCache(InjectedBundle::shared().bundle());
  216. }
  217. void TestRunner::clearApplicationCacheForOrigin(JSStringRef origin)
  218. {
  219. WKBundleClearApplicationCacheForOrigin(InjectedBundle::shared().bundle(), toWK(origin).get());
  220. }
  221. void TestRunner::setAppCacheMaximumSize(uint64_t size)
  222. {
  223. WKBundleSetAppCacheMaximumSize(InjectedBundle::shared().bundle(), size);
  224. }
  225. long long TestRunner::applicationCacheDiskUsageForOrigin(JSStringRef origin)
  226. {
  227. return WKBundleGetAppCacheUsageForOrigin(InjectedBundle::shared().bundle(), toWK(origin).get());
  228. }
  229. void TestRunner::setApplicationCacheOriginQuota(unsigned long long bytes)
  230. {
  231. WKRetainPtr<WKStringRef> origin(AdoptWK, WKStringCreateWithUTF8CString("http://127.0.0.1:8000"));
  232. WKBundleSetApplicationCacheOriginQuota(InjectedBundle::shared().bundle(), origin.get(), bytes);
  233. }
  234. void TestRunner::disallowIncreaseForApplicationCacheQuota()
  235. {
  236. m_disallowIncreaseForApplicationCacheQuota = true;
  237. }
  238. static inline JSValueRef stringArrayToJS(JSContextRef context, WKArrayRef strings)
  239. {
  240. const size_t count = WKArrayGetSize(strings);
  241. OwnArrayPtr<JSValueRef> jsStringsArray = adoptArrayPtr(new JSValueRef[count]);
  242. for (size_t i = 0; i < count; ++i) {
  243. WKStringRef stringRef = static_cast<WKStringRef>(WKArrayGetItemAtIndex(strings, i));
  244. JSRetainPtr<JSStringRef> stringJS = toJS(stringRef);
  245. jsStringsArray[i] = JSValueMakeString(context, stringJS.get());
  246. }
  247. return JSObjectMakeArray(context, count, jsStringsArray.get(), 0);
  248. }
  249. JSValueRef TestRunner::originsWithApplicationCache()
  250. {
  251. WKRetainPtr<WKArrayRef> origins(AdoptWK, WKBundleCopyOriginsWithApplicationCache(InjectedBundle::shared().bundle()));
  252. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  253. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  254. return stringArrayToJS(context, origins.get());
  255. }
  256. bool TestRunner::isCommandEnabled(JSStringRef name)
  257. {
  258. return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
  259. }
  260. void TestRunner::setCanOpenWindows(bool)
  261. {
  262. // It's not clear if or why any tests require opening windows be forbidden.
  263. // For now, just ignore this setting, and if we find later it's needed we can add it.
  264. }
  265. void TestRunner::setXSSAuditorEnabled(bool enabled)
  266. {
  267. WKRetainPtr<WKStringRef> key(AdoptWK, WKStringCreateWithUTF8CString("WebKitXSSAuditorEnabled"));
  268. WKBundleOverrideBoolPreferenceForTestRunner(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), key.get(), enabled);
  269. }
  270. void TestRunner::setAllowUniversalAccessFromFileURLs(bool enabled)
  271. {
  272. WKBundleSetAllowUniversalAccessFromFileURLs(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  273. }
  274. void TestRunner::setAllowFileAccessFromFileURLs(bool enabled)
  275. {
  276. WKBundleSetAllowFileAccessFromFileURLs(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  277. }
  278. void TestRunner::setPluginsEnabled(bool enabled)
  279. {
  280. WKBundleSetPluginsEnabled(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  281. }
  282. void TestRunner::setJavaScriptCanAccessClipboard(bool enabled)
  283. {
  284. WKBundleSetJavaScriptCanAccessClipboard(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  285. }
  286. void TestRunner::setPrivateBrowsingEnabled(bool enabled)
  287. {
  288. WKBundleSetPrivateBrowsingEnabled(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  289. }
  290. void TestRunner::setPopupBlockingEnabled(bool enabled)
  291. {
  292. WKBundleSetPopupBlockingEnabled(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  293. }
  294. void TestRunner::setAuthorAndUserStylesEnabled(bool enabled)
  295. {
  296. WKBundleSetAuthorAndUserStylesEnabled(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  297. }
  298. void TestRunner::addOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
  299. {
  300. WKBundleAddOriginAccessWhitelistEntry(InjectedBundle::shared().bundle(), toWK(sourceOrigin).get(), toWK(destinationProtocol).get(), toWK(destinationHost).get(), allowDestinationSubdomains);
  301. }
  302. void TestRunner::removeOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
  303. {
  304. WKBundleRemoveOriginAccessWhitelistEntry(InjectedBundle::shared().bundle(), toWK(sourceOrigin).get(), toWK(destinationProtocol).get(), toWK(destinationHost).get(), allowDestinationSubdomains);
  305. }
  306. bool TestRunner::isPageBoxVisible(int pageIndex)
  307. {
  308. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  309. return WKBundleIsPageBoxVisible(InjectedBundle::shared().bundle(), mainFrame, pageIndex);
  310. }
  311. void TestRunner::setValueForUser(JSContextRef context, JSValueRef element, JSStringRef value)
  312. {
  313. if (!element || !JSValueIsObject(context, element))
  314. return;
  315. WKRetainPtr<WKBundleNodeHandleRef> nodeHandle(AdoptWK, WKBundleNodeHandleCreate(context, const_cast<JSObjectRef>(element)));
  316. WKBundleNodeHandleSetHTMLInputElementValueForUser(nodeHandle.get(), toWK(value).get());
  317. }
  318. void TestRunner::setAudioResult(JSContextRef context, JSValueRef data)
  319. {
  320. WKRetainPtr<WKDataRef> audioData(AdoptWK, WKBundleCreateWKDataFromUInt8Array(InjectedBundle::shared().bundle(), context, data));
  321. InjectedBundle::shared().setAudioResult(audioData.get());
  322. m_whatToDump = Audio;
  323. m_dumpPixels = false;
  324. }
  325. unsigned TestRunner::windowCount()
  326. {
  327. return InjectedBundle::shared().pageCount();
  328. }
  329. void TestRunner::clearBackForwardList()
  330. {
  331. WKBundleBackForwardListClear(WKBundlePageGetBackForwardList(InjectedBundle::shared().page()->page()));
  332. }
  333. // Object Creation
  334. void TestRunner::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
  335. {
  336. setProperty(context, windowObject, "testRunner", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
  337. }
  338. void TestRunner::showWebInspector()
  339. {
  340. #if ENABLE(INSPECTOR)
  341. WKBundleInspectorShow(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()));
  342. #endif // ENABLE(INSPECTOR)
  343. }
  344. void TestRunner::closeWebInspector()
  345. {
  346. #if ENABLE(INSPECTOR)
  347. WKBundleInspectorClose(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()));
  348. #endif // ENABLE(INSPECTOR)
  349. }
  350. void TestRunner::evaluateInWebInspector(long callID, JSStringRef script)
  351. {
  352. #if ENABLE(INSPECTOR)
  353. WKRetainPtr<WKStringRef> scriptWK = toWK(script);
  354. WKBundleInspectorEvaluateScriptForTest(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()), callID, scriptWK.get());
  355. #endif // ENABLE(INSPECTOR)
  356. }
  357. typedef WTF::HashMap<unsigned, WKRetainPtr<WKBundleScriptWorldRef> > WorldMap;
  358. static WorldMap& worldMap()
  359. {
  360. static WorldMap& map = *new WorldMap;
  361. return map;
  362. }
  363. unsigned TestRunner::worldIDForWorld(WKBundleScriptWorldRef world)
  364. {
  365. WorldMap::const_iterator end = worldMap().end();
  366. for (WorldMap::const_iterator it = worldMap().begin(); it != end; ++it) {
  367. if (it->value == world)
  368. return it->key;
  369. }
  370. return 0;
  371. }
  372. void TestRunner::evaluateScriptInIsolatedWorld(JSContextRef context, unsigned worldID, JSStringRef script)
  373. {
  374. // A worldID of 0 always corresponds to a new world. Any other worldID corresponds to a world
  375. // that is created once and cached forever.
  376. WKRetainPtr<WKBundleScriptWorldRef> world;
  377. if (!worldID)
  378. world.adopt(WKBundleScriptWorldCreateWorld());
  379. else {
  380. WKRetainPtr<WKBundleScriptWorldRef>& worldSlot = worldMap().add(worldID, 0).iterator->value;
  381. if (!worldSlot)
  382. worldSlot.adopt(WKBundleScriptWorldCreateWorld());
  383. world = worldSlot;
  384. }
  385. WKBundleFrameRef frame = WKBundleFrameForJavaScriptContext(context);
  386. if (!frame)
  387. frame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  388. JSGlobalContextRef jsContext = WKBundleFrameGetJavaScriptContextForWorld(frame, world.get());
  389. JSEvaluateScript(jsContext, script, 0, 0, 0, 0);
  390. }
  391. void TestRunner::setPOSIXLocale(JSStringRef locale)
  392. {
  393. char localeBuf[32];
  394. JSStringGetUTF8CString(locale, localeBuf, sizeof(localeBuf));
  395. setlocale(LC_ALL, localeBuf);
  396. }
  397. void TestRunner::setTextDirection(JSStringRef direction)
  398. {
  399. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  400. return WKBundleFrameSetTextDirection(mainFrame, toWK(direction).get());
  401. }
  402. void TestRunner::setShouldStayOnPageAfterHandlingBeforeUnload(bool shouldStayOnPage)
  403. {
  404. InjectedBundle::shared().postNewBeforeUnloadReturnValue(!shouldStayOnPage);
  405. }
  406. void TestRunner::setDefersLoading(bool shouldDeferLoading)
  407. {
  408. WKBundlePageSetDefersLoading(InjectedBundle::shared().page()->page(), shouldDeferLoading);
  409. }
  410. void TestRunner::setPageVisibility(JSStringRef state)
  411. {
  412. WKPageVisibilityState visibilityState = kWKPageVisibilityStateVisible;
  413. if (JSStringIsEqualToUTF8CString(state, "hidden"))
  414. visibilityState = kWKPageVisibilityStateHidden;
  415. else if (JSStringIsEqualToUTF8CString(state, "prerender"))
  416. visibilityState = kWKPageVisibilityStatePrerender;
  417. else if (JSStringIsEqualToUTF8CString(state, "unloaded"))
  418. visibilityState = kWKPageVisibilityStateUnloaded;
  419. InjectedBundle::shared().setVisibilityState(visibilityState, false);
  420. }
  421. void TestRunner::resetPageVisibility()
  422. {
  423. InjectedBundle::shared().setVisibilityState(kWKPageVisibilityStateVisible, true);
  424. }
  425. typedef WTF::HashMap<unsigned, JSValueRef> CallbackMap;
  426. static CallbackMap& callbackMap()
  427. {
  428. static CallbackMap& map = *new CallbackMap;
  429. return map;
  430. }
  431. enum {
  432. AddChromeInputFieldCallbackID = 1,
  433. RemoveChromeInputFieldCallbackID,
  434. FocusWebViewCallbackID,
  435. SetBackingScaleFactorCallbackID
  436. };
  437. static void cacheTestRunnerCallback(unsigned index, JSValueRef callback)
  438. {
  439. if (!callback)
  440. return;
  441. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  442. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  443. JSValueProtect(context, callback);
  444. callbackMap().add(index, callback);
  445. }
  446. static void callTestRunnerCallback(unsigned index)
  447. {
  448. if (!callbackMap().contains(index))
  449. return;
  450. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  451. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  452. JSObjectRef callback = JSValueToObject(context, callbackMap().take(index), 0);
  453. JSObjectCallAsFunction(context, callback, JSContextGetGlobalObject(context), 0, 0, 0);
  454. JSValueUnprotect(context, callback);
  455. }
  456. void TestRunner::addChromeInputField(JSValueRef callback)
  457. {
  458. cacheTestRunnerCallback(AddChromeInputFieldCallbackID, callback);
  459. InjectedBundle::shared().postAddChromeInputField();
  460. }
  461. void TestRunner::removeChromeInputField(JSValueRef callback)
  462. {
  463. cacheTestRunnerCallback(RemoveChromeInputFieldCallbackID, callback);
  464. InjectedBundle::shared().postRemoveChromeInputField();
  465. }
  466. void TestRunner::focusWebView(JSValueRef callback)
  467. {
  468. cacheTestRunnerCallback(FocusWebViewCallbackID, callback);
  469. InjectedBundle::shared().postFocusWebView();
  470. }
  471. void TestRunner::setBackingScaleFactor(double backingScaleFactor, JSValueRef callback)
  472. {
  473. cacheTestRunnerCallback(SetBackingScaleFactorCallbackID, callback);
  474. InjectedBundle::shared().postSetBackingScaleFactor(backingScaleFactor);
  475. }
  476. void TestRunner::setWindowIsKey(bool isKey)
  477. {
  478. InjectedBundle::shared().postSetWindowIsKey(isKey);
  479. }
  480. void TestRunner::callAddChromeInputFieldCallback()
  481. {
  482. callTestRunnerCallback(AddChromeInputFieldCallbackID);
  483. }
  484. void TestRunner::callRemoveChromeInputFieldCallback()
  485. {
  486. callTestRunnerCallback(RemoveChromeInputFieldCallbackID);
  487. }
  488. void TestRunner::callFocusWebViewCallback()
  489. {
  490. callTestRunnerCallback(FocusWebViewCallbackID);
  491. }
  492. void TestRunner::callSetBackingScaleFactorCallback()
  493. {
  494. callTestRunnerCallback(SetBackingScaleFactorCallbackID);
  495. }
  496. static inline bool toBool(JSStringRef value)
  497. {
  498. return JSStringIsEqualToUTF8CString(value, "true") || JSStringIsEqualToUTF8CString(value, "1");
  499. }
  500. void TestRunner::overridePreference(JSStringRef preference, JSStringRef value)
  501. {
  502. // FIXME: handle non-boolean preferences.
  503. WKBundleOverrideBoolPreferenceForTestRunner(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), toWK(preference).get(), toBool(value));
  504. }
  505. void TestRunner::setAlwaysAcceptCookies(bool accept)
  506. {
  507. WKBundleSetAlwaysAcceptCookies(InjectedBundle::shared().bundle(), accept);
  508. }
  509. double TestRunner::preciseTime()
  510. {
  511. return currentTime();
  512. }
  513. void TestRunner::setUserStyleSheetEnabled(bool enabled)
  514. {
  515. m_userStyleSheetEnabled = enabled;
  516. WKRetainPtr<WKStringRef> emptyUrl = adoptWK(WKStringCreateWithUTF8CString(""));
  517. WKStringRef location = enabled ? m_userStyleSheetLocation.get() : emptyUrl.get();
  518. WKBundleSetUserStyleSheetLocation(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), location);
  519. }
  520. void TestRunner::setUserStyleSheetLocation(JSStringRef location)
  521. {
  522. m_userStyleSheetLocation = adoptWK(WKStringCreateWithJSString(location));
  523. if (m_userStyleSheetEnabled)
  524. setUserStyleSheetEnabled(true);
  525. }
  526. void TestRunner::setSpatialNavigationEnabled(bool enabled)
  527. {
  528. WKBundleSetSpatialNavigationEnabled(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  529. }
  530. void TestRunner::setTabKeyCyclesThroughElements(bool enabled)
  531. {
  532. WKBundleSetTabKeyCyclesThroughElements(InjectedBundle::shared().bundle(), InjectedBundle::shared().page()->page(), enabled);
  533. }
  534. void TestRunner::setSerializeHTTPLoads()
  535. {
  536. WKBundleSetSerialLoadingEnabled(InjectedBundle::shared().bundle(), true);
  537. }
  538. void TestRunner::dispatchPendingLoadRequests()
  539. {
  540. WKBundleDispatchPendingLoadRequests(InjectedBundle::shared().bundle());
  541. }
  542. void TestRunner::setCacheModel(int model)
  543. {
  544. WKBundleSetCacheModel(InjectedBundle::shared().bundle(), model);
  545. }
  546. void TestRunner::setAsynchronousSpellCheckingEnabled(bool enabled)
  547. {
  548. WKBundleSetAsynchronousSpellCheckingEnabled(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
  549. }
  550. void TestRunner::grantWebNotificationPermission(JSStringRef origin)
  551. {
  552. WKRetainPtr<WKStringRef> originWK = toWK(origin);
  553. WKBundleSetWebNotificationPermission(InjectedBundle::shared().bundle(), InjectedBundle::shared().page()->page(), originWK.get(), true);
  554. }
  555. void TestRunner::denyWebNotificationPermission(JSStringRef origin)
  556. {
  557. WKRetainPtr<WKStringRef> originWK = toWK(origin);
  558. WKBundleSetWebNotificationPermission(InjectedBundle::shared().bundle(), InjectedBundle::shared().page()->page(), originWK.get(), false);
  559. }
  560. void TestRunner::removeAllWebNotificationPermissions()
  561. {
  562. WKBundleRemoveAllWebNotificationPermissions(InjectedBundle::shared().bundle(), InjectedBundle::shared().page()->page());
  563. }
  564. void TestRunner::simulateWebNotificationClick(JSValueRef notification)
  565. {
  566. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  567. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  568. uint64_t notificationID = WKBundleGetWebNotificationID(InjectedBundle::shared().bundle(), context, notification);
  569. InjectedBundle::shared().postSimulateWebNotificationClick(notificationID);
  570. }
  571. void TestRunner::setGeolocationPermission(bool enabled)
  572. {
  573. // FIXME: this should be done by frame.
  574. InjectedBundle::shared().setGeolocationPermission(enabled);
  575. }
  576. void TestRunner::setMockGeolocationPosition(double latitude, double longitude, double accuracy, JSValueRef jsAltitude, JSValueRef jsAltitudeAccuracy, JSValueRef jsHeading, JSValueRef jsSpeed)
  577. {
  578. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  579. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  580. bool providesAltitude = false;
  581. double altitude = 0.;
  582. if (!JSValueIsUndefined(context, jsAltitude)) {
  583. providesAltitude = true;
  584. altitude = JSValueToNumber(context, jsAltitude, 0);
  585. }
  586. bool providesAltitudeAccuracy = false;
  587. double altitudeAccuracy = 0.;
  588. if (!JSValueIsUndefined(context, jsAltitudeAccuracy)) {
  589. providesAltitudeAccuracy = true;
  590. altitudeAccuracy = JSValueToNumber(context, jsAltitudeAccuracy, 0);
  591. }
  592. bool providesHeading = false;
  593. double heading = 0.;
  594. if (!JSValueIsUndefined(context, jsHeading)) {
  595. providesHeading = true;
  596. heading = JSValueToNumber(context, jsHeading, 0);
  597. }
  598. bool providesSpeed = false;
  599. double speed = 0.;
  600. if (!JSValueIsUndefined(context, jsSpeed)) {
  601. providesSpeed = true;
  602. speed = JSValueToNumber(context, jsSpeed, 0);
  603. }
  604. InjectedBundle::shared().setMockGeolocationPosition(latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed);
  605. }
  606. void TestRunner::setMockGeolocationPositionUnavailableError(JSStringRef message)
  607. {
  608. WKRetainPtr<WKStringRef> messageWK = toWK(message);
  609. InjectedBundle::shared().setMockGeolocationPositionUnavailableError(messageWK.get());
  610. }
  611. bool TestRunner::callShouldCloseOnWebView()
  612. {
  613. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
  614. return WKBundleFrameCallShouldCloseOnWebView(mainFrame);
  615. }
  616. void TestRunner::queueBackNavigation(unsigned howFarBackward)
  617. {
  618. InjectedBundle::shared().queueBackNavigation(howFarBackward);
  619. }
  620. void TestRunner::queueForwardNavigation(unsigned howFarForward)
  621. {
  622. InjectedBundle::shared().queueForwardNavigation(howFarForward);
  623. }
  624. void TestRunner::queueLoad(JSStringRef url, JSStringRef target)
  625. {
  626. WKRetainPtr<WKURLRef> baseURLWK(AdoptWK, WKBundleFrameCopyURL(WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page())));
  627. WKRetainPtr<WKURLRef> urlWK(AdoptWK, WKURLCreateWithBaseURL(baseURLWK.get(), toWTFString(toWK(url)).utf8().data()));
  628. WKRetainPtr<WKStringRef> urlStringWK(AdoptWK, WKURLCopyString(urlWK.get()));
  629. InjectedBundle::shared().queueLoad(urlStringWK.get(), toWK(target).get());
  630. }
  631. void TestRunner::queueLoadHTMLString(JSStringRef content, JSStringRef baseURL, JSStringRef unreachableURL)
  632. {
  633. WKRetainPtr<WKStringRef> contentWK = toWK(content);
  634. WKRetainPtr<WKStringRef> baseURLWK = baseURL ? toWK(baseURL) : WKRetainPtr<WKStringRef>();
  635. WKRetainPtr<WKStringRef> unreachableURLWK = unreachableURL ? toWK(unreachableURL) : WKRetainPtr<WKStringRef>();
  636. InjectedBundle::shared().queueLoadHTMLString(contentWK.get(), baseURLWK.get(), unreachableURLWK.get());
  637. }
  638. void TestRunner::queueReload()
  639. {
  640. InjectedBundle::shared().queueReload();
  641. }
  642. void TestRunner::queueLoadingScript(JSStringRef script)
  643. {
  644. WKRetainPtr<WKStringRef> scriptWK = toWK(script);
  645. InjectedBundle::shared().queueLoadingScript(scriptWK.get());
  646. }
  647. void TestRunner::queueNonLoadingScript(JSStringRef script)
  648. {
  649. WKRetainPtr<WKStringRef> scriptWK = toWK(script);
  650. InjectedBundle::shared().queueNonLoadingScript(scriptWK.get());
  651. }
  652. void TestRunner::setHandlesAuthenticationChallenges(bool handlesAuthenticationChallenges)
  653. {
  654. WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetHandlesAuthenticationChallenge"));
  655. WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(handlesAuthenticationChallenges));
  656. WKBundlePostMessage(InjectedBundle::shared().bundle(), messageName.get(), messageBody.get());
  657. }
  658. void TestRunner::setAuthenticationUsername(JSStringRef username)
  659. {
  660. WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetAuthenticationUsername"));
  661. WKRetainPtr<WKStringRef> messageBody(AdoptWK, WKStringCreateWithJSString(username));
  662. WKBundlePostMessage(InjectedBundle::shared().bundle(), messageName.get(), messageBody.get());
  663. }
  664. void TestRunner::setAuthenticationPassword(JSStringRef password)
  665. {
  666. WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetAuthenticationPassword"));
  667. WKRetainPtr<WKStringRef> messageBody(AdoptWK, WKStringCreateWithJSString(password));
  668. WKBundlePostMessage(InjectedBundle::shared().bundle(), messageName.get(), messageBody.get());
  669. }
  670. bool TestRunner::secureEventInputIsEnabled() const
  671. {
  672. WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SecureEventInputIsEnabled"));
  673. WKTypeRef returnData = 0;
  674. WKBundlePostSynchronousMessage(InjectedBundle::shared().bundle(), messageName.get(), 0, &returnData);
  675. return WKBooleanGetValue(static_cast<WKBooleanRef>(returnData));
  676. }
  677. } // namespace WTR