| 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 void SimulateNetworkConnectionChange( |
| 23 net::NetworkChangeNotifier::ConnectionType type) { |
| 24 connection_type_to_return_ = type; |
| 25 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); |
| 26 MessageLoop::current()->RunAllPending(); |
| 27 } |
| 28 |
| 29 private: |
| 30 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { |
| 31 return connection_type_to_return_; |
| 32 } |
| 33 |
| 34 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| 37 }; |
| 38 |
| 39 // A test fixture class for ResourceRequestAllowedNotifier tests that require |
| 40 // network state simulations. This also acts as the service implementing the |
| 41 // ResourceRequestAllowedNotifier::Observer interface. |
| 42 class ResourceRequestAllowedNotifierTest |
| 43 : public testing::Test, |
| 44 public ResourceRequestAllowedNotifier::Observer { |
| 45 public: |
| 46 ResourceRequestAllowedNotifierTest() |
| 47 : ui_thread(content::BrowserThread::UI, &message_loop), |
| 48 was_notified_(false) { |
| 49 #if defined(OS_CHROMEOS) |
| 50 // Set this flag to false so the Init call sets up the wait on the EULA. |
| 51 SetEulaAccepted(false); |
| 52 #endif |
| 53 resource_request_allowed_notifier_.Init(this); |
| 54 } |
| 55 ~ResourceRequestAllowedNotifierTest() { } |
| 56 |
| 57 bool request_attempted() const { return was_notified_; } |
| 58 |
| 59 // ResourceRequestAllowedNotifier::Observer override: |
| 60 virtual void OnResourceRequestsAllowed() OVERRIDE { |
| 61 was_notified_ = true; |
| 62 } |
| 63 |
| 64 void OverrideRequestsAllowed(bool override) { |
| 65 resource_request_allowed_notifier_.OverrideRequestsAllowed(override); |
| 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 #if defined(OS_CHROMEOS) |
| 80 // Eula manipulation methods: |
| 81 void SetEulaAccepted(bool accepted) { |
| 82 resource_request_allowed_notifier_.SetEulaAccepted(accepted); |
| 83 } |
| 84 |
| 85 void SetWasWaitingForEula(bool waiting) { |
| 86 resource_request_allowed_notifier_.SetWasWaitingForEulaForTesting(waiting); |
| 87 } |
| 88 |
| 89 void SimulateEulaAccepted() { |
| 90 SetEulaAccepted(true); |
| 91 content::NotificationService::current()->Notify( |
| 92 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, |
| 93 content::NotificationService::AllSources(), |
| 94 content::NotificationService::NoDetails()); |
| 95 } |
| 96 |
| 97 // Used in tests involving the EULA. Disables both the EULA accepted state |
| 98 // and the network. |
| 99 void DisableEulaAndNetwork() { |
| 100 SetWasWaitingForNetwork(true); |
| 101 SimulateNetworkConnectionChange( |
| 102 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 103 SetWasWaitingForEula(true); |
| 104 SetEulaAccepted(false); |
| 105 } |
| 106 #endif |
| 107 |
| 108 virtual void SetUp() OVERRIDE { |
| 109 // Disable the override of ResourceRequestsAllowed, as these tests do not |
| 110 // need to mock that state. |
| 111 OverrideRequestsAllowed(false); |
| 112 #if defined(OS_CHROMEOS) |
| 113 // Set default EULA state to done (not waiting and EULA accepted) to |
| 114 // simplify non-ChromeOS tests. |
| 115 SetWasWaitingForEula(false); |
| 116 SetEulaAccepted(true); |
| 117 #endif |
| 118 } |
| 119 |
| 120 private: |
| 121 MessageLoopForUI message_loop; |
| 122 content::TestBrowserThread ui_thread; |
| 123 TestNetworkChangeNotifier network_notifier; |
| 124 TestRequestAllowedNotifier resource_request_allowed_notifier_; |
| 125 bool was_notified_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); |
| 128 }; |
| 129 |
| 130 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) { |
| 131 SetWasWaitingForNetwork(true); |
| 132 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); |
| 133 EXPECT_FALSE(request_attempted()); |
| 134 } |
| 135 |
| 136 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) { |
| 137 SetWasWaitingForNetwork(false); |
| 138 SimulateNetworkConnectionChange( |
| 139 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 140 EXPECT_FALSE(request_attempted()); |
| 141 } |
| 142 |
| 143 TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) { |
| 144 SetWasWaitingForNetwork(true); |
| 145 SimulateNetworkConnectionChange( |
| 146 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 147 EXPECT_TRUE(request_attempted()); |
| 148 } |
| 149 |
| 150 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) { |
| 151 SetWasWaitingForNetwork(false); |
| 152 SimulateNetworkConnectionChange( |
| 153 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 154 EXPECT_FALSE(request_attempted()); |
| 155 SimulateNetworkConnectionChange( |
| 156 net::NetworkChangeNotifier::CONNECTION_3G); |
| 157 EXPECT_FALSE(request_attempted()); |
| 158 SimulateNetworkConnectionChange( |
| 159 net::NetworkChangeNotifier::CONNECTION_4G); |
| 160 EXPECT_FALSE(request_attempted()); |
| 161 SimulateNetworkConnectionChange( |
| 162 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 163 EXPECT_FALSE(request_attempted()); |
| 164 } |
| 165 |
| 166 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnFlakyConnection) { |
| 167 SetWasWaitingForNetwork(false); |
| 168 SimulateNetworkConnectionChange( |
| 169 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 170 EXPECT_FALSE(request_attempted()); |
| 171 SimulateNetworkConnectionChange( |
| 172 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 173 EXPECT_FALSE(request_attempted()); |
| 174 SimulateNetworkConnectionChange( |
| 175 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 176 EXPECT_FALSE(request_attempted()); |
| 177 } |
| 178 |
| 179 #if defined(OS_CHROMEOS) |
| 180 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { |
| 181 DisableEulaAndNetwork(); |
| 182 |
| 183 SimulateEulaAccepted(); |
| 184 EXPECT_FALSE(request_attempted()); |
| 185 } |
| 186 |
| 187 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { |
| 188 SetWasWaitingForNetwork(true); |
| 189 SimulateNetworkConnectionChange( |
| 190 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 191 SetWasWaitingForEula(true); |
| 192 SetEulaAccepted(false); |
| 193 OverrideRequestsAllowed(false); |
| 194 |
| 195 SimulateEulaAccepted(); |
| 196 EXPECT_FALSE(request_attempted()); |
| 197 |
| 198 SimulateNetworkConnectionChange( |
| 199 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 200 EXPECT_TRUE(request_attempted()); |
| 201 } |
| 202 |
| 203 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { |
| 204 SetWasWaitingForNetwork(true); |
| 205 SimulateNetworkConnectionChange( |
| 206 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 207 SetWasWaitingForEula(true); |
| 208 SetEulaAccepted(false); |
| 209 OverrideRequestsAllowed(false); |
| 210 |
| 211 SimulateNetworkConnectionChange( |
| 212 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 213 EXPECT_FALSE(request_attempted()); |
| 214 |
| 215 SimulateEulaAccepted(); |
| 216 EXPECT_TRUE(request_attempted()); |
| 217 } |
| 218 #endif // OS_CHROMEOS |
| OLD | NEW |