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 27 matching lines...) Expand all Loading... |
38 #include "public/platform/WebThread.h" | 38 #include "public/platform/WebThread.h" |
39 #include "public/platform/WebURLRequest.h" | 39 #include "public/platform/WebURLRequest.h" |
40 #include "public/platform/WebURLResponse.h" | 40 #include "public/platform/WebURLResponse.h" |
41 #include "public/platform/WebUnitTestSupport.h" | 41 #include "public/platform/WebUnitTestSupport.h" |
42 #include "public/web/WebFrameWidget.h" | 42 #include "public/web/WebFrameWidget.h" |
43 #include "public/web/WebSettings.h" | 43 #include "public/web/WebSettings.h" |
44 #include "public/web/WebTreeScopeType.h" | 44 #include "public/web/WebTreeScopeType.h" |
45 #include "public/web/WebViewClient.h" | 45 #include "public/web/WebViewClient.h" |
46 #include "web/WebLocalFrameImpl.h" | 46 #include "web/WebLocalFrameImpl.h" |
47 #include "web/WebRemoteFrameImpl.h" | 47 #include "web/WebRemoteFrameImpl.h" |
| 48 #include "wtf/Functional.h" |
48 #include "wtf/StdLibExtras.h" | 49 #include "wtf/StdLibExtras.h" |
49 | 50 |
50 namespace blink { | 51 namespace blink { |
51 namespace FrameTestHelpers { | 52 namespace FrameTestHelpers { |
52 | 53 |
53 namespace { | 54 namespace { |
54 | 55 |
55 // The frame test helpers coordinate frame loads in a carefully choreographed | 56 // The frame test helpers coordinate frame loads in a carefully choreographed |
56 // dance. Since the parser is threaded, simply spinning the run loop once is not | 57 // dance. Since the parser is threaded, simply spinning the run loop once is not |
57 // enough to ensure completion of a load. Instead, the following pattern is | 58 // enough to ensure completion of a load. Instead, the following pattern is |
58 // used to ensure that tests see the final state: | 59 // used to ensure that tests see the final state: |
59 // 1. Post a task to trigger a load (LoadTask/LoadHTMLStringTask/ReloadTask). | 60 // 1. Post a task to trigger a load (bind() with WebFrame::loadRequest/ |
| 61 // runLoadHTMLStringTask/WebFrame::reload). |
60 // 2. Enter the run loop. | 62 // 2. Enter the run loop. |
61 // 3. Posted task triggers the load, and starts pumping pending resource | 63 // 3. Posted task triggers the load, and starts pumping pending resource |
62 // requests using ServeAsyncRequestsTask. | 64 // requests using runServeAsyncRequestsTask(). |
63 // 4. TestWebFrameClient watches for didStartLoading/didStopLoading calls, | 65 // 4. TestWebFrameClient watches for didStartLoading/didStopLoading calls, |
64 // keeping track of how many loads it thinks are in flight. | 66 // keeping track of how many loads it thinks are in flight. |
65 // 5. While ServeAsyncRequestsTask observes TestWebFrameClient to still have | 67 // 5. While runServeAsyncRequestsTask() observes TestWebFrameClient to still |
66 // loads in progress, it posts itself back to the run loop. | 68 // have loads in progress, it posts itself back to the run loop. |
67 // 6. When ServeAsyncRequestsTask notices there are no more loads in progress, | 69 // 6. When runServeAsyncRequestsTask() notices there are no more loads in |
68 // it exits the run loop. | 70 // progress, it exits the run loop. |
69 // 7. At this point, all parsing, resource loads, and layout should be finished. | 71 // 7. At this point, all parsing, resource loads, and layout should be finished. |
70 TestWebFrameClient* testClientForFrame(WebFrame* frame) | 72 TestWebFrameClient* testClientForFrame(WebFrame* frame) |
71 { | 73 { |
72 return static_cast<TestWebFrameClient*>(toWebLocalFrameImpl(frame)->client()
); | 74 return static_cast<TestWebFrameClient*>(toWebLocalFrameImpl(frame)->client()
); |
73 } | 75 } |
74 | 76 |
75 class ServeAsyncRequestsTask : public WebTaskRunner::Task { | 77 void runServeAsyncRequestsTask(TestWebFrameClient* client) |
76 public: | 78 { |
77 explicit ServeAsyncRequestsTask(TestWebFrameClient* client) | 79 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); |
78 : m_client(client) | 80 if (client->isLoading()) |
79 { | 81 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_
HERE, bind(&runServeAsyncRequestsTask, client)); |
80 } | 82 else |
81 | 83 testing::exitRunLoop(); |
82 void run() override | 84 } |
83 { | |
84 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(
); | |
85 if (m_client->isLoading()) | |
86 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_F
ROM_HERE, new ServeAsyncRequestsTask(m_client)); | |
87 else | |
88 testing::exitRunLoop(); | |
89 } | |
90 | |
91 private: | |
92 TestWebFrameClient* const m_client; | |
93 }; | |
94 | 85 |
95 void pumpPendingRequests(WebFrame* frame) | 86 void pumpPendingRequests(WebFrame* frame) |
96 { | 87 { |
97 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, new ServeAsyncRequestsTask(testClientForFrame(frame))); | 88 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, bind(&runServeAsyncRequestsTask, testClientForFrame(frame))); |
98 testing::enterRunLoop(); | 89 testing::enterRunLoop(); |
99 } | 90 } |
100 | 91 |
101 class LoadTask : public WebTaskRunner::Task { | 92 void runLoadHTMLStringTask(WebFrame* frame, const std::string& html, const WebUR
L& baseURL) |
102 public: | 93 { |
103 LoadTask(WebFrame* frame, const WebURLRequest& request) | 94 frame->loadHTMLString(WebData(html.data(), html.size()), baseURL); |
104 : m_frame(frame) | 95 } |
105 , m_request(request) | |
106 { | |
107 } | |
108 | |
109 void run() override | |
110 { | |
111 m_frame->loadRequest(m_request); | |
112 } | |
113 | |
114 private: | |
115 WebFrame* const m_frame; | |
116 const WebURLRequest m_request; | |
117 }; | |
118 | |
119 class LoadHTMLStringTask : public WebTaskRunner::Task { | |
120 public: | |
121 LoadHTMLStringTask(WebFrame* frame, const std::string& html, const WebURL& b
aseURL) | |
122 : m_frame(frame) | |
123 , m_html(html) | |
124 , m_baseURL(baseURL) | |
125 { | |
126 } | |
127 | |
128 void run() override | |
129 { | |
130 m_frame->loadHTMLString(WebData(m_html.data(), m_html.size()), m_baseURL
); | |
131 } | |
132 | |
133 private: | |
134 WebFrame* const m_frame; | |
135 const std::string m_html; | |
136 const WebURL m_baseURL; | |
137 }; | |
138 | |
139 class LoadHistoryItemTask : public WebTaskRunner::Task { | |
140 public: | |
141 LoadHistoryItemTask(WebFrame* frame, const WebHistoryItem& item, WebHistoryL
oadType loadType, WebURLRequest::CachePolicy cachePolicy) | |
142 : m_frame(frame) | |
143 , m_item(item) | |
144 , m_loadType(loadType) | |
145 , m_cachePolicy(cachePolicy) | |
146 { | |
147 } | |
148 | |
149 void run() override | |
150 { | |
151 m_frame->loadHistoryItem(m_item, m_loadType, m_cachePolicy); | |
152 } | |
153 | |
154 private: | |
155 WebFrame* const m_frame; | |
156 const WebHistoryItem m_item; | |
157 const WebHistoryLoadType m_loadType; | |
158 const WebURLRequest::CachePolicy m_cachePolicy; | |
159 }; | |
160 | |
161 class ReloadTask : public WebTaskRunner::Task { | |
162 public: | |
163 ReloadTask(WebFrame* frame, bool ignoreCache) | |
164 : m_frame(frame) | |
165 , m_ignoreCache(ignoreCache) | |
166 { | |
167 } | |
168 | |
169 void run() override | |
170 { | |
171 m_frame->reload(m_ignoreCache); | |
172 } | |
173 | |
174 private: | |
175 WebFrame* const m_frame; | |
176 const bool m_ignoreCache; | |
177 }; | |
178 | 96 |
179 TestWebFrameClient* defaultWebFrameClient() | 97 TestWebFrameClient* defaultWebFrameClient() |
180 { | 98 { |
181 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ()); | 99 DEFINE_STATIC_LOCAL(TestWebFrameClient, client, ()); |
182 return &client; | 100 return &client; |
183 } | 101 } |
184 | 102 |
185 TestWebViewClient* defaultWebViewClient() | 103 TestWebViewClient* defaultWebViewClient() |
186 { | 104 { |
187 DEFINE_STATIC_LOCAL(TestWebViewClient, client, ()); | 105 DEFINE_STATIC_LOCAL(TestWebViewClient, client, ()); |
188 return &client; | 106 return &client; |
189 } | 107 } |
190 | 108 |
191 } // namespace | 109 } // namespace |
192 | 110 |
193 void loadFrame(WebFrame* frame, const std::string& url) | 111 void loadFrame(WebFrame* frame, const std::string& url) |
194 { | 112 { |
195 WebURLRequest urlRequest; | 113 WebURLRequest urlRequest; |
196 urlRequest.initialize(); | 114 urlRequest.initialize(); |
197 urlRequest.setURL(URLTestHelpers::toKURL(url)); | 115 urlRequest.setURL(URLTestHelpers::toKURL(url)); |
198 | 116 |
199 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, new LoadTask(frame, urlRequest)); | 117 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, bind(&WebFrame::loadRequest, frame, urlRequest)); |
200 pumpPendingRequests(frame); | 118 pumpPendingRequests(frame); |
201 } | 119 } |
202 | 120 |
203 void loadHTMLString(WebFrame* frame, const std::string& html, const WebURL& base
URL) | 121 void loadHTMLString(WebFrame* frame, const std::string& html, const WebURL& base
URL) |
204 { | 122 { |
205 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, new LoadHTMLStringTask(frame, html, baseURL)); | 123 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, WTF::bind(&runLoadHTMLStringTask, frame, html, baseURL)); |
206 pumpPendingRequests(frame); | 124 pumpPendingRequests(frame); |
207 } | 125 } |
208 | 126 |
209 void loadHistoryItem(WebFrame* frame, const WebHistoryItem& item, WebHistoryLoad
Type loadType, WebURLRequest::CachePolicy cachePolicy) | 127 void loadHistoryItem(WebFrame* frame, const WebHistoryItem& item, WebHistoryLoad
Type loadType, WebURLRequest::CachePolicy cachePolicy) |
210 { | 128 { |
211 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, new LoadHistoryItemTask(frame, item, loadType, cachePolicy)); | 129 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, bind(&WebFrame::loadHistoryItem, frame, item, loadType, cachePolicy)); |
212 pumpPendingRequests(frame); | 130 pumpPendingRequests(frame); |
213 } | 131 } |
214 | 132 |
215 void reloadFrame(WebFrame* frame) | 133 void reloadFrame(WebFrame* frame) |
216 { | 134 { |
217 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, new ReloadTask(frame, false)); | 135 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, bind(&WebFrame::reload, frame, false)); |
218 pumpPendingRequests(frame); | 136 pumpPendingRequests(frame); |
219 } | 137 } |
220 | 138 |
221 void reloadFrameIgnoringCache(WebFrame* frame) | 139 void reloadFrameIgnoringCache(WebFrame* frame) |
222 { | 140 { |
223 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, new ReloadTask(frame, true)); | 141 Platform::current()->currentThread()->taskRunner()->postTask(BLINK_FROM_HERE
, bind(&WebFrame::reload, frame, true)); |
224 pumpPendingRequests(frame); | 142 pumpPendingRequests(frame); |
225 } | 143 } |
226 | 144 |
227 void pumpPendingRequestsDoNotUse(WebFrame* frame) | 145 void pumpPendingRequestsDoNotUse(WebFrame* frame) |
228 { | 146 { |
229 pumpPendingRequests(frame); | 147 pumpPendingRequests(frame); |
230 } | 148 } |
231 | 149 |
232 WebViewHelper::WebViewHelper(SettingOverrider* settingOverrider) | 150 WebViewHelper::WebViewHelper(SettingOverrider* settingOverrider) |
233 : m_webView(nullptr) | 151 : m_webView(nullptr) |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 } | 283 } |
366 | 284 |
367 void TestWebViewClient::initializeLayerTreeView() | 285 void TestWebViewClient::initializeLayerTreeView() |
368 { | 286 { |
369 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLay
erTreeViewForTesting()); | 287 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLay
erTreeViewForTesting()); |
370 ASSERT(m_layerTreeView); | 288 ASSERT(m_layerTreeView); |
371 } | 289 } |
372 | 290 |
373 } // namespace FrameTestHelpers | 291 } // namespace FrameTestHelpers |
374 } // namespace blink | 292 } // namespace blink |
OLD | NEW |