OLD | NEW |
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_controller.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 } // namespace | 53 } // namespace |
54 | 54 |
55 class WebRestrictionsResourceThrottleTest : public testing::Test { | 55 class WebRestrictionsResourceThrottleTest : public testing::Test { |
56 protected: | 56 protected: |
57 WebRestrictionsResourceThrottleTest() | 57 WebRestrictionsResourceThrottleTest() |
58 : throttle_(&provider_, GURL("http://example.com"), true), | 58 : throttle_(&provider_, GURL("http://example.com"), true), |
59 controller_(run_loop_.QuitClosure()) { | 59 controller_(run_loop_.QuitClosure()) { |
60 throttle_.set_controller_for_testing(&controller_); | 60 throttle_.set_controller_for_testing(&controller_); |
61 } | 61 } |
62 | 62 |
| 63 void SetAuthority(std::string authority) { |
| 64 provider_.SetAuthorityTask(authority); |
| 65 } |
| 66 |
63 void StartProvider() { | 67 void StartProvider() { |
64 provider_.SetAuthority("Good"); | 68 SetAuthority("Good"); |
65 bool defer; | 69 bool defer; |
66 throttle_.WillStartRequest(&defer); | 70 throttle_.WillStartRequest(&defer); |
67 run_loop_.Run(); | 71 run_loop_.Run(); |
68 } | 72 } |
69 | 73 |
70 // Mock the Java WebRestrictionsClient. The real version | 74 // Mock the Java WebRestrictionsClient. The real version |
71 // would need a content provider to do anything. | 75 // would need a content provider to do anything. |
72 web_restrictions::MockWebRestrictionsClient mock_; | 76 web_restrictions::MockWebRestrictionsClient mock_; |
73 content::TestBrowserThreadBundle thread_bundle_; | 77 content::TestBrowserThreadBundle thread_bundle_; |
74 WebRestrictionsClient provider_; | 78 WebRestrictionsClient provider_; |
75 WebRestrictionsResourceThrottle throttle_; | 79 WebRestrictionsResourceThrottle throttle_; |
76 base::RunLoop run_loop_; | 80 base::RunLoop run_loop_; |
77 TestResourceController controller_; | 81 TestResourceController controller_; |
78 }; | 82 }; |
79 | 83 |
80 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_NoAuthority) { | 84 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_NoAuthority) { |
81 WebRestrictionsResourceThrottle throttle(&provider_, | 85 WebRestrictionsResourceThrottle throttle(&provider_, |
82 GURL("http://example.com"), true); | 86 GURL("http://example.com"), true); |
83 bool defer; | 87 bool defer; |
84 throttle.WillStartRequest(&defer); | 88 throttle.WillStartRequest(&defer); |
85 // If there is no authority the request won't be deferred. | 89 // If there is no authority the request won't be deferred. |
86 EXPECT_FALSE(defer); | 90 EXPECT_FALSE(defer); |
87 } | 91 } |
88 | 92 |
89 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) { | 93 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) { |
90 // Test deferring with a resource provider, and that the correct results | 94 // Test deferring with a resource provider, and that the correct results |
91 // are received. | 95 // are received. |
92 provider_.SetAuthority("Good"); | 96 SetAuthority("Good"); |
93 bool defer; | 97 bool defer; |
94 throttle_.WillStartRequest(&defer); | 98 throttle_.WillStartRequest(&defer); |
95 EXPECT_TRUE(defer); | 99 EXPECT_TRUE(defer); |
96 run_loop_.Run(); | 100 run_loop_.Run(); |
97 EXPECT_TRUE(controller_.ResumeCalled()); | 101 EXPECT_TRUE(controller_.ResumeCalled()); |
98 EXPECT_FALSE(controller_.CancelWithErrorCalled()); | 102 EXPECT_FALSE(controller_.CancelWithErrorCalled()); |
99 } | 103 } |
100 | 104 |
101 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredForbid) { | 105 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredForbid) { |
102 provider_.SetAuthority("Bad"); | 106 SetAuthority("Bad"); |
103 bool defer; | 107 bool defer; |
104 throttle_.WillStartRequest(&defer); | 108 throttle_.WillStartRequest(&defer); |
105 EXPECT_TRUE(defer); | 109 EXPECT_TRUE(defer); |
106 run_loop_.Run(); | 110 run_loop_.Run(); |
107 EXPECT_FALSE(controller_.ResumeCalled()); | 111 EXPECT_FALSE(controller_.ResumeCalled()); |
108 EXPECT_TRUE(controller_.CancelWithErrorCalled()); | 112 EXPECT_TRUE(controller_.CancelWithErrorCalled()); |
109 EXPECT_EQ(net::ERR_BLOCKED_BY_ADMINISTRATOR, controller_.GetErrorCode()); | 113 EXPECT_EQ(net::ERR_BLOCKED_BY_ADMINISTRATOR, controller_.GetErrorCode()); |
110 } | 114 } |
111 | 115 |
112 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_Subresource) { | 116 TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_Subresource) { |
113 // Only the main frame should be deferred. | 117 // Only the main frame should be deferred. |
114 // Initialization of the controller is asynchronous, and this will only work | 118 // Initialization of the controller is asynchronous, and this will only work |
115 // correctly if the provider is initialized. Run a main frame through this | 119 // correctly if the provider is initialized. Run a main frame through this |
116 // first to ensure that everything is initialized. | 120 // first to ensure that everything is initialized. |
117 StartProvider(); | 121 StartProvider(); |
118 // Now the real test. | 122 // Now the real test. |
119 WebRestrictionsResourceThrottle throttle( | 123 WebRestrictionsResourceThrottle throttle( |
120 &provider_, GURL("http://example.com/sub"), false); | 124 &provider_, GURL("http://example.com/sub"), false); |
121 base::RunLoop test_run_loop; | 125 base::RunLoop test_run_loop; |
122 TestResourceController test_controller(test_run_loop.QuitClosure()); | 126 TestResourceController test_controller(test_run_loop.QuitClosure()); |
123 throttle.set_controller_for_testing(&test_controller); | 127 throttle.set_controller_for_testing(&test_controller); |
124 bool defer; | 128 bool defer; |
125 throttle.WillStartRequest(&defer); | 129 throttle.WillStartRequest(&defer); |
126 EXPECT_FALSE(defer); | 130 ASSERT_FALSE(defer); |
127 } | 131 } |
128 | 132 |
129 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) { | 133 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) { |
130 // Set up a cached url. | 134 // Set up a cached url. |
131 StartProvider(); | 135 StartProvider(); |
132 // Using the same URL should not be deferred | 136 // Using the same URL should not be deferred |
133 net::RedirectInfo redirect; | 137 net::RedirectInfo redirect; |
134 redirect.new_url = GURL("http://example.com"); | 138 redirect.new_url = GURL("http://example.com"); |
135 bool defer; | 139 bool defer; |
136 throttle_.WillRedirectRequest(redirect, &defer); | 140 throttle_.WillRedirectRequest(redirect, &defer); |
137 EXPECT_FALSE(defer); | 141 ASSERT_FALSE(defer); |
138 } | 142 } |
139 | 143 |
140 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) { | 144 TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) { |
141 // Set up a cached url. | 145 // Set up a cached url. |
142 StartProvider(); | 146 StartProvider(); |
143 // Using a different URL should be deferred | 147 // Using a different URL should be deferred |
144 net::RedirectInfo redirect; | 148 net::RedirectInfo redirect; |
145 redirect.new_url = GURL("http://example.com/2"); | 149 redirect.new_url = GURL("http://example.com/2"); |
146 base::RunLoop test_run_loop; | 150 base::RunLoop test_run_loop; |
147 TestResourceController test_controller(test_run_loop.QuitClosure()); | 151 TestResourceController test_controller(test_run_loop.QuitClosure()); |
148 throttle_.set_controller_for_testing(&test_controller); | 152 throttle_.set_controller_for_testing(&test_controller); |
149 bool defer; | 153 bool defer; |
150 throttle_.WillRedirectRequest(redirect, &defer); | 154 throttle_.WillRedirectRequest(redirect, &defer); |
151 EXPECT_TRUE(defer); | 155 ASSERT_TRUE(defer); |
152 // If we don't wait for the callback it may happen after the exit, which | 156 // If we don't wait for the callback it may happen after the exit, which |
153 // results in accesses the redirect_url after the stack frame is freed. | 157 // results in accesses the redirect_url after the stack frame is freed. |
154 test_run_loop.Run(); | 158 test_run_loop.Run(); |
155 } | 159 } |
156 | 160 |
157 } // namespace web_restrictions | 161 } // namespace web_restrictions |
OLD | NEW |