Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/metrics/variations/resource_request_allowed_notifier_te st_util.h" | |
| 6 #include "chrome/common/chrome_notification_types.h" | |
| 7 #include "chrome/test/base/testing_browser_process.h" | |
| 8 #include "chrome/test/base/testing_pref_service.h" | |
| 9 #include "content/public/browser/notification_service.h" | |
| 10 #include "content/public/test/test_browser_thread.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 // A test class used to validate expected functionality in | |
| 14 // ResourceRequestAllowedNotifier. | |
| 15 class TestRequesterService : public ResourceRequestAllowedNotifier::Observer { | |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
I wonder if you could just merge this with the Res
SteveT
2012/09/21 15:16:17
Yes! That works and is definitely simpler. Thanks.
| |
| 16 public: | |
| 17 TestRequesterService() : was_notified_(false) { | |
| 18 // Set this flag to false so the Init call sets up the wait on the EULA. | |
| 19 SetEulaAccepted(false); | |
| 20 resource_request_allowed_notifier_.Init(this); | |
| 21 } | |
| 22 | |
| 23 virtual ~TestRequesterService() { | |
| 24 } | |
| 25 | |
| 26 void SetWasWaitingForNetworkForTesting(bool waiting) { | |
| 27 resource_request_allowed_notifier_. | |
| 28 SetWasWaitingForNetworkForTesting(waiting); | |
| 29 } | |
| 30 | |
| 31 void SetEulaAccepted(bool accepted) { | |
| 32 resource_request_allowed_notifier_.SetEulaAccepted(accepted); | |
| 33 } | |
| 34 | |
| 35 void OverrideRequestsAllowed(bool override) { | |
| 36 resource_request_allowed_notifier_.OverrideRequestsAllowed(override); | |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
Maybe just expose a getter for |resource_request_a
SteveT
2012/09/21 15:16:17
Not applicable due to the above refactoring.
| |
| 37 } | |
| 38 | |
| 39 #if defined(OS_CHROMEOS) | |
| 40 void SetWasWaitingForEulaForTesting(bool waiting) { | |
| 41 resource_request_allowed_notifier_. | |
| 42 SetWasWaitingForEulaForTesting(waiting); | |
| 43 } | |
| 44 #endif | |
| 45 | |
| 46 bool request_attempted() const { return was_notified_; } | |
| 47 | |
| 48 // ResourceRequestAllowedNotifier::Observer overrides: | |
| 49 virtual void OnResourceRequestsAllowed() OVERRIDE { | |
| 50 was_notified_ = true; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 bool was_notified_; | |
| 55 | |
| 56 TestRequestAllowedNotifier resource_request_allowed_notifier_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(TestRequesterService); | |
| 59 }; | |
| 60 | |
| 61 // Override NetworkChangeNotifier to simulate connection type changes for tests. | |
| 62 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
| 63 public: | |
| 64 TestNetworkChangeNotifier() | |
| 65 : net::NetworkChangeNotifier(), | |
| 66 connection_type_to_return_( | |
| 67 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { | |
| 68 } | |
| 69 | |
| 70 void SimulateNetworkConnectionChange( | |
| 71 net::NetworkChangeNotifier::ConnectionType type) { | |
| 72 connection_type_to_return_ = type; | |
| 73 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
| 74 MessageLoop::current()->RunAllPending(); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
| 79 return connection_type_to_return_; | |
| 80 } | |
| 81 | |
| 82 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | |
| 85 }; | |
| 86 | |
| 87 // A test fixture class for ResourceRequestAllowedNotifier tests that require | |
| 88 // network state simulations. | |
| 89 class ResourceRequestAllowedNotifierTest : public testing::Test { | |
| 90 public: | |
| 91 ResourceRequestAllowedNotifierTest() | |
| 92 : scoped_testing_local_state_( | |
| 93 static_cast<TestingBrowserProcess*>(g_browser_process)), | |
| 94 local_state_(scoped_testing_local_state_.Get()), | |
| 95 ui_thread(content::BrowserThread::UI, &message_loop) { } | |
| 96 ~ResourceRequestAllowedNotifierTest() { } | |
| 97 | |
| 98 void SetWasWaitingForNetwork(bool waiting) { | |
| 99 test_service.SetWasWaitingForNetworkForTesting(waiting); | |
| 100 } | |
| 101 | |
| 102 void SimulateNetworkConnectionChange( | |
| 103 net::NetworkChangeNotifier::ConnectionType type) { | |
| 104 network_notifier.SimulateNetworkConnectionChange(type); | |
| 105 } | |
| 106 | |
| 107 bool request_attempted() const { | |
| 108 return test_service.request_attempted(); | |
| 109 } | |
| 110 | |
| 111 void OverrideRequestsAllowed(bool override) { | |
| 112 test_service.OverrideRequestsAllowed(override); | |
| 113 } | |
| 114 | |
| 115 #if defined(OS_CHROMEOS) | |
| 116 void SetEulaAccepted(bool accepted) { | |
| 117 test_service.SetEulaAccepted(accepted); | |
| 118 } | |
| 119 | |
| 120 void SetWasWaitingForEula(bool waiting) { | |
| 121 test_service.SetWasWaitingForEulaForTesting(waiting); | |
| 122 } | |
| 123 | |
| 124 void SimulateEulaAccepted() { | |
| 125 SetEulaAccepted(true); | |
| 126 content::NotificationService::current()->Notify( | |
| 127 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 128 content::NotificationService::AllSources(), | |
| 129 content::NotificationService::NoDetails()); | |
| 130 } | |
| 131 | |
| 132 // Used in tests involving the EULA. Disables both the EULA accepted state | |
| 133 // and the network. | |
| 134 void DisableEulaAndNetwork() { | |
| 135 SetWasWaitingForNetwork(true); | |
| 136 SimulateNetworkConnectionChange( | |
| 137 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 138 SetWasWaitingForEula(true); | |
| 139 SetEulaAccepted(false); | |
| 140 } | |
| 141 #endif | |
| 142 | |
| 143 void SetUp() { | |
| 144 // Disable the override of ResourceRequestsAllowed, as these tests do not | |
| 145 // need to mock that state. | |
| 146 OverrideRequestsAllowed(false); | |
| 147 #if defined(OS_CHROMEOS) | |
| 148 // Set default EULA state to done (not waiting and EULA accepted) to | |
| 149 // simplify non-ChromeOS tests. | |
| 150 SetWasWaitingForEula(false); | |
| 151 SetEulaAccepted(true); | |
| 152 #endif | |
| 153 } | |
| 154 | |
| 155 private: | |
| 156 MessageLoopForUI message_loop; | |
| 157 ScopedTestingLocalState scoped_testing_local_state_; | |
| 158 TestingPrefService* local_state_; | |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
Do you need the local state / scoped testing local
SteveT
2012/09/21 15:16:17
Thanks for catching that - forgot to remove it aft
| |
| 159 content::TestBrowserThread ui_thread; | |
| 160 TestNetworkChangeNotifier network_notifier; | |
| 161 TestRequesterService test_service; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | |
| 164 }; | |
| 165 | |
| 166 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) { | |
| 167 SetWasWaitingForNetwork(true); | |
| 168 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 169 EXPECT_FALSE(request_attempted()); | |
| 170 } | |
| 171 | |
| 172 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) { | |
| 173 SetWasWaitingForNetwork(false); | |
| 174 SimulateNetworkConnectionChange( | |
| 175 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 176 EXPECT_FALSE(request_attempted()); | |
| 177 } | |
| 178 | |
| 179 TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) { | |
| 180 SetWasWaitingForNetwork(true); | |
| 181 SimulateNetworkConnectionChange( | |
| 182 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 183 EXPECT_TRUE(request_attempted()); | |
| 184 } | |
| 185 | |
| 186 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) { | |
| 187 SetWasWaitingForNetwork(false); | |
| 188 SimulateNetworkConnectionChange( | |
| 189 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 190 EXPECT_FALSE(request_attempted()); | |
| 191 SimulateNetworkConnectionChange( | |
| 192 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 193 EXPECT_FALSE(request_attempted()); | |
| 194 SimulateNetworkConnectionChange( | |
| 195 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 196 EXPECT_FALSE(request_attempted()); | |
| 197 SimulateNetworkConnectionChange( | |
| 198 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 199 EXPECT_FALSE(request_attempted()); | |
| 200 } | |
| 201 | |
| 202 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnFlakyConnection) { | |
| 203 SetWasWaitingForNetwork(false); | |
| 204 SimulateNetworkConnectionChange( | |
| 205 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 206 EXPECT_FALSE(request_attempted()); | |
| 207 SimulateNetworkConnectionChange( | |
| 208 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 209 EXPECT_FALSE(request_attempted()); | |
| 210 SimulateNetworkConnectionChange( | |
| 211 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 212 EXPECT_FALSE(request_attempted()); | |
| 213 } | |
| 214 | |
| 215 #if defined(OS_CHROMEOS) | |
| 216 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { | |
| 217 DisableEulaAndNetwork(); | |
| 218 | |
| 219 SimulateEulaAccepted(); | |
| 220 EXPECT_FALSE(request_attempted()); | |
| 221 } | |
| 222 | |
| 223 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | |
| 224 SetWasWaitingForNetwork(true); | |
| 225 SimulateNetworkConnectionChange( | |
| 226 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 227 SetWasWaitingForEula(true); | |
| 228 SetEulaAccepted(false); | |
| 229 OverrideRequestsAllowed(false); | |
| 230 | |
| 231 SimulateEulaAccepted(); | |
| 232 EXPECT_FALSE(request_attempted()); | |
| 233 | |
| 234 SimulateNetworkConnectionChange( | |
| 235 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 236 EXPECT_TRUE(request_attempted()); | |
| 237 } | |
| 238 | |
| 239 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | |
| 240 SetWasWaitingForNetwork(true); | |
| 241 SimulateNetworkConnectionChange( | |
| 242 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 243 SetWasWaitingForEula(true); | |
| 244 SetEulaAccepted(false); | |
| 245 OverrideRequestsAllowed(false); | |
| 246 | |
| 247 SimulateNetworkConnectionChange( | |
| 248 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 249 EXPECT_FALSE(request_attempted()); | |
| 250 | |
| 251 SimulateEulaAccepted(); | |
| 252 EXPECT_TRUE(request_attempted()); | |
| 253 } | |
| 254 #endif // OS_CHROMEOS | |
| OLD | NEW |