Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: content/browser/transition_browsertest.cc

Issue 297973002: Navigation transitions: Block first response until after transitions have run. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes from review. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 "base/bind.h"
6 #include "content/browser/loader/cross_site_resource_handler.h"
7 #include "content/browser/loader/resource_dispatcher_host_impl.h"
8 #include "content/browser/loader/resource_request_info_impl.h"
9 #include "content/browser/transition_request_manager.h"
10 #include "content/browser/web_contents/web_contents_impl.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/public/test/test_utils.h"
15 #include "content/shell/browser/shell.h"
16 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "net/url_request/url_request.h"
19
20 namespace content {
21
22 class TransitionBrowserTest : public ContentBrowserTest {
23 public:
24 TransitionBrowserTest() {}
25
26 private:
27 DISALLOW_COPY_AND_ASSIGN(TransitionBrowserTest);
28 };
29
30 class TransitionBrowserTestObserver
31 : public WebContentsObserver,
32 public ShellResourceDispatcherHostDelegate {
33 public:
34 TransitionBrowserTestObserver(Shell* shell)
35 : WebContentsObserver(shell->web_contents()),
36 request_(NULL),
37 shell_(shell),
38 provisional_load_started_(false),
39 provisional_load_committed_(false),
40 did_defer_response_(false),
41 is_transition_request_(false) {}
42
43 virtual void RequestBeginning(
44 net::URLRequest* request,
45 ResourceContext* resource_context,
46 appcache::AppCacheService* appcache_service,
47 ResourceType::Type resource_type,
48 int child_id,
49 int route_id,
50 ScopedVector<ResourceThrottle>* throttles) OVERRIDE {
51 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
52 ShellResourceDispatcherHostDelegate::RequestBeginning(request,
53 resource_context,
54 appcache_service,
55 resource_type,
56 child_id,
57 route_id,
58 throttles);
59 request_ = request;
60
61 ResourceRequestInfoImpl* info =
62 ResourceRequestInfoImpl::ForRequest(request_);
63
64 TransitionRequestManager::GetInstance()->SetHasPendingTransitionRequest(
65 child_id, info->GetRenderFrameID(), is_transition_request_);
66
67 static_cast<WebContentsImpl*>(shell_->web_contents())->
jam 2014/06/16 03:46:10 WebContents isn't thread safe, so it shouldn't be
shatch 2014/06/16 22:32:19 Done.
68 set_did_defer_callback_for_testing(
69 base::Bind(&TransitionBrowserTestObserver::OnDidDeferCallback,
70 base::Unretained(this)));
71 }
72
73 virtual void DidStartProvisionalLoadForFrame(
74 int64 frame_id,
75 int64 parent_frame_id,
76 bool is_main_frame,
77 const GURL& validated_url,
78 bool is_error_page,
79 bool is_iframe_srcdoc,
80 RenderViewHost* render_view_host) OVERRIDE {
81 provisional_load_started_ = true;
82 }
83
84 virtual void DidCommitProvisionalLoadForFrame(
85 int64 frame_id,
86 const base::string16& frame_unique_name,
87 bool is_main_frame,
88 const GURL& url,
89 PageTransition transition_type,
90 RenderViewHost* render_view_host) {
91 provisional_load_committed_ = true;
92 }
93
94 void OnDidDeferCallback() {
95 ASSERT_TRUE(provisional_load_started_);
96 ASSERT_FALSE(provisional_load_committed_);
97
98 // Check that the request is currently blocked
99 ResourceRequestInfoImpl* info =
100 ResourceRequestInfoImpl::ForRequest(request_);
101
102 ASSERT_TRUE(info->cross_site_handler()->is_deferred_for_testing());
103
104 static_cast<WebContentsImpl*>(shell_->web_contents())->
105 ResumeResponseDeferredAtStart();
106
107 did_defer_response_ = true;
108 }
109
110 void set_pending_transition_request(bool is_transition_request) {
111 is_transition_request_ = is_transition_request;
112 }
113
114 bool did_defer_response() const { return did_defer_response_; }
115
116 private:
117 net::URLRequest* request_;
118 Shell* shell_;
119 bool provisional_load_started_;
120 bool provisional_load_committed_;
121 bool did_defer_response_;
122 bool is_transition_request_;
123 };
124
125 // This tests that DidDeferAfterResponseStarted is not called on normal
126 // navigations.
127 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest,
128 DidDeferAfterResponseStartedNotCalled) {
129 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
130 scoped_ptr<TransitionBrowserTestObserver> observer(
131 new TransitionBrowserTestObserver(shell()));
132
133 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
134
135 ASSERT_FALSE(observer->did_defer_response());
136 }
137
138 // This tests that DidDeferAfterResponseStarted is called on a transition
139 // navigation, and that the request is deferred.
140 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest, ResponseDeferredIfHandled) {
141 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
142 scoped_ptr<TransitionBrowserTestObserver> observer(
143 new TransitionBrowserTestObserver(shell()));
144
145 ResourceDispatcherHost::Get()->SetDelegate(observer.get());
146 observer->set_pending_transition_request(true);
147
148 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
149
150 ASSERT_TRUE(observer->did_defer_response());
151 }
152
153 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698