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 3// found in the LICENSE file. 4 5#include "base/prefs/testing_pref_service.h" 6#include "chrome/browser/chrome_notification_types.h" 7#include "chrome/browser/web_resource/eula_accepted_notifier.h" 8#include "chrome/browser/web_resource/resource_request_allowed_notifier_test_util.h" 9#include "chrome/test/base/testing_browser_process.h" 10#include "content/public/browser/notification_service.h" 11#include "content/public/test/test_browser_thread.h" 12#include "testing/gtest/include/gtest/gtest.h" 13 14// Override NetworkChangeNotifier to simulate connection type changes for tests. 15class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { 16 public: 17 TestNetworkChangeNotifier() 18 : net::NetworkChangeNotifier(), 19 connection_type_to_return_( 20 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { 21 } 22 23 // Simulates a change of the connection type to |type|. This will notify any 24 // objects that are NetworkChangeNotifiers. 25 void SimulateNetworkConnectionChange( 26 net::NetworkChangeNotifier::ConnectionType type) { 27 connection_type_to_return_ = type; 28 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); 29 base::MessageLoop::current()->RunUntilIdle(); 30 } 31 32 private: 33 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { 34 return connection_type_to_return_; 35 } 36 37 // The currently simulated network connection type. If this is set to 38 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true. 39 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; 40 41 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); 42}; 43 44// EulaAcceptedNotifier test class that allows mocking the EULA accepted state 45// and issuing simulated notifications. 46class TestEulaAcceptedNotifier : public EulaAcceptedNotifier { 47 public: 48 TestEulaAcceptedNotifier() 49 : EulaAcceptedNotifier(NULL), 50 eula_accepted_(false) { 51 } 52 virtual ~TestEulaAcceptedNotifier() { 53 } 54 55 virtual bool IsEulaAccepted() OVERRIDE { 56 return eula_accepted_; 57 } 58 59 void SetEulaAcceptedForTesting(bool eula_accepted) { 60 eula_accepted_ = eula_accepted; 61 } 62 63 void SimulateEulaAccepted() { 64 NotifyObserver(); 65 } 66 67 private: 68 bool eula_accepted_; 69 70 DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier); 71}; 72 73// A test fixture class for ResourceRequestAllowedNotifier tests that require 74// network state simulations. This also acts as the service implementing the 75// ResourceRequestAllowedNotifier::Observer interface. 76class ResourceRequestAllowedNotifierTest 77 : public testing::Test, 78 public ResourceRequestAllowedNotifier::Observer { 79 public: 80 ResourceRequestAllowedNotifierTest() 81 : ui_thread(content::BrowserThread::UI, &message_loop), 82 eula_notifier_(new TestEulaAcceptedNotifier), 83 was_notified_(false) { 84 resource_request_allowed_notifier_.InitWithEulaAcceptNotifier( 85 this, scoped_ptr<EulaAcceptedNotifier>(eula_notifier_)); 86 } 87 virtual ~ResourceRequestAllowedNotifierTest() { } 88 89 bool was_notified() const { return was_notified_; } 90 91 // ResourceRequestAllowedNotifier::Observer override: 92 virtual void OnResourceRequestsAllowed() OVERRIDE { 93 was_notified_ = true; 94 } 95 96 // Network manipulation methods: 97 void SetWaitingForNetwork(bool waiting) { 98 resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting); 99 } 100 101 void SimulateNetworkConnectionChange( 102 net::NetworkChangeNotifier::ConnectionType type) { 103 network_notifier.SimulateNetworkConnectionChange(type); 104 } 105 106 // Simulate a resource request from the test service. It returns true if 107 // resource request is allowed. Otherwise returns false and will change the 108 // result of was_notified() to true when the request is allowed. 109 bool SimulateResourceRequest() { 110 return resource_request_allowed_notifier_.ResourceRequestsAllowed(); 111 } 112 113 void SimulateEulaAccepted() { 114 eula_notifier_->SimulateEulaAccepted(); 115 } 116 117 // Eula manipulation methods: 118 void SetNeedsEulaAcceptance(bool needs_acceptance) { 119 eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance); 120 } 121 122 void SetWaitingForEula(bool waiting) { 123 resource_request_allowed_notifier_.SetWaitingForEulaForTesting(waiting); 124 } 125 126 // Used in tests involving the EULA. Disables both the EULA accepted state 127 // and the network. 128 void DisableEulaAndNetwork() { 129 SetWaitingForNetwork(true); 130 SimulateNetworkConnectionChange( 131 net::NetworkChangeNotifier::CONNECTION_NONE); 132 SetWaitingForEula(true); 133 SetNeedsEulaAcceptance(true); 134 } 135 136 virtual void SetUp() OVERRIDE { 137 // Assume the test service has already requested permission, as all tests 138 // just test that criteria changes notify the server. 139 // Set default EULA state to done (not waiting and EULA accepted) to 140 // simplify non-ChromeOS tests. 141 SetWaitingForEula(false); 142 SetNeedsEulaAcceptance(false); 143 } 144 145 private: 146 base::MessageLoopForUI message_loop; 147 content::TestBrowserThread ui_thread; 148 TestNetworkChangeNotifier network_notifier; 149 TestRequestAllowedNotifier resource_request_allowed_notifier_; 150 TestEulaAcceptedNotifier* eula_notifier_; // Weak, owned by RRAN. 151 bool was_notified_; 152 153 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); 154}; 155 156TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { 157 SetWaitingForNetwork(true); 158 EXPECT_FALSE(SimulateResourceRequest()); 159 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); 160 EXPECT_FALSE(was_notified()); 161} 162 163TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { 164 SetWaitingForNetwork(false); 165 EXPECT_TRUE(SimulateResourceRequest()); 166 SimulateNetworkConnectionChange( 167 net::NetworkChangeNotifier::CONNECTION_ETHERNET); 168 EXPECT_FALSE(was_notified()); 169} 170 171TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { 172 SetWaitingForNetwork(true); 173 EXPECT_FALSE(SimulateResourceRequest()); 174 SimulateNetworkConnectionChange( 175 net::NetworkChangeNotifier::CONNECTION_ETHERNET); 176 EXPECT_TRUE(was_notified()); 177} 178 179TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { 180 SetWaitingForNetwork(false); 181 EXPECT_TRUE(SimulateResourceRequest()); 182 SimulateNetworkConnectionChange( 183 net::NetworkChangeNotifier::CONNECTION_WIFI); 184 EXPECT_FALSE(was_notified()); 185 SimulateNetworkConnectionChange( 186 net::NetworkChangeNotifier::CONNECTION_3G); 187 EXPECT_FALSE(was_notified()); 188 SimulateNetworkConnectionChange( 189 net::NetworkChangeNotifier::CONNECTION_4G); 190 EXPECT_FALSE(was_notified()); 191 SimulateNetworkConnectionChange( 192 net::NetworkChangeNotifier::CONNECTION_WIFI); 193 EXPECT_FALSE(was_notified()); 194} 195 196TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { 197 // SimulateResourceRequest() returns true because network is online. 198 SetWaitingForNetwork(false); 199 EXPECT_TRUE(SimulateResourceRequest()); 200 // The callback is nerver invoked whatever happens on network connection. 201 SimulateNetworkConnectionChange( 202 net::NetworkChangeNotifier::CONNECTION_WIFI); 203 EXPECT_FALSE(was_notified()); 204 SimulateNetworkConnectionChange( 205 net::NetworkChangeNotifier::CONNECTION_NONE); 206 EXPECT_FALSE(was_notified()); 207 SimulateNetworkConnectionChange( 208 net::NetworkChangeNotifier::CONNECTION_WIFI); 209 EXPECT_FALSE(was_notified()); 210} 211 212TEST_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 230TEST_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 244TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { 245 // Ensure that if the observing service does not request access, it does not 246 // get notified, even if the criteria is met. Note that this is done by not 247 // calling SimulateResourceRequest here. 248 SetWaitingForNetwork(true); 249 SimulateNetworkConnectionChange( 250 net::NetworkChangeNotifier::CONNECTION_ETHERNET); 251 EXPECT_FALSE(was_notified()); 252} 253 254TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { 255 DisableEulaAndNetwork(); 256 EXPECT_FALSE(SimulateResourceRequest()); 257 258 SimulateEulaAccepted(); 259 EXPECT_FALSE(was_notified()); 260} 261 262TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { 263 DisableEulaAndNetwork(); 264 EXPECT_FALSE(SimulateResourceRequest()); 265 266 SimulateEulaAccepted(); 267 EXPECT_FALSE(was_notified()); 268 269 SimulateNetworkConnectionChange( 270 net::NetworkChangeNotifier::CONNECTION_WIFI); 271 EXPECT_TRUE(was_notified()); 272} 273 274TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { 275 DisableEulaAndNetwork(); 276 EXPECT_FALSE(SimulateResourceRequest()); 277 278 SimulateNetworkConnectionChange( 279 net::NetworkChangeNotifier::CONNECTION_WIFI); 280 EXPECT_FALSE(was_notified()); 281 282 SimulateEulaAccepted(); 283 EXPECT_TRUE(was_notified()); 284} 285 286TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) { 287 // Ensure that if the observing service does not request access, it does not 288 // get notified, even if the criteria is met. Note that this is done by not 289 // calling SimulateResourceRequest here. 290 DisableEulaAndNetwork(); 291 292 SimulateNetworkConnectionChange( 293 net::NetworkChangeNotifier::CONNECTION_WIFI); 294 EXPECT_FALSE(was_notified()); 295 296 SimulateEulaAccepted(); 297 EXPECT_FALSE(was_notified()); 298} 299