ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_tracker_unittest.cc
blob8c692de2025f9b7b212a58248c253164d2972506
1 // Copyright (c) 2012 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.
5 #include <set>
6 #include <utility>
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/net/url_request_mock_util.h"
14 #include "chrome/browser/prerender/prerender_contents.h"
15 #include "chrome/browser/prerender/prerender_manager.h"
16 #include "chrome/browser/prerender/prerender_resource_throttle.h"
17 #include "chrome/browser/prerender/prerender_tracker.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "content/public/browser/resource_controller.h"
20 #include "content/public/browser/resource_request_info.h"
21 #include "content/public/test/test_browser_thread.h"
22 #include "ipc/ipc_message.h"
23 #include "net/base/request_priority.h"
24 #include "net/test/url_request/url_request_mock_http_job.h"
25 #include "net/url_request/redirect_info.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 using content::BrowserThread;
31 using content::ResourceType;
33 namespace prerender {
35 namespace {
37 class TestPrerenderContents : public PrerenderContents {
38 public:
39 TestPrerenderContents(PrerenderManager* prerender_manager,
40 int child_id, int route_id)
41 : PrerenderContents(prerender_manager, static_cast<Profile*>(NULL),
42 GURL(), content::Referrer(), ORIGIN_NONE,
43 PrerenderManager::kNoExperiment),
44 child_id_(child_id),
45 route_id_(route_id) {
46 PrerenderResourceThrottle::OverridePrerenderContentsForTesting(this);
49 ~TestPrerenderContents() override {
50 if (final_status() == FINAL_STATUS_MAX)
51 SetFinalStatus(FINAL_STATUS_USED);
52 PrerenderResourceThrottle::OverridePrerenderContentsForTesting(NULL);
55 bool GetChildId(int* child_id) const override {
56 *child_id = child_id_;
57 return true;
60 bool GetRouteId(int* route_id) const override {
61 *route_id = route_id_;
62 return true;
65 void Start() {
66 prerendering_has_started_ = true;
67 NotifyPrerenderStart();
70 void Cancel() {
71 Destroy(FINAL_STATUS_CANCELLED);
74 void Use() {
75 PrepareForUse();
78 private:
79 int child_id_;
80 int route_id_;
83 class TestPrerenderManager : public PrerenderManager {
84 public:
85 explicit TestPrerenderManager(PrerenderTracker* prerender_tracker) :
86 PrerenderManager(NULL, prerender_tracker) {
87 mutable_config().rate_limit_enabled = false;
90 // We never allocate our PrerenderContents in PrerenderManager, so we don't
91 // ever want the default pending delete behaviour.
92 void MoveEntryToPendingDelete(PrerenderContents* entry,
93 FinalStatus final_status) override {}
96 class DeferredRedirectDelegate : public net::URLRequest::Delegate,
97 public content::ResourceController {
98 public:
99 DeferredRedirectDelegate()
100 : throttle_(NULL),
101 was_deferred_(false),
102 cancel_called_(false),
103 resume_called_(false) {
106 void SetThrottle(PrerenderResourceThrottle* throttle) {
107 throttle_ = throttle;
108 throttle_->set_controller_for_testing(this);
111 void Run() {
112 run_loop_.reset(new base::RunLoop());
113 run_loop_->Run();
116 bool was_deferred() const { return was_deferred_; }
117 bool cancel_called() const { return cancel_called_; }
118 bool resume_called() const { return resume_called_; }
120 // net::URLRequest::Delegate implementation:
121 void OnReceivedRedirect(net::URLRequest* request,
122 const net::RedirectInfo& redirect_info,
123 bool* defer_redirect) override {
124 // Defer the redirect either way.
125 *defer_redirect = true;
127 // Find out what the throttle would have done.
128 throttle_->WillRedirectRequest(redirect_info, &was_deferred_);
129 run_loop_->Quit();
131 void OnResponseStarted(net::URLRequest* request) override {}
132 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {}
134 // content::ResourceController implementation:
135 void Cancel() override {
136 EXPECT_FALSE(cancel_called_);
137 EXPECT_FALSE(resume_called_);
139 cancel_called_ = true;
140 run_loop_->Quit();
142 void CancelAndIgnore() override { Cancel(); }
143 void CancelWithError(int error_code) override { Cancel(); }
144 void Resume() override {
145 EXPECT_TRUE(was_deferred_);
146 EXPECT_FALSE(cancel_called_);
147 EXPECT_FALSE(resume_called_);
149 resume_called_ = true;
150 run_loop_->Quit();
153 private:
154 scoped_ptr<base::RunLoop> run_loop_;
155 PrerenderResourceThrottle* throttle_;
156 bool was_deferred_;
157 bool cancel_called_;
158 bool resume_called_;
160 DISALLOW_COPY_AND_ASSIGN(DeferredRedirectDelegate);
163 } // namespace
165 class PrerenderTrackerTest : public testing::Test {
166 public:
167 static const int kDefaultChildId = 0;
168 static const int kDefaultRouteId = 100;
170 PrerenderTrackerTest() :
171 ui_thread_(BrowserThread::UI, &message_loop_),
172 io_thread_(BrowserThread::IO, &message_loop_),
173 prerender_manager_(prerender_tracker()),
174 test_contents_(&prerender_manager_, kDefaultChildId, kDefaultRouteId) {
175 chrome_browser_net::SetUrlRequestMocksEnabled(true);
178 ~PrerenderTrackerTest() override {
179 chrome_browser_net::SetUrlRequestMocksEnabled(false);
181 // Cleanup work so the file IO tasks from URLRequestMockHTTPJob
182 // are gone.
183 content::BrowserThread::GetBlockingPool()->FlushForTesting();
184 RunEvents();
187 PrerenderTracker* prerender_tracker() {
188 return g_browser_process->prerender_tracker();
191 TestPrerenderManager* prerender_manager() {
192 return &prerender_manager_;
195 TestPrerenderContents* test_contents() {
196 return &test_contents_;
199 // Runs any tasks queued on either thread.
200 void RunEvents() {
201 message_loop_.RunUntilIdle();
204 private:
205 base::MessageLoopForIO message_loop_;
206 content::TestBrowserThread ui_thread_;
207 content::TestBrowserThread io_thread_;
209 TestPrerenderManager prerender_manager_;
210 TestPrerenderContents test_contents_;
213 // Checks that deferred redirects are throttled and resumed correctly.
214 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectResume) {
215 const base::FilePath::CharType kRedirectPath[] =
216 FILE_PATH_LITERAL("prerender/image-deferred.png");
218 test_contents()->Start();
219 RunEvents();
221 // Fake a request.
222 net::TestURLRequestContext url_request_context;
223 DeferredRedirectDelegate delegate;
224 scoped_ptr<net::URLRequest> request(url_request_context.CreateRequest(
225 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)),
226 net::DEFAULT_PRIORITY,
227 &delegate,
228 NULL));
229 content::ResourceRequestInfo::AllocateForTesting(
230 request.get(),
231 content::RESOURCE_TYPE_IMAGE,
232 NULL,
233 kDefaultChildId,
234 kDefaultRouteId,
235 MSG_ROUTING_NONE,
236 false, // is_main_frame
237 false, // parent_is_main_frame
238 true, // allow_download
239 true); // is_async
241 // Install a prerender throttle.
242 PrerenderResourceThrottle throttle(request.get());
243 delegate.SetThrottle(&throttle);
245 // Start the request and wait for a redirect.
246 request->Start();
247 delegate.Run();
248 EXPECT_TRUE(delegate.was_deferred());
249 // This calls WillRedirectRequestOnUI().
250 RunEvents();
252 // Display the prerendered RenderView and wait for the throttle to
253 // notice.
254 test_contents()->Use();
255 delegate.Run();
256 EXPECT_TRUE(delegate.resume_called());
257 EXPECT_FALSE(delegate.cancel_called());
260 // Checks that redirects in main frame loads are not deferred.
261 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectMainFrame) {
262 const base::FilePath::CharType kRedirectPath[] =
263 FILE_PATH_LITERAL("prerender/image-deferred.png");
265 test_contents()->Start();
266 RunEvents();
268 // Fake a request.
269 net::TestURLRequestContext url_request_context;
270 DeferredRedirectDelegate delegate;
271 scoped_ptr<net::URLRequest> request(url_request_context.CreateRequest(
272 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)),
273 net::DEFAULT_PRIORITY,
274 &delegate,
275 NULL));
276 content::ResourceRequestInfo::AllocateForTesting(
277 request.get(),
278 content::RESOURCE_TYPE_MAIN_FRAME,
279 NULL,
280 kDefaultChildId,
281 kDefaultRouteId,
282 MSG_ROUTING_NONE,
283 true, // is_main_frame
284 false, // parent_is_main_frame
285 true, // allow_download
286 true); // is_async
288 // Install a prerender throttle.
289 PrerenderResourceThrottle throttle(request.get());
290 delegate.SetThrottle(&throttle);
292 // Start the request and wait for a redirect. This time, it should
293 // not be deferred.
294 request->Start();
295 delegate.Run();
296 // This calls WillRedirectRequestOnUI().
297 RunEvents();
299 // Cleanup work so the prerender is gone.
300 test_contents()->Cancel();
301 RunEvents();
304 // Checks that attempting to defer a synchronous request aborts the
305 // prerender.
306 TEST_F(PrerenderTrackerTest, PrerenderThrottledRedirectSyncXHR) {
307 const base::FilePath::CharType kRedirectPath[] =
308 FILE_PATH_LITERAL("prerender/image-deferred.png");
310 test_contents()->Start();
311 RunEvents();
313 // Fake a request.
314 net::TestURLRequestContext url_request_context;
315 DeferredRedirectDelegate delegate;
316 scoped_ptr<net::URLRequest> request(url_request_context.CreateRequest(
317 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath)),
318 net::DEFAULT_PRIORITY,
319 &delegate,
320 NULL));
321 content::ResourceRequestInfo::AllocateForTesting(
322 request.get(),
323 content::RESOURCE_TYPE_XHR,
324 NULL,
325 kDefaultChildId,
326 kDefaultRouteId,
327 MSG_ROUTING_NONE,
328 false, // is_main_frame
329 false, // parent_is_main_frame
330 true, // allow_download
331 false); // is_async
333 // Install a prerender throttle.
334 PrerenderResourceThrottle throttle(request.get());
335 delegate.SetThrottle(&throttle);
337 // Start the request and wait for a redirect.
338 request->Start();
339 delegate.Run();
340 // This calls WillRedirectRequestOnUI().
341 RunEvents();
343 // We should have cancelled the prerender.
344 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT,
345 test_contents()->final_status());
347 // Cleanup work so the prerender is gone.
348 test_contents()->Cancel();
349 RunEvents();
352 } // namespace prerender