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.
7 #include "base/memory/scoped_vector.h"
8 #include "base/run_loop.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "extensions/browser/deferred_start_render_host.h"
11 #include "extensions/browser/extensions_test.h"
12 #include "extensions/browser/load_monitoring_extension_host_queue.h"
13 #include "extensions/browser/serial_extension_host_queue.h"
15 namespace extensions
{
19 class StubDeferredStartRenderHost
: public DeferredStartRenderHost
{
21 // Returns true if this host is being observed by |observer|.
22 bool IsObservedBy(DeferredStartRenderHostObserver
* observer
) {
23 return observers_
.count(observer
) > 0;
27 // DeferredStartRenderHost:
28 void AddDeferredStartRenderHostObserver(
29 DeferredStartRenderHostObserver
* observer
) override
{
30 observers_
.insert(observer
);
32 void RemoveDeferredStartRenderHostObserver(
33 DeferredStartRenderHostObserver
* observer
) override
{
34 observers_
.erase(observer
);
36 void CreateRenderViewNow() override
{}
38 std::set
<DeferredStartRenderHostObserver
*> observers_
;
41 const size_t g_invalid_size_t
= std::numeric_limits
<size_t>::max();
45 class LoadMonitoringExtensionHostQueueTest
: public ExtensionsTest
{
47 LoadMonitoringExtensionHostQueueTest()
49 // Arbitrary choice of an invalid size_t.
50 num_queued_(g_invalid_size_t
),
51 num_loaded_(g_invalid_size_t
),
52 max_awaiting_loading_(g_invalid_size_t
),
53 max_active_loading_(g_invalid_size_t
) {}
55 void SetUp() override
{
56 queue_
.reset(new LoadMonitoringExtensionHostQueue(
57 // Use a SerialExtensionHostQueue because it's simple.
58 scoped_ptr
<ExtensionHostQueue
>(new SerialExtensionHostQueue()),
59 base::TimeDelta(), // no delay, easier to test
60 base::Bind(&LoadMonitoringExtensionHostQueueTest::Finished
,
61 base::Unretained(this))));
65 // Creates a new DeferredStartRenderHost. Ownership is held by this class,
66 // not passed to caller.
67 StubDeferredStartRenderHost
* CreateHost() {
68 StubDeferredStartRenderHost
* stub
= new StubDeferredStartRenderHost();
69 stubs_
.push_back(stub
);
73 // Our single LoadMonitoringExtensionHostQueue instance.
74 LoadMonitoringExtensionHostQueue
* queue() { return queue_
.get(); }
76 // Returns true if the queue has finished monitoring.
77 bool finished() const { return finished_
; }
79 // These are available after the queue has finished (in which case finished()
81 size_t num_queued() { return num_queued_
; }
82 size_t num_loaded() { return num_loaded_
; }
83 size_t max_awaiting_loading() { return max_awaiting_loading_
; }
84 size_t max_active_loading() { return max_active_loading_
; }
87 // Callback when queue has finished monitoring.
88 void Finished(size_t num_queued
,
90 size_t max_awaiting_loading
,
91 size_t max_active_loading
) {
94 num_queued_
= num_queued
;
95 num_loaded_
= num_loaded
;
96 max_awaiting_loading_
= max_awaiting_loading
;
97 max_active_loading_
= max_active_loading
;
100 content::TestBrowserThreadBundle thread_bundle_
;
101 scoped_ptr
<LoadMonitoringExtensionHostQueue
> queue_
;
102 ScopedVector
<StubDeferredStartRenderHost
> stubs_
;
104 // Set after the queue has finished monitoring.
108 size_t max_awaiting_loading_
;
109 size_t max_active_loading_
;
112 // Tests that if monitoring is never started, nor any hosts added, nothing is
114 TEST_F(LoadMonitoringExtensionHostQueueTest
, NeverStarted
) {
115 base::RunLoop().RunUntilIdle();
116 ASSERT_FALSE(finished());
119 // Tests that if monitoring has started but no hosts added, it's recorded as 0.
120 TEST_F(LoadMonitoringExtensionHostQueueTest
, NoHosts
) {
121 queue()->StartMonitoring();
123 base::RunLoop().RunUntilIdle();
124 ASSERT_TRUE(finished());
125 EXPECT_EQ(0u, num_queued());
126 EXPECT_EQ(0u, num_loaded());
127 EXPECT_EQ(0u, max_awaiting_loading());
128 EXPECT_EQ(0u, max_active_loading());
131 // Tests that adding a host starts monitoring.
132 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddOneHost
) {
133 queue()->Add(CreateHost());
135 base::RunLoop().RunUntilIdle();
136 ASSERT_TRUE(finished());
137 EXPECT_EQ(1u, num_queued());
138 EXPECT_EQ(0u, num_loaded());
139 EXPECT_EQ(1u, max_awaiting_loading());
140 EXPECT_EQ(0u, max_active_loading());
143 // Tests that a host added and removed is still recorded, but not as a load
145 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndRemoveOneHost
) {
146 DeferredStartRenderHost
* host
= CreateHost();
148 queue()->Remove(host
);
150 base::RunLoop().RunUntilIdle();
151 ASSERT_TRUE(finished());
152 EXPECT_EQ(1u, num_queued());
153 EXPECT_EQ(0u, num_loaded());
154 EXPECT_EQ(1u, max_awaiting_loading());
155 EXPECT_EQ(0u, max_active_loading());
158 // Tests adding and starting a single host.
159 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndStartOneHost
) {
160 DeferredStartRenderHost
* host
= CreateHost();
162 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host
);
164 base::RunLoop().RunUntilIdle();
165 ASSERT_TRUE(finished());
166 EXPECT_EQ(1u, num_queued());
167 EXPECT_EQ(0u, num_loaded());
168 EXPECT_EQ(1u, max_awaiting_loading());
169 EXPECT_EQ(1u, max_active_loading());
172 // Tests adding and destroying a single host without starting it.
173 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndDestroyOneHost
) {
174 DeferredStartRenderHost
* host
= CreateHost();
176 queue()->OnDeferredStartRenderHostDestroyed(host
);
178 base::RunLoop().RunUntilIdle();
179 ASSERT_TRUE(finished());
180 EXPECT_EQ(1u, num_queued());
181 EXPECT_EQ(0u, num_loaded());
182 EXPECT_EQ(1u, max_awaiting_loading());
183 EXPECT_EQ(0u, max_active_loading());
186 // Tests adding, starting, and stopping a single host.
187 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndStartAndStopOneHost
) {
188 DeferredStartRenderHost
* host
= CreateHost();
190 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host
);
191 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host
);
193 base::RunLoop().RunUntilIdle();
194 ASSERT_TRUE(finished());
195 EXPECT_EQ(1u, num_queued());
196 EXPECT_EQ(1u, num_loaded());
197 EXPECT_EQ(1u, max_awaiting_loading());
198 EXPECT_EQ(1u, max_active_loading());
201 // Tests adding, starting, and stopping a single host - twice.
202 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndStartAndStopOneHostTwice
) {
203 DeferredStartRenderHost
* host
= CreateHost();
205 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host
);
206 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host
);
208 // Re-starting loading should also be recorded fine (e.g. navigations could
210 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host
);
211 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host
);
213 base::RunLoop().RunUntilIdle();
214 ASSERT_TRUE(finished());
215 EXPECT_EQ(1u, num_queued());
216 EXPECT_EQ(2u, num_loaded()); // 2 loaded this time, because we ran twice
217 EXPECT_EQ(1u, max_awaiting_loading());
218 EXPECT_EQ(1u, max_active_loading());
221 // Tests adding, starting, and destroying (i.e. an implicit stop) a single host.
222 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndStartAndDestroyOneHost
) {
223 DeferredStartRenderHost
* host
= CreateHost();
225 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host
);
226 queue()->OnDeferredStartRenderHostDestroyed(host
);
228 base::RunLoop().RunUntilIdle();
229 ASSERT_TRUE(finished());
230 EXPECT_EQ(1u, num_queued());
231 EXPECT_EQ(1u, num_loaded());
232 EXPECT_EQ(1u, max_awaiting_loading());
233 EXPECT_EQ(1u, max_active_loading());
236 // Tests adding, starting, and removing a single host.
237 TEST_F(LoadMonitoringExtensionHostQueueTest
, AddAndStartAndRemoveOneHost
) {
238 DeferredStartRenderHost
* host
= CreateHost();
240 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host
);
241 queue()->Remove(host
);
243 base::RunLoop().RunUntilIdle();
244 ASSERT_TRUE(finished());
245 EXPECT_EQ(1u, num_queued());
246 EXPECT_EQ(0u, num_loaded());
247 EXPECT_EQ(1u, max_awaiting_loading());
248 EXPECT_EQ(1u, max_active_loading());
251 // Tests monitoring a sequence of hosts.
252 TEST_F(LoadMonitoringExtensionHostQueueTest
, Sequence
) {
255 // 6 hosts will be added, only 5 will start loading, with a maximum of 4 in
256 // the queue and 3 loading at any time. Only 2 will finish.
257 DeferredStartRenderHost
* host1
= CreateHost();
258 DeferredStartRenderHost
* host2
= CreateHost();
259 DeferredStartRenderHost
* host3
= CreateHost();
260 DeferredStartRenderHost
* host4
= CreateHost();
261 DeferredStartRenderHost
* host5
= CreateHost();
262 DeferredStartRenderHost
* host6
= CreateHost();
268 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host1
);
269 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host2
);
270 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host1
);
276 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host3
);
277 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host4
);
278 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host4
);
279 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host5
);
281 base::RunLoop().RunUntilIdle();
282 ASSERT_TRUE(finished());
283 EXPECT_EQ(6u, num_queued());
284 EXPECT_EQ(2u, num_loaded());
285 EXPECT_EQ(4u, max_awaiting_loading());
286 EXPECT_EQ(3u, max_active_loading());
288 // Complete a realistic sequence by stopping and/or destroying all hosts.
289 queue()->OnDeferredStartRenderHostDestroyed(host1
);
290 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host2
);
291 queue()->OnDeferredStartRenderHostDestroyed(host2
);
292 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host3
);
293 queue()->OnDeferredStartRenderHostDestroyed(host3
);
294 queue()->OnDeferredStartRenderHostDestroyed(host4
);
295 queue()->OnDeferredStartRenderHostDestroyed(host5
); // never stopped
296 queue()->OnDeferredStartRenderHostDestroyed(host6
); // never started/stopped
299 // Tests that the queue is observing Hosts from adding them through to being
300 // removed - that the load sequence itself is irrelevant.
302 // This is an unfortunate implementation-style test, but it used to be a bug
303 // and difficult to catch outside of a proper test framework - one in which we
304 // don't have to trigger events by hand.
305 TEST_F(LoadMonitoringExtensionHostQueueTest
, ObserverLifetime
) {
306 StubDeferredStartRenderHost
* host1
= CreateHost();
307 StubDeferredStartRenderHost
* host2
= CreateHost();
308 StubDeferredStartRenderHost
* host3
= CreateHost();
309 StubDeferredStartRenderHost
* host4
= CreateHost();
311 EXPECT_FALSE(host1
->IsObservedBy(queue()));
312 EXPECT_FALSE(host2
->IsObservedBy(queue()));
313 EXPECT_FALSE(host3
->IsObservedBy(queue()));
314 EXPECT_FALSE(host4
->IsObservedBy(queue()));
321 EXPECT_TRUE(host1
->IsObservedBy(queue()));
322 EXPECT_TRUE(host2
->IsObservedBy(queue()));
323 EXPECT_TRUE(host3
->IsObservedBy(queue()));
324 EXPECT_TRUE(host4
->IsObservedBy(queue()));
326 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host1
);
327 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host2
);
328 queue()->OnDeferredStartRenderHostDidStartFirstLoad(host3
);
329 // host4 will test that we Remove before Starting - so don't start.
331 EXPECT_TRUE(host1
->IsObservedBy(queue()));
332 EXPECT_TRUE(host2
->IsObservedBy(queue()));
333 EXPECT_TRUE(host3
->IsObservedBy(queue()));
334 EXPECT_TRUE(host4
->IsObservedBy(queue()));
336 queue()->OnDeferredStartRenderHostDidStopFirstLoad(host1
);
337 queue()->OnDeferredStartRenderHostDestroyed(host2
);
339 EXPECT_TRUE(host1
->IsObservedBy(queue()));
340 EXPECT_TRUE(host2
->IsObservedBy(queue()));
342 queue()->Remove(host1
);
343 queue()->Remove(host2
);
344 queue()->Remove(host3
);
345 queue()->Remove(host4
);
347 EXPECT_FALSE(host1
->IsObservedBy(queue()));
348 EXPECT_FALSE(host2
->IsObservedBy(queue()));
349 EXPECT_FALSE(host3
->IsObservedBy(queue()));
350 EXPECT_FALSE(host4
->IsObservedBy(queue()));
353 } // namespace extensions