| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/testing/URLTestHelpers.h" | 5 #include "platform/testing/URLTestHelpers.h" |
| 6 #include "public/platform/Platform.h" | 6 #include "public/platform/Platform.h" |
| 7 #include "public/platform/WebURLLoaderClient.h" | 7 #include "public/platform/WebURLLoaderClient.h" |
| 8 #include "public/platform/WebUnitTestSupport.h" | 8 #include "public/platform/WebURLLoaderMockFactory.h" |
| 9 #include "public/web/WebCache.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "web/WebLocalFrameImpl.h" | 11 #include "web/WebLocalFrameImpl.h" |
| 11 #include "web/tests/FrameTestHelpers.h" | 12 #include "web/tests/FrameTestHelpers.h" |
| 12 #include "wtf/TemporaryChange.h" | 13 #include "wtf/TemporaryChange.h" |
| 13 #include <queue> | 14 #include <queue> |
| 14 | 15 |
| 15 namespace blink { | 16 namespace blink { |
| 16 | 17 |
| 17 // TODO(dcheng): Ideally, enough of FrameTestHelpers would be in core/ that | 18 // TODO(dcheng): Ideally, enough of FrameTestHelpers would be in core/ that |
| 18 // placing a test for a core/ class in web/ wouldn't be necessary. | 19 // placing a test for a core/ class in web/ wouldn't be necessary. |
| 19 class DocumentLoaderTest : public ::testing::Test { | 20 class DocumentLoaderTest : public ::testing::Test { |
| 20 protected: | 21 protected: |
| 21 void SetUp() override | 22 void SetUp() override |
| 22 { | 23 { |
| 23 m_webViewHelper.initialize(); | 24 m_webViewHelper.initialize(); |
| 24 URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL( | 25 URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL( |
| 25 "https://example.com/foo.html"), "foo.html"); | 26 "https://example.com/foo.html"), "foo.html"); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void TearDown() override | 29 void TearDown() override |
| 29 { | 30 { |
| 30 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | 31 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
| 32 WebCache::clear(); |
| 31 } | 33 } |
| 32 | 34 |
| 33 WebLocalFrameImpl* mainFrame() | 35 WebLocalFrameImpl* mainFrame() |
| 34 { | 36 { |
| 35 return m_webViewHelper.webViewImpl()->mainFrameImpl(); | 37 return m_webViewHelper.webViewImpl()->mainFrameImpl(); |
| 36 } | 38 } |
| 37 | 39 |
| 38 FrameTestHelpers::WebViewHelper m_webViewHelper; | 40 FrameTestHelpers::WebViewHelper m_webViewHelper; |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 TEST_F(DocumentLoaderTest, SingleChunk) | 43 TEST_F(DocumentLoaderTest, SingleChunk) |
| 42 { | 44 { |
| 43 class TestDelegate : public WebURLLoaderTestDelegate { | 45 class TestDelegate : public WebURLLoaderTestDelegate { |
| 44 public: | 46 public: |
| 45 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override | 47 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override |
| 46 { | 48 { |
| 47 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; | 49 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; |
| 48 originalClient->didReceiveData(loader, data, dataLength, encodedData
Length); | 50 originalClient->didReceiveData(loader, data, dataLength, encodedData
Length); |
| 49 } | 51 } |
| 50 } delegate; | 52 } delegate; |
| 51 | 53 |
| 52 Platform::current()->unitTestSupport()->setLoaderDelegate(&delegate); | 54 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; |
| 53 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 55 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 54 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr); | 56 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 55 | 57 |
| 56 // TODO(dcheng): How should the test verify that the original callback is | 58 // TODO(dcheng): How should the test verify that the original callback is |
| 57 // invoked? The test currently still passes even if the test delegate | 59 // invoked? The test currently still passes even if the test delegate |
| 58 // forgets to invoke the callback. | 60 // forgets to invoke the callback. |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, | 63 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, |
| 62 // with no reentrancy. | 64 // with no reentrancy. |
| 63 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) | 65 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) |
| 64 { | 66 { |
| 65 class TestDelegate : public WebURLLoaderTestDelegate { | 67 class TestDelegate : public WebURLLoaderTestDelegate { |
| 66 public: | 68 public: |
| 67 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override | 69 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override |
| 68 { | 70 { |
| 69 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; | 71 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; |
| 70 // Chunk the reply into one byte chunks. | 72 // Chunk the reply into one byte chunks. |
| 71 for (int i = 0; i < dataLength; ++i) | 73 for (int i = 0; i < dataLength; ++i) |
| 72 originalClient->didReceiveData(loader, &data[i], 1, 1); | 74 originalClient->didReceiveData(loader, &data[i], 1, 1); |
| 73 } | 75 } |
| 74 } delegate; | 76 } delegate; |
| 75 | 77 |
| 76 Platform::current()->unitTestSupport()->setLoaderDelegate(&delegate); | 78 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; |
| 77 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 79 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 78 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr); | 80 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 79 } | 81 } |
| 80 | 82 |
| 81 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). | 83 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). |
| 82 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) | 84 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) |
| 83 { | 85 { |
| 84 // This test delegate chunks the response stage into three distinct stages: | 86 // This test delegate chunks the response stage into three distinct stages: |
| 85 // 1. The first dataReceived() callback, which triggers frame detach due to | 87 // 1. The first dataReceived() callback, which triggers frame detach due to |
| 86 // commiting a provisional load. | 88 // commiting a provisional load. |
| 87 // 2. The middle part of the response, which is dispatched to | 89 // 2. The middle part of the response, which is dispatched to |
| 88 // dataReceived() reentrantly. | 90 // dataReceived() reentrantly. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 std::queue<char> m_data; | 149 std::queue<char> m_data; |
| 148 bool m_dispatchingDidReceiveData; | 150 bool m_dispatchingDidReceiveData; |
| 149 bool m_servedReentrantly; | 151 bool m_servedReentrantly; |
| 150 } delegate; | 152 } delegate; |
| 151 m_webViewHelper.initialize(false, &delegate); | 153 m_webViewHelper.initialize(false, &delegate); |
| 152 | 154 |
| 153 // This doesn't go through the mocked URL load path: it's just intended to | 155 // This doesn't go through the mocked URL load path: it's just intended to |
| 154 // setup a situation where didReceiveData() can be invoked reentrantly. | 156 // setup a situation where didReceiveData() can be invoked reentrantly. |
| 155 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", URLTestHe
lpers::toKURL("about:blank")); | 157 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", URLTestHe
lpers::toKURL("about:blank")); |
| 156 | 158 |
| 157 Platform::current()->unitTestSupport()->setLoaderDelegate(&delegate); | 159 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; |
| 158 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 160 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 159 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr); | 161 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 160 | 162 |
| 161 EXPECT_TRUE(delegate.servedReentrantly()); | 163 EXPECT_TRUE(delegate.servedReentrantly()); |
| 162 | 164 |
| 163 // delegate is a WebFrameClient and stack-allocated, so manually reset() the | 165 // delegate is a WebFrameClient and stack-allocated, so manually reset() the |
| 164 // WebViewHelper here. | 166 // WebViewHelper here. |
| 165 m_webViewHelper.reset(); | 167 m_webViewHelper.reset(); |
| 166 } | 168 } |
| 167 | 169 |
| 168 } // namespace blink | 170 } // namespace blink |
| OLD | NEW |