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