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 void SetObserverRequestedPermission(bool requested) { | |
| 80 resource_request_allowed_notifier_. | |
| 81 SetObserverRequestedPermissionForTesting(requested); | |
| 82 } | |
| 83 | |
| 84 #if defined(OS_CHROMEOS) | |
| 85 // Eula manipulation methods: | |
| 86 void SetNeedsEulaAcceptance(bool needs_acceptance) { | |
| 87 resource_request_allowed_notifier_.SetNeedsEulaAcceptance(needs_acceptance); | |
| 88 } | |
| 89 | |
| 90 void SetWasWaitingForEula(bool waiting) { | |
| 91 resource_request_allowed_notifier_.SetWasWaitingForEulaForTesting(waiting); | |
| 92 } | |
| 93 | |
| 94 void SimulateEulaAccepted() { | |
| 95 SetNeedsEulaAcceptance(false); | |
| 96 content::NotificationService::current()->Notify( | |
| 97 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 98 content::NotificationService::AllSources(), | |
| 99 content::NotificationService::NoDetails()); | |
| 100 } | |
| 101 | |
| 102 // Used in tests involving the EULA. Disables both the EULA accepted state | |
| 103 // and the network. | |
| 104 void DisableEulaAndNetwork() { | |
| 105 SetWasWaitingForNetwork(true); | |
| 106 SimulateNetworkConnectionChange( | |
| 107 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 108 SetWasWaitingForEula(true); | |
| 109 SetNeedsEulaAcceptance(true); | |
| 110 } | |
| 111 #endif | |
| 112 | |
| 113 virtual void SetUp() OVERRIDE { | |
| 114 // Assume the test service has already requested permission, as all tests | |
| 115 // just test that criteria changes notify the server. | |
| 116 SetObserverRequestedPermission(true); | |
|
Alexei Svitkine (slow)
2012/09/24 20:47:05
I would suggest to default to false and have tests
SteveT
2012/09/24 23:30:37
N/A after your comment about getting rid of SetObs
| |
| 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 SetWasWaitingForNetwork(true); | |
| 137 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 138 EXPECT_FALSE(was_notified()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { | |
| 142 SetWasWaitingForNetwork(false); | |
| 143 SimulateNetworkConnectionChange( | |
| 144 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 145 EXPECT_FALSE(was_notified()); | |
| 146 } | |
| 147 | |
| 148 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { | |
| 149 SetWasWaitingForNetwork(true); | |
| 150 SimulateNetworkConnectionChange( | |
| 151 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 152 EXPECT_TRUE(was_notified()); | |
| 153 } | |
| 154 | |
| 155 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { | |
| 156 SetWasWaitingForNetwork(false); | |
| 157 SimulateNetworkConnectionChange( | |
| 158 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 159 EXPECT_FALSE(was_notified()); | |
| 160 SimulateNetworkConnectionChange( | |
| 161 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 162 EXPECT_FALSE(was_notified()); | |
| 163 SimulateNetworkConnectionChange( | |
| 164 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 165 EXPECT_FALSE(was_notified()); | |
| 166 SimulateNetworkConnectionChange( | |
| 167 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 168 EXPECT_FALSE(was_notified()); | |
| 169 } | |
| 170 | |
| 171 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { | |
| 172 SetWasWaitingForNetwork(false); | |
| 173 SimulateNetworkConnectionChange( | |
| 174 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 175 EXPECT_FALSE(was_notified()); | |
| 176 SimulateNetworkConnectionChange( | |
| 177 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 178 EXPECT_FALSE(was_notified()); | |
| 179 SimulateNetworkConnectionChange( | |
| 180 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 181 EXPECT_FALSE(was_notified()); | |
| 182 } | |
| 183 | |
| 184 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { | |
| 185 // Ensure that if the observing service does not request access, it does not | |
| 186 // get notified, even if the criteria is met. | |
| 187 SetObserverRequestedPermission(false); | |
| 188 SetWasWaitingForNetwork(true); | |
| 189 SimulateNetworkConnectionChange( | |
| 190 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 191 EXPECT_FALSE(was_notified()); | |
| 192 } | |
| 193 | |
| 194 #if defined(OS_CHROMEOS) | |
| 195 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { | |
| 196 DisableEulaAndNetwork(); | |
| 197 | |
| 198 SimulateEulaAccepted(); | |
| 199 EXPECT_FALSE(was_notified()); | |
| 200 } | |
| 201 | |
| 202 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | |
| 203 DisableEulaAndNetwork(); | |
| 204 | |
| 205 SimulateEulaAccepted(); | |
| 206 EXPECT_FALSE(was_notified()); | |
| 207 | |
| 208 SimulateNetworkConnectionChange( | |
| 209 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 210 EXPECT_TRUE(was_notified()); | |
| 211 } | |
| 212 | |
| 213 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | |
| 214 SetWasWaitingForNetwork(true); | |
| 215 SimulateNetworkConnectionChange( | |
| 216 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 217 SetWasWaitingForEula(true); | |
| 218 SetNeedsEulaAcceptance(true); | |
| 219 | |
| 220 SimulateNetworkConnectionChange( | |
| 221 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 222 EXPECT_FALSE(was_notified()); | |
| 223 | |
| 224 SimulateEulaAccepted(); | |
| 225 EXPECT_TRUE(was_notified()); | |
| 226 } | |
| 227 | |
| 228 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) { | |
| 229 SetObserverRequestedPermission(false); | |
| 230 SetWasWaitingForNetwork(true); | |
| 231 SimulateNetworkConnectionChange( | |
| 232 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 233 SetWasWaitingForEula(true); | |
| 234 SetNeedsEulaAcceptance(true); | |
| 235 | |
| 236 SimulateNetworkConnectionChange( | |
| 237 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 238 EXPECT_FALSE(was_notified()); | |
| 239 | |
| 240 SimulateEulaAccepted(); | |
| 241 EXPECT_FALSE(was_notified()); | |
| 242 } | |
| 243 #endif // OS_CHROMEOS | |
| OLD | NEW |