Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/prefs/testing_pref_service.h" | 5 #include "base/prefs/testing_pref_service.h" |
| 6 #include "chrome/browser/metrics/variations/eula_accepted_notifier.h" | |
| 6 #include "chrome/browser/metrics/variations/resource_request_allowed_notifier_te st_util.h" | 7 #include "chrome/browser/metrics/variations/resource_request_allowed_notifier_te st_util.h" |
| 7 #include "chrome/common/chrome_notification_types.h" | 8 #include "chrome/common/chrome_notification_types.h" |
| 8 #include "chrome/test/base/testing_browser_process.h" | 9 #include "chrome/test/base/testing_browser_process.h" |
| 9 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| 10 #include "content/public/test/test_browser_thread.h" | 11 #include "content/public/test/test_browser_thread.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 // Override NetworkChangeNotifier to simulate connection type changes for tests. | 14 // Override NetworkChangeNotifier to simulate connection type changes for tests. |
| 14 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | 15 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { |
| 15 public: | 16 public: |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 33 return connection_type_to_return_; | 34 return connection_type_to_return_; |
| 34 } | 35 } |
| 35 | 36 |
| 36 // The currently simulated network connection type. If this is set to | 37 // The currently simulated network connection type. If this is set to |
| 37 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true. | 38 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true. |
| 38 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; | 39 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; |
| 39 | 40 |
| 40 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | 41 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| 41 }; | 42 }; |
| 42 | 43 |
| 44 // EulaAcceptedNotifier test class that allows mocking the eula accepted state | |
|
SteveT
2013/04/05 22:16:41
eula -> EULA
Alexei Svitkine (slow)
2013/04/08 14:48:41
Done.
| |
| 45 // and issuing simulated notifications. | |
| 46 class TestEulaAcceptedNotifier : public EulaAcceptedNotifier { | |
| 47 public: | |
| 48 TestEulaAcceptedNotifier() : eula_accepted_(false) { | |
| 49 } | |
| 50 virtual ~TestEulaAcceptedNotifier() { | |
| 51 } | |
| 52 | |
| 53 virtual bool IsEulaAccepted() OVERRIDE { | |
| 54 return eula_accepted_; | |
| 55 } | |
| 56 | |
| 57 void SetEulaAcceptedForTesting(bool eula_accepted) { | |
| 58 eula_accepted_ = eula_accepted; | |
| 59 } | |
| 60 | |
| 61 void SimulateEulaAccepted() { | |
| 62 NotifyObserver(); | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 bool eula_accepted_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier); | |
| 69 }; | |
| 70 | |
| 43 // A test fixture class for ResourceRequestAllowedNotifier tests that require | 71 // A test fixture class for ResourceRequestAllowedNotifier tests that require |
| 44 // network state simulations. This also acts as the service implementing the | 72 // network state simulations. This also acts as the service implementing the |
| 45 // ResourceRequestAllowedNotifier::Observer interface. | 73 // ResourceRequestAllowedNotifier::Observer interface. |
| 46 class ResourceRequestAllowedNotifierTest | 74 class ResourceRequestAllowedNotifierTest |
| 47 : public testing::Test, | 75 : public testing::Test, |
| 48 public ResourceRequestAllowedNotifier::Observer { | 76 public ResourceRequestAllowedNotifier::Observer { |
| 49 public: | 77 public: |
| 50 ResourceRequestAllowedNotifierTest() | 78 ResourceRequestAllowedNotifierTest() |
| 51 : ui_thread(content::BrowserThread::UI, &message_loop), | 79 : ui_thread(content::BrowserThread::UI, &message_loop), |
| 80 eula_notifier_(new TestEulaAcceptedNotifier), | |
| 52 was_notified_(false) { | 81 was_notified_(false) { |
| 53 #if defined(OS_CHROMEOS) | 82 resource_request_allowed_notifier_.InitWithEulaAcceptNotifier( |
| 54 // Set this flag to true so the Init call sets up the wait on the EULA. | 83 this, scoped_ptr<EulaAcceptedNotifier>(eula_notifier_)); |
| 55 SetNeedsEulaAcceptance(true); | |
| 56 #endif | |
| 57 resource_request_allowed_notifier_.Init(this); | |
| 58 } | 84 } |
| 59 virtual ~ResourceRequestAllowedNotifierTest() { } | 85 virtual ~ResourceRequestAllowedNotifierTest() { } |
| 60 | 86 |
| 61 bool was_notified() const { return was_notified_; } | 87 bool was_notified() const { return was_notified_; } |
| 62 | 88 |
| 63 // ResourceRequestAllowedNotifier::Observer override: | 89 // ResourceRequestAllowedNotifier::Observer override: |
| 64 virtual void OnResourceRequestsAllowed() OVERRIDE { | 90 virtual void OnResourceRequestsAllowed() OVERRIDE { |
| 65 was_notified_ = true; | 91 was_notified_ = true; |
| 66 } | 92 } |
| 67 | 93 |
| 68 // Network manipulation methods: | 94 // Network manipulation methods: |
| 69 void SetWasWaitingForNetwork(bool waiting) { | 95 void SetWaitingForNetwork(bool waiting) { |
| 70 resource_request_allowed_notifier_. | 96 resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting); |
| 71 SetWasWaitingForNetworkForTesting(waiting); | |
| 72 } | 97 } |
| 73 | 98 |
| 74 void SimulateNetworkConnectionChange( | 99 void SimulateNetworkConnectionChange( |
| 75 net::NetworkChangeNotifier::ConnectionType type) { | 100 net::NetworkChangeNotifier::ConnectionType type) { |
| 76 network_notifier.SimulateNetworkConnectionChange(type); | 101 network_notifier.SimulateNetworkConnectionChange(type); |
| 77 } | 102 } |
| 78 | 103 |
| 79 // Simulate a resource request from the test service. | 104 // Simulate a resource request from the test service. |
| 80 void SimulateResourceRequest() { | 105 void SimulateResourceRequest() { |
| 81 resource_request_allowed_notifier_.ResourceRequestsAllowed(); | 106 resource_request_allowed_notifier_.ResourceRequestsAllowed(); |
| 82 } | 107 } |
| 83 | 108 |
| 84 #if defined(OS_CHROMEOS) | 109 void SimulateEulaAccepted() { |
| 110 eula_notifier_->SimulateEulaAccepted(); | |
| 111 } | |
| 112 | |
| 85 // Eula manipulation methods: | 113 // Eula manipulation methods: |
| 86 void SetNeedsEulaAcceptance(bool needs_acceptance) { | 114 void SetNeedsEulaAcceptance(bool needs_acceptance) { |
| 87 resource_request_allowed_notifier_.SetNeedsEulaAcceptance(needs_acceptance); | 115 eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance); |
| 88 } | 116 } |
| 89 | 117 |
| 90 void SetWasWaitingForEula(bool waiting) { | 118 void SetWaitingForEula(bool waiting) { |
| 91 resource_request_allowed_notifier_.SetWasWaitingForEulaForTesting(waiting); | 119 resource_request_allowed_notifier_.SetWaitingForEulaForTesting(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 } | 120 } |
| 101 | 121 |
| 102 // Used in tests involving the EULA. Disables both the EULA accepted state | 122 // Used in tests involving the EULA. Disables both the EULA accepted state |
| 103 // and the network. | 123 // and the network. |
| 104 void DisableEulaAndNetwork() { | 124 void DisableEulaAndNetwork() { |
| 105 SetWasWaitingForNetwork(true); | 125 SetWaitingForNetwork(true); |
| 106 SimulateNetworkConnectionChange( | 126 SimulateNetworkConnectionChange( |
| 107 net::NetworkChangeNotifier::CONNECTION_NONE); | 127 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 108 SetWasWaitingForEula(true); | 128 SetWaitingForEula(true); |
| 109 SetNeedsEulaAcceptance(true); | 129 SetNeedsEulaAcceptance(true); |
| 110 } | 130 } |
| 111 #endif | |
| 112 | 131 |
| 113 virtual void SetUp() OVERRIDE { | 132 virtual void SetUp() OVERRIDE { |
| 114 // Assume the test service has already requested permission, as all tests | 133 // Assume the test service has already requested permission, as all tests |
| 115 // just test that criteria changes notify the server. | 134 // 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 | 135 // Set default EULA state to done (not waiting and EULA accepted) to |
| 118 // simplify non-ChromeOS tests. | 136 // simplify non-ChromeOS tests. |
| 119 SetWasWaitingForEula(false); | 137 SetWaitingForEula(false); |
| 120 SetNeedsEulaAcceptance(false); | 138 SetNeedsEulaAcceptance(false); |
| 121 #endif | |
| 122 } | 139 } |
| 123 | 140 |
| 124 private: | 141 private: |
| 125 MessageLoopForUI message_loop; | 142 MessageLoopForUI message_loop; |
| 126 content::TestBrowserThread ui_thread; | 143 content::TestBrowserThread ui_thread; |
| 127 TestNetworkChangeNotifier network_notifier; | 144 TestNetworkChangeNotifier network_notifier; |
| 128 TestRequestAllowedNotifier resource_request_allowed_notifier_; | 145 TestRequestAllowedNotifier resource_request_allowed_notifier_; |
| 146 TestEulaAcceptedNotifier* eula_notifier_; // Weak, owned by RRAN. | |
| 129 bool was_notified_; | 147 bool was_notified_; |
| 130 | 148 |
| 131 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | 149 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); |
| 132 }; | 150 }; |
| 133 | 151 |
| 134 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { | 152 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) { |
| 135 SimulateResourceRequest(); | 153 SimulateResourceRequest(); |
| 136 SetWasWaitingForNetwork(true); | 154 SetWaitingForNetwork(true); |
| 137 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | 155 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); |
| 138 EXPECT_FALSE(was_notified()); | 156 EXPECT_FALSE(was_notified()); |
| 139 } | 157 } |
| 140 | 158 |
| 141 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { | 159 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) { |
| 142 SimulateResourceRequest(); | 160 SimulateResourceRequest(); |
| 143 SetWasWaitingForNetwork(false); | 161 SetWaitingForNetwork(false); |
| 144 SimulateNetworkConnectionChange( | 162 SimulateNetworkConnectionChange( |
| 145 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 163 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 146 EXPECT_FALSE(was_notified()); | 164 EXPECT_FALSE(was_notified()); |
| 147 } | 165 } |
| 148 | 166 |
| 149 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { | 167 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) { |
| 150 SimulateResourceRequest(); | 168 SimulateResourceRequest(); |
| 151 SetWasWaitingForNetwork(true); | 169 SetWaitingForNetwork(true); |
| 152 SimulateNetworkConnectionChange( | 170 SimulateNetworkConnectionChange( |
| 153 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 171 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 154 EXPECT_TRUE(was_notified()); | 172 EXPECT_TRUE(was_notified()); |
| 155 } | 173 } |
| 156 | 174 |
| 157 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { | 175 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) { |
| 158 SimulateResourceRequest(); | 176 SimulateResourceRequest(); |
| 159 SetWasWaitingForNetwork(false); | 177 SetWaitingForNetwork(false); |
| 160 SimulateNetworkConnectionChange( | 178 SimulateNetworkConnectionChange( |
| 161 net::NetworkChangeNotifier::CONNECTION_WIFI); | 179 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 162 EXPECT_FALSE(was_notified()); | 180 EXPECT_FALSE(was_notified()); |
| 163 SimulateNetworkConnectionChange( | 181 SimulateNetworkConnectionChange( |
| 164 net::NetworkChangeNotifier::CONNECTION_3G); | 182 net::NetworkChangeNotifier::CONNECTION_3G); |
| 165 EXPECT_FALSE(was_notified()); | 183 EXPECT_FALSE(was_notified()); |
| 166 SimulateNetworkConnectionChange( | 184 SimulateNetworkConnectionChange( |
| 167 net::NetworkChangeNotifier::CONNECTION_4G); | 185 net::NetworkChangeNotifier::CONNECTION_4G); |
| 168 EXPECT_FALSE(was_notified()); | 186 EXPECT_FALSE(was_notified()); |
| 169 SimulateNetworkConnectionChange( | 187 SimulateNetworkConnectionChange( |
| 170 net::NetworkChangeNotifier::CONNECTION_WIFI); | 188 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 171 EXPECT_FALSE(was_notified()); | 189 EXPECT_FALSE(was_notified()); |
| 172 } | 190 } |
| 173 | 191 |
| 174 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { | 192 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) { |
| 175 SimulateResourceRequest(); | 193 SimulateResourceRequest(); |
| 176 SetWasWaitingForNetwork(false); | 194 SetWaitingForNetwork(false); |
| 177 SimulateNetworkConnectionChange( | 195 SimulateNetworkConnectionChange( |
| 178 net::NetworkChangeNotifier::CONNECTION_WIFI); | 196 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 179 EXPECT_FALSE(was_notified()); | 197 EXPECT_FALSE(was_notified()); |
| 180 SimulateNetworkConnectionChange( | 198 SimulateNetworkConnectionChange( |
| 181 net::NetworkChangeNotifier::CONNECTION_NONE); | 199 net::NetworkChangeNotifier::CONNECTION_NONE); |
| 182 EXPECT_FALSE(was_notified()); | 200 EXPECT_FALSE(was_notified()); |
| 183 SimulateNetworkConnectionChange( | 201 SimulateNetworkConnectionChange( |
| 184 net::NetworkChangeNotifier::CONNECTION_WIFI); | 202 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 185 EXPECT_FALSE(was_notified()); | 203 EXPECT_FALSE(was_notified()); |
| 186 } | 204 } |
| 187 | 205 |
| 188 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { | 206 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { |
| 189 // Ensure that if the observing service does not request access, it does not | 207 // 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 | 208 // get notified, even if the criteria is met. Note that this is done by not |
| 191 // calling SimulateResourceRequest here. | 209 // calling SimulateResourceRequest here. |
| 192 SetWasWaitingForNetwork(true); | 210 SetWaitingForNetwork(true); |
| 193 SimulateNetworkConnectionChange( | 211 SimulateNetworkConnectionChange( |
| 194 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 212 net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| 195 EXPECT_FALSE(was_notified()); | 213 EXPECT_FALSE(was_notified()); |
| 196 } | 214 } |
| 197 | 215 |
| 198 #if defined(OS_CHROMEOS) | |
| 199 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { | 216 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) { |
| 200 SimulateResourceRequest(); | 217 SimulateResourceRequest(); |
| 201 DisableEulaAndNetwork(); | 218 DisableEulaAndNetwork(); |
| 202 | 219 |
| 203 SimulateEulaAccepted(); | 220 SimulateEulaAccepted(); |
| 204 EXPECT_FALSE(was_notified()); | 221 EXPECT_FALSE(was_notified()); |
| 205 } | 222 } |
| 206 | 223 |
| 207 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | 224 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { |
| 208 SimulateResourceRequest(); | 225 SimulateResourceRequest(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 234 // calling SimulateResourceRequest here. | 251 // calling SimulateResourceRequest here. |
| 235 DisableEulaAndNetwork(); | 252 DisableEulaAndNetwork(); |
| 236 | 253 |
| 237 SimulateNetworkConnectionChange( | 254 SimulateNetworkConnectionChange( |
| 238 net::NetworkChangeNotifier::CONNECTION_WIFI); | 255 net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 239 EXPECT_FALSE(was_notified()); | 256 EXPECT_FALSE(was_notified()); |
| 240 | 257 |
| 241 SimulateEulaAccepted(); | 258 SimulateEulaAccepted(); |
| 242 EXPECT_FALSE(was_notified()); | 259 EXPECT_FALSE(was_notified()); |
| 243 } | 260 } |
| 244 #endif // OS_CHROMEOS | |
| OLD | NEW |