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 void SimulateNetworkConnectionChange( | |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Add a comment.
SteveT
2012/09/24 18:04:27
Done.
| |
| 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_; | |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Add a comment.
SteveT
2012/09/24 18:04:27
Done.
| |
| 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_; } | |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Rename the getter too.
SteveT
2012/09/24 18:04:27
Done.
| |
| 58 | |
| 59 // ResourceRequestAllowedNotifier::Observer override: | |
| 60 virtual void OnResourceRequestsAllowed() OVERRIDE { | |
| 61 was_notified_ = true; | |
| 62 } | |
| 63 | |
| 64 // Network manipulation methods: | |
| 65 void SetWasWaitingForNetwork(bool waiting) { | |
| 66 resource_request_allowed_notifier_. | |
| 67 SetWasWaitingForNetworkForTesting(waiting); | |
| 68 } | |
| 69 | |
| 70 void SimulateNetworkConnectionChange( | |
| 71 net::NetworkChangeNotifier::ConnectionType type) { | |
| 72 network_notifier.SimulateNetworkConnectionChange(type); | |
| 73 } | |
| 74 | |
| 75 void SetObserverRequestedPermission(bool requested) { | |
| 76 resource_request_allowed_notifier_. | |
| 77 SetObserverRequestedPermissionForTesting(requested); | |
| 78 } | |
| 79 | |
| 80 #if defined(OS_CHROMEOS) | |
| 81 // Eula manipulation methods: | |
| 82 void SetEulaAccepted(bool accepted) { | |
| 83 resource_request_allowed_notifier_.SetEulaAccepted(accepted); | |
| 84 } | |
| 85 | |
| 86 void SetWasWaitingForEula(bool waiting) { | |
| 87 resource_request_allowed_notifier_.SetWasWaitingForEulaForTesting(waiting); | |
| 88 } | |
| 89 | |
| 90 void SimulateEulaAccepted() { | |
| 91 SetEulaAccepted(true); | |
| 92 content::NotificationService::current()->Notify( | |
| 93 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 94 content::NotificationService::AllSources(), | |
| 95 content::NotificationService::NoDetails()); | |
| 96 } | |
| 97 | |
| 98 // Used in tests involving the EULA. Disables both the EULA accepted state | |
| 99 // and the network. | |
| 100 void DisableEulaAndNetwork() { | |
| 101 SetWasWaitingForNetwork(true); | |
| 102 SimulateNetworkConnectionChange( | |
| 103 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 104 SetWasWaitingForEula(true); | |
| 105 SetEulaAccepted(false); | |
| 106 } | |
| 107 #endif | |
| 108 | |
| 109 virtual void SetUp() OVERRIDE { | |
| 110 // Assume the test service has already requested permission, as all tests | |
| 111 // just test that criteria changes notify the server. | |
| 112 SetObserverRequestedPermission(true); | |
| 113 #if defined(OS_CHROMEOS) | |
| 114 // Set default EULA state to done (not waiting and EULA accepted) to | |
| 115 // simplify non-ChromeOS tests. | |
| 116 SetWasWaitingForEula(false); | |
| 117 SetEulaAccepted(true); | |
| 118 #endif | |
| 119 } | |
| 120 | |
| 121 private: | |
| 122 MessageLoopForUI message_loop; | |
| 123 content::TestBrowserThread ui_thread; | |
| 124 TestNetworkChangeNotifier network_notifier; | |
| 125 TestRequestAllowedNotifier resource_request_allowed_notifier_; | |
| 126 bool was_notified_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | |
| 129 }; | |
| 130 | |
| 131 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) { | |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Rename tests to not say "request", use the wo
SteveT
2012/09/24 18:04:27
Done.
| |
| 132 SetWasWaitingForNetwork(true); | |
| 133 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 134 EXPECT_FALSE(request_attempted()); | |
| 135 } | |
| 136 | |
| 137 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) { | |
| 138 SetWasWaitingForNetwork(false); | |
| 139 SimulateNetworkConnectionChange( | |
| 140 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 141 EXPECT_FALSE(request_attempted()); | |
| 142 } | |
| 143 | |
| 144 TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) { | |
| 145 SetWasWaitingForNetwork(true); | |
| 146 SimulateNetworkConnectionChange( | |
| 147 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 148 EXPECT_TRUE(request_attempted()); | |
| 149 } | |
| 150 | |
| 151 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) { | |
| 152 SetWasWaitingForNetwork(false); | |
| 153 SimulateNetworkConnectionChange( | |
| 154 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 155 EXPECT_FALSE(request_attempted()); | |
| 156 SimulateNetworkConnectionChange( | |
| 157 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 158 EXPECT_FALSE(request_attempted()); | |
| 159 SimulateNetworkConnectionChange( | |
| 160 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 161 EXPECT_FALSE(request_attempted()); | |
| 162 SimulateNetworkConnectionChange( | |
| 163 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 164 EXPECT_FALSE(request_attempted()); | |
| 165 } | |
| 166 | |
| 167 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnFlakyConnection) { | |
| 168 SetWasWaitingForNetwork(false); | |
| 169 SimulateNetworkConnectionChange( | |
| 170 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 171 EXPECT_FALSE(request_attempted()); | |
| 172 SimulateNetworkConnectionChange( | |
| 173 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 174 EXPECT_FALSE(request_attempted()); | |
| 175 SimulateNetworkConnectionChange( | |
| 176 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 177 EXPECT_FALSE(request_attempted()); | |
| 178 } | |
| 179 | |
| 180 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { | |
| 181 // Ensure that if the observing service does not request access, it does not | |
| 182 // get notified, even if the criteria is met. | |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Can you add a similar test but with the Eula condi
SteveT
2012/09/24 18:04:27
Done.
| |
| 183 SetObserverRequestedPermission(false); | |
| 184 SetWasWaitingForNetwork(true); | |
| 185 SimulateNetworkConnectionChange( | |
| 186 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 187 EXPECT_FALSE(request_attempted()); | |
| 188 } | |
| 189 | |
| 190 #if defined(OS_CHROMEOS) | |
| 191 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { | |
| 192 DisableEulaAndNetwork(); | |
| 193 | |
| 194 SimulateEulaAccepted(); | |
| 195 EXPECT_FALSE(request_attempted()); | |
| 196 } | |
| 197 | |
| 198 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | |
| 199 SetWasWaitingForNetwork(true); | |
| 200 SimulateNetworkConnectionChange( | |
| 201 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 202 SetWasWaitingForEula(true); | |
| 203 SetEulaAccepted(false); | |
| 204 | |
| 205 SimulateEulaAccepted(); | |
| 206 EXPECT_FALSE(request_attempted()); | |
| 207 | |
| 208 SimulateNetworkConnectionChange( | |
| 209 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 210 EXPECT_TRUE(request_attempted()); | |
| 211 } | |
| 212 | |
| 213 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | |
| 214 SetWasWaitingForNetwork(true); | |
| 215 SimulateNetworkConnectionChange( | |
| 216 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 217 SetWasWaitingForEula(true); | |
| 218 SetEulaAccepted(false); | |
| 219 | |
| 220 SimulateNetworkConnectionChange( | |
| 221 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 222 EXPECT_FALSE(request_attempted()); | |
| 223 | |
| 224 SimulateEulaAccepted(); | |
| 225 EXPECT_TRUE(request_attempted()); | |
| 226 } | |
| 227 #endif // OS_CHROMEOS | |
| OLD | NEW |