| 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/WebURLLoaderMockFactory.h" | 8 #include "public/platform/WebURLLoaderMockFactory.h" |
| 9 #include "public/web/WebCache.h" | 9 #include "public/web/WebCache.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 FrameTestHelpers::WebViewHelper m_webViewHelper; | 40 FrameTestHelpers::WebViewHelper m_webViewHelper; |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 TEST_F(DocumentLoaderTest, SingleChunk) | 43 TEST_F(DocumentLoaderTest, SingleChunk) |
| 44 { | 44 { |
| 45 class TestDelegate : public WebURLLoaderTestDelegate { | 45 class TestDelegate : public WebURLLoaderTestDelegate { |
| 46 public: | 46 public: |
| 47 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 |
| 48 { | 48 { |
| 49 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"; |
| 50 originalClient->didReceiveData(loader, data, dataLength, encodedData
Length); | 50 originalClient->didReceiveData(loader, data, dataLength, encodedData
Length, dataLength); |
| 51 } | 51 } |
| 52 } delegate; | 52 } delegate; |
| 53 | 53 |
| 54 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; | 54 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; |
| 55 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 55 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 56 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); | 56 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 57 | 57 |
| 58 // 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 |
| 59 // invoked? The test currently still passes even if the test delegate | 59 // invoked? The test currently still passes even if the test delegate |
| 60 // forgets to invoke the callback. | 60 // forgets to invoke the callback. |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, | 63 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, |
| 64 // with no reentrancy. | 64 // with no reentrancy. |
| 65 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) | 65 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) |
| 66 { | 66 { |
| 67 class TestDelegate : public WebURLLoaderTestDelegate { | 67 class TestDelegate : public WebURLLoaderTestDelegate { |
| 68 public: | 68 public: |
| 69 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 |
| 70 { | 70 { |
| 71 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"; |
| 72 // Chunk the reply into one byte chunks. | 72 // Chunk the reply into one byte chunks. |
| 73 for (int i = 0; i < dataLength; ++i) | 73 for (int i = 0; i < dataLength; ++i) |
| 74 originalClient->didReceiveData(loader, &data[i], 1, 1); | 74 originalClient->didReceiveData(loader, &data[i], 1, 1, 1); |
| 75 } | 75 } |
| 76 } delegate; | 76 } delegate; |
| 77 | 77 |
| 78 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; | 78 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; |
| 79 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 79 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 80 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); | 80 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). | 83 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). |
| 84 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) | 84 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 dispatchOneByte(); | 131 dispatchOneByte(); |
| 132 m_servedReentrantly = true; | 132 m_servedReentrantly = true; |
| 133 } | 133 } |
| 134 TestWebFrameClient::frameDetached(frame, detachType); | 134 TestWebFrameClient::frameDetached(frame, detachType); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void dispatchOneByte() | 137 void dispatchOneByte() |
| 138 { | 138 { |
| 139 char c = m_data.front(); | 139 char c = m_data.front(); |
| 140 m_data.pop(); | 140 m_data.pop(); |
| 141 m_loaderClient->didReceiveData(m_loader, &c, 1, 1); | 141 m_loaderClient->didReceiveData(m_loader, &c, 1, 1, 1); |
| 142 } | 142 } |
| 143 | 143 |
| 144 bool servedReentrantly() const { return m_servedReentrantly; } | 144 bool servedReentrantly() const { return m_servedReentrantly; } |
| 145 | 145 |
| 146 private: | 146 private: |
| 147 WebURLLoaderClient* m_loaderClient; | 147 WebURLLoaderClient* m_loaderClient; |
| 148 WebURLLoader* m_loader; | 148 WebURLLoader* m_loader; |
| 149 std::queue<char> m_data; | 149 std::queue<char> m_data; |
| 150 bool m_dispatchingDidReceiveData; | 150 bool m_dispatchingDidReceiveData; |
| 151 bool m_servedReentrantly; | 151 bool m_servedReentrantly; |
| 152 } delegate; | 152 } delegate; |
| 153 m_webViewHelper.initialize(false, &delegate); | 153 m_webViewHelper.initialize(false, &delegate); |
| 154 | 154 |
| 155 // 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 |
| 156 // setup a situation where didReceiveData() can be invoked reentrantly. | 156 // setup a situation where didReceiveData() can be invoked reentrantly. |
| 157 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", URLTestHe
lpers::toKURL("about:blank")); | 157 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", URLTestHe
lpers::toKURL("about:blank")); |
| 158 | 158 |
| 159 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; | 159 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate)
; |
| 160 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 160 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 161 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); | 161 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 162 | 162 |
| 163 EXPECT_TRUE(delegate.servedReentrantly()); | 163 EXPECT_TRUE(delegate.servedReentrantly()); |
| 164 | 164 |
| 165 // delegate is a WebFrameClient and stack-allocated, so manually reset() the | 165 // delegate is a WebFrameClient and stack-allocated, so manually reset() the |
| 166 // WebViewHelper here. | 166 // WebViewHelper here. |
| 167 m_webViewHelper.reset(); | 167 m_webViewHelper.reset(); |
| 168 } | 168 } |
| 169 | 169 |
| 170 } // namespace blink | 170 } // namespace blink |
| OLD | NEW |