Check for captive portal correctly in OAuth2TokenFetcher.
[chromium-blink-merge.git] / extensions / browser / load_monitoring_extension_host_queue_unittest.cc
blob22a68cd3de83b01d92a35f816df9e2c87e6c1830
1 // Copyright 2015 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 <limits>
7 #include "base/run_loop.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "extensions/browser/deferred_start_render_host.h"
10 #include "extensions/browser/extensions_test.h"
11 #include "extensions/browser/load_monitoring_extension_host_queue.h"
12 #include "extensions/browser/serial_extension_host_queue.h"
14 namespace extensions {
16 namespace {
18 class StubDeferredStartRenderHost : public DeferredStartRenderHost {
19 public:
20 // Returns true if this host is being observed by |observer|.
21 bool IsObservedBy(DeferredStartRenderHostObserver* observer) {
22 return observers_.count(observer) > 0;
25 private:
26 // DeferredStartRenderHost:
27 void AddDeferredStartRenderHostObserver(
28 DeferredStartRenderHostObserver* observer) override {
29 observers_.insert(observer);
31 void RemoveDeferredStartRenderHostObserver(
32 DeferredStartRenderHostObserver* observer) override {
33 observers_.erase(observer);
35 void CreateRenderViewNow() override {}
37 std::set<DeferredStartRenderHostObserver*> observers_;
40 const size_t g_invalid_size_t = std::numeric_limits<size_t>::max();
42 } // namespace
44 class LoadMonitoringExtensionHostQueueTest : public ExtensionsTest {
45 public:
46 LoadMonitoringExtensionHostQueueTest()
47 : finished_(false),
48 // Arbitrary choice of an invalid size_t.
49 num_queued_(g_invalid_size_t),
50 num_loaded_(g_invalid_size_t),
51 max_awaiting_loading_(g_invalid_size_t),
52 max_active_loading_(g_invalid_size_t) {}
54 void SetUp() override {
55 queue_.reset(new LoadMonitoringExtensionHostQueue(
56 // Use a SerialExtensionHostQueue because it's simple.
57 scoped_ptr<ExtensionHostQueue>(new SerialExtensionHostQueue()),
58 base::TimeDelta(), // no delay, easier to test
59 base::Bind(&LoadMonitoringExtensionHostQueueTest::Finished,
60 base::Unretained(this))));
63 protected:
64 // Creates a new DeferredStartRenderHost. Ownership is held by this class,
65 // not passed to caller.
66 StubDeferredStartRenderHost* CreateHost() {
67 StubDeferredStartRenderHost* stub = new StubDeferredStartRenderHost();
68 stubs_.push_back(stub);
69 return stub;
72 // Our single LoadMonitoringExtensionHostQueue instance.
73 LoadMonitoringExtensionHostQueue* queue() { return queue_.get(); }
75 // Returns true if the queue has finished monitoring.
76 bool finished() const { return finished_; }
78 // These are available after the queue has finished (in which case finished()
79 // will return true).
80 size_t num_queued() { return num_queued_; }
81 size_t num_loaded() { return num_loaded_; }
82 size_t max_awaiting_loading() { return max_awaiting_loading_; }
83 size_t max_active_loading() { return max_active_loading_; }
85 private:
86 // Callback when queue has finished monitoring.
87 void Finished(size_t num_queued,
88 size_t num_loaded,
89 size_t max_awaiting_loading,
90 size_t max_active_loading) {
91 CHECK(!finished_);
92 finished_ = true;
93 num_queued_ = num_queued;
94 num_loaded_ = num_loaded;
95 max_awaiting_loading_ = max_awaiting_loading;
96 max_active_loading_ = max_active_loading;
99 content::TestBrowserThreadBundle thread_bundle_;
100 scoped_ptr<LoadMonitoringExtensionHostQueue> queue_;
101 ScopedVector<StubDeferredStartRenderHost> stubs_;
103 // Set after the queue has finished monitoring.
104 bool finished_;
105 size_t num_queued_;
106 size_t num_loaded_;
107 size_t max_awaiting_loading_;
108 size_t max_active_loading_;
111 // Tests that if monitoring is never started, nor any hosts added, nothing is
112 // recorded.
113 TEST_F(LoadMonitoringExtensionHostQueueTest, NeverStarted) {
114 base::RunLoop().RunUntilIdle();
115 ASSERT_FALSE(finished());
118 // Tests that if monitoring has started but no hosts added, it's recorded as 0.
119 TEST_F(LoadMonitoringExtensionHostQueueTest, NoHosts) {
120 queue()->StartMonitoring();
122 base::RunLoop().RunUntilIdle();
123 ASSERT_TRUE(finished());
124 EXPECT_EQ(0u, num_queued());
125 EXPECT_EQ(0u, num_loaded());
126 EXPECT_EQ(0u, max_awaiting_loading());
127 EXPECT_EQ(0u, max_active_loading());
130 // Tests that adding a host starts monitoring.
131 TEST_F(LoadMonitoringExtensionHostQueueTest, AddOneHost) {
132 queue()->Add(CreateHost());
134 base::RunLoop().RunUntilIdle();
135 ASSERT_TRUE(finished());
136 EXPECT_EQ(1u, num_queued());
137 EXPECT_EQ(0u, num_loaded());
138 EXPECT_EQ(1u, max_awaiting_loading());
139 EXPECT_EQ(0u, max_active_loading());
142 // Tests that a host added and removed is still recorded, but not as a load
143 // finished.
144 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndRemoveOneHost) {
145 DeferredStartRenderHost* host = CreateHost();
146 queue()->Add(host);
147 queue()->Remove(host);
149 base::RunLoop().RunUntilIdle();
150 ASSERT_TRUE(finished());
151 EXPECT_EQ(1u, num_queued());
152 EXPECT_EQ(0u, num_loaded());
153 EXPECT_EQ(1u, max_awaiting_loading());
154 EXPECT_EQ(0u, max_active_loading());
157 // Tests adding and starting a single host.
158 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndStartOneHost) {
159 DeferredStartRenderHost* host = CreateHost();
160 queue()->Add(host);
161 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host);
163 base::RunLoop().RunUntilIdle();
164 ASSERT_TRUE(finished());
165 EXPECT_EQ(1u, num_queued());
166 EXPECT_EQ(0u, num_loaded());
167 EXPECT_EQ(1u, max_awaiting_loading());
168 EXPECT_EQ(1u, max_active_loading());
171 // Tests adding and destroying a single host without starting it.
172 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndDestroyOneHost) {
173 DeferredStartRenderHost* host = CreateHost();
174 queue()->Add(host);
175 queue()->OnDeferredStartRenderHostDestroyed(host);
177 base::RunLoop().RunUntilIdle();
178 ASSERT_TRUE(finished());
179 EXPECT_EQ(1u, num_queued());
180 EXPECT_EQ(0u, num_loaded());
181 EXPECT_EQ(1u, max_awaiting_loading());
182 EXPECT_EQ(0u, max_active_loading());
185 // Tests adding, starting, and stopping a single host.
186 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndStartAndStopOneHost) {
187 DeferredStartRenderHost* host = CreateHost();
188 queue()->Add(host);
189 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host);
190 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host);
192 base::RunLoop().RunUntilIdle();
193 ASSERT_TRUE(finished());
194 EXPECT_EQ(1u, num_queued());
195 EXPECT_EQ(1u, num_loaded());
196 EXPECT_EQ(1u, max_awaiting_loading());
197 EXPECT_EQ(1u, max_active_loading());
200 // Tests adding, starting, and stopping a single host - twice.
201 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndStartAndStopOneHostTwice) {
202 DeferredStartRenderHost* host = CreateHost();
203 queue()->Add(host);
204 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host);
205 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host);
207 // Re-starting loading should also be recorded fine (e.g. navigations could
208 // trigger this).
209 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host);
210 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host);
212 base::RunLoop().RunUntilIdle();
213 ASSERT_TRUE(finished());
214 EXPECT_EQ(1u, num_queued());
215 EXPECT_EQ(2u, num_loaded()); // 2 loaded this time, because we ran twice
216 EXPECT_EQ(1u, max_awaiting_loading());
217 EXPECT_EQ(1u, max_active_loading());
220 // Tests adding, starting, and destroying (i.e. an implicit stop) a single host.
221 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndStartAndDestroyOneHost) {
222 DeferredStartRenderHost* host = CreateHost();
223 queue()->Add(host);
224 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host);
225 queue()->OnDeferredStartRenderHostDestroyed(host);
227 base::RunLoop().RunUntilIdle();
228 ASSERT_TRUE(finished());
229 EXPECT_EQ(1u, num_queued());
230 EXPECT_EQ(1u, num_loaded());
231 EXPECT_EQ(1u, max_awaiting_loading());
232 EXPECT_EQ(1u, max_active_loading());
235 // Tests adding, starting, and removing a single host.
236 TEST_F(LoadMonitoringExtensionHostQueueTest, AddAndStartAndRemoveOneHost) {
237 DeferredStartRenderHost* host = CreateHost();
238 queue()->Add(host);
239 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host);
240 queue()->Remove(host);
242 base::RunLoop().RunUntilIdle();
243 ASSERT_TRUE(finished());
244 EXPECT_EQ(1u, num_queued());
245 EXPECT_EQ(0u, num_loaded());
246 EXPECT_EQ(1u, max_awaiting_loading());
247 EXPECT_EQ(1u, max_active_loading());
250 // Tests monitoring a sequence of hosts.
251 TEST_F(LoadMonitoringExtensionHostQueueTest, Sequence) {
252 // Scenario:
254 // 6 hosts will be added, only 5 will start loading, with a maximum of 4 in
255 // the queue and 3 loading at any time. Only 2 will finish.
256 DeferredStartRenderHost* host1 = CreateHost();
257 DeferredStartRenderHost* host2 = CreateHost();
258 DeferredStartRenderHost* host3 = CreateHost();
259 DeferredStartRenderHost* host4 = CreateHost();
260 DeferredStartRenderHost* host5 = CreateHost();
261 DeferredStartRenderHost* host6 = CreateHost();
263 queue()->Add(host1);
264 queue()->Add(host2);
265 queue()->Add(host3);
267 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host1);
268 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host2);
269 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host1);
271 queue()->Add(host4);
272 queue()->Add(host5);
273 queue()->Add(host6);
275 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host3);
276 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host4);
277 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host4);
278 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host5);
280 base::RunLoop().RunUntilIdle();
281 ASSERT_TRUE(finished());
282 EXPECT_EQ(6u, num_queued());
283 EXPECT_EQ(2u, num_loaded());
284 EXPECT_EQ(4u, max_awaiting_loading());
285 EXPECT_EQ(3u, max_active_loading());
287 // Complete a realistic sequence by stopping and/or destroying all hosts.
288 queue()->OnDeferredStartRenderHostDestroyed(host1);
289 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host2);
290 queue()->OnDeferredStartRenderHostDestroyed(host2);
291 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host3);
292 queue()->OnDeferredStartRenderHostDestroyed(host3);
293 queue()->OnDeferredStartRenderHostDestroyed(host4);
294 queue()->OnDeferredStartRenderHostDestroyed(host5); // never stopped
295 queue()->OnDeferredStartRenderHostDestroyed(host6); // never started/stopped
298 // Tests that the queue is observing Hosts from adding them through to being
299 // removed - that the load sequence itself is irrelevant.
301 // This is an unfortunate implementation-style test, but it used to be a bug
302 // and difficult to catch outside of a proper test framework - one in which we
303 // don't have to trigger events by hand.
304 TEST_F(LoadMonitoringExtensionHostQueueTest, ObserverLifetime) {
305 StubDeferredStartRenderHost* host1 = CreateHost();
306 StubDeferredStartRenderHost* host2 = CreateHost();
307 StubDeferredStartRenderHost* host3 = CreateHost();
308 StubDeferredStartRenderHost* host4 = CreateHost();
310 EXPECT_FALSE(host1->IsObservedBy(queue()));
311 EXPECT_FALSE(host2->IsObservedBy(queue()));
312 EXPECT_FALSE(host3->IsObservedBy(queue()));
313 EXPECT_FALSE(host4->IsObservedBy(queue()));
315 queue()->Add(host1);
316 queue()->Add(host2);
317 queue()->Add(host3);
318 queue()->Add(host4);
320 EXPECT_TRUE(host1->IsObservedBy(queue()));
321 EXPECT_TRUE(host2->IsObservedBy(queue()));
322 EXPECT_TRUE(host3->IsObservedBy(queue()));
323 EXPECT_TRUE(host4->IsObservedBy(queue()));
325 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host1);
326 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host2);
327 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host3);
328 // host4 will test that we Remove before Starting - so don't start.
330 EXPECT_TRUE(host1->IsObservedBy(queue()));
331 EXPECT_TRUE(host2->IsObservedBy(queue()));
332 EXPECT_TRUE(host3->IsObservedBy(queue()));
333 EXPECT_TRUE(host4->IsObservedBy(queue()));
335 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host1);
336 queue()->OnDeferredStartRenderHostDestroyed(host2);
338 EXPECT_TRUE(host1->IsObservedBy(queue()));
339 EXPECT_TRUE(host2->IsObservedBy(queue()));
341 queue()->Remove(host1);
342 queue()->Remove(host2);
343 queue()->Remove(host3);
344 queue()->Remove(host4);
346 EXPECT_FALSE(host1->IsObservedBy(queue()));
347 EXPECT_FALSE(host2->IsObservedBy(queue()));
348 EXPECT_FALSE(host3->IsObservedBy(queue()));
349 EXPECT_FALSE(host4->IsObservedBy(queue()));
352 } // namespace extensions