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::NotifyObserversOfConnectionTypeChangeForTests(
27 connection_type_to_return_
);
28 base::MessageLoop::current()->RunUntilIdle();
32 ConnectionType
GetCurrentConnectionType() const override
{
33 return connection_type_to_return_
;
36 // The currently simulated network connection type. If this is set to
37 // CONNECTION_NONE, then NetworkChangeNotifier::IsOffline will return true.
38 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_
;
40 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier
);
43 // EulaAcceptedNotifier test class that allows mocking the EULA accepted state
44 // and issuing simulated notifications.
45 class TestEulaAcceptedNotifier
: public EulaAcceptedNotifier
{
47 TestEulaAcceptedNotifier()
48 : EulaAcceptedNotifier(nullptr),
49 eula_accepted_(false) {
51 ~TestEulaAcceptedNotifier() override
{}
53 bool IsEulaAccepted() override
{ return eula_accepted_
; }
55 void SetEulaAcceptedForTesting(bool eula_accepted
) {
56 eula_accepted_
= eula_accepted
;
59 void SimulateEulaAccepted() {
66 DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier
);
69 // A test fixture class for ResourceRequestAllowedNotifier tests that require
70 // network state simulations. This also acts as the service implementing the
71 // ResourceRequestAllowedNotifier::Observer interface.
72 class ResourceRequestAllowedNotifierTest
73 : public testing::Test
,
74 public ResourceRequestAllowedNotifier::Observer
{
76 ResourceRequestAllowedNotifierTest()
77 : resource_request_allowed_notifier_(&prefs_
),
78 eula_notifier_(new TestEulaAcceptedNotifier
),
79 was_notified_(false) {
80 resource_request_allowed_notifier_
.InitWithEulaAcceptNotifier(
81 this, scoped_ptr
<EulaAcceptedNotifier
>(eula_notifier_
));
83 ~ResourceRequestAllowedNotifierTest() override
{}
85 bool was_notified() const { return was_notified_
; }
87 // ResourceRequestAllowedNotifier::Observer override:
88 void OnResourceRequestsAllowed() override
{ was_notified_
= true; }
90 // Network manipulation methods:
91 void SetWaitingForNetwork(bool waiting
) {
92 resource_request_allowed_notifier_
.SetWaitingForNetworkForTesting(waiting
);
95 void SimulateNetworkConnectionChange(
96 net::NetworkChangeNotifier::ConnectionType type
) {
97 network_notifier
.SimulateNetworkConnectionChange(type
);
100 // Simulate a resource request from the test service. It returns true if
101 // resource request is allowed. Otherwise returns false and will change the
102 // result of was_notified() to true when the request is allowed.
103 bool SimulateResourceRequest() {
104 return resource_request_allowed_notifier_
.ResourceRequestsAllowed();
107 void SimulateEulaAccepted() {
108 eula_notifier_
->SimulateEulaAccepted();
111 // Eula manipulation methods:
112 void SetNeedsEulaAcceptance(bool needs_acceptance
) {
113 eula_notifier_
->SetEulaAcceptedForTesting(!needs_acceptance
);
116 void SetWaitingForEula(bool waiting
) {
117 resource_request_allowed_notifier_
.SetWaitingForEulaForTesting(waiting
);
120 // Used in tests involving the EULA. Disables both the EULA accepted state
122 void DisableEulaAndNetwork() {
123 SetWaitingForNetwork(true);
124 SimulateNetworkConnectionChange(
125 net::NetworkChangeNotifier::CONNECTION_NONE
);
126 SetWaitingForEula(true);
127 SetNeedsEulaAcceptance(true);
130 void SetUp() override
{
131 // Assume the test service has already requested permission, as all tests
132 // just test that criteria changes notify the server.
133 // Set default EULA state to done (not waiting and EULA accepted) to
134 // simplify non-ChromeOS tests.
135 SetWaitingForEula(false);
136 SetNeedsEulaAcceptance(false);
140 base::MessageLoopForUI message_loop
;
141 TestNetworkChangeNotifier network_notifier
;
142 TestingPrefServiceSimple prefs_
;
143 TestRequestAllowedNotifier resource_request_allowed_notifier_
;
144 TestEulaAcceptedNotifier
* eula_notifier_
; // Weak, owned by RRAN.
147 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest
);
150 TEST_F(ResourceRequestAllowedNotifierTest
, DoNotNotifyIfOffline
) {
151 SetWaitingForNetwork(true);
152 EXPECT_FALSE(SimulateResourceRequest());
153 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
154 EXPECT_FALSE(was_notified());
157 TEST_F(ResourceRequestAllowedNotifierTest
, DoNotNotifyIfOnlineToOnline
) {
158 SetWaitingForNetwork(false);
159 EXPECT_TRUE(SimulateResourceRequest());
160 SimulateNetworkConnectionChange(
161 net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
162 EXPECT_FALSE(was_notified());
165 TEST_F(ResourceRequestAllowedNotifierTest
, NotifyOnReconnect
) {
166 SetWaitingForNetwork(true);
167 EXPECT_FALSE(SimulateResourceRequest());
168 SimulateNetworkConnectionChange(
169 net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
170 EXPECT_TRUE(was_notified());
173 TEST_F(ResourceRequestAllowedNotifierTest
, NoNotifyOnWardriving
) {
174 SetWaitingForNetwork(false);
175 EXPECT_TRUE(SimulateResourceRequest());
176 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
177 EXPECT_FALSE(was_notified());
178 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_3G
);
179 EXPECT_FALSE(was_notified());
180 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_4G
);
181 EXPECT_FALSE(was_notified());
182 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
183 EXPECT_FALSE(was_notified());
186 TEST_F(ResourceRequestAllowedNotifierTest
, NoNotifyOnFlakyConnection
) {
187 // SimulateResourceRequest() returns true because network is online.
188 SetWaitingForNetwork(false);
189 EXPECT_TRUE(SimulateResourceRequest());
190 // The callback is nerver invoked whatever happens on network connection.
191 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
192 EXPECT_FALSE(was_notified());
193 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
194 EXPECT_FALSE(was_notified());
195 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
196 EXPECT_FALSE(was_notified());
199 TEST_F(ResourceRequestAllowedNotifierTest
, NotifyOnFlakyConnection
) {
200 SetWaitingForNetwork(false);
201 EXPECT_TRUE(SimulateResourceRequest());
202 // Network goes online, but not notified because SimulateResourceRequest()
203 // returns true before.
204 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
205 EXPECT_FALSE(was_notified());
206 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
207 EXPECT_FALSE(SimulateResourceRequest());
208 // Now, SimulateResourceRequest() returns false and will be notified later.
209 EXPECT_FALSE(was_notified());
210 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
211 EXPECT_TRUE(was_notified());
214 TEST_F(ResourceRequestAllowedNotifierTest
, NoNotifyOnEulaAfterGoOffline
) {
215 DisableEulaAndNetwork();
216 EXPECT_FALSE(SimulateResourceRequest());
218 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
219 EXPECT_FALSE(was_notified());
220 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE
);
221 EXPECT_FALSE(was_notified());
222 SimulateEulaAccepted();
223 EXPECT_FALSE(was_notified());
226 TEST_F(ResourceRequestAllowedNotifierTest
, NoRequestNoNotify
) {
227 // Ensure that if the observing service does not request access, it does not
228 // get notified, even if the criteria is met. Note that this is done by not
229 // calling SimulateResourceRequest here.
230 SetWaitingForNetwork(true);
231 SimulateNetworkConnectionChange(
232 net::NetworkChangeNotifier::CONNECTION_ETHERNET
);
233 EXPECT_FALSE(was_notified());
236 TEST_F(ResourceRequestAllowedNotifierTest
, EulaOnlyNetworkOffline
) {
237 DisableEulaAndNetwork();
238 EXPECT_FALSE(SimulateResourceRequest());
240 SimulateEulaAccepted();
241 EXPECT_FALSE(was_notified());
244 TEST_F(ResourceRequestAllowedNotifierTest
, EulaFirst
) {
245 DisableEulaAndNetwork();
246 EXPECT_FALSE(SimulateResourceRequest());
248 SimulateEulaAccepted();
249 EXPECT_FALSE(was_notified());
251 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
252 EXPECT_TRUE(was_notified());
255 TEST_F(ResourceRequestAllowedNotifierTest
, NetworkFirst
) {
256 DisableEulaAndNetwork();
257 EXPECT_FALSE(SimulateResourceRequest());
259 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
260 EXPECT_FALSE(was_notified());
262 SimulateEulaAccepted();
263 EXPECT_TRUE(was_notified());
266 TEST_F(ResourceRequestAllowedNotifierTest
, NoRequestNoNotifyEula
) {
267 // Ensure that if the observing service does not request access, it does not
268 // get notified, even if the criteria is met. Note that this is done by not
269 // calling SimulateResourceRequest here.
270 DisableEulaAndNetwork();
272 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_WIFI
);
273 EXPECT_FALSE(was_notified());
275 SimulateEulaAccepted();
276 EXPECT_FALSE(was_notified());
279 } // namespace web_resource