OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 | |
7 #include "platform/testing/URLTestHelpers.h" | |
8 #include "public/platform/Platform.h" | |
9 #include "public/platform/WebURLLoaderClient.h" | |
10 #include "public/platform/WebUnitTestSupport.h" | |
11 #include "web/WebLocalFrameImpl.h" | |
12 #include "web/tests/FrameTestHelpers.h" | |
13 #include "wtf/TemporaryChange.h" | |
14 #include <gtest/gtest.h> | |
15 #include <queue> | |
16 #include <vector> | |
17 | |
18 namespace blink { | |
19 | |
20 // TODO(dcheng): Ideally, enough of FrameTestHelpers would be in core/ that | |
21 // placing a test for a core/ class in web/ wouldn't be necessary. | |
22 class DocumentLoaderTest : public ::testing::Test { | |
23 protected: | |
24 void SetUp() override | |
25 { | |
26 m_webViewHelper.initialize(); | |
27 URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL( | |
28 "https://example.com/foo.html"), "foo.html"); | |
29 } | |
30 | |
31 void TearDown() override | |
32 { | |
33 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
34 } | |
35 | |
36 WebLocalFrameImpl* mainFrame() | |
37 { | |
38 return m_webViewHelper.webViewImpl()->mainFrameImpl(); | |
39 } | |
40 | |
41 FrameTestHelpers::WebViewHelper m_webViewHelper; | |
42 }; | |
43 | |
44 TEST_F(DocumentLoaderTest, SingleChunk) | |
45 { | |
46 class TestDelegate : public WebURLLoaderTestDelegate { | |
47 public: | |
48 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override | |
49 { | |
50 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; | |
51 originalClient->didReceiveData(loader, data, dataLength, encodedData
Length); | |
52 } | |
53 } delegate; | |
54 | |
55 Platform::current()->unitTestSupport()->setLoaderDelegate(&delegate); | |
56 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | |
57 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr); | |
58 | |
59 // TODO(dcheng): How should the test verify that the original callback is | |
60 // invoked? The test currently still passes even if the test delegate | |
61 // forgets to invoke the callback. | |
62 } | |
63 | |
64 // Test normal case of DocumentLoader::dataReceived(): data in multiple chunks, | |
65 // with no reentrancy. | |
66 TEST_F(DocumentLoaderTest, MultiChunkNoReentrancy) | |
67 { | |
68 class TestDelegate : public WebURLLoaderTestDelegate { | |
69 public: | |
70 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override | |
71 { | |
72 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; | |
73 // Chunk the reply into one byte chunks. | |
74 for (int i = 0; i < dataLength; ++i) | |
75 originalClient->didReceiveData(loader, &data[i], 1, 1); | |
76 } | |
77 } delegate; | |
78 | |
79 Platform::current()->unitTestSupport()->setLoaderDelegate(&delegate); | |
80 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | |
81 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr); | |
82 } | |
83 | |
84 // Finally, test reentrant callbacks to DocumentLoader::dataReceived(). | |
85 TEST_F(DocumentLoaderTest, MultiChunkWithReentrancy) | |
86 { | |
87 // This test delegate chunks the response stage into three distinct stages: | |
88 // 1. The first dataReceived() callback, which triggers frame detach due to | |
89 // commiting a provisional load. | |
90 // 2. The middle part of the response, which is dispatched to | |
91 // dataReceived() reentrantly. | |
92 // 3. The final chunk, which is dispatched normally at the top-level. | |
93 class TestDelegate | |
94 : public WebURLLoaderTestDelegate, public FrameTestHelpers::TestWebFrame
Client { | |
95 public: | |
96 TestDelegate() : m_loaderClient(nullptr), m_loader(nullptr), m_dispatchi
ngDidReceiveData(false), m_servedReentrantly(false) { } | |
97 | |
98 // WebURLLoaderTestDelegate overrides: | |
99 void didReceiveData(WebURLLoaderClient* originalClient, WebURLLoader* lo
ader, const char* data, int dataLength, int encodedDataLength) override | |
100 { | |
101 EXPECT_EQ(34, dataLength) << "foo.html was not served in a single ch
unk"; | |
102 | |
103 m_loaderClient = originalClient; | |
104 m_loader = loader; | |
105 for (int i = 0; i < dataLength; ++i) | |
106 m_data.push(data[i]); | |
107 | |
108 { | |
109 // Serve the first byte to the real WebURLLoaderCLient, which | |
110 // should trigger frameDetach() due to committing a provisional | |
111 // load. | |
112 TemporaryChange<bool> dispatching(m_dispatchingDidReceiveData, t
rue); | |
113 dispatchOneByte(); | |
114 } | |
115 // Serve the remaining bytes to complete the load. | |
116 EXPECT_FALSE(m_data.empty()); | |
117 while (!m_data.empty()) | |
118 dispatchOneByte(); | |
119 } | |
120 | |
121 // WebFrameClient overrides: | |
122 void frameDetached(WebFrame* frame, DetachType detachType) override | |
123 { | |
124 if (m_dispatchingDidReceiveData) { | |
125 // This should be called by the first didReceiveData() call, sin
ce | |
126 // it should commit the provisional load. | |
127 EXPECT_GT(m_data.size(), 10u); | |
128 // Dispatch dataReceived() callbacks for part of the remaining | |
129 // data, saving the rest to be dispatched at the top-level as | |
130 // normal. | |
131 while (m_data.size() > 10) | |
132 dispatchOneByte(); | |
133 m_servedReentrantly = true; | |
134 } | |
135 TestWebFrameClient::frameDetached(frame, detachType); | |
136 } | |
137 | |
138 void dispatchOneByte() | |
139 { | |
140 char c = m_data.front(); | |
141 m_data.pop(); | |
142 m_loaderClient->didReceiveData(m_loader, &c, 1, 1); | |
143 } | |
144 | |
145 bool servedReentrantly() const { return m_servedReentrantly; } | |
146 | |
147 private: | |
148 WebURLLoaderClient* m_loaderClient; | |
149 WebURLLoader* m_loader; | |
150 std::queue<char> m_data; | |
151 bool m_dispatchingDidReceiveData; | |
152 bool m_servedReentrantly; | |
153 } delegate; | |
154 m_webViewHelper.initialize(false, &delegate); | |
155 | |
156 // This doesn't go through the mocked URL load path: it's just intended to | |
157 // setup a situation where didReceiveData() can be invoked reentrantly. | |
158 FrameTestHelpers::loadHTMLString(mainFrame(), "<iframe></iframe>", URLTestHe
lpers::toKURL("about:blank")); | |
159 | |
160 Platform::current()->unitTestSupport()->setLoaderDelegate(&delegate); | |
161 FrameTestHelpers::loadFrame(mainFrame(), "https://example.com/foo.html"); | |
162 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr); | |
163 | |
164 EXPECT_TRUE(delegate.servedReentrantly()); | |
165 | |
166 // delegate is a WebFrameClient and stack-allocated, so manually reset() the | |
167 // WebViewHelper here. | |
168 m_webViewHelper.reset(); | |
169 } | |
170 | |
171 } // namespace blink | |
OLD | NEW |