| 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 "core/loader/DocumentLoader.h" | 5 #include "core/loader/DocumentLoader.h" |
| 6 | 6 |
| 7 #include "core/page/Page.h" | 7 #include "core/page/Page.h" |
| 8 #include "platform/testing/URLTestHelpers.h" | 8 #include "platform/testing/URLTestHelpers.h" |
| 9 #include "public/platform/Platform.h" | 9 #include "public/platform/Platform.h" |
| 10 #include "public/platform/WebURLLoaderClient.h" | 10 #include "public/platform/WebURLLoaderClient.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 return m_webViewHelper.webView()->mainFrameImpl(); | 37 return m_webViewHelper.webView()->mainFrameImpl(); |
| 38 } | 38 } |
| 39 | 39 |
| 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 class TestDelegate : public WebURLLoaderTestDelegate { | 44 class TestDelegate : public WebURLLoaderTestDelegate { |
| 45 public: | 45 public: |
| 46 void didReceiveData(WebURLLoaderClient* originalClient, | 46 void didReceiveData(WebURLLoaderClient* originalClient, |
| 47 WebURLLoader* loader, | |
| 48 const char* data, | 47 const char* data, |
| 49 int dataLength, | 48 int dataLength, |
| 50 int encodedDataLength) override { | 49 int encodedDataLength) override { |
| 51 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single chunk"; | 50 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single chunk"; |
| 52 originalClient->didReceiveData(loader, data, dataLength, | 51 originalClient->didReceiveData(data, dataLength, encodedDataLength); |
| 53 encodedDataLength); | |
| 54 } | 52 } |
| 55 } delegate; | 53 } delegate; |
| 56 | 54 |
| 57 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate); | 55 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate); |
| 58 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 56 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 59 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); | 57 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 60 | 58 |
| 61 // TODO(dcheng): How should the test verify that the original callback is | 59 // TODO(dcheng): How should the test verify that the original callback is |
| 62 // invoked? The test currently still passes even if the test delegate | 60 // invoked? The test currently still passes even if the test delegate |
| 63 // forgets to invoke the callback. | 61 // forgets to invoke the callback. |
| 64 } | 62 } |
| 65 | 63 |
| 66 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, | 64 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, |
| 67 // with no reentrancy. | 65 // with no reentrancy. |
| 68 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) { | 66 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) { |
| 69 class TestDelegate : public WebURLLoaderTestDelegate { | 67 class TestDelegate : public WebURLLoaderTestDelegate { |
| 70 public: | 68 public: |
| 71 void didReceiveData(WebURLLoaderClient* originalClient, | 69 void didReceiveData(WebURLLoaderClient* originalClient, |
| 72 WebURLLoader* loader, | |
| 73 const char* data, | 70 const char* data, |
| 74 int dataLength, | 71 int dataLength, |
| 75 int encodedDataLength) override { | 72 int encodedDataLength) override { |
| 76 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single chunk"; | 73 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single chunk"; |
| 77 // Chunk the reply into one byte chunks. | 74 // Chunk the reply into one byte chunks. |
| 78 for (int i = 0; i < dataLength; ++i) | 75 for (int i = 0; i < dataLength; ++i) |
| 79 originalClient->didReceiveData(loader, &data[i], 1, 1); | 76 originalClient->didReceiveData(&data[i], 1, 1); |
| 80 } | 77 } |
| 81 } delegate; | 78 } delegate; |
| 82 | 79 |
| 83 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate); | 80 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(&delegate); |
| 84 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | 81 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); |
| 85 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); | 82 Platform::current()->getURLLoaderMockFactory()->setLoaderDelegate(nullptr); |
| 86 } | 83 } |
| 87 | 84 |
| 88 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). | 85 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). |
| 89 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) { | 86 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) { |
| 90 // This test delegate chunks the response stage into three distinct stages: | 87 // This test delegate chunks the response stage into three distinct stages: |
| 91 // 1. The first dataReceived() callback, which triggers frame detach due to | 88 // 1. The first dataReceived() callback, which triggers frame detach due to |
| 92 // commiting a provisional load. | 89 // commiting a provisional load. |
| 93 // 2. The middle part of the response, which is dispatched to | 90 // 2. The middle part of the response, which is dispatched to |
| 94 // dataReceived() reentrantly. | 91 // dataReceived() reentrantly. |
| 95 // 3. The final chunk, which is dispatched normally at the top-level. | 92 // 3. The final chunk, which is dispatched normally at the top-level. |
| 96 class TestDelegate : public WebURLLoaderTestDelegate, | 93 class TestDelegate : public WebURLLoaderTestDelegate, |
| 97 public FrameTestHelpers::TestWebFrameClient { | 94 public FrameTestHelpers::TestWebFrameClient { |
| 98 public: | 95 public: |
| 99 TestDelegate() | 96 TestDelegate() |
| 100 : m_loaderClient(nullptr), | 97 : m_loaderClient(nullptr), |
| 101 m_loader(nullptr), | |
| 102 m_dispatchingDidReceiveData(false), | 98 m_dispatchingDidReceiveData(false), |
| 103 m_servedReentrantly(false) {} | 99 m_servedReentrantly(false) {} |
| 104 | 100 |
| 105 // WebURLLoaderTestDelegate overrides: | 101 // WebURLLoaderTestDelegate overrides: |
| 106 void didReceiveData(WebURLLoaderClient* originalClient, | 102 void didReceiveData(WebURLLoaderClient* originalClient, |
| 107 WebURLLoader* loader, | |
| 108 const char* data, | 103 const char* data, |
| 109 int dataLength, | 104 int dataLength, |
| 110 int encodedDataLength) override { | 105 int encodedDataLength) override { |
| 111 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single chunk"; | 106 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single chunk"; |
| 112 | 107 |
| 113 m_loaderClient = originalClient; | 108 m_loaderClient = originalClient; |
| 114 m_loader = loader; | |
| 115 for (int i = 0; i < dataLength; ++i) | 109 for (int i = 0; i < dataLength; ++i) |
| 116 m_data.push(data[i]); | 110 m_data.push(data[i]); |
| 117 | 111 |
| 118 { | 112 { |
| 119 // Serve the first byte to the real WebURLLoaderCLient, which | 113 // Serve the first byte to the real WebURLLoaderCLient, which |
| 120 // should trigger frameDetach() due to committing a provisional | 114 // should trigger frameDetach() due to committing a provisional |
| 121 // load. | 115 // load. |
| 122 AutoReset<bool> dispatching(&m_dispatchingDidReceiveData, true); | 116 AutoReset<bool> dispatching(&m_dispatchingDidReceiveData, true); |
| 123 dispatchOneByte(); | 117 dispatchOneByte(); |
| 124 } | 118 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 140 while (m_data.size() > 10) | 134 while (m_data.size() > 10) |
| 141 dispatchOneByte(); | 135 dispatchOneByte(); |
| 142 m_servedReentrantly = true; | 136 m_servedReentrantly = true; |
| 143 } | 137 } |
| 144 TestWebFrameClient::frameDetached(frame, detachType); | 138 TestWebFrameClient::frameDetached(frame, detachType); |
| 145 } | 139 } |
| 146 | 140 |
| 147 void dispatchOneByte() { | 141 void dispatchOneByte() { |
| 148 char c = m_data.front(); | 142 char c = m_data.front(); |
| 149 m_data.pop(); | 143 m_data.pop(); |
| 150 m_loaderClient->didReceiveData(m_loader, &c, 1, 1); | 144 m_loaderClient->didReceiveData(&c, 1, 1); |
| 151 } | 145 } |
| 152 | 146 |
| 153 bool servedReentrantly() const { return m_servedReentrantly; } | 147 bool servedReentrantly() const { return m_servedReentrantly; } |
| 154 | 148 |
| 155 private: | 149 private: |
| 156 WebURLLoaderClient* m_loaderClient; | 150 WebURLLoaderClient* m_loaderClient; |
| 157 WebURLLoader* m_loader; | |
| 158 std::queue<char> m_data; | 151 std::queue<char> m_data; |
| 159 bool m_dispatchingDidReceiveData; | 152 bool m_dispatchingDidReceiveData; |
| 160 bool m_servedReentrantly; | 153 bool m_servedReentrantly; |
| 161 } delegate; | 154 } delegate; |
| 162 m_webViewHelper.initialize(false, &delegate); | 155 m_webViewHelper.initialize(false, &delegate); |
| 163 | 156 |
| 164 // This doesn't go through the mocked URL load path: it's just intended to | 157 // This doesn't go through the mocked URL load path: it's just intended to |
| 165 // setup a situation where didReceiveData() can be invoked reentrantly. | 158 // setup a situation where didReceiveData() can be invoked reentrantly. |
| 166 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", | 159 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", |
| 167 URLTestHelpers::toKURL("about:blank")); | 160 URLTestHelpers::toKURL("about:blank")); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 180 TEST_F(DocumentLoaderTest, isCommittedButEmpty) { | 173 TEST_F(DocumentLoaderTest, isCommittedButEmpty) { |
| 181 WebViewImpl* webViewImpl = | 174 WebViewImpl* webViewImpl = |
| 182 m_webViewHelper.initializeAndLoad("about:blank", true); | 175 m_webViewHelper.initializeAndLoad("about:blank", true); |
| 183 EXPECT_TRUE(toLocalFrame(webViewImpl->page()->mainFrame()) | 176 EXPECT_TRUE(toLocalFrame(webViewImpl->page()->mainFrame()) |
| 184 ->loader() | 177 ->loader() |
| 185 .documentLoader() | 178 .documentLoader() |
| 186 ->isCommittedButEmpty()); | 179 ->isCommittedButEmpty()); |
| 187 } | 180 } |
| 188 | 181 |
| 189 } // namespace blink | 182 } // namespace blink |
| OLD | NEW |