OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "web/tests/FrameTestHelpers.h" | 32 #include "web/tests/FrameTestHelpers.h" |
33 | 33 |
34 #include "public/platform/Platform.h" | 34 #include "public/platform/Platform.h" |
35 #include "public/platform/WebData.h" | |
36 #include "public/platform/WebString.h" | 35 #include "public/platform/WebString.h" |
37 #include "public/platform/WebThread.h" | 36 #include "public/platform/WebThread.h" |
38 #include "public/platform/WebURLRequest.h" | 37 #include "public/platform/WebURLRequest.h" |
39 #include "public/platform/WebURLResponse.h" | 38 #include "public/platform/WebURLResponse.h" |
40 #include "public/platform/WebUnitTestSupport.h" | 39 #include "public/platform/WebUnitTestSupport.h" |
41 #include "public/web/WebSettings.h" | 40 #include "public/web/WebSettings.h" |
42 #include "public/web/WebViewClient.h" | 41 #include "public/web/WebViewClient.h" |
43 #include "web/WebLocalFrameImpl.h" | 42 #include "web/WebLocalFrameImpl.h" |
44 #include "web/tests/URLTestHelpers.h" | 43 #include "web/tests/URLTestHelpers.h" |
45 #include "wtf/StdLibExtras.h" | 44 #include "wtf/StdLibExtras.h" |
46 | 45 |
47 namespace blink { | 46 namespace blink { |
48 namespace FrameTestHelpers { | 47 namespace FrameTestHelpers { |
49 | 48 |
50 namespace { | 49 namespace { |
51 | 50 |
52 // The frame test helpers coordinate frame loads in a carefully choreographed | |
53 // dance. Since the parser is threaded, simply spinning the run loop once is not | |
54 // enough to ensure completion of a load. Instead, the following pattern is | |
55 // used to ensure that tests see the final state: | |
56 // 1. Post a task to trigger a load (LoadTask/LoadHTMLStringTask/ReloadTask). | |
57 // 2. Enter the run loop. | |
58 // 3. Posted task triggers the load, and starts pumping pending resource | |
59 // requests using ServeAsyncRequestsTask. | |
60 // 4. TestWebFrameClient watches for didStartLoading/didStopLoading calls, | |
61 // keeping track of how many loads it thinks are in flight. | |
62 // 5. While ServeAsyncRequestsTask observes TestWebFrameClient to still have | |
63 // loads in progress, it posts itself back to the run loop. | |
64 // 6. When ServeAsyncRequestsTask notices there are no more loads in progress, | |
65 // it exits the run loop. | |
66 // 7. At this point, all parsing, resource loads, and layout should be finished. | |
67 TestWebFrameClient* testClientForFrame(WebFrame* frame) | |
68 { | |
69 return static_cast<TestWebFrameClient*>(toWebLocalFrameImpl(frame)->client()
); | |
70 } | |
71 | |
72 class QuitTask : public WebThread::Task { | 51 class QuitTask : public WebThread::Task { |
73 public: | 52 public: |
74 void PostThis(WebCore::Timer<QuitTask>*) | 53 void PostThis(WebCore::Timer<QuitTask>*) |
75 { | 54 { |
76 // We don't just quit here because the SharedTimer may be part-way | 55 // We don't just quit here because the SharedTimer may be part-way |
77 // through the current queue of tasks when runPendingTasks was called, | 56 // through the current queue of tasks when runPendingTasks was called, |
78 // and we can't miss the tasks that were behind it. | 57 // and we can't miss the tasks that were behind it. |
79 // Takes ownership of |this|. | 58 // Takes ownership of |this|. |
80 Platform::current()->currentThread()->postTask(this); | 59 Platform::current()->currentThread()->postTask(this); |
81 } | 60 } |
82 | 61 |
83 virtual void run() | 62 virtual void run() |
84 { | 63 { |
85 Platform::current()->currentThread()->exitRunLoop(); | 64 Platform::current()->currentThread()->exitRunLoop(); |
86 } | 65 } |
87 }; | 66 }; |
88 | 67 |
89 class ServeAsyncRequestsTask : public WebThread::Task { | 68 WebFrameClient* defaultWebFrameClient() |
90 public: | |
91 explicit ServeAsyncRequestsTask(TestWebFrameClient* client) | |
92 : m_client(client) | |
93 { | |
94 } | |
95 | |
96 virtual void run() OVERRIDE | |
97 { | |
98 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(
); | |
99 if (m_client->isLoading()) | |
100 Platform::current()->currentThread()->postTask(new ServeAsyncRequest
sTask(m_client)); | |
101 else | |
102 Platform::current()->currentThread()->exitRunLoop(); | |
103 } | |
104 | |
105 private: | |
106 TestWebFrameClient* const m_client; | |
107 }; | |
108 | |
109 void pumpPendingRequests(WebFrame* frame) | |
110 { | |
111 Platform::current()->currentThread()->postTask(new ServeAsyncRequestsTask(te
stClientForFrame(frame))); | |
112 Platform::current()->currentThread()->enterRunLoop(); | |
113 } | |
114 | |
115 class LoadTask : public WebThread::Task { | |
116 public: | |
117 LoadTask(WebFrame* frame, const WebURLRequest& request) | |
118 : m_frame(frame) | |
119 , m_request(request) | |
120 { | |
121 } | |
122 | |
123 virtual void run() OVERRIDE | |
124 { | |
125 m_frame->loadRequest(m_request); | |
126 } | |
127 | |
128 private: | |
129 WebFrame* const m_frame; | |
130 const WebURLRequest m_request; | |
131 }; | |
132 | |
133 class LoadHTMLStringTask : public WebThread::Task { | |
134 public: | |
135 LoadHTMLStringTask(WebFrame* frame, const std::string& html, const WebURL& b
aseURL) | |
136 : m_frame(frame) | |
137 , m_html(html) | |
138 , m_baseURL(baseURL) | |
139 { | |
140 } | |
141 | |
142 virtual void run() OVERRIDE | |
143 { | |
144 m_frame->loadHTMLString(WebData(m_html.data(), m_html.size()), m_baseURL
); | |
145 } | |
146 | |
147 private: | |
148 WebFrame* const m_frame; | |
149 const std::string m_html; | |
150 const WebURL m_baseURL; | |
151 }; | |
152 | |
153 class ReloadTask : public WebThread::Task { | |
154 public: | |
155 ReloadTask(WebFrame* frame, bool ignoreCache) | |
156 : m_frame(frame) | |
157 , m_ignoreCache(ignoreCache) | |
158 { | |
159 } | |
160 | |
161 virtual void run() OVERRIDE | |
162 { | |
163 m_frame->reload(m_ignoreCache); | |
164 } | |
165 | |
166 private: | |
167 WebFrame* const m_frame; | |
168 const bool m_ignoreCache; | |
169 }; | |
170 | |
171 TestWebFrameClient* defaultWebFrameClient() | |
172 { | 69 { |
173 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ()); | 70 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ()); |
174 return &client; | 71 return &client; |
175 } | 72 } |
176 | 73 |
177 WebViewClient* defaultWebViewClient() | 74 WebViewClient* defaultWebViewClient() |
178 { | 75 { |
179 DEFINE_STATIC_LOCAL(TestWebViewClient, client, ()); | 76 DEFINE_STATIC_LOCAL(TestWebViewClient, client, ()); |
180 return &client; | 77 return &client; |
181 } | 78 } |
182 | 79 |
183 } // namespace | 80 } // namespace |
184 | 81 |
185 void loadFrame(WebFrame* frame, const std::string& url) | 82 void loadFrame(WebFrame* frame, const std::string& url) |
186 { | 83 { |
187 WebURLRequest urlRequest; | 84 WebURLRequest urlRequest; |
188 urlRequest.initialize(); | 85 urlRequest.initialize(); |
189 urlRequest.setURL(URLTestHelpers::toKURL(url)); | 86 urlRequest.setURL(URLTestHelpers::toKURL(url)); |
190 | 87 frame->loadRequest(urlRequest); |
191 Platform::current()->currentThread()->postTask(new LoadTask(frame, urlReques
t)); | |
192 pumpPendingRequests(frame); | |
193 } | 88 } |
194 | 89 |
195 void loadHTMLString(WebFrame* frame, const std::string& html, const WebURL& base
URL) | |
196 { | |
197 Platform::current()->currentThread()->postTask(new LoadHTMLStringTask(frame,
html, baseURL)); | |
198 pumpPendingRequests(frame); | |
199 } | |
200 | |
201 void reloadFrame(WebFrame* frame) | |
202 { | |
203 Platform::current()->currentThread()->postTask(new ReloadTask(frame, false))
; | |
204 pumpPendingRequests(frame); | |
205 } | |
206 | |
207 void reloadFrameIgnoringCache(WebFrame* frame) | |
208 { | |
209 Platform::current()->currentThread()->postTask(new ReloadTask(frame, true)); | |
210 pumpPendingRequests(frame); | |
211 } | |
212 | |
213 void pumpPendingRequestsDoNotUse(WebFrame* frame) | |
214 { | |
215 pumpPendingRequests(frame); | |
216 } | |
217 | |
218 // FIXME: There's a duplicate implementation in UnitTestHelpers.cpp. Remove one. | |
219 void runPendingTasks() | 90 void runPendingTasks() |
220 { | 91 { |
221 // Pending tasks include Timers that have been scheduled. | 92 // Pending tasks include Timers that have been scheduled. |
222 WebCore::Timer<QuitTask> quitOnTimeout(new QuitTask, &QuitTask::PostThis); | 93 WebCore::Timer<QuitTask> quitOnTimeout(new QuitTask, &QuitTask::PostThis); |
223 quitOnTimeout.startOneShot(0, FROM_HERE); | 94 quitOnTimeout.startOneShot(0, FROM_HERE); |
224 Platform::current()->currentThread()->enterRunLoop(); | 95 Platform::current()->currentThread()->enterRunLoop(); |
225 } | 96 } |
226 | 97 |
227 WebViewHelper::WebViewHelper() | 98 WebViewHelper::WebViewHelper() |
228 : m_webView(0) | 99 : m_webView(0) |
229 { | 100 { |
230 } | 101 } |
231 | 102 |
232 WebViewHelper::~WebViewHelper() | 103 WebViewHelper::~WebViewHelper() |
233 { | 104 { |
234 reset(); | 105 reset(); |
235 } | 106 } |
236 | 107 |
237 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, TestWebFrameClient
* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSe
ttings*)) | 108 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, WebFrameClient* we
bFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettin
gs*)) |
238 { | 109 { |
239 reset(); | 110 reset(); |
240 | 111 |
241 if (!webFrameClient) | 112 if (!webFrameClient) |
242 webFrameClient = defaultWebFrameClient(); | 113 webFrameClient = defaultWebFrameClient(); |
243 if (!webViewClient) | 114 if (!webViewClient) |
244 webViewClient = defaultWebViewClient(); | 115 webViewClient = defaultWebViewClient(); |
245 m_webView = WebViewImpl::create(webViewClient); | 116 m_webView = WebViewImpl::create(webViewClient); |
246 m_webView->settings()->setJavaScriptEnabled(enableJavascript); | 117 m_webView->settings()->setJavaScriptEnabled(enableJavascript); |
247 if (updateSettingsFunc) { | 118 if (updateSettingsFunc) { |
248 updateSettingsFunc(m_webView->settings()); | 119 updateSettingsFunc(m_webView->settings()); |
249 } else { | 120 } else { |
250 m_webView->settings()->setDeviceSupportsMouse(false); | 121 m_webView->settings()->setDeviceSupportsMouse(false); |
251 m_webView->settings()->setForceCompositingMode(true); | 122 m_webView->settings()->setForceCompositingMode(true); |
252 } | 123 } |
253 | 124 |
254 m_webView->setMainFrame(WebLocalFrameImpl::create(webFrameClient)); | 125 m_webView->setMainFrame(WebLocalFrameImpl::create(webFrameClient)); |
255 | 126 |
256 return m_webView; | 127 return m_webView; |
257 } | 128 } |
258 | 129 |
259 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enabl
eJavascript, TestWebFrameClient* webFrameClient, WebViewClient* webViewClient, v
oid (*updateSettingsFunc)(WebSettings*)) | 130 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enabl
eJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void
(*updateSettingsFunc)(WebSettings*)) |
260 { | 131 { |
261 initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFu
nc); | 132 initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFu
nc); |
262 | 133 |
263 loadFrame(webView()->mainFrame(), url); | 134 loadFrame(webView()->mainFrame(), url); |
| 135 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); |
264 | 136 |
265 return webViewImpl(); | 137 return webViewImpl(); |
266 } | 138 } |
267 | 139 |
268 void WebViewHelper::reset() | 140 void WebViewHelper::reset() |
269 { | 141 { |
270 if (m_webView) { | 142 if (m_webView) { |
271 ASSERT(!testClientForFrame(m_webView->mainFrame())->isLoading()); | |
272 m_webView->close(); | 143 m_webView->close(); |
273 m_webView = 0; | 144 m_webView = 0; |
274 } | 145 } |
275 } | 146 } |
276 | 147 |
277 TestWebFrameClient::TestWebFrameClient() : m_loadsInProgress(0) | |
278 { | |
279 } | |
280 | |
281 WebFrame* TestWebFrameClient::createChildFrame(WebLocalFrame* parent, const WebS
tring& frameName) | 148 WebFrame* TestWebFrameClient::createChildFrame(WebLocalFrame* parent, const WebS
tring& frameName) |
282 { | 149 { |
283 WebFrame* frame = WebLocalFrame::create(this); | 150 WebFrame* frame = WebLocalFrame::create(this); |
284 parent->appendChild(frame); | 151 parent->appendChild(frame); |
285 return frame; | 152 return frame; |
286 } | 153 } |
287 | 154 |
288 void TestWebFrameClient::frameDetached(WebFrame* frame) | 155 void TestWebFrameClient::frameDetached(WebFrame* frame) |
289 { | 156 { |
290 if (frame->parent()) | 157 if (frame->parent()) |
291 frame->parent()->removeChild(frame); | 158 frame->parent()->removeChild(frame); |
292 frame->close(); | 159 frame->close(); |
293 } | 160 } |
294 | 161 |
295 void TestWebFrameClient::didStartLoading(bool) | |
296 { | |
297 ++m_loadsInProgress; | |
298 } | |
299 | |
300 void TestWebFrameClient::didStopLoading() | |
301 { | |
302 ASSERT(m_loadsInProgress > 0); | |
303 --m_loadsInProgress; | |
304 } | |
305 | |
306 void TestWebViewClient::initializeLayerTreeView() | 162 void TestWebViewClient::initializeLayerTreeView() |
307 { | 163 { |
308 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLay
erTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest)); | 164 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLay
erTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest)); |
309 ASSERT(m_layerTreeView); | 165 ASSERT(m_layerTreeView); |
310 } | 166 } |
311 | 167 |
312 } // namespace FrameTestHelpers | 168 } // namespace FrameTestHelpers |
313 } // namespace blink | 169 } // namespace blink |
OLD | NEW |