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 "components/web_resource/eula_accepted_notifier.h"
7 #include "components/web_resource/resource_request_allowed_notifier_test_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace web_resource
{
12 // Override NetworkChangeNotifier to simulate connection type changes for tests.
13 class TestNetworkChangeNotifier
: public net::NetworkChangeNotifier
{
15 TestNetworkChangeNotifier()
16 : net::NetworkChangeNotifier(),
17 connection_type_to_return_(
18 net::NetworkChangeNotifier::CONNECTION_UNKNOWN
) {
21 // Simulates a change of the connection type to |type|. This will notify any
22 // objects that are NetworkChangeNotifiers.
23 void SimulateNetworkConnectionChange(
24 net::NetworkChangeNotifier::ConnectionType type
) {
25 connection_type_to_return_
= type
;
26 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
27 base::MessageLoop::current()->RunUntilIdle();
31 ConnectionType
GetCurrentConnectionType() const override
{
32 return connection_type_to_return_
;
35 // The currently simulated network connection type. If this is set to
36 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true.
37 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_
;
39 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier
);
42 // EulaAcceptedNotifier test class that allows mocking the EULA accepted state
43 // and issuing simulated notifications.
44 class TestEulaAcceptedNotifier
: public EulaAcceptedNotifier
{
46 TestEulaAcceptedNotifier()
47 : EulaAcceptedNotifier(nullptr),
48 eula_accepted_(false) {
50 ~TestEulaAcceptedNotifier() override
{}
52 bool IsEulaAccepted() override
{ return eula_accepted_
; }
54 void SetEulaAcceptedForTesting(bool eula_accepted
) {
55 eula_accepted_
= eula_accepted
;
58 void SimulateEulaAccepted() {
65 DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier
);
68 // A test fixture class for ResourceRequestAllowedNotifier tests that require
69 // network state simulations. This also acts as the service implementing the
70 // ResourceRequestAllowedNotifier::Observer interface.
71 class ResourceRequestAllowedNotifierTest
72 : public testing::Test
,
73 public ResourceRequestAllowedNotifier::Observer
{
75 ResourceRequestAllowedNotifierTest()
76 : resource_request_allowed_notifier_(&prefs_
),
77 eula_notifier_(new TestEulaAcceptedNotifier
),
78 was_notified_(false) {
79 resource_request_allowed_notifier_
.InitWithEulaAcceptNotifier(
80 this, scoped_ptr
<EulaAcceptedNotifier
>(eula_notifier_
));
82 ~ResourceRequestAllowedNotifierTest() override
{}
84 bool was_notified() const { return was_notified_
; }
86 // ResourceRequestAllowedNotifier::Observer override:
87 void OnResourceRequestsAllowed() override
{ was_notified_
= true; }
89 // Network manipulation methods:
90 void SetWaitingForNetwork(bool waiting
) {
91 resource_request_allowed_notifier_
.SetWaitingForNetworkForTesting(waiting
);
94 void SimulateNetworkConnectionChange(
95 net::NetworkChangeNotifier::ConnectionType type
) {
96 network_notifier
.SimulateNetworkConnectionChange(type
);
99 // Simulate a resource request from the test service. It returns true if
100 // resource request is allowed. Otherwise returns false and will change the
101 // result of was_notified() to true when the request is allowed.
102 bool SimulateResourceRequest() {
103 return resource_request_allowed_notifier_
.ResourceRequestsAllowed();
106 void SimulateEulaAccepted() {
107 eula_notifier_
->SimulateEulaAccepted();
110 // Eula manipulation methods:
111 void SetNeedsEulaAcceptance(bool needs_acceptance
) {
112 eula_notifier_
->SetEulaAcceptedForTesting(!needs_acceptance
);
115 void SetWaitingForEula(bool waiting
) {
116 resource_request_allowed_notifier_
.SetWaitingForEulaForTesting(waiting
);
119 // Used in tests involving the EULA. Disables both the EULA accepted state
121 void DisableEulaAndNetwork() {
122 SetWaitingForNetwork(true);
123 SimulateNetworkConnectionChange(
124 net::NetworkChangeNotifier::CONNECTION_NONE
);
125 SetWaitingForEula(true);
126 SetNeedsEulaAcceptance(true);
129 void SetUp() override
{
130 // Assume the test service has already requested permission, as all tests
131 // just test that criteria changes notify the server.
132 // Set default EULA state to done (not waiting and EULA accepted) to
133 // simplify non-ChromeOS tests.
134 SetWaitingForEula(false);
135 SetNeedsEulaAcceptance(false);
139 base::MessageLoopForUI message_loop
;
140 TestNetworkChangeNotifier network_notifier
;
141 TestingPrefServiceSimple prefs_
;
142 TestRequestAllowedNotifier resource_request_allowed_notifier_
;
143 TestEulaAcceptedNotifier
* eula_notifier_
; // Weak, owned by RRAN.
146 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest
);
149 TEST_F(ResourceRequestAllowedNotifierTest
, DoNotNotifyIfOffline
) {
150 SetWaitingForNetwork(true);
151 EXPECT_FALSE(SimulateResourceRequest());
152 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
153 EXPECT_FALSE(was_notified());
156 TEST_F(ResourceRequestAllowedNotifierTest
, DoNotNotifyIfOnlineToOnline
) {
157 SetWaitingForNetwork(false);
158 EXPECT_TRUE(SimulateResourceRequest());
159 SimulateNetworkConnectionChange(
160 net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
161 EXPECT_FALSE(was_notified());
164 TEST_F(ResourceRequestAllowedNotifierTest
, NotifyOnReconnect
) {
165 SetWaitingForNetwork(true);
166 EXPECT_FALSE(SimulateResourceRequest());
167 SimulateNetworkConnectionChange(
168 net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
169 EXPECT_TRUE(was_notified());
172 TEST_F(ResourceRequestAllowedNotifierTest
, NoNotifyOnWardriving
) {
173 SetWaitingForNetwork(false);
174 EXPECT_TRUE(SimulateResourceRequest());
175 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
176 EXPECT_FALSE(was_notified());
177 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_3G
);
178 EXPECT_FALSE(was_notified());
179 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_4G
);
180 EXPECT_FALSE(was_notified());
181 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
182 EXPECT_FALSE(was_notified());
185 TEST_F(ResourceRequestAllowedNotifierTest
, NoNotifyOnFlakyConnection
) {
186 // SimulateResourceRequest() returns true because network is online.
187 SetWaitingForNetwork(false);
188 EXPECT_TRUE(SimulateResourceRequest());
189 // The callback is nerver invoked whatever happens on network connection.
190 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
191 EXPECT_FALSE(was_notified());
192 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
193 EXPECT_FALSE(was_notified());
194 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
195 EXPECT_FALSE(was_notified());
198 TEST_F(ResourceRequestAllowedNotifierTest
, NotifyOnFlakyConnection
) {
199 SetWaitingForNetwork(false);
200 EXPECT_TRUE(SimulateResourceRequest());
201 // Network goes online, but not notified because SimulateResourceRequest()
202 // returns true before.
203 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
204 EXPECT_FALSE(was_notified());
205 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
206 EXPECT_FALSE(SimulateResourceRequest());
207 // Now, SimulateResourceRequest() returns false and will be notified later.
208 EXPECT_FALSE(was_notified());
209 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
210 EXPECT_TRUE(was_notified());
213 TEST_F(ResourceRequestAllowedNotifierTest
, NoNotifyOnEulaAfterGoOffline
) {
214 DisableEulaAndNetwork();
215 EXPECT_FALSE(SimulateResourceRequest());
217 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
218 EXPECT_FALSE(was_notified());
219 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
220 EXPECT_FALSE(was_notified());
221 SimulateEulaAccepted();
222 EXPECT_FALSE(was_notified());
225 TEST_F(ResourceRequestAllowedNotifierTest
, NoRequestNoNotify
) {
226 // Ensure that if the observing service does not request access, it does not
227 // get notified, even if the criteria is met. Note that this is done by not
228 // calling SimulateResourceRequest here.
229 SetWaitingForNetwork(true);
230 SimulateNetworkConnectionChange(
231 net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
232 EXPECT_FALSE(was_notified());
235 TEST_F(ResourceRequestAllowedNotifierTest
, EulaOnlyNetworkOffline
) {
236 DisableEulaAndNetwork();
237 EXPECT_FALSE(SimulateResourceRequest());
239 SimulateEulaAccepted();
240 EXPECT_FALSE(was_notified());
243 TEST_F(ResourceRequestAllowedNotifierTest
, EulaFirst
) {
244 DisableEulaAndNetwork();
245 EXPECT_FALSE(SimulateResourceRequest());
247 SimulateEulaAccepted();
248 EXPECT_FALSE(was_notified());
250 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
251 EXPECT_TRUE(was_notified());
254 TEST_F(ResourceRequestAllowedNotifierTest
, NetworkFirst
) {
255 DisableEulaAndNetwork();
256 EXPECT_FALSE(SimulateResourceRequest());
258 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
259 EXPECT_FALSE(was_notified());
261 SimulateEulaAccepted();
262 EXPECT_TRUE(was_notified());
265 TEST_F(ResourceRequestAllowedNotifierTest
, NoRequestNoNotifyEula
) {
266 // Ensure that if the observing service does not request access, it does not
267 // get notified, even if the criteria is met. Note that this is done by not
268 // calling SimulateResourceRequest here.
269 DisableEulaAndNetwork();
271 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
272 EXPECT_FALSE(was_notified());
274 SimulateEulaAccepted();
275 EXPECT_FALSE(was_notified());
278 } // namespace web_resource