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