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 // Override NetworkChangeNotifier to simulate connection type changes for tests. | |
| 14 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
| 15 public: | |
| 16 TestNetworkChangeNotifier() | |
| 17 : net::NetworkChangeNotifier(), | |
| 18 connection_type_to_return_( | |
| 19 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { | |
| 20 } | |
| 21 | |
| 22 // Simulates a change of the connection type to |type|. This will notify any | |
| 23 // objects that are NetworkChangeNotifiers. | |
| 24 void SimulateNetworkConnectionChange( | |
| 25 net::NetworkChangeNotifier::ConnectionType type) { | |
| 26 connection_type_to_return_ = type; | |
| 27 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
| 28 MessageLoop::current()->RunAllPending(); | |
| 29 } | |
| 30 | |
| 31 private: | |
| 32 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
| 33 return connection_type_to_return_; | |
| 34 } | |
| 35 | |
| 36 // The currently simulated network connection type. If this is set to | |
| 37 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true. | |
| 38 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | |
| 41 }; | |
| 42 | |
| 43 // A test fixture class for ResourceRequestAllowedNotifier tests that require | |
| 44 // network state simulations. This also acts as the service implementing the | |
| 45 // ResourceRequestAllowedNotifier::Observer interface. | |
| 46 class ResourceRequestAllowedNotifierTest | |
| 47 : public testing::Test, | |
| 48 public ResourceRequestAllowedNotifier::Observer { | |
| 49 public: | |
| 50 ResourceRequestAllowedNotifierTest() | |
| 51 : ui_thread(content::BrowserThread::UI, &message_loop), | |
| 52 was_notified_(false) { | |
| 53 #if defined(OS_CHROMEOS) | |
| 54 // Set this flag to true so the Init call sets up the wait on the EULA. | |
| 55 SetNeedsEulaAcceptance(true); | |
| 56 #endif | |
| 57 resource_request_allowed_notifier_.Init(this); | |
| 58 } | |
| 59 ~ResourceRequestAllowedNotifierTest() { } | |
| 60 | |
| 61 bool was_notified() const { return was_notified_; } | |
| 62 | |
| 63 // ResourceRequestAllowedNotifier::Observer override: | |
| 64 virtual void OnResourceRequestsAllowed() OVERRIDE { | |
| 65 was_notified_ = true; | |
| 66 } | |
| 67 | |
| 68 // Network manipulation methods: | |
| 69 void SetWasWaitingForNetwork(bool waiting) { | |
| 70 resource_request_allowed_notifier_. | |
| 71 SetWasWaitingForNetworkForTesting(waiting); | |
| 72 } | |
| 73 | |
| 74 void SimulateNetworkConnectionChange( | |
| 75 net::NetworkChangeNotifier::ConnectionType type) { | |
| 76 network_notifier.SimulateNetworkConnectionChange(type); | |
| 77 } | |
| 78 | |
| 79 // Simulate a resource request from the test service. | |
| 80 void SimulateResourceRequest() { | |
| 81 resource_request_allowed_notifier_. | |
| 82 ResourceRequestsAllowed(); | |
|
Alexei Svitkine (slow)
2012/09/25 15:34:34
Nit: I think this can fit on the previous line.
SteveT
2012/09/25 15:44:11
Done.
| |
| 83 } | |
| 84 | |
| 85 #if defined(OS_CHROMEOS) | |
| 86 // Eula manipulation methods: | |
| 87 void SetNeedsEulaAcceptance(bool needs_acceptance) { | |
| 88 resource_request_allowed_notifier_.SetNeedsEulaAcceptance(needs_acceptance); | |
| 89 } | |
| 90 | |
| 91 void SetWasWaitingForEula(bool waiting) { | |
| 92 resource_request_allowed_notifier_.SetWasWaitingForEulaForTesting(waiting); | |
| 93 } | |
| 94 | |
| 95 void SimulateEulaAccepted() { | |
| 96 SetNeedsEulaAcceptance(false); | |
| 97 content::NotificationService::current()->Notify( | |
| 98 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 99 content::NotificationService::AllSources(), | |
| 100 content::NotificationService::NoDetails()); | |
| 101 } | |
| 102 | |
| 103 // Used in tests involving the EULA. Disables both the EULA accepted state | |
| 104 // and the network. | |
| 105 void DisableEulaAndNetwork() { | |
| 106 SetWasWaitingForNetwork(true); | |
| 107 SimulateNetworkConnectionChange( | |
| 108 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 109 SetWasWaitingForEula(true); | |
| 110 SetNeedsEulaAcceptance(true); | |
| 111 } | |
| 112 #endif | |
| 113 | |
| 114 virtual void SetUp() OVERRIDE { | |
| 115 // Assume the test service has already requested permission, as all tests | |
| 116 // just test that criteria changes notify the server. | |
| 117 #if defined(OS_CHROMEOS) | |
| 118 // Set default EULA state to done (not waiting and EULA accepted) to | |
| 119 // simplify non-ChromeOS tests. | |
| 120 SetWasWaitingForEula(false); | |
| 121 SetNeedsEulaAcceptance(false); | |
| 122 #endif | |
| 123 } | |
| 124 | |
| 125 private: | |
| 126 MessageLoopForUI message_loop; | |
| 127 content::TestBrowserThread ui_thread; | |
| 128 TestNetworkChangeNotifier network_notifier; | |
| 129 TestRequestAllowedNotifier resource_request_allowed_notifier_; | |
| 130 bool was_notified_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | |
| 133 }; | |
| 134 | |
| 135 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { | |
| 136 SimulateResourceRequest(); | |
| 137 SetWasWaitingForNetwork(true); | |
| 138 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 139 EXPECT_FALSE(was_notified()); | |
| 140 } | |
| 141 | |
| 142 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { | |
| 143 SimulateResourceRequest(); | |
| 144 SetWasWaitingForNetwork(false); | |
| 145 SimulateNetworkConnectionChange( | |
| 146 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 147 EXPECT_FALSE(was_notified()); | |
| 148 } | |
| 149 | |
| 150 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { | |
| 151 SimulateResourceRequest(); | |
| 152 SetWasWaitingForNetwork(true); | |
| 153 SimulateNetworkConnectionChange( | |
| 154 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 155 EXPECT_TRUE(was_notified()); | |
| 156 } | |
| 157 | |
| 158 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { | |
| 159 SimulateResourceRequest(); | |
| 160 SetWasWaitingForNetwork(false); | |
| 161 SimulateNetworkConnectionChange( | |
| 162 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 163 EXPECT_FALSE(was_notified()); | |
| 164 SimulateNetworkConnectionChange( | |
| 165 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 166 EXPECT_FALSE(was_notified()); | |
| 167 SimulateNetworkConnectionChange( | |
| 168 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 169 EXPECT_FALSE(was_notified()); | |
| 170 SimulateNetworkConnectionChange( | |
| 171 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 172 EXPECT_FALSE(was_notified()); | |
| 173 } | |
| 174 | |
| 175 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { | |
| 176 SimulateResourceRequest(); | |
| 177 SetWasWaitingForNetwork(false); | |
| 178 SimulateNetworkConnectionChange( | |
| 179 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 180 EXPECT_FALSE(was_notified()); | |
| 181 SimulateNetworkConnectionChange( | |
| 182 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 183 EXPECT_FALSE(was_notified()); | |
| 184 SimulateNetworkConnectionChange( | |
| 185 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 186 EXPECT_FALSE(was_notified()); | |
| 187 } | |
| 188 | |
| 189 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { | |
| 190 // Ensure that if the observing service does not request access, it does not | |
| 191 // get notified, even if the criteria is met. Note that this is done by not | |
| 192 // calling SimulateResourceRequest here. | |
| 193 SetWasWaitingForNetwork(true); | |
| 194 SimulateNetworkConnectionChange( | |
| 195 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 196 EXPECT_FALSE(was_notified()); | |
| 197 } | |
| 198 | |
| 199 #if defined(OS_CHROMEOS) | |
| 200 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { | |
| 201 SimulateResourceRequest(); | |
| 202 DisableEulaAndNetwork(); | |
| 203 | |
| 204 SimulateEulaAccepted(); | |
| 205 EXPECT_FALSE(was_notified()); | |
| 206 } | |
| 207 | |
| 208 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | |
| 209 SimulateResourceRequest(); | |
| 210 DisableEulaAndNetwork(); | |
| 211 | |
| 212 SimulateEulaAccepted(); | |
| 213 EXPECT_FALSE(was_notified()); | |
| 214 | |
| 215 SimulateNetworkConnectionChange( | |
| 216 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 217 EXPECT_TRUE(was_notified()); | |
| 218 } | |
| 219 | |
| 220 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | |
| 221 SimulateResourceRequest(); | |
| 222 DisableEulaAndNetwork(); | |
| 223 | |
| 224 SimulateNetworkConnectionChange( | |
| 225 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 226 EXPECT_FALSE(was_notified()); | |
| 227 | |
| 228 SimulateEulaAccepted(); | |
| 229 EXPECT_TRUE(was_notified()); | |
| 230 } | |
| 231 | |
| 232 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) { | |
| 233 // Ensure that if the observing service does not request access, it does not | |
| 234 // get notified, even if the criteria is met. Note that this is done by not | |
| 235 // calling SimulateResourceRequest here. | |
| 236 DisableEulaAndNetwork(); | |
| 237 | |
| 238 SimulateNetworkConnectionChange( | |
| 239 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 240 EXPECT_FALSE(was_notified()); | |
| 241 | |
| 242 SimulateEulaAccepted(); | |
| 243 EXPECT_FALSE(was_notified()); | |
| 244 } | |
| 245 #endif // OS_CHROMEOS | |
| OLD | NEW |