| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/prefs/testing_pref_service.h" | 5 #include "base/prefs/testing_pref_service.h" |
| 6 #include "chrome/browser/web_resource/eula_accepted_notifier.h" | 6 #include "chrome/browser/web_resource/eula_accepted_notifier.h" |
| 7 #include "chrome/browser/web_resource/resource_request_allowed_notifier_test_uti
l.h" | 7 #include "chrome/browser/web_resource/resource_request_allowed_notifier_test_uti
l.h" |
| 8 #include "chrome/common/chrome_notification_types.h" | 8 #include "chrome/common/chrome_notification_types.h" |
| 9 #include "chrome/test/base/testing_browser_process.h" | 9 #include "chrome/test/base/testing_browser_process.h" |
| 10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // Network manipulation methods: | 96 // Network manipulation methods: |
| 97 void SetWaitingForNetwork(bool waiting) { | 97 void SetWaitingForNetwork(bool waiting) { |
| 98 resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting); | 98 resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void SimulateNetworkConnectionChange( | 101 void SimulateNetworkConnectionChange( |
| 102 net::NetworkChangeNotifier::ConnectionType type) { | 102 net::NetworkChangeNotifier::ConnectionType type) { |
| 103 network_notifier.SimulateNetworkConnectionChange(type); | 103 network_notifier.SimulateNetworkConnectionChange(type); |
| 104 } | 104 } |
| 105 | 105 |
| 106 // Simulate a resource request from the test service. | 106 // Simulate a resource request from the test service. It returns true if |
| 107 void SimulateResourceRequest() { | 107 // resource request is allowed. Otherwise returns false and will change the |
| 108 resource_request_allowed_notifier_.ResourceRequestsAllowed(); | 108 // result of was_notified() to true when the request is allowed. |
| 109 bool SimulateResourceRequest() { |
| 110 return resource_request_allowed_notifier_.ResourceRequestsAllowed(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void SimulateEulaAccepted() { | 113 void SimulateEulaAccepted() { |
| 112 eula_notifier_->SimulateEulaAccepted(); | 114 eula_notifier_->SimulateEulaAccepted(); |
| 113 } | 115 } |
| 114 | 116 |
| 115 // Eula manipulation methods: | 117 // Eula manipulation methods: |
| 116 void SetNeedsEulaAcceptance(bool needs_acceptance) { | 118 void SetNeedsEulaAcceptance(bool needs_acceptance) { |
| 117 eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance); | 119 eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance); |
| 118 } | 120 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 145 content::TestBrowserThread ui_thread; | 147 content::TestBrowserThread ui_thread; |
| 146 TestNetworkChangeNotifier network_notifier; | 148 TestNetworkChangeNotifier network_notifier; |
| 147 TestRequestAllowedNotifier resource_request_allowed_notifier_; | 149 TestRequestAllowedNotifier resource_request_allowed_notifier_; |
| 148 TestEulaAcceptedNotifier* eula_notifier_; // Weak, owned by RRAN. | 150 TestEulaAcceptedNotifier* eula_notifier_; // Weak, owned by RRAN. |
| 149 bool was_notified_; | 151 bool was_notified_; |
| 150 | 152 |
| 151 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | 153 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); |
| 152 }; | 154 }; |
| 153 | 155 |
| 154 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { | 156 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { |
| 155 SimulateResourceRequest(); | |
| 156 SetWaitingForNetwork(true); | 157 SetWaitingForNetwork(true); |
| 158 EXPECT_FALSE(SimulateResourceRequest()); |
| 157 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | 159 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); |
| 158 EXPECT_FALSE(was_notified()); | 160 EXPECT_FALSE(was_notified()); |
| 159 } | 161 } |
| 160 | 162 |
| 161 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { | 163 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { |
| 162 SimulateResourceRequest(); | |
| 163 SetWaitingForNetwork(false); | 164 SetWaitingForNetwork(false); |
| 165 EXPECT_TRUE(SimulateResourceRequest()); |
| 164 SimulateNetworkConnectionChange( | 166 SimulateNetworkConnectionChange( |
| 165 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 167 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 166 EXPECT_FALSE(was_notified()); | 168 EXPECT_FALSE(was_notified()); |
| 167 } | 169 } |
| 168 | 170 |
| 169 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { | 171 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { |
| 170 SimulateResourceRequest(); | |
| 171 SetWaitingForNetwork(true); | 172 SetWaitingForNetwork(true); |
| 173 EXPECT_FALSE(SimulateResourceRequest()); |
| 172 SimulateNetworkConnectionChange( | 174 SimulateNetworkConnectionChange( |
| 173 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 175 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 174 EXPECT_TRUE(was_notified()); | 176 EXPECT_TRUE(was_notified()); |
| 175 } | 177 } |
| 176 | 178 |
| 177 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { | 179 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { |
| 178 SimulateResourceRequest(); | |
| 179 SetWaitingForNetwork(false); | 180 SetWaitingForNetwork(false); |
| 181 EXPECT_TRUE(SimulateResourceRequest()); |
| 180 SimulateNetworkConnectionChange( | 182 SimulateNetworkConnectionChange( |
| 181 net::NetworkChangeNotifier::CONNECTION_WIFI); | 183 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 182 EXPECT_FALSE(was_notified()); | 184 EXPECT_FALSE(was_notified()); |
| 183 SimulateNetworkConnectionChange( | 185 SimulateNetworkConnectionChange( |
| 184 net::NetworkChangeNotifier::CONNECTION_3G); | 186 net::NetworkChangeNotifier::CONNECTION_3G); |
| 185 EXPECT_FALSE(was_notified()); | 187 EXPECT_FALSE(was_notified()); |
| 186 SimulateNetworkConnectionChange( | 188 SimulateNetworkConnectionChange( |
| 187 net::NetworkChangeNotifier::CONNECTION_4G); | 189 net::NetworkChangeNotifier::CONNECTION_4G); |
| 188 EXPECT_FALSE(was_notified()); | 190 EXPECT_FALSE(was_notified()); |
| 189 SimulateNetworkConnectionChange( | 191 SimulateNetworkConnectionChange( |
| 190 net::NetworkChangeNotifier::CONNECTION_WIFI); | 192 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 191 EXPECT_FALSE(was_notified()); | 193 EXPECT_FALSE(was_notified()); |
| 192 } | 194 } |
| 193 | 195 |
| 194 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { | 196 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { |
| 195 SimulateResourceRequest(); | 197 // SimulateResourceRequest() returns true because network is online. |
| 196 SetWaitingForNetwork(false); | 198 SetWaitingForNetwork(false); |
| 199 EXPECT_TRUE(SimulateResourceRequest()); |
| 200 // The callback is nerver invoked whatever happens on network connection. |
| 197 SimulateNetworkConnectionChange( | 201 SimulateNetworkConnectionChange( |
| 198 net::NetworkChangeNotifier::CONNECTION_WIFI); | 202 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 199 EXPECT_FALSE(was_notified()); | 203 EXPECT_FALSE(was_notified()); |
| 200 SimulateNetworkConnectionChange( | 204 SimulateNetworkConnectionChange( |
| 201 net::NetworkChangeNotifier::CONNECTION_NONE); | 205 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 202 EXPECT_FALSE(was_notified()); | 206 EXPECT_FALSE(was_notified()); |
| 203 SimulateNetworkConnectionChange( | 207 SimulateNetworkConnectionChange( |
| 204 net::NetworkChangeNotifier::CONNECTION_WIFI); | 208 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 205 EXPECT_FALSE(was_notified()); | 209 EXPECT_FALSE(was_notified()); |
| 206 } | 210 } |
| 207 | 211 |
| 212 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnFlakyConnection) { |
| 213 SetWaitingForNetwork(false); |
| 214 EXPECT_TRUE(SimulateResourceRequest()); |
| 215 // Network goes online, but not notified because SimulateResourceRequest() |
| 216 // returns true before. |
| 217 SimulateNetworkConnectionChange( |
| 218 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 219 EXPECT_FALSE(was_notified()); |
| 220 SimulateNetworkConnectionChange( |
| 221 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 222 EXPECT_FALSE(SimulateResourceRequest()); |
| 223 // Now, SimulateResourceRequest() returns false and will be notified later. |
| 224 EXPECT_FALSE(was_notified()); |
| 225 SimulateNetworkConnectionChange( |
| 226 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 227 EXPECT_TRUE(was_notified()); |
| 228 } |
| 229 |
| 230 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnEulaAfterGoOffline) { |
| 231 DisableEulaAndNetwork(); |
| 232 EXPECT_FALSE(SimulateResourceRequest()); |
| 233 |
| 234 SimulateNetworkConnectionChange( |
| 235 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 236 EXPECT_FALSE(was_notified()); |
| 237 SimulateNetworkConnectionChange( |
| 238 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 239 EXPECT_FALSE(was_notified()); |
| 240 SimulateEulaAccepted(); |
| 241 EXPECT_FALSE(was_notified()); |
| 242 } |
| 243 |
| 208 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { | 244 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { |
| 209 // Ensure that if the observing service does not request access, it does not | 245 // Ensure that if the observing service does not request access, it does not |
| 210 // get notified, even if the criteria is met. Note that this is done by not | 246 // get notified, even if the criteria is met. Note that this is done by not |
| 211 // calling SimulateResourceRequest here. | 247 // calling SimulateResourceRequest here. |
| 212 SetWaitingForNetwork(true); | 248 SetWaitingForNetwork(true); |
| 213 SimulateNetworkConnectionChange( | 249 SimulateNetworkConnectionChange( |
| 214 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 250 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 215 EXPECT_FALSE(was_notified()); | 251 EXPECT_FALSE(was_notified()); |
| 216 } | 252 } |
| 217 | 253 |
| 218 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { | 254 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { |
| 219 SimulateResourceRequest(); | |
| 220 DisableEulaAndNetwork(); | 255 DisableEulaAndNetwork(); |
| 256 EXPECT_FALSE(SimulateResourceRequest()); |
| 221 | 257 |
| 222 SimulateEulaAccepted(); | 258 SimulateEulaAccepted(); |
| 223 EXPECT_FALSE(was_notified()); | 259 EXPECT_FALSE(was_notified()); |
| 224 } | 260 } |
| 225 | 261 |
| 226 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | 262 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { |
| 227 SimulateResourceRequest(); | |
| 228 DisableEulaAndNetwork(); | 263 DisableEulaAndNetwork(); |
| 264 EXPECT_FALSE(SimulateResourceRequest()); |
| 229 | 265 |
| 230 SimulateEulaAccepted(); | 266 SimulateEulaAccepted(); |
| 231 EXPECT_FALSE(was_notified()); | 267 EXPECT_FALSE(was_notified()); |
| 232 | 268 |
| 233 SimulateNetworkConnectionChange( | 269 SimulateNetworkConnectionChange( |
| 234 net::NetworkChangeNotifier::CONNECTION_WIFI); | 270 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 235 EXPECT_TRUE(was_notified()); | 271 EXPECT_TRUE(was_notified()); |
| 236 } | 272 } |
| 237 | 273 |
| 238 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | 274 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { |
| 239 SimulateResourceRequest(); | |
| 240 DisableEulaAndNetwork(); | 275 DisableEulaAndNetwork(); |
| 276 EXPECT_FALSE(SimulateResourceRequest()); |
| 241 | 277 |
| 242 SimulateNetworkConnectionChange( | 278 SimulateNetworkConnectionChange( |
| 243 net::NetworkChangeNotifier::CONNECTION_WIFI); | 279 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 244 EXPECT_FALSE(was_notified()); | 280 EXPECT_FALSE(was_notified()); |
| 245 | 281 |
| 246 SimulateEulaAccepted(); | 282 SimulateEulaAccepted(); |
| 247 EXPECT_TRUE(was_notified()); | 283 EXPECT_TRUE(was_notified()); |
| 248 } | 284 } |
| 249 | 285 |
| 250 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) { | 286 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) { |
| 251 // Ensure that if the observing service does not request access, it does not | 287 // Ensure that if the observing service does not request access, it does not |
| 252 // get notified, even if the criteria is met. Note that this is done by not | 288 // get notified, even if the criteria is met. Note that this is done by not |
| 253 // calling SimulateResourceRequest here. | 289 // calling SimulateResourceRequest here. |
| 254 DisableEulaAndNetwork(); | 290 DisableEulaAndNetwork(); |
| 255 | 291 |
| 256 SimulateNetworkConnectionChange( | 292 SimulateNetworkConnectionChange( |
| 257 net::NetworkChangeNotifier::CONNECTION_WIFI); | 293 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 258 EXPECT_FALSE(was_notified()); | 294 EXPECT_FALSE(was_notified()); |
| 259 | 295 |
| 260 SimulateEulaAccepted(); | 296 SimulateEulaAccepted(); |
| 261 EXPECT_FALSE(was_notified()); | 297 EXPECT_FALSE(was_notified()); |
| 262 } | 298 } |
| OLD | NEW |