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

Side by Side Diff: components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc

Issue 2535723005: Stop using ResourceController in ResourceThrottle (Closed)
Patch Set: Addressed #62 Created 4 years 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/run_loop.h" 6 #include "base/run_loop.h"
7 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" 7 #include "components/web_restrictions/browser/mock_web_restrictions_client.h"
8 #include "components/web_restrictions/browser/web_restrictions_client.h" 8 #include "components/web_restrictions/browser/web_restrictions_client.h"
9 #include "components/web_restrictions/browser/web_restrictions_resource_throttle .h" 9 #include "components/web_restrictions/browser/web_restrictions_resource_throttle .h"
10 #include "content/public/browser/resource_controller.h" 10 #include "content/public/browser/resource_throttle.h"
11 #include "content/public/test/test_browser_thread_bundle.h" 11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
13 #include "net/url_request/redirect_info.h" 13 #include "net/url_request/redirect_info.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace web_restrictions { 16 namespace web_restrictions {
17 17
18 namespace { 18 namespace {
19 19
20 class TestResourceController : public content::ResourceController { 20 class TestResourceThrottleDelegate
21 : public content::ResourceThrottle::Delegate {
21 public: 22 public:
22 TestResourceController(const base::Closure& quit_closure) 23 TestResourceThrottleDelegate(const base::Closure& quit_closure)
23 : resume_called_(false), 24 : resume_called_(false),
24 cancel_with_error_called_(false), 25 cancel_with_error_called_(false),
25 error_code_(0), 26 error_code_(0),
26 quit_closure_(quit_closure) {} 27 quit_closure_(quit_closure) {}
27 28
28 void Cancel() override {} 29 void Cancel() override {}
29 void CancelAndIgnore() override {} 30 void CancelAndIgnore() override {}
30 void CancelWithError(int error_code) override { 31 void CancelWithError(int error_code) override {
31 cancel_with_error_called_ = true; 32 cancel_with_error_called_ = true;
32 error_code_ = error_code; 33 error_code_ = error_code;
(...skipping 16 matching lines...) Expand all
49 int error_code_; 50 int error_code_;
50 base::Closure quit_closure_; 51 base::Closure quit_closure_;
51 }; 52 };
52 53
53 } // namespace 54 } // namespace
54 55
55 class WebRestrictionsResourceThrottleTest : public testing::Test { 56 class WebRestrictionsResourceThrottleTest : public testing::Test {
56 protected: 57 protected:
57 WebRestrictionsResourceThrottleTest() 58 WebRestrictionsResourceThrottleTest()
58 : throttle_(&provider_, GURL("http://example.com"), true), 59 : throttle_(&provider_, GURL("http://example.com"), true),
59 controller_(run_loop_.QuitClosure()) { 60 delegate_(run_loop_.QuitClosure()) {
60 throttle_.set_controller_for_testing(&controller_); 61 throttle_.set_delegate_for_testing(&delegate_);
61 } 62 }
62 63
63 void SetAuthority(std::string authority) { 64 void SetAuthority(std::string authority) {
64 provider_.SetAuthorityTask(authority); 65 provider_.SetAuthorityTask(authority);
65 } 66 }
66 67
67 void StartProvider() { 68 void StartProvider() {
68 SetAuthority("Good"); 69 SetAuthority("Good");
69 bool defer; 70 bool defer;
70 throttle_.WillStartRequest(&defer); 71 throttle_.WillStartRequest(&defer);
71 run_loop_.Run(); 72 run_loop_.Run();
72 } 73 }
73 74
74 // Mock the Java WebRestrictionsClient. The real version 75 // Mock the Java WebRestrictionsClient. The real version
75 // would need a content provider to do anything. 76 // would need a content provider to do anything.
76 web_restrictions::MockWebRestrictionsClient mock_; 77 web_restrictions::MockWebRestrictionsClient mock_;
77 content::TestBrowserThreadBundle thread_bundle_; 78 content::TestBrowserThreadBundle thread_bundle_;
78 WebRestrictionsClient provider_; 79 WebRestrictionsClient provider_;
79 WebRestrictionsResourceThrottle throttle_; 80 WebRestrictionsResourceThrottle throttle_;
80 base::RunLoop run_loop_; 81 base::RunLoop run_loop_;
81 TestResourceController controller_; 82 TestResourceThrottleDelegate delegate_;
82 }; 83 };
83 84
84 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_NoAuthority) { 85 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_NoAuthority) {
85 WebRestrictionsResourceThrottle throttle(&provider_, 86 WebRestrictionsResourceThrottle throttle(&provider_,
86 GURL("http://example.com"), true); 87 GURL("http://example.com"), true);
87 bool defer; 88 bool defer;
88 throttle.WillStartRequest(&defer); 89 throttle.WillStartRequest(&defer);
89 // If there is no authority the request won't be deferred. 90 // If there is no authority the request won't be deferred.
90 EXPECT_FALSE(defer); 91 EXPECT_FALSE(defer);
91 } 92 }
92 93
93 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) { 94 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) {
94 // Test deferring with a resource provider, and that the correct results 95 // Test deferring with a resource provider, and that the correct results
95 // are received. 96 // are received.
96 SetAuthority("Good"); 97 SetAuthority("Good");
97 bool defer; 98 bool defer;
98 throttle_.WillStartRequest(&defer); 99 throttle_.WillStartRequest(&defer);
99 EXPECT_TRUE(defer); 100 EXPECT_TRUE(defer);
100 run_loop_.Run(); 101 run_loop_.Run();
101 EXPECT_TRUE(controller_.ResumeCalled()); 102 EXPECT_TRUE(delegate_.ResumeCalled());
102 EXPECT_FALSE(controller_.CancelWithErrorCalled()); 103 EXPECT_FALSE(delegate_.CancelWithErrorCalled());
103 } 104 }
104 105
105 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredForbid) { 106 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredForbid) {
106 SetAuthority("Bad"); 107 SetAuthority("Bad");
107 bool defer; 108 bool defer;
108 throttle_.WillStartRequest(&defer); 109 throttle_.WillStartRequest(&defer);
109 EXPECT_TRUE(defer); 110 EXPECT_TRUE(defer);
110 run_loop_.Run(); 111 run_loop_.Run();
111 EXPECT_FALSE(controller_.ResumeCalled()); 112 EXPECT_FALSE(delegate_.ResumeCalled());
112 EXPECT_TRUE(controller_.CancelWithErrorCalled()); 113 EXPECT_TRUE(delegate_.CancelWithErrorCalled());
113 EXPECT_EQ(net::ERR_BLOCKED_BY_ADMINISTRATOR, controller_.GetErrorCode()); 114 EXPECT_EQ(net::ERR_BLOCKED_BY_ADMINISTRATOR, delegate_.GetErrorCode());
114 } 115 }
115 116
116 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_Subresource) { 117 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_Subresource) {
117 // Only the main frame should be deferred. 118 // Only the main frame should be deferred.
118 // Initialization of the controller is asynchronous, and this will only work 119 // Initialization of the delegate is asynchronous, and this will only work
119 // correctly if the provider is initialized. Run a main frame through this 120 // correctly if the provider is initialized. Run a main frame through this
120 // first to ensure that everything is initialized. 121 // first to ensure that everything is initialized.
121 StartProvider(); 122 StartProvider();
122 // Now the real test. 123 // Now the real test.
123 WebRestrictionsResourceThrottle throttle( 124 WebRestrictionsResourceThrottle throttle(
124 &provider_, GURL("http://example.com/sub"), false); 125 &provider_, GURL("http://example.com/sub"), false);
125 base::RunLoop test_run_loop; 126 base::RunLoop test_run_loop;
126 TestResourceController test_controller(test_run_loop.QuitClosure()); 127 TestResourceThrottleDelegate test_delegate(test_run_loop.QuitClosure());
127 throttle.set_controller_for_testing(&test_controller); 128 throttle.set_delegate_for_testing(&test_delegate);
128 bool defer; 129 bool defer;
129 throttle.WillStartRequest(&defer); 130 throttle.WillStartRequest(&defer);
130 ASSERT_FALSE(defer); 131 ASSERT_FALSE(defer);
131 } 132 }
132 133
133 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) { 134 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) {
134 // Set up a cached url. 135 // Set up a cached url.
135 StartProvider(); 136 StartProvider();
136 // Using the same URL should not be deferred 137 // Using the same URL should not be deferred
137 net::RedirectInfo redirect; 138 net::RedirectInfo redirect;
138 redirect.new_url = GURL("http://example.com"); 139 redirect.new_url = GURL("http://example.com");
139 bool defer; 140 bool defer;
140 throttle_.WillRedirectRequest(redirect, &defer); 141 throttle_.WillRedirectRequest(redirect, &defer);
141 ASSERT_FALSE(defer); 142 ASSERT_FALSE(defer);
142 } 143 }
143 144
144 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) { 145 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) {
145 // Set up a cached url. 146 // Set up a cached url.
146 StartProvider(); 147 StartProvider();
147 // Using a different URL should be deferred 148 // Using a different URL should be deferred
148 net::RedirectInfo redirect; 149 net::RedirectInfo redirect;
149 redirect.new_url = GURL("http://example.com/2"); 150 redirect.new_url = GURL("http://example.com/2");
150 base::RunLoop test_run_loop; 151 base::RunLoop test_run_loop;
151 TestResourceController test_controller(test_run_loop.QuitClosure()); 152 TestResourceThrottleDelegate test_delegate(test_run_loop.QuitClosure());
152 throttle_.set_controller_for_testing(&test_controller); 153 throttle_.set_delegate_for_testing(&test_delegate);
153 bool defer; 154 bool defer;
154 throttle_.WillRedirectRequest(redirect, &defer); 155 throttle_.WillRedirectRequest(redirect, &defer);
155 ASSERT_TRUE(defer); 156 ASSERT_TRUE(defer);
156 // If we don't wait for the callback it may happen after the exit, which 157 // If we don't wait for the callback it may happen after the exit, which
157 // results in accesses the redirect_url after the stack frame is freed. 158 // results in accesses the redirect_url after the stack frame is freed.
158 test_run_loop.Run(); 159 test_run_loop.Run();
159 } 160 }
160 161
161 } // namespace web_restrictions 162 } // namespace web_restrictions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698