1 // Copyright 2013 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.
5 #include "base/prefs/testing_pref_service.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/web_resource/eula_accepted_notifier.h"
8 #include "chrome/browser/web_resource/resource_request_allowed_notifier_test_util.h"
9 #include "chrome/test/base/testing_browser_process.h"
10 #include "content/public/browser/notification_service.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // Override NetworkChangeNotifier to simulate connection type changes for tests.
15 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
17 TestNetworkChangeNotifier()
18 : net::NetworkChangeNotifier(),
19 connection_type_to_return_(
20 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {
23 // Simulates a change of the connection type to |type|. This will notify any
24 // objects that are NetworkChangeNotifiers.
25 void SimulateNetworkConnectionChange(
26 net::NetworkChangeNotifier::ConnectionType type) {
27 connection_type_to_return_ = type;
28 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
29 base::MessageLoop::current()->RunUntilIdle();
33 ConnectionType GetCurrentConnectionType() const override {
34 return connection_type_to_return_;
37 // The currently simulated network connection type. If this is set to
38 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true.
39 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_;
41 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
44 // EulaAcceptedNotifier test class that allows mocking the EULA accepted state
45 // and issuing simulated notifications.
46 class TestEulaAcceptedNotifier : public EulaAcceptedNotifier {
48 TestEulaAcceptedNotifier()
49 : EulaAcceptedNotifier(NULL),
50 eula_accepted_(false) {
52 ~TestEulaAcceptedNotifier() override {}
54 bool IsEulaAccepted() override { return eula_accepted_; }
56 void SetEulaAcceptedForTesting(bool eula_accepted) {
57 eula_accepted_ = eula_accepted;
60 void SimulateEulaAccepted() {
67 DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier);
70 // A test fixture class for ResourceRequestAllowedNotifier tests that require
71 // network state simulations. This also acts as the service implementing the
72 // ResourceRequestAllowedNotifier::Observer interface.
73 class ResourceRequestAllowedNotifierTest
74 : public testing::Test,
75 public ResourceRequestAllowedNotifier::Observer {
77 ResourceRequestAllowedNotifierTest()
78 : ui_thread(content::BrowserThread::UI, &message_loop),
79 eula_notifier_(new TestEulaAcceptedNotifier),
80 was_notified_(false) {
81 resource_request_allowed_notifier_.InitWithEulaAcceptNotifier(
82 this, scoped_ptr<EulaAcceptedNotifier>(eula_notifier_));
84 ~ResourceRequestAllowedNotifierTest() override {}
86 bool was_notified() const { return was_notified_; }
88 // ResourceRequestAllowedNotifier::Observer override:
89 void OnResourceRequestsAllowed() override { was_notified_ = true; }
91 // Network manipulation methods:
92 void SetWaitingForNetwork(bool waiting) {
93 resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting);
96 void SimulateNetworkConnectionChange(
97 net::NetworkChangeNotifier::ConnectionType type) {
98 network_notifier.SimulateNetworkConnectionChange(type);
101 // Simulate a resource request from the test service. It returns true if
102 // resource request is allowed. Otherwise returns false and will change the
103 // result of was_notified() to true when the request is allowed.
104 bool SimulateResourceRequest() {
105 return resource_request_allowed_notifier_.ResourceRequestsAllowed();
108 void SimulateEulaAccepted() {
109 eula_notifier_->SimulateEulaAccepted();
112 // Eula manipulation methods:
113 void SetNeedsEulaAcceptance(bool needs_acceptance) {
114 eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance);
117 void SetWaitingForEula(bool waiting) {
118 resource_request_allowed_notifier_.SetWaitingForEulaForTesting(waiting);
121 // Used in tests involving the EULA. Disables both the EULA accepted state
123 void DisableEulaAndNetwork() {
124 SetWaitingForNetwork(true);
125 SimulateNetworkConnectionChange(
126 net::NetworkChangeNotifier::CONNECTION_NONE);
127 SetWaitingForEula(true);
128 SetNeedsEulaAcceptance(true);
131 void SetUp() override {
132 // Assume the test service has already requested permission, as all tests
133 // just test that criteria changes notify the server.
134 // Set default EULA state to done (not waiting and EULA accepted) to
135 // simplify non-ChromeOS tests.
136 SetWaitingForEula(false);
137 SetNeedsEulaAcceptance(false);
141 base::MessageLoopForUI message_loop;
142 content::TestBrowserThread ui_thread;
143 TestNetworkChangeNotifier network_notifier;
144 TestRequestAllowedNotifier resource_request_allowed_notifier_;
145 TestEulaAcceptedNotifier* eula_notifier_; // Weak, owned by RRAN.
148 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest);
151 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) {
152 SetWaitingForNetwork(true);
153 EXPECT_FALSE(SimulateResourceRequest());
154 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE);
155 EXPECT_FALSE(was_notified());
158 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) {
159 SetWaitingForNetwork(false);
160 EXPECT_TRUE(SimulateResourceRequest());
161 SimulateNetworkConnectionChange(
162 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
163 EXPECT_FALSE(was_notified());
166 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) {
167 SetWaitingForNetwork(true);
168 EXPECT_FALSE(SimulateResourceRequest());
169 SimulateNetworkConnectionChange(
170 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
171 EXPECT_TRUE(was_notified());
174 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) {
175 SetWaitingForNetwork(false);
176 EXPECT_TRUE(SimulateResourceRequest());
177 SimulateNetworkConnectionChange(
178 net::NetworkChangeNotifier::CONNECTION_WIFI);
179 EXPECT_FALSE(was_notified());
180 SimulateNetworkConnectionChange(
181 net::NetworkChangeNotifier::CONNECTION_3G);
182 EXPECT_FALSE(was_notified());
183 SimulateNetworkConnectionChange(
184 net::NetworkChangeNotifier::CONNECTION_4G);
185 EXPECT_FALSE(was_notified());
186 SimulateNetworkConnectionChange(
187 net::NetworkChangeNotifier::CONNECTION_WIFI);
188 EXPECT_FALSE(was_notified());
191 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) {
192 // SimulateResourceRequest() returns true because network is online.
193 SetWaitingForNetwork(false);
194 EXPECT_TRUE(SimulateResourceRequest());
195 // The callback is nerver invoked whatever happens on network connection.
196 SimulateNetworkConnectionChange(
197 net::NetworkChangeNotifier::CONNECTION_WIFI);
198 EXPECT_FALSE(was_notified());
199 SimulateNetworkConnectionChange(
200 net::NetworkChangeNotifier::CONNECTION_NONE);
201 EXPECT_FALSE(was_notified());
202 SimulateNetworkConnectionChange(
203 net::NetworkChangeNotifier::CONNECTION_WIFI);
204 EXPECT_FALSE(was_notified());
207 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnFlakyConnection) {
208 SetWaitingForNetwork(false);
209 EXPECT_TRUE(SimulateResourceRequest());
210 // Network goes online, but not notified because SimulateResourceRequest()
211 // returns true before.
212 SimulateNetworkConnectionChange(
213 net::NetworkChangeNotifier::CONNECTION_WIFI);
214 EXPECT_FALSE(was_notified());
215 SimulateNetworkConnectionChange(
216 net::NetworkChangeNotifier::CONNECTION_NONE);
217 EXPECT_FALSE(SimulateResourceRequest());
218 // Now, SimulateResourceRequest() returns false and will be notified later.
219 EXPECT_FALSE(was_notified());
220 SimulateNetworkConnectionChange(
221 net::NetworkChangeNotifier::CONNECTION_WIFI);
222 EXPECT_TRUE(was_notified());
225 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnEulaAfterGoOffline) {
226 DisableEulaAndNetwork();
227 EXPECT_FALSE(SimulateResourceRequest());
229 SimulateNetworkConnectionChange(
230 net::NetworkChangeNotifier::CONNECTION_WIFI);
231 EXPECT_FALSE(was_notified());
232 SimulateNetworkConnectionChange(
233 net::NetworkChangeNotifier::CONNECTION_NONE);
234 EXPECT_FALSE(was_notified());
235 SimulateEulaAccepted();
236 EXPECT_FALSE(was_notified());
239 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) {
240 // Ensure that if the observing service does not request access, it does not
241 // get notified, even if the criteria is met. Note that this is done by not
242 // calling SimulateResourceRequest here.
243 SetWaitingForNetwork(true);
244 SimulateNetworkConnectionChange(
245 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
246 EXPECT_FALSE(was_notified());
249 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) {
250 DisableEulaAndNetwork();
251 EXPECT_FALSE(SimulateResourceRequest());
253 SimulateEulaAccepted();
254 EXPECT_FALSE(was_notified());
257 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) {
258 DisableEulaAndNetwork();
259 EXPECT_FALSE(SimulateResourceRequest());
261 SimulateEulaAccepted();
262 EXPECT_FALSE(was_notified());
264 SimulateNetworkConnectionChange(
265 net::NetworkChangeNotifier::CONNECTION_WIFI);
266 EXPECT_TRUE(was_notified());
269 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) {
270 DisableEulaAndNetwork();
271 EXPECT_FALSE(SimulateResourceRequest());
273 SimulateNetworkConnectionChange(
274 net::NetworkChangeNotifier::CONNECTION_WIFI);
275 EXPECT_FALSE(was_notified());
277 SimulateEulaAccepted();
278 EXPECT_TRUE(was_notified());
281 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) {
282 // Ensure that if the observing service does not request access, it does not
283 // get notified, even if the criteria is met. Note that this is done by not
284 // calling SimulateResourceRequest here.
285 DisableEulaAndNetwork();
287 SimulateNetworkConnectionChange(
288 net::NetworkChangeNotifier::CONNECTION_WIFI);
289 EXPECT_FALSE(was_notified());
291 SimulateEulaAccepted();
292 EXPECT_FALSE(was_notified());