[Win] Various DumpRenderTree Fixes.
[WebKit-https.git] / Tools / DumpRenderTree / win / FrameLoadDelegate.cpp
index a84e0f3..036d8eb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005, 2006, 2007, 2009 Apple Inc. All rights reserved.
+ * Copyright (C) 2005, 2006, 2007, 2009, 2014 Apple Inc. All rights reserved.
  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -11,7 +11,7 @@
  * 2.  Redistributions in binary form must reproduce the above copyright
  *     notice, this list of conditions and the following disclaimer in the
  *     documentation and/or other materials provided with the distribution. 
- * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
  *     its contributors may be used to endorse or promote products derived
  *     from this software without specific prior written permission. 
  *
 #include "FrameLoadDelegate.h"
 
 #include "AccessibilityController.h"
+#include <comutil.h>
 #include "DumpRenderTree.h"
 #include "EventSender.h"
 #include "GCController.h"
-#include "LayoutTestController.h"
+#include "TestRunner.h"
+#include "TextInputController.h"
+#include "WebCoreTestSupport.h"
 #include "WorkQueueItem.h"
 #include "WorkQueue.h"
 #include <WebCore/COMPtr.h>
-#include <JavaScriptCore/Assertions.h>
 #include <JavaScriptCore/JavaScriptCore.h>
 #include <WebKit/WebKit.h>
-#include <wtf/Vector.h>
 #include <stdio.h>
 #include <string>
+#include <wtf/Assertions.h>
+#include <wtf/NeverDestroyed.h>
+#include <wtf/Vector.h>
 
 using std::string;
 
@@ -59,21 +63,21 @@ string descriptionSuitableForTestResult(IWebFrame* webFrame)
     if (FAILED(webView->mainFrame(&mainFrame)))
         return string();
 
-    BSTR frameNameBSTR;
-    if (FAILED(webFrame->name(&frameNameBSTR)) || toUTF8(frameNameBSTR).empty())
+    _bstr_t frameNameBSTR;
+    if (FAILED(webFrame->name(&frameNameBSTR.GetBSTR())) || !frameNameBSTR.length())
         return (webFrame == mainFrame) ? "main frame" : string();
 
     string frameName = (webFrame == mainFrame) ? "main frame" : "frame";
     frameName += " \"" + toUTF8(frameNameBSTR) + "\""; 
 
-    SysFreeString(frameNameBSTR); 
     return frameName;
 }
 
 FrameLoadDelegate::FrameLoadDelegate()
     : m_refCount(1)
-    , m_gcController(new GCController)
-    , m_accessibilityController(new AccessibilityController)
+    , m_gcController(std::make_unique<GCController>())
+    , m_accessibilityController(std::make_unique<AccessibilityController>())
+    , m_textInputController(std::make_unique<TextInputController>())
 {
 }
 
@@ -81,7 +85,7 @@ FrameLoadDelegate::~FrameLoadDelegate()
 {
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
+HRESULT FrameLoadDelegate::QueryInterface(REFIID riid, void** ppvObject)
 {
     *ppvObject = 0;
     if (IsEqualGUID(riid, IID_IUnknown))
@@ -99,12 +103,12 @@ HRESULT STDMETHODCALLTYPE FrameLoadDelegate::QueryInterface(REFIID riid, void**
     return S_OK;
 }
 
-ULONG STDMETHODCALLTYPE FrameLoadDelegate::AddRef(void)
+ULONG FrameLoadDelegate::AddRef(void)
 {
     return ++m_refCount;
 }
 
-ULONG STDMETHODCALLTYPE FrameLoadDelegate::Release(void)
+ULONG FrameLoadDelegate::Release(void)
 {
     ULONG newRef = --m_refCount;
     if (!newRef)
@@ -114,11 +118,9 @@ ULONG STDMETHODCALLTYPE FrameLoadDelegate::Release(void)
 }
 
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didStartProvisionalLoadForFrame( 
-    /* [in] */ IWebView* webView,
-    /* [in] */ IWebFrame* frame) 
+HRESULT FrameLoadDelegate::didStartProvisionalLoadForFrame(IWebView* /*webView*/, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didStartProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
 
     // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
@@ -129,33 +131,26 @@ HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didStartProvisionalLoadForFrame(
     return S_OK; 
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveServerRedirectForProvisionalLoadForFrame( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didReceiveServerRedirectForProvisionalLoadForFrame(IWebView* /*webView*/, IWebFrame* frame)
 { 
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didReceiveServerRedirectForProvisionalLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
 
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailProvisionalLoadWithError( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ IWebError *error,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didFailProvisionalLoadWithError(IWebView* /*webView*/, IWebError* error, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didFailProvisionalLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
 
     locationChangeDone(error, frame);
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCommitLoadForFrame( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didCommitLoadForFrame(IWebView* webView, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didCommitLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
 
     COMPtr<IWebViewPrivate> webViewPrivate;
@@ -167,24 +162,21 @@ HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCommitLoadForFrame(
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didReceiveTitle( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ BSTR title,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didReceiveTitle(IWebView* /*webView*/, BSTR title, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didReceiveTitle: %S\n", descriptionSuitableForTestResult(frame).c_str(), title);
 
-    if (::gLayoutTestController->dumpTitleChanges() && !done)
-        printf("TITLE CHANGED: %S\n", title ? title : L"");
+    if (::gTestRunner->dumpTitleChanges() && !done)
+        printf("TITLE CHANGED: '%S'\n", title ? title : L"");
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didChangeIcons(
+HRESULT FrameLoadDelegate::didChangeIcons(
     /* [in] */ IWebView* webView,
     /* [in] */ IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpIconChanges())
+    if (!done && gTestRunner->dumpIconChanges())
         printf("%s - didChangeIcons\n", descriptionSuitableForTestResult(frame).c_str());
 
     return S_OK;
@@ -197,7 +189,7 @@ void FrameLoadDelegate::processWork()
         return;
 
     // if we finish all the commands, we're ready to dump state
-    if (WorkQueue::shared()->processWork() && !::gLayoutTestController->waitToDump())
+    if (WorkQueue::shared()->processWork() && !::gTestRunner->waitToDump())
         dump();
 }
 
@@ -206,12 +198,26 @@ void FrameLoadDelegate::resetToConsistentState()
     m_accessibilityController->resetToConsistentState();
 }
 
-static void CALLBACK processWorkTimer(HWND, UINT, UINT_PTR id, DWORD)
+typedef Vector<COMPtr<FrameLoadDelegate> > DelegateVector;
+static DelegateVector& delegatesWithDelayedWork()
 {
-    ::KillTimer(0, id);
-    FrameLoadDelegate* d = g_delegateWaitingOnTimer;
-    g_delegateWaitingOnTimer = 0;
-    d->processWork();
+    static NeverDestroyed<DelegateVector> delegates;
+    return delegates;
+}
+
+static UINT_PTR processWorkTimerID;
+
+static void CALLBACK processWorkTimer(HWND hwnd, UINT, UINT_PTR id, DWORD)
+{
+    ASSERT_ARG(id, id == processWorkTimerID);
+    ::KillTimer(hwnd, id);
+    processWorkTimerID = 0;
+
+    DelegateVector delegates;
+    delegates.swap(delegatesWithDelayedWork());
+
+    for (size_t i = 0; i < delegates.size(); ++i)
+        delegates[i]->processWork();
 }
 
 void FrameLoadDelegate::locationChangeDone(IWebError*, IWebFrame* frame)
@@ -222,70 +228,56 @@ void FrameLoadDelegate::locationChangeDone(IWebError*, IWebFrame* frame)
     topLoadingFrame = 0;
     WorkQueue::shared()->setFrozen(true);
 
-    if (::gLayoutTestController->waitToDump())
+    if (::gTestRunner->waitToDump())
         return;
 
     if (WorkQueue::shared()->count()) {
-        ASSERT(!g_delegateWaitingOnTimer);
-        g_delegateWaitingOnTimer = this;
-        ::SetTimer(0, 0, 0, processWorkTimer);
+        if (!processWorkTimerID)
+            processWorkTimerID = ::SetTimer(0, 0, 0, processWorkTimer);
+        delegatesWithDelayedWork().append(this);
         return;
     }
 
     dump();
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishLoadForFrame( 
-    /* [in] */ IWebView* webView,
-    /* [in] */ IWebFrame* frame)
+HRESULT FrameLoadDelegate::didFinishLoadForFrame(IWebView* /*webView*/, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didFinishLoadForFrame\n", descriptionSuitableForTestResult(frame).c_str());
 
     locationChangeDone(0, frame);
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFailLoadWithError( 
-    /* [in] */ IWebView* webView,
-    /* [in] */ IWebError* error,
-    /* [in] */ IWebFrame* frame)
+HRESULT FrameLoadDelegate::didFailLoadWithError(IWebView* /*webView*/, IWebError* error, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didFailLoadWithError\n", descriptionSuitableForTestResult(frame).c_str());
 
     locationChangeDone(error, frame);
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willPerformClientRedirectToURL( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ BSTR url,  
-    /* [in] */ double delaySeconds,
-    /* [in] */ DATE fireDate,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::willPerformClientRedirectToURL(IWebView* /*webView*/, BSTR url, double /*delaySeconds*/, DATE /*fireDate*/, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - willPerformClientRedirectToURL: %S \n", descriptionSuitableForTestResult(frame).c_str(),
                 urlSuitableForTestResult(std::wstring(url, ::SysStringLen(url))).c_str());
 
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didCancelClientRedirectForFrame( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didCancelClientRedirectForFrame(IWebView* /*webView*/, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didCancelClientRedirectForFrame\n", descriptionSuitableForTestResult(frame).c_str());
 
     return S_OK;
 }
 
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::willCloseFrame( 
-    /* [in] */ IWebView *webView,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::willCloseFrame(IWebView* /*webView*/, IWebFrame* /*frame*/)
 {
     return E_NOTIMPL;
 }
@@ -342,7 +334,7 @@ void FrameLoadDelegate::didClearWindowObjectForFrameInStandardWorld(IWebFrame* f
 
     JSValueRef exception = 0;
 
-    ::gLayoutTestController->makeWindowObject(context, windowObject, &exception);
+    ::gTestRunner->makeWindowObject(context, windowObject, &exception);
     ASSERT(!exception);
 
     m_gcController->makeWindowObject(context, windowObject, &exception);
@@ -351,17 +343,20 @@ void FrameLoadDelegate::didClearWindowObjectForFrameInStandardWorld(IWebFrame* f
     m_accessibilityController->makeWindowObject(context, windowObject, &exception);
     ASSERT(!exception);
 
+    m_textInputController->makeWindowObject(context, windowObject, &exception);
+    ASSERT(!exception);
+
     JSStringRef eventSenderStr = JSStringCreateWithUTF8CString("eventSender");
     JSValueRef eventSender = makeEventSender(context, !parentFrame);
     JSObjectSetProperty(context, windowObject, eventSenderStr, eventSender, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
     JSStringRelease(eventSenderStr);
+
+    WebCoreTestSupport::injectInternalsObject(context);
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishDocumentLoadForFrame( 
-    /* [in] */ IWebView *sender,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didFinishDocumentLoadForFrame(IWebView* /*sender*/, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didFinishDocumentLoadForFrame\n",
                 descriptionSuitableForTestResult(frame).c_str());
     if (!done) {
@@ -381,40 +376,32 @@ HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFinishDocumentLoadForFrame(
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didHandleOnloadEventsForFrame( 
-    /* [in] */ IWebView *sender,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didHandleOnloadEventsForFrame(IWebView* /*sender*/, IWebFrame* frame)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("%s - didHandleOnloadEventsForFrame\n",
                 descriptionSuitableForTestResult(frame).c_str());
 
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didFirstVisuallyNonEmptyLayoutInFrame( 
-    /* [in] */ IWebView *sender,
-    /* [in] */ IWebFrame *frame)
+HRESULT FrameLoadDelegate::didFirstVisuallyNonEmptyLayoutInFrame(IWebView* /*sender*/, IWebFrame* /*frame*/)
 {
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didDisplayInsecureContent( 
-    /* [in] */ IWebView *sender)
+HRESULT FrameLoadDelegate::didDisplayInsecureContent(IWebView* /*sender*/)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("didDisplayInsecureContent\n");
 
     return S_OK;
 }
 
-HRESULT STDMETHODCALLTYPE FrameLoadDelegate::didRunInsecureContent( 
-    /* [in] */ IWebView *sender,
-    /* [in] */ IWebSecurityOrigin *origin)
+HRESULT FrameLoadDelegate::didRunInsecureContent(IWebView* /*sender*/, IWebSecurityOrigin* /*origin*/)
 {
-    if (!done && gLayoutTestController->dumpFrameLoadCallbacks())
+    if (!done && gTestRunner->dumpFrameLoadCallbacks())
         printf("didRunInsecureContent\n");
 
     return S_OK;
 }
-