| 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_.ResourceRequestsAllowed(); |
| 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 #if defined(OS_CHROMEOS) |
| 117 // Set default EULA state to done (not waiting and EULA accepted) to |
| 118 // simplify non-ChromeOS tests. |
| 119 SetWasWaitingForEula(false); |
| 120 SetNeedsEulaAcceptance(false); |
| 121 #endif |
| 122 } |
| 123 |
| 124 private: |
| 125 MessageLoopForUI message_loop; |
| 126 content::TestBrowserThread ui_thread; |
| 127 TestNetworkChangeNotifier network_notifier; |
| 128 TestRequestAllowedNotifier resource_request_allowed_notifier_; |
| 129 bool was_notified_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); |
| 132 }; |
| 133 |
| 134 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { |
| 135 SimulateResourceRequest(); |
| 136 SetWasWaitingForNetwork(true); |
| 137 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); |
| 138 EXPECT_FALSE(was_notified()); |
| 139 } |
| 140 |
| 141 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { |
| 142 SimulateResourceRequest(); |
| 143 SetWasWaitingForNetwork(false); |
| 144 SimulateNetworkConnectionChange( |
| 145 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 146 EXPECT_FALSE(was_notified()); |
| 147 } |
| 148 |
| 149 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { |
| 150 SimulateResourceRequest(); |
| 151 SetWasWaitingForNetwork(true); |
| 152 SimulateNetworkConnectionChange( |
| 153 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 154 EXPECT_TRUE(was_notified()); |
| 155 } |
| 156 |
| 157 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { |
| 158 SimulateResourceRequest(); |
| 159 SetWasWaitingForNetwork(false); |
| 160 SimulateNetworkConnectionChange( |
| 161 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 162 EXPECT_FALSE(was_notified()); |
| 163 SimulateNetworkConnectionChange( |
| 164 net::NetworkChangeNotifier::CONNECTION_3G); |
| 165 EXPECT_FALSE(was_notified()); |
| 166 SimulateNetworkConnectionChange( |
| 167 net::NetworkChangeNotifier::CONNECTION_4G); |
| 168 EXPECT_FALSE(was_notified()); |
| 169 SimulateNetworkConnectionChange( |
| 170 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 171 EXPECT_FALSE(was_notified()); |
| 172 } |
| 173 |
| 174 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { |
| 175 SimulateResourceRequest(); |
| 176 SetWasWaitingForNetwork(false); |
| 177 SimulateNetworkConnectionChange( |
| 178 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 179 EXPECT_FALSE(was_notified()); |
| 180 SimulateNetworkConnectionChange( |
| 181 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 182 EXPECT_FALSE(was_notified()); |
| 183 SimulateNetworkConnectionChange( |
| 184 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 185 EXPECT_FALSE(was_notified()); |
| 186 } |
| 187 |
| 188 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { |
| 189 // Ensure that if the observing service does not request access, it does not |
| 190 // get notified, even if the criteria is met. Note that this is done by not |
| 191 // calling SimulateResourceRequest here. |
| 192 SetWasWaitingForNetwork(true); |
| 193 SimulateNetworkConnectionChange( |
| 194 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 195 EXPECT_FALSE(was_notified()); |
| 196 } |
| 197 |
| 198 #if defined(OS_CHROMEOS) |
| 199 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { |
| 200 SimulateResourceRequest(); |
| 201 DisableEulaAndNetwork(); |
| 202 |
| 203 SimulateEulaAccepted(); |
| 204 EXPECT_FALSE(was_notified()); |
| 205 } |
| 206 |
| 207 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { |
| 208 SimulateResourceRequest(); |
| 209 DisableEulaAndNetwork(); |
| 210 |
| 211 SimulateEulaAccepted(); |
| 212 EXPECT_FALSE(was_notified()); |
| 213 |
| 214 SimulateNetworkConnectionChange( |
| 215 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 216 EXPECT_TRUE(was_notified()); |
| 217 } |
| 218 |
| 219 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { |
| 220 SimulateResourceRequest(); |
| 221 DisableEulaAndNetwork(); |
| 222 |
| 223 SimulateNetworkConnectionChange( |
| 224 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 225 EXPECT_FALSE(was_notified()); |
| 226 |
| 227 SimulateEulaAccepted(); |
| 228 EXPECT_TRUE(was_notified()); |
| 229 } |
| 230 |
| 231 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) { |
| 232 // Ensure that if the observing service does not request access, it does not |
| 233 // get notified, even if the criteria is met. Note that this is done by not |
| 234 // calling SimulateResourceRequest here. |
| 235 DisableEulaAndNetwork(); |
| 236 |
| 237 SimulateNetworkConnectionChange( |
| 238 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 239 EXPECT_FALSE(was_notified()); |
| 240 |
| 241 SimulateEulaAccepted(); |
| 242 EXPECT_FALSE(was_notified()); |
| 243 } |
| 244 #endif // OS_CHROMEOS |
| OLD | NEW |