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.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 // A test class used to validate expected functionality in | |
| 14 // ResourceRequestAllowedNotifier. | |
| 15 class TestRequesterService : public ResourceRequestAllowedNotifier::Observer { | |
| 16 public: | |
| 17 TestRequesterService() : was_notified_(false) { | |
| 18 resource_request_allowed_notifier_.SetObserver(this); | |
| 19 } | |
| 20 | |
| 21 virtual ~TestRequesterService() { | |
| 22 resource_request_allowed_notifier_.ClearObserver(); | |
| 23 } | |
| 24 | |
| 25 void SetWasWaitingForNetworkForTesting(bool waiting) { | |
| 26 resource_request_allowed_notifier_. | |
| 27 SetWasWaitingForNetworkForTesting(waiting); | |
| 28 } | |
| 29 | |
| 30 #if defined(OS_CHROMEOS) | |
| 31 void SetWasWaitingForEulaForTesting(bool waiting) { | |
| 32 resource_request_allowed_notifier_. | |
| 33 SetWasWaitingForEulaForTesting(waiting); | |
| 34 } | |
| 35 #endif | |
| 36 | |
| 37 bool request_attempted() const { return was_notified_; } | |
| 38 | |
| 39 // ResourceRequestAllowedNotifier::Observer overrides: | |
| 40 virtual void OnResourceRequestsAllowed() OVERRIDE { | |
| 41 was_notified_ = true; | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 bool was_notified_; | |
| 46 | |
| 47 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(TestRequesterService); | |
| 50 }; | |
| 51 | |
| 52 // Override NetworkChangeNotifier to simulate connection type changes for tests. | |
| 53 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
| 54 public: | |
| 55 TestNetworkChangeNotifier() | |
| 56 : net::NetworkChangeNotifier(), | |
| 57 connection_type_to_return_( | |
| 58 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { | |
| 59 } | |
| 60 | |
| 61 void SimulateNetworkConnectionChange( | |
| 62 net::NetworkChangeNotifier::ConnectionType type) { | |
| 63 connection_type_to_return_ = type; | |
| 64 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
| 65 MessageLoop::current()->RunAllPending(); | |
| 66 } | |
| 67 | |
| 68 private: | |
| 69 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
| 70 return connection_type_to_return_; | |
| 71 } | |
| 72 | |
| 73 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | |
| 76 }; | |
| 77 | |
| 78 // A test fixture class for ResourceRequestAllowedNotifier tests that require | |
| 79 // network state simulations. | |
| 80 class ResourceRequestAllowedNotifierTest : public testing::Test { | |
| 81 public: | |
| 82 ResourceRequestAllowedNotifierTest() | |
| 83 : scoped_testing_local_state_( | |
| 84 static_cast<TestingBrowserProcess*>(g_browser_process)), | |
| 85 local_state_(scoped_testing_local_state_.Get()), | |
| 86 ui_thread(content::BrowserThread::UI, &message_loop) { } | |
| 87 ~ResourceRequestAllowedNotifierTest() { } | |
| 88 | |
| 89 void SetWasWaitingForNetwork(bool waiting) { | |
| 90 test_service.SetWasWaitingForNetworkForTesting(waiting); | |
| 91 } | |
| 92 | |
| 93 void SimulateNetworkConnectionChange( | |
| 94 net::NetworkChangeNotifier::ConnectionType type) { | |
| 95 notifier.SimulateNetworkConnectionChange(type); | |
| 96 } | |
| 97 | |
| 98 bool request_attempted() const { | |
| 99 return test_service.request_attempted(); | |
| 100 } | |
| 101 | |
| 102 #if defined(OS_CHROMEOS) | |
| 103 void SetEulaAcceptedPref(bool accepted) { | |
| 104 // TODO(stevet): Share this string with the wizard_controller file that | |
| 105 // defines it. | |
| 106 local_state_->SetUserPref("EulaAccepted", | |
|
Alexei Svitkine (slow)
2012/09/19 19:44:28
Can you instead wrap chromeos::WizardController::I
SteveT
2012/09/20 19:40:18
Done. Note that this made fairly substantial chang
| |
| 107 Value::CreateBooleanValue(accepted)); | |
| 108 } | |
| 109 | |
| 110 void SetWasWaitingForEula(bool waiting) { | |
| 111 test_service.SetWasWaitingForEulaForTesting(waiting); | |
| 112 } | |
| 113 | |
| 114 void SimulateEulaAccepted() { | |
| 115 SetEulaAcceptedPref(true); | |
| 116 content::NotificationService::current()->Notify( | |
| 117 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 118 content::NotificationService::AllSources(), | |
| 119 content::NotificationService::NoDetails()); | |
| 120 } | |
| 121 | |
| 122 void SetUp() { | |
| 123 // Set default EULA state to done (not waiting and EULA accepted) to | |
| 124 // simplify non-ChromeOS tests. | |
| 125 SetWasWaitingForEula(false); | |
| 126 SetEulaAcceptedPref(true); | |
| 127 } | |
| 128 #endif | |
| 129 | |
| 130 private: | |
| 131 MessageLoopForUI message_loop; | |
| 132 ScopedTestingLocalState scoped_testing_local_state_; | |
| 133 TestingPrefService* local_state_; | |
| 134 content::TestBrowserThread ui_thread; | |
| 135 TestNetworkChangeNotifier notifier; | |
| 136 TestRequesterService test_service; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | |
| 139 }; | |
| 140 | |
| 141 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) { | |
| 142 SetWasWaitingForNetwork(true); | |
| 143 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 144 EXPECT_FALSE(request_attempted()); | |
| 145 } | |
| 146 | |
| 147 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) { | |
| 148 SetWasWaitingForNetwork(false); | |
| 149 SimulateNetworkConnectionChange( | |
| 150 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 151 EXPECT_FALSE(request_attempted()); | |
| 152 } | |
| 153 | |
| 154 TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) { | |
| 155 SetWasWaitingForNetwork(true); | |
| 156 SimulateNetworkConnectionChange( | |
| 157 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 158 EXPECT_TRUE(request_attempted()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) { | |
| 162 SetWasWaitingForNetwork(false); | |
| 163 SimulateNetworkConnectionChange( | |
| 164 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 165 EXPECT_FALSE(request_attempted()); | |
| 166 SimulateNetworkConnectionChange( | |
| 167 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 168 EXPECT_FALSE(request_attempted()); | |
| 169 SimulateNetworkConnectionChange( | |
| 170 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 171 EXPECT_FALSE(request_attempted()); | |
| 172 SimulateNetworkConnectionChange( | |
| 173 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 174 EXPECT_FALSE(request_attempted()); | |
| 175 } | |
| 176 | |
| 177 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnFlakyConnection) { | |
| 178 SetWasWaitingForNetwork(false); | |
| 179 SimulateNetworkConnectionChange( | |
| 180 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 181 EXPECT_FALSE(request_attempted()); | |
| 182 SimulateNetworkConnectionChange( | |
| 183 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 184 EXPECT_FALSE(request_attempted()); | |
| 185 SimulateNetworkConnectionChange( | |
| 186 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 187 EXPECT_FALSE(request_attempted()); | |
| 188 } | |
| 189 | |
| 190 #if defined(OS_CHROMEOS) | |
| 191 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnly) { | |
| 192 SetWasWaitingForNetwork(true); | |
| 193 SetEulaAcceptedPref(false); | |
| 194 SimulateEulaAccepted(); | |
| 195 EXPECT_FALSE(request_attempted()); | |
| 196 } | |
| 197 | |
| 198 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | |
| 199 SetWasWaitingForNetwork(true); | |
| 200 SetEulaAcceptedPref(false); | |
| 201 SimulateEulaAccepted(); | |
| 202 EXPECT_FALSE(request_attempted()); | |
| 203 SimulateNetworkConnectionChange( | |
| 204 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 205 EXPECT_TRUE(request_attempted()); | |
| 206 } | |
| 207 | |
| 208 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | |
| 209 SetWasWaitingForNetwork(true); | |
| 210 SetWasWaitingForEula(true); | |
| 211 SetEulaAcceptedPref(false); | |
| 212 SimulateNetworkConnectionChange( | |
| 213 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 214 EXPECT_FALSE(request_attempted()); | |
| 215 SimulateEulaAccepted(); | |
| 216 EXPECT_TRUE(request_attempted()); | |
| 217 } | |
| 218 #endif // OS_CHROMEOS | |
| OLD | NEW |