Stop using testRunner.setPrivateBrowsingEnabled_DEPRECATED() in http/tests/cookies...
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 11 Sep 2019 00:35:19 +0000 (00:35 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 11 Sep 2019 00:35:19 +0000 (00:35 +0000)
https://bugs.webkit.org/show_bug.cgi?id=201648

Reviewed by Alex Christensen.

Use testRunner.setShouldSwapToEphemeralSessionOnNextNavigation() / setShouldSwapToDefaultSessionOnNextNavigation() to
truly switch session on WebKit2 instead of using the testRunner.setPrivateBrowsingEnabled_DEPRECATED() hack.

* http/tests/cookies/private-cookie-storage-expected.txt:
* http/tests/cookies/private-cookie-storage.html:
* http/tests/cookies/resources/cookies-test-pre.js:
(testCookies):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@249746 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/cookies/private-cookie-storage-expected.txt
LayoutTests/http/tests/cookies/private-cookie-storage.html
LayoutTests/http/tests/cookies/resources/cookies-test-pre.js
Tools/DumpRenderTree/PlatformWin.cmake
Tools/DumpRenderTree/TestRunner.cpp
Tools/DumpRenderTree/win/DefaultPolicyDelegate.cpp [new file with mode: 0644]
Tools/DumpRenderTree/win/DefaultPolicyDelegate.h [new file with mode: 0644]
Tools/DumpRenderTree/win/DumpRenderTree.cpp

index 2b53a1a..8ed2445 100644 (file)
@@ -1,3 +1,18 @@
+2019-09-10  Chris Dumez  <cdumez@apple.com>
+
+        Stop using testRunner.setPrivateBrowsingEnabled_DEPRECATED() in http/tests/cookies/private-cookie-storage.html
+        https://bugs.webkit.org/show_bug.cgi?id=201648
+
+        Reviewed by Alex Christensen.
+
+        Use testRunner.setShouldSwapToEphemeralSessionOnNextNavigation() / setShouldSwapToDefaultSessionOnNextNavigation() to
+        truly switch session on WebKit2 instead of using the testRunner.setPrivateBrowsingEnabled_DEPRECATED() hack.
+
+        * http/tests/cookies/private-cookie-storage-expected.txt:
+        * http/tests/cookies/private-cookie-storage.html:
+        * http/tests/cookies/resources/cookies-test-pre.js:
+        (testCookies):
+
 2019-09-10  Ryan Haddad  <ryanhaddad@apple.com>
 
         [iOS] Layout Test imported/w3c/web-platform-tests/service-workers/service-worker/registration-mime-types.https.html is flaky
index b2f6b7c..a4ff8d6 100644 (file)
@@ -3,13 +3,6 @@ This test checks that cookies set from a Set-Cookie header in private browsing m
 On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
 
 
-Cookies before setting (should be empty):
-Cookies before enabling private browsing (should be key1=value1):key1=value1
-PASS cookie is 'key1=value1'.
-Cookies after enabling private browsing (should be empty):
-PASS cookie is ''.
-Cookies after setting in private browsing (should be key2=value2):key2=value2
-PASS cookie is 'key2=value2'.
 Cookies after disabling private browsing (should be key1=value1):key1=value1
 PASS cookie is 'key1=value1'.
 PASS successfullyParsed is true
index 6fb9b36..6896a6d 100644 (file)
@@ -1,26 +1,46 @@
 <script src="resources/cookies-test-pre.js"></script>
 <script>
 description("This test checks that cookies set from a Set-Cookie header in private browsing mode are stored in the same cookie storage as cookies set with document.cookie.");
+jsTestIsAsync = true;
 
-debug("Cookies before setting (should be empty):" + document.cookie);
+function runTest() {
+    if (window.location.search === "?stepTwo") {
+        debug("Cookies after enabling private browsing (should be empty): " + document.cookie);
+        if (!testCookies("")) { // We're using a different cookie storage now.  key1=value1 is still in the default cookie storage.
+            finishJSTest();
+            return;
+        }
+        document.cookie = "key2=value2";
+        debug("Cookies after setting in private browsing (should be key2=value2):" + document.cookie);
+        if (!testCookies("key2=value2")) {
+            finishJSTest();
+            return;
+        }
+        clearCookies();
 
-document.cookie = "key1=value1";
-debug("Cookies before enabling private browsing (should be key1=value1):" + document.cookie);
-testCookies("key1=value1");
+        if (window.testRunner)
+            testRunner.setShouldSwapToDefaultSessionOnNextNavigation(true);
+        window.location = "/cookies/private-cookie-storage.html?stepThree";
+    } else if (window.location.search === "?stepThree") {
+        debug("Cookies after disabling private browsing (should be key1=value1):" + document.cookie);
+        testCookies("key1=value1");
+        finishJSTest();
+    } else {
+        debug("Cookies before setting (should be empty):" + document.cookie);
 
-if (window.testRunner) { testRunner.setPrivateBrowsingEnabled_DEPRECATED(true); }
-
-debug("Cookies after enabling private browsing (should be empty):" + document.cookie);
-testCookies(""); // We're using a different cookie storage now.  key1=value1 is still in the default cookie storage.
-document.cookie = "key2=value2";
-debug("Cookies after setting in private browsing (should be key2=value2):" + document.cookie);
-testCookies("key2=value2");
-clearCookies();
-
-if (window.testRunner) { testRunner.setPrivateBrowsingEnabled_DEPRECATED(false); }
-
-debug("Cookies after disabling private browsing (should be key1=value1):" + document.cookie);
-testCookies("key1=value1");
+        document.cookie = "key1=value1";
+        debug("Cookies before enabling private browsing (should be key1=value1):" + document.cookie);
+        if (!testCookies("key1=value1")) {
+            finishJSTest();
+            return;
+        }
+    
+        if (window.testRunner)
+            testRunner.setShouldSwapToEphemeralSessionOnNextNavigation(true);
+        window.location = "/cookies/private-cookie-storage.html?stepTwo";
+    }
+}
 
+onload = runTest;
 </script>
 <script src="resources/cookies-test-post.js"></script>
index 851609d..86037c8 100644 (file)
@@ -47,10 +47,13 @@ function testCookies(result)
     result = normalizeCookie(result);
     cookie = normalizeCookie(cookie);
     
-    if (cookie === result)
+    if (cookie === result) {
         testPassed("cookie is '" + cookie + "'.");
-    else
+        return true;
+    } else {
         testFailed("cookie was '" + cookie + "'. Expected '" + result + "'.");
+        return false;
+    }
 }
 
 function clearAllCookies()
index db4f6d3..ee66e7f 100644 (file)
@@ -6,6 +6,7 @@ set(DumpRenderTreeLib_SOURCES
     win/DRTDataObject.cpp
     win/DRTDesktopNotificationPresenter.cpp
     win/DRTDropSource.cpp
+    win/DefaultPolicyDelegate.cpp
     win/DumpRenderTree.cpp
     win/EditingDelegate.cpp
     win/EventSender.cpp
index c8317d2..8fb9393 100644 (file)
@@ -2567,5 +2567,7 @@ void TestRunner::willNavigate()
     if (m_shouldSwapToEphemeralSessionOnNextNavigation || m_shouldSwapToDefaultSessionOnNextNavigation) {
         ASSERT(m_shouldSwapToEphemeralSessionOnNextNavigation != m_shouldSwapToDefaultSessionOnNextNavigation);
         setPrivateBrowsingEnabled(m_shouldSwapToEphemeralSessionOnNextNavigation);
+        m_shouldSwapToEphemeralSessionOnNextNavigation = false;
+        m_shouldSwapToDefaultSessionOnNextNavigation = false;
     }
 }
diff --git a/Tools/DumpRenderTree/win/DefaultPolicyDelegate.cpp b/Tools/DumpRenderTree/win/DefaultPolicyDelegate.cpp
new file mode 100644 (file)
index 0000000..29c3d37
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * Copyright (C) 2007-2019 Apple Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#include "DefaultPolicyDelegate.h"
+
+#include "DumpRenderTree.h"
+#include "TestRunner.h"
+#include <comutil.h>
+#include <WebCore/BString.h>
+#include <WebCore/COMPtr.h>
+#include <wtf/text/WTFString.h>
+
+using namespace WebCore;
+
+// FIXME: move this enum to a separate header file when other code begins to use it.
+typedef enum WebExtraNavigationType {
+    WebNavigationTypePlugInRequest = WebNavigationTypeOther + 1
+} WebExtraNavigationType;
+
+// DefaultPolicyDelegate ----------------------------------------------------------------
+
+DefaultPolicyDelegate::DefaultPolicyDelegate()
+{
+}
+
+DefaultPolicyDelegate::~DefaultPolicyDelegate()
+{
+}
+
+DefaultPolicyDelegate* DefaultPolicyDelegate::sharedInstance()
+{
+    static COMPtr<DefaultPolicyDelegate> shared;
+    if (!shared)
+        shared.adoptRef(DefaultPolicyDelegate::createInstance());
+    return shared.get();
+}
+
+DefaultPolicyDelegate* DefaultPolicyDelegate::createInstance()
+{
+    DefaultPolicyDelegate* instance = new DefaultPolicyDelegate();
+    instance->AddRef();
+    return instance;
+}
+
+// IUnknown -------------------------------------------------------------------
+
+HRESULT DefaultPolicyDelegate::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject)
+{
+    if (!ppvObject)
+        return E_POINTER;
+    *ppvObject = nullptr;
+    if (IsEqualGUID(riid, IID_IUnknown))
+        *ppvObject = static_cast<IUnknown*>(this);
+    else if (IsEqualGUID(riid, IID_IWebPolicyDelegate))
+        *ppvObject = static_cast<IWebPolicyDelegate*>(this);
+    else
+        return E_NOINTERFACE;
+
+    AddRef();
+    return S_OK;
+}
+
+ULONG DefaultPolicyDelegate::AddRef()
+{
+    return ++m_refCount;
+}
+
+ULONG DefaultPolicyDelegate::Release()
+{
+    ULONG newRef = --m_refCount;
+    if (!newRef)
+        delete(this);
+
+    return newRef;
+}
+
+HRESULT DefaultPolicyDelegate::decidePolicyForNavigationAction(_In_opt_ IWebView* webView, _In_opt_ IPropertyBag* actionInformation,
+    _In_opt_ IWebURLRequest* request, _In_opt_ IWebFrame* /*frame*/, _In_opt_ IWebPolicyDecisionListener* listener)
+{
+    int navType = 0;
+    _variant_t var;
+    if (SUCCEEDED(actionInformation->Read(WebActionNavigationTypeKey, &var.GetVARIANT(), nullptr))) {
+        var.ChangeType(VT_I4, nullptr);
+        navType = V_I4(&var);
+    }
+    COMPtr<IWebViewPrivate2> wvPrivate(Query, webView);
+    if (wvPrivate) {
+        BOOL canHandleRequest = FALSE;
+        if (SUCCEEDED(wvPrivate->canHandleRequest(request, &canHandleRequest)) && canHandleRequest) {
+            gTestRunner->willNavigate();
+            listener->use();
+        } else if (navType == WebNavigationTypePlugInRequest) {
+            gTestRunner->willNavigate();
+            listener->use();
+        } else {
+            BString url;
+            // A file URL shouldn't fall through to here, but if it did,
+            // it would be a security risk to open it.
+            if (SUCCEEDED(request->URL(&url)) && !String(url, SysStringLen(url)).startsWith("file:")) {
+                // FIXME: Open the URL not by means of a webframe, but by handing it over to the system and letting it determine how to open that particular URL scheme.  See documentation for [NSWorkspace openURL]
+                ;
+            }
+            listener->ignore();
+        }
+    }
+    return S_OK;
+}
+
+HRESULT DefaultPolicyDelegate::decidePolicyForNewWindowAction(_In_opt_ IWebView* /*webView*/, _In_opt_ IPropertyBag* /*actionInformation*/,
+    _In_opt_ IWebURLRequest* /*request*/, _In_ BSTR /*frameName*/, _In_opt_ IWebPolicyDecisionListener* listener)
+{
+    if (!listener)
+        return E_POINTER;
+
+    listener->use();
+    return S_OK;
+}
+
+HRESULT DefaultPolicyDelegate::decidePolicyForMIMEType(_In_opt_ IWebView* webView, _In_ BSTR type, _In_opt_ IWebURLRequest* request,
+    _In_opt_ IWebFrame* /*frame*/, _In_opt_ IWebPolicyDecisionListener* listener)
+{
+    BOOL canShowMIMEType;
+    if (FAILED(webView->canShowMIMEType(type, &canShowMIMEType)))
+        canShowMIMEType = FALSE;
+
+    BString url;
+    request->URL(&url);
+
+    if (String(url, SysStringLen(url)).startsWith("file:")) {
+        BOOL isDirectory = FALSE;
+        WIN32_FILE_ATTRIBUTE_DATA attrs;
+        if (GetFileAttributesEx(url, GetFileExInfoStandard, &attrs))
+            isDirectory = !!(attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
+
+        if (isDirectory)
+            listener->ignore();
+        else if(canShowMIMEType)
+            listener->use();
+        else
+            listener->ignore();
+    } else if (canShowMIMEType)
+        listener->use();
+    else
+        listener->ignore();
+    return S_OK;
+}
+
+HRESULT DefaultPolicyDelegate::unableToImplementPolicyWithError(_In_opt_ IWebView* /*webView*/, _In_opt_ IWebError* error, _In_opt_ IWebFrame* frame)
+{
+    if (!error || !frame)
+        return E_POINTER;
+
+    BString errorStr;
+    error->localizedDescription(&errorStr);
+
+    BString frameName;
+    frame->name(&frameName);
+
+    LOG_ERROR("called unableToImplementPolicyWithError:%S inFrame:%S", errorStr ? static_cast<BSTR>(errorStr) : TEXT(""), frameName ? static_cast<BSTR>(frameName) : TEXT(""));
+
+    return S_OK;
+}
diff --git a/Tools/DumpRenderTree/win/DefaultPolicyDelegate.h b/Tools/DumpRenderTree/win/DefaultPolicyDelegate.h
new file mode 100644 (file)
index 0000000..9621091
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2007-2019 Apple Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#ifndef DefaultPolicyDelegate_h
+#define DefaultPolicyDelegate_h
+
+#include <WebKitLegacy/WebKit.h>
+
+class DefaultPolicyDelegate final : public IWebPolicyDelegate {
+public:
+    static DefaultPolicyDelegate* sharedInstance();
+    static DefaultPolicyDelegate* createInstance();
+private:
+    DefaultPolicyDelegate();
+    ~DefaultPolicyDelegate();
+public:
+    // IUnknown
+    virtual HRESULT STDMETHODCALLTYPE QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject);
+    virtual ULONG STDMETHODCALLTYPE AddRef();
+    virtual ULONG STDMETHODCALLTYPE Release();
+
+    // IWebPolicyDelegate
+    virtual HRESULT STDMETHODCALLTYPE decidePolicyForNavigationAction(_In_opt_ IWebView*, _In_opt_ IPropertyBag* actionInformation,
+        _In_opt_ IWebURLRequest*, _In_opt_ IWebFrame*, _In_opt_ IWebPolicyDecisionListener*);
+    
+    virtual HRESULT STDMETHODCALLTYPE decidePolicyForNewWindowAction(_In_opt_ IWebView*, _In_opt_ IPropertyBag* actionInformation,
+        _In_opt_ IWebURLRequest*, _In_ BSTR frameName, _In_opt_ IWebPolicyDecisionListener*);
+    
+    virtual HRESULT STDMETHODCALLTYPE decidePolicyForMIMEType(_In_opt_ IWebView*, _In_ BSTR type, _In_opt_ IWebURLRequest*,
+        _In_opt_ IWebFrame*, _In_opt_ IWebPolicyDecisionListener*);
+    
+    virtual HRESULT STDMETHODCALLTYPE unableToImplementPolicyWithError(_In_opt_ IWebView*, _In_opt_ IWebError*, _In_opt_ IWebFrame*);
+
+protected:
+    ULONG m_refCount { 0 };
+};
+
+#endif // DefaultPolicyDelegate_h
index ffdf06b..1d555c4 100644 (file)
@@ -29,6 +29,7 @@
 #include "config.h"
 #include "DumpRenderTree.h"
 
+#include "DefaultPolicyDelegate.h"
 #include "EditingDelegate.h"
 #include "FrameLoadDelegate.h"
 #include "GCController.h"
@@ -995,7 +996,7 @@ static void resetWebViewToConsistentStateBeforeTesting(const TestOptions& option
         webViewPrivate->setTabKeyCyclesThroughElements(TRUE);
     }
 
-    webView->setPolicyDelegate(nullptr);
+    webView->setPolicyDelegate(DefaultPolicyDelegate::sharedInstance());
     policyDelegate->setPermissive(false);
     policyDelegate->setControllerToNotifyDone(nullptr);
     sharedFrameLoadDelegate->resetToConsistentState();