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" | |
35 #include "public/platform/WebString.h" | 36 #include "public/platform/WebString.h" |
36 #include "public/platform/WebThread.h" | 37 #include "public/platform/WebThread.h" |
37 #include "public/platform/WebURLRequest.h" | 38 #include "public/platform/WebURLRequest.h" |
38 #include "public/platform/WebURLResponse.h" | 39 #include "public/platform/WebURLResponse.h" |
39 #include "public/platform/WebUnitTestSupport.h" | 40 #include "public/platform/WebUnitTestSupport.h" |
40 #include "public/web/WebSettings.h" | 41 #include "public/web/WebSettings.h" |
41 #include "public/web/WebViewClient.h" | 42 #include "public/web/WebViewClient.h" |
42 #include "web/WebLocalFrameImpl.h" | 43 #include "web/WebLocalFrameImpl.h" |
43 #include "web/tests/URLTestHelpers.h" | 44 #include "web/tests/URLTestHelpers.h" |
44 #include "wtf/StdLibExtras.h" | 45 #include "wtf/StdLibExtras.h" |
45 | 46 |
46 namespace blink { | 47 namespace blink { |
47 namespace FrameTestHelpers { | 48 namespace FrameTestHelpers { |
48 | 49 |
49 namespace { | 50 namespace { |
50 | 51 |
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 | |
51 class QuitTask : public WebThread::Task { | 72 class QuitTask : public WebThread::Task { |
52 public: | 73 public: |
53 void PostThis(WebCore::Timer<QuitTask>*) | 74 void PostThis(WebCore::Timer<QuitTask>*) |
54 { | 75 { |
55 // We don't just quit here because the SharedTimer may be part-way | 76 // We don't just quit here because the SharedTimer may be part-way |
56 // through the current queue of tasks when runPendingTasks was called, | 77 // through the current queue of tasks when runPendingTasks was called, |
57 // and we can't miss the tasks that were behind it. | 78 // and we can't miss the tasks that were behind it. |
58 // Takes ownership of |this|. | 79 // Takes ownership of |this|. |
59 Platform::current()->currentThread()->postTask(this); | 80 Platform::current()->currentThread()->postTask(this); |
60 } | 81 } |
61 | 82 |
62 virtual void run() | 83 virtual void run() |
63 { | 84 { |
64 Platform::current()->currentThread()->exitRunLoop(); | 85 Platform::current()->currentThread()->exitRunLoop(); |
65 } | 86 } |
66 }; | 87 }; |
67 | 88 |
68 WebFrameClient* defaultWebFrameClient() | 89 class ServeAsyncRequestsTask : public WebThread::Task { |
90 public: | |
91 explicit ServeAsyncRequestsTask(TestWebFrameClient* client) : m_client(clien t) | |
abarth-chromium
2014/05/09 03:00:53
: m_client(client) goes on its own line.
dcheng
2014/05/09 06:00:00
Done.
| |
92 { | |
93 } | |
94 | |
95 virtual void run() OVERRIDE | |
96 { | |
97 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests( ); | |
98 if (m_client->isLoading()) | |
99 Platform::current()->currentThread()->postTask(new ServeAsyncRequest sTask(m_client)); | |
100 else | |
101 Platform::current()->currentThread()->exitRunLoop(); | |
102 } | |
103 | |
104 private: | |
105 TestWebFrameClient* const m_client; | |
106 }; | |
107 | |
108 void pumpPendingRequests(WebFrame* frame) | |
109 { | |
110 Platform::current()->currentThread()->postTask(new ServeAsyncRequestsTask(te stClientForFrame(frame))); | |
111 } | |
112 | |
113 class LoadTask : public WebThread::Task { | |
114 public: | |
115 LoadTask(WebFrame* frame, const WebURLRequest& request) | |
116 : m_frame(frame) | |
117 , m_request(request) | |
118 { | |
119 } | |
120 | |
121 virtual void run() OVERRIDE | |
122 { | |
123 m_frame->loadRequest(m_request); | |
124 pumpPendingRequests(m_frame); | |
125 } | |
126 | |
127 private: | |
128 WebFrame* const m_frame; | |
129 const WebURLRequest m_request; | |
130 }; | |
131 | |
132 class LoadHTMLStringTask : public WebThread::Task { | |
133 public: | |
134 LoadHTMLStringTask(WebFrame* frame, const std::string& html, const WebURL& b aseURL) | |
135 : m_frame(frame), m_html(html), m_baseURL(baseURL) | |
136 { | |
137 } | |
138 | |
139 virtual void run() OVERRIDE | |
140 { | |
141 m_frame->loadHTMLString(WebData(m_html.data(), m_html.size()), m_baseURL ); | |
142 pumpPendingRequests(m_frame); | |
143 } | |
144 | |
145 private: | |
146 WebFrame* const m_frame; | |
147 const std::string m_html; | |
148 const WebURL m_baseURL; | |
149 }; | |
150 | |
151 class ReloadTask : public WebThread::Task { | |
152 public: | |
153 ReloadTask(WebFrame* frame, bool ignoreCache) : m_frame(frame), m_ignoreCach e(ignoreCache) | |
abarth-chromium
2014/05/09 03:00:53
This should be three lines.
dcheng
2014/05/09 06:00:00
Done.
| |
154 { | |
155 } | |
156 | |
157 virtual void run() OVERRIDE | |
158 { | |
159 m_frame->reload(m_ignoreCache); | |
160 pumpPendingRequests(m_frame); | |
161 } | |
162 | |
163 private: | |
164 WebFrame* const m_frame; | |
165 const bool m_ignoreCache; | |
166 }; | |
167 | |
168 TestWebFrameClient* defaultWebFrameClient() | |
69 { | 169 { |
70 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ()); | 170 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ()); |
71 return &client; | 171 return &client; |
72 } | 172 } |
73 | 173 |
74 WebViewClient* defaultWebViewClient() | 174 WebViewClient* defaultWebViewClient() |
75 { | 175 { |
76 DEFINE_STATIC_LOCAL(TestWebViewClient, client, ()); | 176 DEFINE_STATIC_LOCAL(TestWebViewClient, client, ()); |
77 return &client; | 177 return &client; |
78 } | 178 } |
79 | 179 |
80 } // namespace | 180 } // namespace |
81 | 181 |
82 void loadFrame(WebFrame* frame, const std::string& url) | 182 void loadFrame(WebFrame* frame, const std::string& url) |
83 { | 183 { |
84 WebURLRequest urlRequest; | 184 WebURLRequest urlRequest; |
85 urlRequest.initialize(); | 185 urlRequest.initialize(); |
86 urlRequest.setURL(URLTestHelpers::toKURL(url)); | 186 urlRequest.setURL(URLTestHelpers::toKURL(url)); |
87 frame->loadRequest(urlRequest); | 187 |
188 Platform::current()->currentThread()->postTask(new LoadTask(frame, urlReques t)); | |
189 Platform::current()->currentThread()->enterRunLoop(); | |
88 } | 190 } |
89 | 191 |
192 void loadHTMLString(WebFrame* frame, const std::string& html, const WebURL& base URL) | |
193 { | |
194 Platform::current()->currentThread()->postTask(new LoadHTMLStringTask(frame, html, baseURL)); | |
195 Platform::current()->currentThread()->enterRunLoop(); | |
196 } | |
197 | |
198 void reloadFrame(WebFrame* frame) | |
199 { | |
200 Platform::current()->currentThread()->postTask(new ReloadTask(frame, false)) ; | |
201 Platform::current()->currentThread()->enterRunLoop(); | |
202 } | |
203 | |
204 void reloadFrameIgnoringCache(WebFrame* frame) | |
205 { | |
206 Platform::current()->currentThread()->postTask(new ReloadTask(frame, true)); | |
207 Platform::current()->currentThread()->enterRunLoop(); | |
208 } | |
209 | |
210 void pumpPendingRequestsDoNotUse(WebFrame* frame) | |
211 { | |
212 pumpPendingRequests(frame); | |
213 } | |
214 | |
215 // FIXME: There's a duplicate implementation in UnitTestHelpers.cpp. Remove one. | |
90 void runPendingTasks() | 216 void runPendingTasks() |
91 { | 217 { |
92 // Pending tasks include Timers that have been scheduled. | 218 // Pending tasks include Timers that have been scheduled. |
93 WebCore::Timer<QuitTask> quitOnTimeout(new QuitTask, &QuitTask::PostThis); | 219 WebCore::Timer<QuitTask> quitOnTimeout(new QuitTask, &QuitTask::PostThis); |
94 quitOnTimeout.startOneShot(0, FROM_HERE); | 220 quitOnTimeout.startOneShot(0, FROM_HERE); |
95 Platform::current()->currentThread()->enterRunLoop(); | 221 Platform::current()->currentThread()->enterRunLoop(); |
96 } | 222 } |
97 | 223 |
98 WebViewHelper::WebViewHelper() | 224 WebViewHelper::WebViewHelper() |
99 : m_webView(0) | 225 : m_webView(0) |
100 { | 226 { |
101 } | 227 } |
102 | 228 |
103 WebViewHelper::~WebViewHelper() | 229 WebViewHelper::~WebViewHelper() |
104 { | 230 { |
105 reset(); | 231 reset(); |
106 } | 232 } |
107 | 233 |
108 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, WebFrameClient* we bFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettin gs*)) | 234 WebViewImpl* WebViewHelper::initialize(bool enableJavascript, TestWebFrameClient * webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSe ttings*)) |
109 { | 235 { |
110 reset(); | 236 reset(); |
111 | 237 |
112 if (!webFrameClient) | 238 if (!webFrameClient) |
113 webFrameClient = defaultWebFrameClient(); | 239 webFrameClient = defaultWebFrameClient(); |
114 if (!webViewClient) | 240 if (!webViewClient) |
115 webViewClient = defaultWebViewClient(); | 241 webViewClient = defaultWebViewClient(); |
116 m_webView = WebViewImpl::create(webViewClient); | 242 m_webView = WebViewImpl::create(webViewClient); |
117 m_webView->settings()->setJavaScriptEnabled(enableJavascript); | 243 m_webView->settings()->setJavaScriptEnabled(enableJavascript); |
118 if (updateSettingsFunc) { | 244 if (updateSettingsFunc) { |
119 updateSettingsFunc(m_webView->settings()); | 245 updateSettingsFunc(m_webView->settings()); |
120 } else { | 246 } else { |
121 m_webView->settings()->setDeviceSupportsMouse(false); | 247 m_webView->settings()->setDeviceSupportsMouse(false); |
122 m_webView->settings()->setForceCompositingMode(true); | 248 m_webView->settings()->setForceCompositingMode(true); |
123 } | 249 } |
124 | 250 |
125 m_webView->setMainFrame(WebLocalFrameImpl::create(webFrameClient)); | 251 m_webView->setMainFrame(WebLocalFrameImpl::create(webFrameClient)); |
126 | 252 |
127 return m_webView; | 253 return m_webView; |
128 } | 254 } |
129 | 255 |
130 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enabl eJavascript, WebFrameClient* webFrameClient, WebViewClient* webViewClient, void (*updateSettingsFunc)(WebSettings*)) | 256 WebViewImpl* WebViewHelper::initializeAndLoad(const std::string& url, bool enabl eJavascript, TestWebFrameClient* webFrameClient, WebViewClient* webViewClient, v oid (*updateSettingsFunc)(WebSettings*)) |
131 { | 257 { |
132 initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFu nc); | 258 initialize(enableJavascript, webFrameClient, webViewClient, updateSettingsFu nc); |
133 | 259 |
134 loadFrame(webView()->mainFrame(), url); | 260 loadFrame(webView()->mainFrame(), url); |
135 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | |
136 | 261 |
137 return webViewImpl(); | 262 return webViewImpl(); |
138 } | 263 } |
139 | 264 |
140 void WebViewHelper::reset() | 265 void WebViewHelper::reset() |
141 { | 266 { |
142 if (m_webView) { | 267 if (m_webView) { |
268 ASSERT(!testClientForFrame(m_webView->mainFrame())->isLoading()); | |
143 m_webView->close(); | 269 m_webView->close(); |
144 m_webView = 0; | 270 m_webView = 0; |
145 } | 271 } |
146 } | 272 } |
147 | 273 |
274 TestWebFrameClient::TestWebFrameClient() : m_loadsInProgress(0) | |
275 { | |
276 } | |
277 | |
148 WebFrame* TestWebFrameClient::createChildFrame(WebLocalFrame* parent, const WebS tring& frameName) | 278 WebFrame* TestWebFrameClient::createChildFrame(WebLocalFrame* parent, const WebS tring& frameName) |
149 { | 279 { |
150 WebFrame* frame = WebLocalFrame::create(this); | 280 WebFrame* frame = WebLocalFrame::create(this); |
151 parent->appendChild(frame); | 281 parent->appendChild(frame); |
152 return frame; | 282 return frame; |
153 } | 283 } |
154 | 284 |
155 void TestWebFrameClient::frameDetached(WebFrame* frame) | 285 void TestWebFrameClient::frameDetached(WebFrame* frame) |
156 { | 286 { |
157 if (frame->parent()) | 287 if (frame->parent()) |
158 frame->parent()->removeChild(frame); | 288 frame->parent()->removeChild(frame); |
159 frame->close(); | 289 frame->close(); |
160 } | 290 } |
161 | 291 |
292 void TestWebFrameClient::didStartLoading(bool) | |
293 { | |
294 ++m_loadsInProgress; | |
295 } | |
296 | |
297 void TestWebFrameClient::didStopLoading() | |
298 { | |
299 ASSERT(m_loadsInProgress > 0); | |
300 --m_loadsInProgress; | |
301 } | |
302 | |
162 void TestWebViewClient::initializeLayerTreeView() | 303 void TestWebViewClient::initializeLayerTreeView() |
163 { | 304 { |
164 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLay erTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest)); | 305 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLay erTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest)); |
165 ASSERT(m_layerTreeView); | 306 ASSERT(m_layerTreeView); |
166 } | 307 } |
167 | 308 |
168 } // namespace FrameTestHelpers | 309 } // namespace FrameTestHelpers |
169 } // namespace blink | 310 } // namespace blink |
OLD | NEW |