Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / web_resource / resource_request_allowed_notifier_unittest.cc
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.
4
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"
13
14 // Override NetworkChangeNotifier to simulate connection type changes for tests.
15 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
16  public:
17   TestNetworkChangeNotifier()
18       : net::NetworkChangeNotifier(),
19         connection_type_to_return_(
20             net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {
21   }
22
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();
30   }
31
32  private:
33   ConnectionType GetCurrentConnectionType() const override {
34     return connection_type_to_return_;
35   }
36
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_;
40
41   DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
42 };
43
44 // EulaAcceptedNotifier test class that allows mocking the EULA accepted state
45 // and issuing simulated notifications.
46 class TestEulaAcceptedNotifier : public EulaAcceptedNotifier {
47  public:
48   TestEulaAcceptedNotifier()
49       : EulaAcceptedNotifier(NULL),
50         eula_accepted_(false) {
51   }
52   ~TestEulaAcceptedNotifier() override {}
53
54   bool IsEulaAccepted() override { return eula_accepted_; }
55
56   void SetEulaAcceptedForTesting(bool eula_accepted) {
57     eula_accepted_ = eula_accepted;
58   }
59
60   void SimulateEulaAccepted() {
61     NotifyObserver();
62   }
63
64  private:
65   bool eula_accepted_;
66
67   DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier);
68 };
69
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 {
76  public:
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_));
83   }
84   ~ResourceRequestAllowedNotifierTest() override {}
85
86   bool was_notified() const { return was_notified_; }
87
88   // ResourceRequestAllowedNotifier::Observer override:
89   void OnResourceRequestsAllowed() override { was_notified_ = true; }
90
91   // Network manipulation methods:
92   void SetWaitingForNetwork(bool waiting) {
93     resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting);
94   }
95
96   void SimulateNetworkConnectionChange(
97       net::NetworkChangeNotifier::ConnectionType type) {
98     network_notifier.SimulateNetworkConnectionChange(type);
99   }
100
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();
106   }
107
108   void SimulateEulaAccepted() {
109     eula_notifier_->SimulateEulaAccepted();
110   }
111
112   // Eula manipulation methods:
113   void SetNeedsEulaAcceptance(bool needs_acceptance) {
114     eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance);
115   }
116
117   void SetWaitingForEula(bool waiting) {
118     resource_request_allowed_notifier_.SetWaitingForEulaForTesting(waiting);
119   }
120
121   // Used in tests involving the EULA. Disables both the EULA accepted state
122   // and the network.
123   void DisableEulaAndNetwork() {
124     SetWaitingForNetwork(true);
125     SimulateNetworkConnectionChange(
126         net::NetworkChangeNotifier::CONNECTION_NONE);
127     SetWaitingForEula(true);
128     SetNeedsEulaAcceptance(true);
129   }
130
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);
138   }
139
140  private:
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.
146   bool was_notified_;
147
148   DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest);
149 };
150
151 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) {
152   SetWaitingForNetwork(true);
153   EXPECT_FALSE(SimulateResourceRequest());
154   SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE);
155   EXPECT_FALSE(was_notified());
156 }
157
158 TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) {
159   SetWaitingForNetwork(false);
160   EXPECT_TRUE(SimulateResourceRequest());
161   SimulateNetworkConnectionChange(
162       net::NetworkChangeNotifier::CONNECTION_ETHERNET);
163   EXPECT_FALSE(was_notified());
164 }
165
166 TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) {
167   SetWaitingForNetwork(true);
168   EXPECT_FALSE(SimulateResourceRequest());
169   SimulateNetworkConnectionChange(
170       net::NetworkChangeNotifier::CONNECTION_ETHERNET);
171   EXPECT_TRUE(was_notified());
172 }
173
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());
189 }
190
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());
205 }
206
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());
223 }
224
225 TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnEulaAfterGoOffline) {
226   DisableEulaAndNetwork();
227   EXPECT_FALSE(SimulateResourceRequest());
228
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());
237 }
238
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());
247 }
248
249 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) {
250   DisableEulaAndNetwork();
251   EXPECT_FALSE(SimulateResourceRequest());
252
253   SimulateEulaAccepted();
254   EXPECT_FALSE(was_notified());
255 }
256
257 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) {
258   DisableEulaAndNetwork();
259   EXPECT_FALSE(SimulateResourceRequest());
260
261   SimulateEulaAccepted();
262   EXPECT_FALSE(was_notified());
263
264   SimulateNetworkConnectionChange(
265       net::NetworkChangeNotifier::CONNECTION_WIFI);
266   EXPECT_TRUE(was_notified());
267 }
268
269 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) {
270   DisableEulaAndNetwork();
271   EXPECT_FALSE(SimulateResourceRequest());
272
273   SimulateNetworkConnectionChange(
274       net::NetworkChangeNotifier::CONNECTION_WIFI);
275   EXPECT_FALSE(was_notified());
276
277   SimulateEulaAccepted();
278   EXPECT_TRUE(was_notified());
279 }
280
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();
286
287   SimulateNetworkConnectionChange(
288       net::NetworkChangeNotifier::CONNECTION_WIFI);
289   EXPECT_FALSE(was_notified());
290
291   SimulateEulaAccepted();
292   EXPECT_FALSE(was_notified());
293 }