2011-02-06 Maciej Stachowiak <mjs@apple.com>
[WebKit.git] / Tools / WebKitTestRunner / InjectedBundle / LayoutTestController.cpp
index aa3dbf45348a67562acf9eeb35a6db72ce5574c0..4707ed8b58f99437192e92524649ccff4ca95cc3 100644 (file)
 #include <WebKit2/WKBundleBackForwardList.h>
 #include <WebKit2/WKBundleFrame.h>
 #include <WebKit2/WKBundleFramePrivate.h>
+#include <WebKit2/WKBundleInspector.h>
 #include <WebKit2/WKBundlePagePrivate.h>
 #include <WebKit2/WKBundleScriptWorld.h>
 #include <WebKit2/WKBundlePrivate.h>
 #include <WebKit2/WKRetainPtr.h>
 #include <WebKit2/WebKit2.h>
+#include <wtf/HashMap.h>
 
 namespace WTR {
 
@@ -95,6 +97,7 @@ LayoutTestController::LayoutTestController()
     , m_waitToDump(false)
     , m_testRepaint(false)
     , m_testRepaintSweepHorizontally(false)
+    , m_willSendRequestReturnsNull(false)
 {
     platformInitialize();
 }
@@ -132,8 +135,9 @@ void LayoutTestController::notifyDone()
     if (!InjectedBundle::shared().isTestRunning())
         return;
 
-    if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
+    if (m_waitToDump && !InjectedBundle::shared().topLoadingFrame())
         InjectedBundle::shared().page()->dump();
+
     m_waitToDump = false;
 }
 
@@ -307,4 +311,72 @@ void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef wi
     setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
 }
 
+void LayoutTestController::showWebInspector()
+{
+    WKBundleInspectorShow(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()));
+}
+
+void LayoutTestController::closeWebInspector()
+{
+    WKBundleInspectorClose(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()));
+}
+
+void LayoutTestController::evaluateInWebInspector(long callID, JSStringRef script)
+{
+    WKRetainPtr<WKStringRef> scriptWK = toWK(script);
+    WKBundleInspectorEvaluateScriptForTest(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()), callID, scriptWK.get());
+}
+
+void LayoutTestController::setTimelineProfilingEnabled(bool enabled)
+{
+    WKBundleInspectorSetPageProfilingEnabled(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()), enabled);
+}
+
+typedef WTF::HashMap<unsigned, WKRetainPtr<WKBundleScriptWorldRef> > WorldMap;
+static WorldMap& worldMap()
+{
+    static WorldMap& map = *new WorldMap;
+    return map;
+}
+
+unsigned LayoutTestController::worldIDForWorld(WKBundleScriptWorldRef world)
+{
+    WorldMap::const_iterator end = worldMap().end();
+    for (WorldMap::const_iterator it = worldMap().begin(); it != end; ++it) {
+        if (it->second == world)
+            return it->first;
+    }
+
+    return 0;
+}
+
+void LayoutTestController::evaluateScriptInIsolatedWorld(JSContextRef context, unsigned worldID, JSStringRef script)
+{
+    // A worldID of 0 always corresponds to a new world. Any other worldID corresponds to a world
+    // that is created once and cached forever.
+    WKRetainPtr<WKBundleScriptWorldRef> world;
+    if (!worldID)
+        world.adopt(WKBundleScriptWorldCreateWorld());
+    else {
+        WKRetainPtr<WKBundleScriptWorldRef>& worldSlot = worldMap().add(worldID, 0).first->second;
+        if (!worldSlot)
+            worldSlot.adopt(WKBundleScriptWorldCreateWorld());
+        world = worldSlot;
+    }
+
+    WKBundleFrameRef frame = WKBundleFrameForJavaScriptContext(context);
+    if (!frame)
+        frame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
+
+    JSGlobalContextRef jsContext = WKBundleFrameGetJavaScriptContextForWorld(frame, world.get());
+    JSEvaluateScript(jsContext, script, 0, 0, 0, 0); 
+}
+
+void LayoutTestController::setPOSIXLocale(JSStringRef locale)
+{
+    char localeBuf[32];
+    JSStringGetUTF8CString(locale, localeBuf, sizeof(localeBuf));
+    setlocale(LC_ALL, localeBuf);
+}
+
 } // namespace WTR