Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / loader / resource_scheduler_unittest.cc
blobfb54951f5350519dd0280178cc90ebe47567506f
1 // Copyright (c) 2012 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 "content/browser/loader/resource_scheduler.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/timer/mock_timer.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/loader/resource_dispatcher_host_impl.h"
15 #include "content/browser/loader/resource_message_filter.h"
16 #include "content/browser/loader/resource_request_info_impl.h"
17 #include "content/common/resource_messages.h"
18 #include "content/public/browser/resource_context.h"
19 #include "content/public/browser/resource_controller.h"
20 #include "content/public/browser/resource_throttle.h"
21 #include "content/public/common/process_type.h"
22 #include "content/public/common/resource_type.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/test/test_render_view_host_factory.h"
26 #include "content/test/test_web_contents.h"
27 #include "net/base/host_port_pair.h"
28 #include "net/base/request_priority.h"
29 #include "net/http/http_server_properties_impl.h"
30 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "ui/events/latency_info.h"
35 namespace content {
37 namespace {
39 class TestRequestFactory;
41 const int kChildId = 30;
42 const int kRouteId = 75;
43 const int kChildId2 = 43;
44 const int kRouteId2 = 67;
45 const int kBackgroundChildId = 35;
46 const int kBackgroundRouteId = 43;
47 const int kBackgroundChildId2 = 54;
48 const int kBackgroundRouteId2 = 82;
50 class TestRequest : public ResourceController {
51 public:
52 TestRequest(scoped_ptr<ResourceThrottle> throttle,
53 scoped_ptr<net::URLRequest> url_request)
54 : started_(false),
55 throttle_(throttle.Pass()),
56 url_request_(url_request.Pass()) {
57 throttle_->set_controller_for_testing(this);
59 ~TestRequest() override {}
61 bool started() const { return started_; }
63 void Start() {
64 bool deferred = false;
65 throttle_->WillStartRequest(&deferred);
66 started_ = !deferred;
69 void Cancel() override {
70 // Alert the scheduler that the request can be deleted.
71 throttle_.reset(0);
74 const net::URLRequest* url_request() const { return url_request_.get(); }
76 protected:
77 // ResourceController interface:
78 void CancelAndIgnore() override {}
79 void CancelWithError(int error_code) override {}
80 void Resume() override { started_ = true; }
82 private:
83 bool started_;
84 scoped_ptr<ResourceThrottle> throttle_;
85 scoped_ptr<net::URLRequest> url_request_;
88 class CancelingTestRequest : public TestRequest {
89 public:
90 CancelingTestRequest(scoped_ptr<ResourceThrottle> throttle,
91 scoped_ptr<net::URLRequest> url_request)
92 : TestRequest(throttle.Pass(), url_request.Pass()) {}
94 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) {
95 request_to_cancel_ = request_to_cancel.Pass();
98 private:
99 void Resume() override {
100 TestRequest::Resume();
101 request_to_cancel_.reset();
104 scoped_ptr<TestRequest> request_to_cancel_;
107 class FakeResourceContext : public ResourceContext {
108 private:
109 net::HostResolver* GetHostResolver() override { return NULL; }
110 net::URLRequestContext* GetRequestContext() override { return NULL; }
113 class FakeResourceMessageFilter : public ResourceMessageFilter {
114 public:
115 FakeResourceMessageFilter(int child_id)
116 : ResourceMessageFilter(
117 child_id,
118 PROCESS_TYPE_RENDERER,
119 NULL /* appcache_service */,
120 NULL /* blob_storage_context */,
121 NULL /* file_system_context */,
122 NULL /* service_worker_context */,
123 NULL /* host_zoom_level_context */,
124 base::Bind(&FakeResourceMessageFilter::GetContexts,
125 base::Unretained(this))) {
128 private:
129 ~FakeResourceMessageFilter() override {}
131 void GetContexts(const ResourceHostMsg_Request& request,
132 ResourceContext** resource_context,
133 net::URLRequestContext** request_context) {
134 *resource_context = &context_;
135 *request_context = NULL;
138 FakeResourceContext context_;
141 class ResourceSchedulerTest : public testing::Test {
142 protected:
143 ResourceSchedulerTest()
144 : next_request_id_(0),
145 ui_thread_(BrowserThread::UI, &message_loop_),
146 io_thread_(BrowserThread::IO, &message_loop_),
147 mock_timer_(new base::MockTimer(true, true)) {
148 scheduler_.set_timer_for_testing(scoped_ptr<base::Timer>(mock_timer_));
150 // TODO(aiolos): Remove when throttling and coalescing have both landed.
151 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
152 false /* should_coalesce */);
154 scheduler_.OnClientCreated(kChildId, kRouteId, true, false);
155 scheduler_.OnClientCreated(
156 kBackgroundChildId, kBackgroundRouteId, false, false);
157 context_.set_http_server_properties(http_server_properties_.GetWeakPtr());
160 ~ResourceSchedulerTest() override {
161 scheduler_.OnClientDeleted(kChildId, kRouteId);
162 scheduler_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
165 scoped_ptr<net::URLRequest> NewURLRequestWithChildAndRoute(
166 const char* url,
167 net::RequestPriority priority,
168 int child_id,
169 int route_id,
170 bool is_async) {
171 scoped_ptr<net::URLRequest> url_request(
172 context_.CreateRequest(GURL(url), priority, NULL, NULL));
173 ResourceRequestInfoImpl* info = new ResourceRequestInfoImpl(
174 PROCESS_TYPE_RENDERER, // process_type
175 child_id, // child_id
176 route_id, // route_id
177 0, // origin_pid
178 ++next_request_id_, // request_id
179 MSG_ROUTING_NONE, // render_frame_id
180 false, // is_main_frame
181 false, // parent_is_main_frame
182 0, // parent_render_frame_id
183 RESOURCE_TYPE_SUB_RESOURCE, // resource_type
184 ui::PAGE_TRANSITION_LINK, // transition_type
185 false, // should_replace_current_entry
186 false, // is_download
187 false, // is_stream
188 true, // allow_download
189 false, // has_user_gesture
190 false, // enable_load_timing
191 false, // enable_upload_progress
192 blink::WebReferrerPolicyDefault, // referrer_policy
193 blink::WebPageVisibilityStateVisible, // visibility_state
194 NULL, // context
195 base::WeakPtr<ResourceMessageFilter>(), // filter
196 is_async); // is_async
197 info->AssociateWithRequest(url_request.get());
198 return url_request.Pass();
201 scoped_ptr<net::URLRequest> NewURLRequest(const char* url,
202 net::RequestPriority priority) {
203 return NewURLRequestWithChildAndRoute(
204 url, priority, kChildId, kRouteId, true);
207 TestRequest* NewRequestWithRoute(const char* url,
208 net::RequestPriority priority,
209 int route_id) {
210 return NewRequestWithChildAndRoute(url, priority, route_id, kChildId);
213 TestRequest* NewRequestWithChildAndRoute(const char* url,
214 net::RequestPriority priority,
215 int child_id,
216 int route_id) {
217 return GetNewTestRequest(url, priority, child_id, route_id, true);
220 TestRequest* NewRequest(const char* url, net::RequestPriority priority) {
221 return NewRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
224 TestRequest* NewBackgroundRequest(const char* url,
225 net::RequestPriority priority) {
226 return NewRequestWithChildAndRoute(
227 url, priority, kBackgroundChildId, kBackgroundRouteId);
230 TestRequest* NewSyncRequest(const char* url, net::RequestPriority priority) {
231 return NewSyncRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
234 TestRequest* NewBackgroundSyncRequest(const char* url,
235 net::RequestPriority priority) {
236 return NewSyncRequestWithChildAndRoute(
237 url, priority, kBackgroundChildId, kBackgroundRouteId);
240 TestRequest* NewSyncRequestWithChildAndRoute(const char* url,
241 net::RequestPriority priority,
242 int child_id,
243 int route_id) {
244 return GetNewTestRequest(url, priority, child_id, route_id, false);
247 TestRequest* GetNewTestRequest(const char* url,
248 net::RequestPriority priority,
249 int child_id,
250 int route_id,
251 bool is_async) {
252 scoped_ptr<net::URLRequest> url_request(NewURLRequestWithChildAndRoute(
253 url, priority, child_id, route_id, is_async));
254 scoped_ptr<ResourceThrottle> throttle(
255 scheduler_.ScheduleRequest(child_id, route_id, url_request.get()));
256 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass());
257 request->Start();
258 return request;
262 void ChangeRequestPriority(TestRequest* request,
263 net::RequestPriority new_priority,
264 int intra_priority = 0) {
265 scoped_refptr<FakeResourceMessageFilter> filter(
266 new FakeResourceMessageFilter(kChildId));
267 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(
268 request->url_request());
269 const GlobalRequestID& id = info->GetGlobalRequestID();
270 ResourceHostMsg_DidChangePriority msg(id.request_id, new_priority,
271 intra_priority);
272 rdh_.OnMessageReceived(msg, filter.get());
275 void FireCoalescingTimer() {
276 EXPECT_TRUE(mock_timer_->IsRunning());
277 mock_timer_->Fire();
280 int next_request_id_;
281 base::MessageLoopForIO message_loop_;
282 BrowserThreadImpl ui_thread_;
283 BrowserThreadImpl io_thread_;
284 ResourceDispatcherHostImpl rdh_;
285 ResourceScheduler scheduler_;
286 base::MockTimer* mock_timer_;
287 net::HttpServerPropertiesImpl http_server_properties_;
288 net::TestURLRequestContext context_;
291 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
292 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST));
293 EXPECT_TRUE(request->started());
296 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilIdle) {
297 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
298 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
299 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
300 EXPECT_TRUE(high->started());
301 EXPECT_TRUE(low->started());
302 EXPECT_FALSE(low2->started());
303 high.reset();
304 EXPECT_TRUE(low2->started());
307 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInserted) {
308 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
309 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
310 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
311 EXPECT_TRUE(high->started());
312 EXPECT_TRUE(low->started());
313 EXPECT_FALSE(low2->started());
314 high.reset();
315 scheduler_.OnWillInsertBody(kChildId, kRouteId);
316 EXPECT_TRUE(low2->started());
319 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilCriticalComplete) {
320 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
321 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
322 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
323 EXPECT_TRUE(high->started());
324 EXPECT_TRUE(low->started());
325 EXPECT_FALSE(low2->started());
326 scheduler_.OnWillInsertBody(kChildId, kRouteId);
327 EXPECT_FALSE(low2->started());
328 high.reset();
329 EXPECT_TRUE(low2->started());
332 TEST_F(ResourceSchedulerTest, LowDoesNotBlockCriticalComplete) {
333 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
334 scoped_ptr<TestRequest> lowest(NewRequest("http://host/lowest", net::LOWEST));
335 scoped_ptr<TestRequest> lowest2(
336 NewRequest("http://host/lowest", net::LOWEST));
337 EXPECT_TRUE(low->started());
338 EXPECT_TRUE(lowest->started());
339 EXPECT_FALSE(lowest2->started());
340 scheduler_.OnWillInsertBody(kChildId, kRouteId);
341 EXPECT_TRUE(lowest2->started());
344 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) {
345 http_server_properties_.SetSupportsSpdy(
346 net::HostPortPair("spdyhost", 443), true);
347 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
348 scoped_ptr<TestRequest> low_spdy(
349 NewRequest("https://spdyhost/low", net::LOWEST));
350 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
351 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
352 EXPECT_TRUE(high->started());
353 EXPECT_TRUE(low_spdy->started());
354 EXPECT_TRUE(low->started());
355 EXPECT_FALSE(low2->started());
356 scheduler_.OnWillInsertBody(kChildId, kRouteId);
357 high.reset();
358 EXPECT_TRUE(low2->started());
361 TEST_F(ResourceSchedulerTest, NavigationResetsState) {
362 scheduler_.OnWillInsertBody(kChildId, kRouteId);
363 scheduler_.OnNavigate(kChildId, kRouteId);
364 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
365 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
366 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
367 EXPECT_TRUE(high->started());
368 EXPECT_TRUE(low->started());
369 EXPECT_FALSE(low2->started());
372 TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
373 const int route_id = 0; // Indicates a background request.
374 scoped_ptr<TestRequest> request(NewRequestWithRoute("http://host/1",
375 net::LOWEST, route_id));
376 EXPECT_TRUE(request->started());
379 TEST_F(ResourceSchedulerTest, StartMultipleLowRequestsWhenIdle) {
380 scoped_ptr<TestRequest> high1(NewRequest("http://host/high1", net::HIGHEST));
381 scoped_ptr<TestRequest> high2(NewRequest("http://host/high2", net::HIGHEST));
382 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
383 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
384 EXPECT_TRUE(high1->started());
385 EXPECT_TRUE(high2->started());
386 EXPECT_TRUE(low->started());
387 EXPECT_FALSE(low2->started());
388 high1.reset();
389 EXPECT_FALSE(low2->started());
390 high2.reset();
391 EXPECT_TRUE(low2->started());
394 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) {
395 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
396 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST));
398 scoped_ptr<net::URLRequest> url_request(
399 NewURLRequest("http://host/low2", net::LOWEST));
400 scoped_ptr<ResourceThrottle> throttle(
401 scheduler_.ScheduleRequest(kChildId, kRouteId, url_request.get()));
402 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest(
403 throttle.Pass(), url_request.Pass()));
404 low2->Start();
406 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST));
407 low2->set_request_to_cancel(low3.Pass());
408 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST));
410 EXPECT_TRUE(high->started());
411 EXPECT_FALSE(low2->started());
412 high.reset();
413 EXPECT_TRUE(low1->started());
414 EXPECT_TRUE(low2->started());
415 EXPECT_TRUE(low4->started());
418 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
419 // We only load low priority resources if there's a body.
420 scheduler_.OnWillInsertBody(kChildId, kRouteId);
422 // Throw in one high priority request to make sure that's not a factor.
423 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
424 EXPECT_TRUE(high->started());
426 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
427 const int kMaxNumDelayableRequestsPerHost = 6;
428 ScopedVector<TestRequest> lows_singlehost;
429 // Queue up to the per-host limit (we subtract the current high-pri request).
430 for (int i = 0; i < kMaxNumDelayableRequestsPerHost - 1; ++i) {
431 string url = "http://host/low" + base::IntToString(i);
432 lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
433 EXPECT_TRUE(lows_singlehost[i]->started());
436 scoped_ptr<TestRequest> second_last_singlehost(NewRequest("http://host/last",
437 net::LOWEST));
438 scoped_ptr<TestRequest> last_singlehost(NewRequest("http://host/s_last",
439 net::LOWEST));
441 EXPECT_FALSE(second_last_singlehost->started());
442 high.reset();
443 EXPECT_TRUE(second_last_singlehost->started());
444 EXPECT_FALSE(last_singlehost->started());
445 lows_singlehost.erase(lows_singlehost.begin());
446 EXPECT_TRUE(last_singlehost->started());
448 // Queue more requests from different hosts until we reach the total limit.
449 int expected_slots_left =
450 kMaxNumDelayableRequestsPerClient - kMaxNumDelayableRequestsPerHost;
451 EXPECT_GT(expected_slots_left, 0);
452 ScopedVector<TestRequest> lows_differenthosts;
453 for (int i = 0; i < expected_slots_left; ++i) {
454 string url = "http://host" + base::IntToString(i) + "/low";
455 lows_differenthosts.push_back(NewRequest(url.c_str(), net::LOWEST));
456 EXPECT_TRUE(lows_differenthosts[i]->started());
459 scoped_ptr<TestRequest> last_differenthost(NewRequest("http://host_new/last",
460 net::LOWEST));
461 EXPECT_FALSE(last_differenthost->started());
464 TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) {
465 // Dummies to enforce scheduling.
466 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
467 scoped_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
469 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
470 EXPECT_FALSE(request->started());
472 ChangeRequestPriority(request.get(), net::HIGHEST);
473 EXPECT_TRUE(request->started());
476 TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) {
477 // Dummies to enforce scheduling.
478 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
479 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
481 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
482 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
483 EXPECT_FALSE(request->started());
484 EXPECT_FALSE(idle->started());
486 ChangeRequestPriority(request.get(), net::LOWEST);
487 EXPECT_FALSE(request->started());
488 EXPECT_FALSE(idle->started());
490 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
491 ScopedVector<TestRequest> lows;
492 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
493 string url = "http://host/low" + base::IntToString(i);
494 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
497 scheduler_.OnWillInsertBody(kChildId, kRouteId);
498 high.reset();
500 EXPECT_TRUE(request->started());
501 EXPECT_FALSE(idle->started());
504 TEST_F(ResourceSchedulerTest, LowerPriority) {
505 // Dummies to enforce scheduling.
506 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
507 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
509 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
510 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
511 EXPECT_FALSE(request->started());
512 EXPECT_FALSE(idle->started());
514 ChangeRequestPriority(request.get(), net::IDLE);
515 EXPECT_FALSE(request->started());
516 EXPECT_FALSE(idle->started());
518 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
519 // 2 fewer filler requests: 1 for the "low" dummy at the start, and 1 for the
520 // one at the end, which will be tested.
521 const int kNumFillerRequests = kMaxNumDelayableRequestsPerClient - 2;
522 ScopedVector<TestRequest> lows;
523 for (int i = 0; i < kNumFillerRequests; ++i) {
524 string url = "http://host" + base::IntToString(i) + "/low";
525 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
528 scheduler_.OnWillInsertBody(kChildId, kRouteId);
529 high.reset();
531 EXPECT_FALSE(request->started());
532 EXPECT_TRUE(idle->started());
535 TEST_F(ResourceSchedulerTest, ReprioritizedRequestGoesToBackOfQueue) {
536 // Dummies to enforce scheduling.
537 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
538 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
540 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
541 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
542 EXPECT_FALSE(request->started());
543 EXPECT_FALSE(idle->started());
545 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
546 ScopedVector<TestRequest> lows;
547 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
548 string url = "http://host/low" + base::IntToString(i);
549 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
552 ChangeRequestPriority(request.get(), net::IDLE);
553 EXPECT_FALSE(request->started());
554 EXPECT_FALSE(idle->started());
556 ChangeRequestPriority(request.get(), net::LOWEST);
557 EXPECT_FALSE(request->started());
558 EXPECT_FALSE(idle->started());
560 scheduler_.OnWillInsertBody(kChildId, kRouteId);
561 EXPECT_FALSE(request->started());
562 EXPECT_FALSE(idle->started());
565 TEST_F(ResourceSchedulerTest, HigherIntraPriorityGoesToFrontOfQueue) {
566 // Dummies to enforce scheduling.
567 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
568 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
570 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
571 ScopedVector<TestRequest> lows;
572 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
573 string url = "http://host/low" + base::IntToString(i);
574 lows.push_back(NewRequest(url.c_str(), net::IDLE));
577 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
578 EXPECT_FALSE(request->started());
580 ChangeRequestPriority(request.get(), net::IDLE, 1);
581 EXPECT_FALSE(request->started());
583 scheduler_.OnWillInsertBody(kChildId, kRouteId);
584 high.reset();
585 EXPECT_TRUE(request->started());
588 TEST_F(ResourceSchedulerTest, NonHTTPSchedulesImmediately) {
589 // Dummies to enforce scheduling.
590 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
591 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
593 scoped_ptr<TestRequest> request(
594 NewRequest("chrome-extension://req", net::LOWEST));
595 EXPECT_TRUE(request->started());
598 TEST_F(ResourceSchedulerTest, ActiveLoadingSyncSchedulesImmediately) {
599 // TODO(aiolos): remove when throttling and coalescing have both landed
600 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
601 false /* should_coalesce */);
602 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
603 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
604 // Dummies to enforce scheduling.
605 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
606 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
608 scoped_ptr<TestRequest> request(
609 NewSyncRequest("http://host/req", net::LOWEST));
610 EXPECT_TRUE(request->started());
613 TEST_F(ResourceSchedulerTest, UnthrottledSyncSchedulesImmediately) {
614 // TODO(aiolos): remove when throttling and coalescing have both landed
615 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
616 false /* should_coalesce */);
617 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
618 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
619 scheduler_.GetClientStateForTesting(kBackgroundChildId,
620 kBackgroundRouteId));
621 // Dummies to enforce scheduling.
622 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
623 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
625 scoped_ptr<TestRequest> request(
626 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
627 EXPECT_TRUE(request->started());
630 TEST_F(ResourceSchedulerTest, SpdyProxySchedulesImmediately) {
631 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
632 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
634 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
635 EXPECT_FALSE(request->started());
637 scheduler_.OnReceivedSpdyProxiedHttpResponse(kChildId, kRouteId);
638 EXPECT_TRUE(request->started());
640 scoped_ptr<TestRequest> after(NewRequest("http://host/after", net::IDLE));
641 EXPECT_TRUE(after->started());
644 TEST_F(ResourceSchedulerTest, NewSpdyHostInDelayableRequests) {
645 scheduler_.OnWillInsertBody(kChildId, kRouteId);
646 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
648 scoped_ptr<TestRequest> low1_spdy(
649 NewRequest("http://spdyhost1:8080/low", net::LOWEST));
650 // Cancel a request after we learn the server supports SPDY.
651 ScopedVector<TestRequest> lows;
652 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
653 string url = "http://host" + base::IntToString(i) + "/low";
654 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
656 scoped_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
657 EXPECT_FALSE(low1->started());
658 http_server_properties_.SetSupportsSpdy(
659 net::HostPortPair("spdyhost1", 8080), true);
660 low1_spdy.reset();
661 EXPECT_TRUE(low1->started());
663 low1.reset();
664 scoped_ptr<TestRequest> low2_spdy(
665 NewRequest("http://spdyhost2:8080/low", net::IDLE));
666 // Reprioritize a request after we learn the server supports SPDY.
667 EXPECT_TRUE(low2_spdy->started());
668 http_server_properties_.SetSupportsSpdy(
669 net::HostPortPair("spdyhost2", 8080), true);
670 ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
671 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
672 EXPECT_TRUE(low2->started());
675 TEST_F(ResourceSchedulerTest, ThrottledClientCreation) {
676 // TODO(aiolos): remove when throttling and coalescing have both landed
677 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
678 false /* should_coalesce */);
679 EXPECT_TRUE(scheduler_.should_throttle());
680 scheduler_.OnClientCreated(
681 kBackgroundChildId2, kBackgroundRouteId2, false, false);
683 EXPECT_EQ(ResourceScheduler::THROTTLED,
684 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
685 kBackgroundRouteId2));
686 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
689 TEST_F(ResourceSchedulerTest, ActiveClientThrottleUpdateOnLoadingChange) {
690 // TODO(aiolos): remove when throttling and coalescing have both landed
691 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
692 false /* should_coalesce */);
693 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
694 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
695 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
696 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
697 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
698 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
699 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
700 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
703 TEST_F(ResourceSchedulerTest, CoalesceBackgroundClientOnLoadCompletion) {
704 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
705 true /* should_coalesce */);
706 EXPECT_EQ(ResourceScheduler::THROTTLED,
707 scheduler_.GetClientStateForTesting(kBackgroundChildId,
708 kBackgroundRouteId));
709 scheduler_.OnLoadingStateChanged(
710 kBackgroundChildId, kBackgroundRouteId, true);
711 EXPECT_EQ(ResourceScheduler::THROTTLED,
712 scheduler_.GetClientStateForTesting(kBackgroundChildId,
713 kBackgroundRouteId));
714 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
715 EXPECT_EQ(ResourceScheduler::COALESCED,
716 scheduler_.GetClientStateForTesting(kBackgroundChildId,
717 kBackgroundRouteId));
720 TEST_F(ResourceSchedulerTest, UnthrottleBackgroundClientOnLoadingStarted) {
721 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
722 true /* should_coalesce */);
723 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
724 scheduler_.OnLoadingStateChanged(
725 kBackgroundChildId, kBackgroundRouteId, true);
726 EXPECT_EQ(ResourceScheduler::COALESCED,
727 scheduler_.GetClientStateForTesting(kBackgroundChildId,
728 kBackgroundRouteId));
730 scheduler_.OnLoadingStateChanged(
731 kBackgroundChildId, kBackgroundRouteId, false);
732 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
733 scheduler_.GetClientStateForTesting(kBackgroundChildId,
734 kBackgroundRouteId));
737 TEST_F(ResourceSchedulerTest, OneRequestPerThrottledClient) {
738 // TODO(aiolos): remove when throttling and coalescing have both landed
739 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
740 false /* should_coalesce */);
741 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
742 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
743 EXPECT_EQ(ResourceScheduler::THROTTLED,
744 scheduler_.GetClientStateForTesting(kBackgroundChildId,
745 kBackgroundRouteId));
746 scoped_ptr<TestRequest> high(
747 NewBackgroundRequest("http://host/high", net::HIGHEST));
748 scoped_ptr<TestRequest> request(
749 NewBackgroundRequest("http://host/req", net::IDLE));
751 EXPECT_TRUE(high->started());
752 EXPECT_FALSE(request->started());
755 TEST_F(ResourceSchedulerTest, UnthrottleNewlyVisibleClient) {
756 // TODO(aiolos): remove when throttling and coalescing have both landed
757 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
758 false /* should_coalesce */);
759 EXPECT_EQ(ResourceScheduler::THROTTLED,
760 scheduler_.GetClientStateForTesting(kBackgroundChildId,
761 kBackgroundRouteId));
762 scoped_ptr<TestRequest> high(
763 NewBackgroundRequest("http://host/high", net::HIGHEST));
764 scoped_ptr<TestRequest> request(
765 NewBackgroundRequest("http://host/req", net::IDLE));
766 EXPECT_FALSE(request->started());
768 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
769 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
770 scheduler_.GetClientStateForTesting(kBackgroundChildId,
771 kBackgroundRouteId));
772 EXPECT_TRUE(request->started());
775 TEST_F(ResourceSchedulerTest, UnthrottleNewlyAudibleClient) {
776 // TODO(aiolos): remove when throttling and coalescing have both landed
777 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
778 false /* should_coalesce */);
779 EXPECT_EQ(ResourceScheduler::THROTTLED,
780 scheduler_.GetClientStateForTesting(kBackgroundChildId,
781 kBackgroundRouteId));
782 scoped_ptr<TestRequest> high(
783 NewBackgroundRequest("http://host/high", net::HIGHEST));
784 scoped_ptr<TestRequest> request(
785 NewBackgroundRequest("http://host/req", net::IDLE));
786 EXPECT_FALSE(request->started());
788 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
789 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
790 scheduler_.GetClientStateForTesting(kBackgroundChildId,
791 kBackgroundRouteId));
792 EXPECT_TRUE(request->started());
795 TEST_F(ResourceSchedulerTest, VisibleClientStillUnthrottledOnAudabilityChange) {
796 // TODO(aiolos): remove when throttling and coalescing have both landed
797 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
798 false /* should_coalesce */);
799 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
800 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
801 EXPECT_EQ(ResourceScheduler::THROTTLED,
802 scheduler_.GetClientStateForTesting(kBackgroundChildId,
803 kBackgroundRouteId));
805 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
806 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
807 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
808 EXPECT_EQ(ResourceScheduler::THROTTLED,
809 scheduler_.GetClientStateForTesting(kBackgroundChildId,
810 kBackgroundRouteId));
812 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
813 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
814 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
815 EXPECT_EQ(ResourceScheduler::THROTTLED,
816 scheduler_.GetClientStateForTesting(kBackgroundChildId,
817 kBackgroundRouteId));
820 TEST_F(ResourceSchedulerTest, AudibleClientStillUnthrottledOnVisabilityChange) {
821 // TODO(aiolos): remove when throttling and coalescing have both landed
822 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
823 false /* should_coalesce */);
824 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
825 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
826 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
827 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
828 EXPECT_EQ(ResourceScheduler::THROTTLED,
829 scheduler_.GetClientStateForTesting(kBackgroundChildId,
830 kBackgroundRouteId));
832 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
833 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
834 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
835 EXPECT_EQ(ResourceScheduler::THROTTLED,
836 scheduler_.GetClientStateForTesting(kBackgroundChildId,
837 kBackgroundRouteId));
839 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
840 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
841 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
842 EXPECT_EQ(ResourceScheduler::THROTTLED,
843 scheduler_.GetClientStateForTesting(kBackgroundChildId,
844 kBackgroundRouteId));
847 TEST_F(ResourceSchedulerTest, ThrottledClientStartsNextHighestPriorityRequest) {
848 // TODO(aiolos): remove when throttling and coalescing have both landed
849 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
850 false /* should_coalesce */);
851 scoped_ptr<TestRequest> request(
852 NewBackgroundRequest("http://host/req", net::IDLE));
853 // Lower priority request started first to test request prioritizaton.
854 scoped_ptr<TestRequest> low(
855 NewBackgroundRequest("http://host/high", net::IDLE));
856 scoped_ptr<TestRequest> high(
857 NewBackgroundRequest("http://host/high", net::HIGHEST));
859 EXPECT_FALSE(low->started());
860 EXPECT_FALSE(high->started());
862 // request->CancelRequest();
863 request->Cancel();
864 EXPECT_TRUE(high->started());
865 EXPECT_FALSE(low->started());
868 TEST_F(ResourceSchedulerTest, ThrottledSpdyProxySchedulesImmediately) {
869 // TODO(aiolos): remove when throttling and coalescing have both landed
870 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
871 false /* should_coalesce */);
872 EXPECT_EQ(ResourceScheduler::THROTTLED,
873 scheduler_.GetClientStateForTesting(kBackgroundChildId,
874 kBackgroundRouteId));
875 scoped_ptr<TestRequest> high(
876 NewBackgroundRequest("http://host/high", net::HIGHEST));
877 scoped_ptr<TestRequest> request(
878 NewBackgroundRequest("http://host/req", net::IDLE));
880 EXPECT_FALSE(request->started());
882 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
883 kBackgroundRouteId);
884 EXPECT_TRUE(request->started());
886 scoped_ptr<TestRequest> after(
887 NewBackgroundRequest("http://host/after", net::IDLE));
888 EXPECT_TRUE(after->started());
891 TEST_F(ResourceSchedulerTest, CoalescedClientIssuesNoRequests) {
892 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
893 true /* should_coalesce */);
894 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
895 scheduler_.OnLoadingStateChanged(
896 kBackgroundChildId, kBackgroundRouteId, true);
897 EXPECT_EQ(ResourceScheduler::COALESCED,
898 scheduler_.GetClientStateForTesting(kBackgroundChildId,
899 kBackgroundRouteId));
900 scoped_ptr<TestRequest> high(
901 NewBackgroundRequest("http://host/high", net::HIGHEST));
902 scoped_ptr<TestRequest> request(
903 NewBackgroundRequest("http://host/req", net::IDLE));
905 EXPECT_FALSE(high->started());
906 EXPECT_FALSE(request->started());
908 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
909 kBackgroundRouteId);
910 EXPECT_FALSE(high->started());
912 scoped_ptr<TestRequest> after(
913 NewBackgroundRequest("http://host/after", net::HIGHEST));
914 EXPECT_FALSE(after->started());
917 TEST_F(ResourceSchedulerTest, CoalescedSpdyProxyWaits) {
918 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
919 true /* should_coalesce */);
920 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
921 scheduler_.OnLoadingStateChanged(
922 kBackgroundChildId, kBackgroundRouteId, true);
923 EXPECT_EQ(ResourceScheduler::COALESCED,
924 scheduler_.GetClientStateForTesting(kBackgroundChildId,
925 kBackgroundRouteId));
926 scoped_ptr<TestRequest> high(
927 NewBackgroundRequest("http://host/high", net::HIGHEST));
928 scoped_ptr<TestRequest> request(
929 NewBackgroundRequest("http://host/req", net::IDLE));
931 EXPECT_FALSE(request->started());
933 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
934 kBackgroundRouteId);
935 EXPECT_FALSE(request->started());
937 scoped_ptr<TestRequest> after(
938 NewBackgroundRequest("http://host/after", net::IDLE));
939 EXPECT_FALSE(after->started());
942 TEST_F(ResourceSchedulerTest, ThrottledNonHTTPSchedulesImmediately) {
943 // TODO(aiolos): remove when throttling and coalescing have both landed
944 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
945 false /* should_coalesce */);
946 // Dummies to enforce scheduling.
947 scoped_ptr<TestRequest> high(
948 NewBackgroundRequest("http://host/high", net::HIGHEST));
949 scoped_ptr<TestRequest> low(
950 NewBackgroundRequest("http://host/low", net::LOWEST));
952 scoped_ptr<TestRequest> request(
953 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
954 EXPECT_TRUE(request->started());
955 EXPECT_FALSE(low->started());
958 TEST_F(ResourceSchedulerTest, CoalescedNonHTTPSchedulesImmediately) {
959 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
960 true /* should_coalesce */);
961 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
962 scheduler_.OnLoadingStateChanged(
963 kBackgroundChildId, kBackgroundRouteId, true);
964 EXPECT_EQ(ResourceScheduler::COALESCED,
965 scheduler_.GetClientStateForTesting(kBackgroundChildId,
966 kBackgroundRouteId));
967 // Dummies to enforce scheduling.
968 scoped_ptr<TestRequest> high(
969 NewBackgroundRequest("http://host/high", net::HIGHEST));
970 scoped_ptr<TestRequest> low(
971 NewBackgroundRequest("http://host/low", net::LOWEST));
973 scoped_ptr<TestRequest> request(
974 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
975 EXPECT_TRUE(request->started());
976 EXPECT_FALSE(low->started());
979 TEST_F(ResourceSchedulerTest, ThrottledSyncSchedulesImmediately) {
980 // TODO(aiolos): remove when throttling and coalescing have both landed
981 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
982 false /* should_coalesce */);
983 // Dummies to enforce scheduling.
984 scoped_ptr<TestRequest> high(
985 NewBackgroundRequest("http://host/high", net::HIGHEST));
986 scoped_ptr<TestRequest> low(
987 NewBackgroundRequest("http://host/low", net::LOWEST));
989 scoped_ptr<TestRequest> request(
990 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
991 EXPECT_TRUE(request->started());
992 EXPECT_FALSE(low->started());
995 TEST_F(ResourceSchedulerTest, CoalescedSyncSchedulesImmediately) {
996 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
997 true /* should_coalesce */);
998 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
999 scheduler_.OnLoadingStateChanged(
1000 kBackgroundChildId, kBackgroundRouteId, true);
1001 EXPECT_EQ(ResourceScheduler::COALESCED,
1002 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1003 kBackgroundRouteId));
1004 // Dummies to enforce scheduling.
1005 scoped_ptr<TestRequest> high(
1006 NewBackgroundRequest("http://host/high", net::HIGHEST));
1007 scoped_ptr<TestRequest> low(
1008 NewBackgroundRequest("http://host/low", net::LOWEST));
1010 scoped_ptr<TestRequest> request(
1011 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
1012 EXPECT_TRUE(request->started());
1013 EXPECT_FALSE(low->started());
1014 EXPECT_FALSE(high->started());
1017 TEST_F(ResourceSchedulerTest, AllBackgroundClientsUnthrottle) {
1018 // TODO(aiolos): remove when throttling and coalescing have both landed
1019 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1020 false /* should_coalesce */);
1021 EXPECT_EQ(ResourceScheduler::THROTTLED,
1022 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1023 kBackgroundRouteId));
1024 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1025 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1026 EXPECT_FALSE(scheduler_.active_clients_loaded());
1028 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1029 EXPECT_TRUE(scheduler_.active_clients_loaded());
1030 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1031 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1032 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1033 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1034 kBackgroundRouteId));
1036 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1037 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1038 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1039 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1040 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1041 kBackgroundRouteId));
1043 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1044 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1045 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1046 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1047 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1048 kBackgroundRouteId));
1050 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1051 scheduler_.OnLoadingStateChanged(
1052 kBackgroundChildId, kBackgroundRouteId, true);
1053 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1054 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1055 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1056 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1057 kBackgroundRouteId));
1060 TEST_F(ResourceSchedulerTest,
1061 UnloadedClientVisibilityChangedCorrectlyUnthrottles) {
1062 // TODO(aiolos): remove when throttling and coalescing have both landed
1063 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1064 false /* should_coalesce */);
1065 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1066 scheduler_.OnClientCreated(
1067 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1068 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1069 scheduler_.OnLoadingStateChanged(
1070 kBackgroundChildId2, kBackgroundRouteId2, true);
1072 // 1 visible, 3 hidden
1073 EXPECT_FALSE(scheduler_.active_clients_loaded());
1074 EXPECT_EQ(ResourceScheduler::THROTTLED,
1075 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1076 kBackgroundRouteId));
1077 EXPECT_EQ(ResourceScheduler::THROTTLED,
1078 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1079 kBackgroundRouteId2));
1080 EXPECT_EQ(ResourceScheduler::THROTTLED,
1081 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1082 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1083 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1085 // 2 visible, 2 hidden
1086 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1087 EXPECT_FALSE(scheduler_.active_clients_loaded());
1088 EXPECT_EQ(ResourceScheduler::THROTTLED,
1089 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1090 kBackgroundRouteId));
1091 EXPECT_EQ(ResourceScheduler::THROTTLED,
1092 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1093 kBackgroundRouteId2));
1094 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1095 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1096 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1097 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1099 // 1 visible, 3 hidden
1100 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1101 EXPECT_FALSE(scheduler_.active_clients_loaded());
1102 EXPECT_EQ(ResourceScheduler::THROTTLED,
1103 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1104 kBackgroundRouteId));
1105 EXPECT_EQ(ResourceScheduler::THROTTLED,
1106 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1107 kBackgroundRouteId2));
1108 EXPECT_EQ(ResourceScheduler::THROTTLED,
1109 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1110 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1111 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1113 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1114 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1117 TEST_F(ResourceSchedulerTest,
1118 UnloadedClientAudibilityChangedCorrectlyUnthrottles) {
1119 // TODO(aiolos): remove when throttling and coalescing have both landed
1120 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1121 false /* should_coalesce */);
1122 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1123 scheduler_.OnClientCreated(
1124 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1125 scheduler_.OnLoadingStateChanged(
1126 kBackgroundChildId2, kBackgroundRouteId2, true);
1127 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1128 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1130 // 1 audible, 3 hidden
1131 EXPECT_FALSE(scheduler_.active_clients_loaded());
1132 EXPECT_EQ(ResourceScheduler::THROTTLED,
1133 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1134 kBackgroundRouteId));
1135 EXPECT_EQ(ResourceScheduler::THROTTLED,
1136 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1137 kBackgroundRouteId2));
1138 EXPECT_EQ(ResourceScheduler::THROTTLED,
1139 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1140 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1141 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1143 // 2 audible, 2 hidden
1144 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1145 EXPECT_FALSE(scheduler_.active_clients_loaded());
1146 EXPECT_EQ(ResourceScheduler::THROTTLED,
1147 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1148 kBackgroundRouteId));
1149 EXPECT_EQ(ResourceScheduler::THROTTLED,
1150 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1151 kBackgroundRouteId2));
1152 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1153 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1154 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1155 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1157 // 1 audible, 3 hidden
1158 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1159 EXPECT_FALSE(scheduler_.active_clients_loaded());
1160 EXPECT_EQ(ResourceScheduler::THROTTLED,
1161 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1162 kBackgroundRouteId));
1163 EXPECT_EQ(ResourceScheduler::THROTTLED,
1164 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1165 kBackgroundRouteId2));
1166 EXPECT_EQ(ResourceScheduler::THROTTLED,
1167 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1168 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1169 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1171 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1172 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1175 TEST_F(ResourceSchedulerTest,
1176 LoadedClientVisibilityChangedCorrectlyUnthrottles) {
1177 // TODO(aiolos): remove when throttling and coalescing have both landed
1178 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1179 false /* should_coalesce */);
1180 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1181 scheduler_.OnClientCreated(
1182 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1183 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1184 scheduler_.OnLoadingStateChanged(
1185 kBackgroundChildId2, kBackgroundRouteId2, true);
1186 // 1 visible, 3 hidden
1187 EXPECT_FALSE(scheduler_.active_clients_loaded());
1188 EXPECT_EQ(ResourceScheduler::THROTTLED,
1189 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1190 kBackgroundRouteId));
1191 EXPECT_EQ(ResourceScheduler::THROTTLED,
1192 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1193 kBackgroundRouteId2));
1194 EXPECT_EQ(ResourceScheduler::THROTTLED,
1195 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1196 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1197 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1199 // 2 visible, 2 hidden
1200 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1201 EXPECT_FALSE(scheduler_.active_clients_loaded());
1202 EXPECT_EQ(ResourceScheduler::THROTTLED,
1203 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1204 kBackgroundRouteId));
1205 EXPECT_EQ(ResourceScheduler::THROTTLED,
1206 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1207 kBackgroundRouteId2));
1208 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1209 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1210 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1211 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1213 // 1 visible, 3 hidden
1214 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1215 EXPECT_FALSE(scheduler_.active_clients_loaded());
1216 EXPECT_EQ(ResourceScheduler::THROTTLED,
1217 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1218 kBackgroundRouteId));
1219 EXPECT_EQ(ResourceScheduler::THROTTLED,
1220 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1221 kBackgroundRouteId2));
1222 EXPECT_EQ(ResourceScheduler::THROTTLED,
1223 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1224 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1225 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1227 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1228 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1231 TEST_F(ResourceSchedulerTest,
1232 LoadedClientAudibilityChangedCorrectlyUnthrottles) {
1233 // TODO(aiolos): remove when throttling and coalescing have both landed
1234 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1235 false /* should_coalesce */);
1236 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1237 scheduler_.OnClientCreated(
1238 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1239 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1240 scheduler_.OnLoadingStateChanged(
1241 kBackgroundChildId2, kBackgroundRouteId2, true);
1242 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1243 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1244 // 1 audible, 3 hidden
1245 EXPECT_FALSE(scheduler_.active_clients_loaded());
1246 EXPECT_EQ(ResourceScheduler::THROTTLED,
1247 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1248 kBackgroundRouteId));
1249 EXPECT_EQ(ResourceScheduler::THROTTLED,
1250 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1251 kBackgroundRouteId2));
1252 EXPECT_EQ(ResourceScheduler::THROTTLED,
1253 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1254 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1255 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1257 // 2 audible, 2 hidden
1258 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1259 EXPECT_FALSE(scheduler_.active_clients_loaded());
1260 EXPECT_EQ(ResourceScheduler::THROTTLED,
1261 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1262 kBackgroundRouteId));
1263 EXPECT_EQ(ResourceScheduler::THROTTLED,
1264 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1265 kBackgroundRouteId2));
1266 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1267 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1268 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1269 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1271 // 1 audible, 3 hidden
1272 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1273 EXPECT_FALSE(scheduler_.active_clients_loaded());
1274 EXPECT_EQ(ResourceScheduler::THROTTLED,
1275 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1276 kBackgroundRouteId));
1277 EXPECT_EQ(ResourceScheduler::THROTTLED,
1278 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1279 kBackgroundRouteId2));
1280 EXPECT_EQ(ResourceScheduler::THROTTLED,
1281 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1282 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1283 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1285 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1286 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1289 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesHiddenCorrectlyUnthrottles) {
1290 // TODO(aiolos): remove when throttling and coalescing have both landed
1291 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1292 false /* should_coalesce */);
1293 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1294 scheduler_.OnClientCreated(
1295 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1296 scheduler_.OnLoadingStateChanged(
1297 kBackgroundChildId2, kBackgroundRouteId2, true);
1299 // 2 visible, 2 hidden
1300 EXPECT_FALSE(scheduler_.active_clients_loaded());
1301 EXPECT_EQ(ResourceScheduler::THROTTLED,
1302 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1303 kBackgroundRouteId));
1304 EXPECT_EQ(ResourceScheduler::THROTTLED,
1305 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1306 kBackgroundRouteId2));
1307 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1308 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1309 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1310 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1312 // 1 visible, 3 hidden
1313 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1314 EXPECT_FALSE(scheduler_.active_clients_loaded());
1315 EXPECT_EQ(ResourceScheduler::THROTTLED,
1316 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1317 kBackgroundRouteId));
1318 EXPECT_EQ(ResourceScheduler::THROTTLED,
1319 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1320 kBackgroundRouteId2));
1321 EXPECT_EQ(ResourceScheduler::THROTTLED,
1322 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1323 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1324 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1326 // 0 visible, 4 hidden
1327 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1328 EXPECT_TRUE(scheduler_.active_clients_loaded());
1329 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1330 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1331 kBackgroundRouteId));
1332 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1333 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1334 kBackgroundRouteId2));
1335 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1336 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1337 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1338 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1340 // 1 visible, 3 hidden
1341 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
1342 EXPECT_FALSE(scheduler_.active_clients_loaded());
1343 EXPECT_EQ(ResourceScheduler::THROTTLED,
1344 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1345 kBackgroundRouteId));
1346 EXPECT_EQ(ResourceScheduler::THROTTLED,
1347 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1348 kBackgroundRouteId2));
1349 EXPECT_EQ(ResourceScheduler::THROTTLED,
1350 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1351 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1352 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1354 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1355 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1358 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesSilentCorrectlyUnthrottles) {
1359 // TODO(aiolos): remove when throttling and coalescing have both landed
1360 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1361 false /* should_coalesce */);
1362 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, true);
1363 scheduler_.OnClientCreated(
1364 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1365 scheduler_.OnLoadingStateChanged(
1366 kBackgroundChildId2, kBackgroundRouteId2, true);
1367 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1368 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1369 // 2 audible, 2 hidden
1370 EXPECT_FALSE(scheduler_.active_clients_loaded());
1371 EXPECT_EQ(ResourceScheduler::THROTTLED,
1372 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1373 kBackgroundRouteId));
1374 EXPECT_EQ(ResourceScheduler::THROTTLED,
1375 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1376 kBackgroundRouteId2));
1377 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1378 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1379 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1380 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1382 // 1 audible, 3 hidden
1383 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1384 EXPECT_FALSE(scheduler_.active_clients_loaded());
1385 EXPECT_EQ(ResourceScheduler::THROTTLED,
1386 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1387 kBackgroundRouteId));
1388 EXPECT_EQ(ResourceScheduler::THROTTLED,
1389 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1390 kBackgroundRouteId2));
1391 EXPECT_EQ(ResourceScheduler::THROTTLED,
1392 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1393 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1394 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1396 // 0 audible, 4 hidden
1397 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1398 EXPECT_TRUE(scheduler_.active_clients_loaded());
1399 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1400 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1401 kBackgroundRouteId));
1402 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1403 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1404 kBackgroundRouteId2));
1405 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1406 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1407 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1408 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1410 // 1 audible, 3 hidden
1411 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1412 EXPECT_FALSE(scheduler_.active_clients_loaded());
1413 EXPECT_EQ(ResourceScheduler::THROTTLED,
1414 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1415 kBackgroundRouteId));
1416 EXPECT_EQ(ResourceScheduler::THROTTLED,
1417 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1418 kBackgroundRouteId2));
1419 EXPECT_EQ(ResourceScheduler::THROTTLED,
1420 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1421 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1422 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1424 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1425 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1428 TEST_F(ResourceSchedulerTest, LoadedClientBecomesHiddenCorrectlyThrottles) {
1429 // TODO(aiolos): remove when throttling and coalescing have both landed
1430 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1431 false /* should_coalesce */);
1432 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1433 scheduler_.OnClientCreated(
1434 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1435 scheduler_.OnLoadingStateChanged(
1436 kBackgroundChildId2, kBackgroundRouteId2, true);
1437 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1438 // 2 visible, 2 hidden
1439 EXPECT_FALSE(scheduler_.active_clients_loaded());
1440 EXPECT_EQ(ResourceScheduler::THROTTLED,
1441 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1442 kBackgroundRouteId));
1443 EXPECT_EQ(ResourceScheduler::THROTTLED,
1444 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1445 kBackgroundRouteId2));
1446 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1447 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1448 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1449 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1451 // 1 visible, 3 hidden
1452 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1453 EXPECT_FALSE(scheduler_.active_clients_loaded());
1454 EXPECT_EQ(ResourceScheduler::THROTTLED,
1455 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1456 kBackgroundRouteId));
1457 EXPECT_EQ(ResourceScheduler::THROTTLED,
1458 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1459 kBackgroundRouteId2));
1460 EXPECT_EQ(ResourceScheduler::THROTTLED,
1461 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1462 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1463 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1465 // 0 visible, 4 hidden
1466 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1467 EXPECT_TRUE(scheduler_.active_clients_loaded());
1468 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1469 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1470 kBackgroundRouteId));
1471 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1472 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1473 kBackgroundRouteId2));
1474 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1475 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1476 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1477 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1479 // 1 visible, 3 hidden
1480 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1481 EXPECT_TRUE(scheduler_.active_clients_loaded());
1482 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1483 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1484 kBackgroundRouteId));
1485 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1486 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1487 kBackgroundRouteId2));
1488 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1489 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1490 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1491 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1493 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1494 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1497 TEST_F(ResourceSchedulerTest, LoadedClientBecomesSilentCorrectlyThrottles) {
1498 // TODO(aiolos): remove when throttling and coalescing have both landed
1499 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1500 false /* should_coalesce */);
1501 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, true);
1502 scheduler_.OnClientCreated(
1503 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1504 scheduler_.OnLoadingStateChanged(
1505 kBackgroundChildId2, kBackgroundRouteId2, true);
1506 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1507 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1508 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1509 // 2 audible, 2 hidden
1510 EXPECT_FALSE(scheduler_.active_clients_loaded());
1511 EXPECT_EQ(ResourceScheduler::THROTTLED,
1512 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1513 kBackgroundRouteId));
1514 EXPECT_EQ(ResourceScheduler::THROTTLED,
1515 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1516 kBackgroundRouteId2));
1517 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1518 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1519 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1520 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1522 // 1 audible, 3 hidden
1523 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1524 EXPECT_FALSE(scheduler_.active_clients_loaded());
1525 EXPECT_EQ(ResourceScheduler::THROTTLED,
1526 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1527 kBackgroundRouteId));
1528 EXPECT_EQ(ResourceScheduler::THROTTLED,
1529 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1530 kBackgroundRouteId2));
1531 EXPECT_EQ(ResourceScheduler::THROTTLED,
1532 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1533 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1534 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1536 // 0 audible, 4 hidden
1537 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1538 EXPECT_TRUE(scheduler_.active_clients_loaded());
1539 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1540 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1541 kBackgroundRouteId));
1542 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1543 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1544 kBackgroundRouteId2));
1545 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1546 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1547 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1548 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1550 // 1 audible, 3 hidden
1551 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1552 EXPECT_TRUE(scheduler_.active_clients_loaded());
1553 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1554 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1555 kBackgroundRouteId));
1556 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1557 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1558 kBackgroundRouteId2));
1559 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1560 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1561 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1562 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1564 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1565 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1568 TEST_F(ResourceSchedulerTest, HiddenLoadedChangesCorrectlyStayThrottled) {
1569 // TODO(aiolos): remove when throttling and coalescing have both landed
1570 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1571 false /* should_coalesce */);
1572 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1573 scheduler_.OnClientCreated(
1574 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1576 // 1 visible and 2 hidden loading, 1 visible loaded
1577 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1578 EXPECT_FALSE(scheduler_.active_clients_loaded());
1579 EXPECT_EQ(ResourceScheduler::THROTTLED,
1580 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1581 kBackgroundRouteId));
1582 EXPECT_EQ(ResourceScheduler::THROTTLED,
1583 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1584 kBackgroundRouteId2));
1585 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1586 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1587 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1588 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1590 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1591 scheduler_.OnLoadingStateChanged(
1592 kBackgroundChildId2, kBackgroundRouteId2, true);
1593 EXPECT_FALSE(scheduler_.active_clients_loaded());
1594 EXPECT_EQ(ResourceScheduler::THROTTLED,
1595 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1596 kBackgroundRouteId));
1597 EXPECT_EQ(ResourceScheduler::THROTTLED,
1598 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1599 kBackgroundRouteId2));
1600 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1601 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1602 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1603 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1605 // 1 visible loading, 1 visible and 2 hidden loaded
1606 scheduler_.OnLoadingStateChanged(
1607 kBackgroundChildId, kBackgroundRouteId, true);
1608 EXPECT_FALSE(scheduler_.active_clients_loaded());
1609 EXPECT_EQ(ResourceScheduler::THROTTLED,
1610 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1611 kBackgroundRouteId));
1612 EXPECT_EQ(ResourceScheduler::THROTTLED,
1613 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1614 kBackgroundRouteId2));
1615 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1616 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1617 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1618 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1620 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1621 scheduler_.OnLoadingStateChanged(
1622 kBackgroundChildId2, kBackgroundRouteId2, true);
1623 EXPECT_FALSE(scheduler_.active_clients_loaded());
1624 EXPECT_EQ(ResourceScheduler::THROTTLED,
1625 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1626 kBackgroundRouteId));
1627 EXPECT_EQ(ResourceScheduler::THROTTLED,
1628 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1629 kBackgroundRouteId2));
1630 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1631 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1632 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1633 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1635 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1636 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1639 TEST_F(ResourceSchedulerTest, PartialVisibleClientLoadedDoesNotUnthrottle) {
1640 // TODO(aiolos): remove when throttling and coalescing have both landed
1641 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1642 false /* should_coalesce */);
1643 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1644 scheduler_.OnClientCreated(
1645 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1647 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1648 scheduler_.OnLoadingStateChanged(
1649 kBackgroundChildId2, kBackgroundRouteId2, true);
1650 EXPECT_FALSE(scheduler_.active_clients_loaded());
1651 EXPECT_EQ(ResourceScheduler::THROTTLED,
1652 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1653 kBackgroundRouteId));
1654 EXPECT_EQ(ResourceScheduler::THROTTLED,
1655 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1656 kBackgroundRouteId2));
1657 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1658 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1659 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1660 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1662 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1663 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1664 EXPECT_FALSE(scheduler_.active_clients_loaded());
1665 EXPECT_EQ(ResourceScheduler::THROTTLED,
1666 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1667 kBackgroundRouteId));
1668 EXPECT_EQ(ResourceScheduler::THROTTLED,
1669 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1670 kBackgroundRouteId2));
1671 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1672 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1673 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1674 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1676 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1677 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1678 EXPECT_FALSE(scheduler_.active_clients_loaded());
1679 EXPECT_EQ(ResourceScheduler::THROTTLED,
1680 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1681 kBackgroundRouteId));
1682 EXPECT_EQ(ResourceScheduler::THROTTLED,
1683 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1684 kBackgroundRouteId2));
1685 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1686 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1687 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1688 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1690 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1691 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1694 TEST_F(ResourceSchedulerTest, FullVisibleLoadedCorrectlyUnthrottle) {
1695 // TODO(aiolos): remove when throttling and coalescing have both landed
1696 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1697 false /* should_coalesce */);
1698 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1699 scheduler_.OnClientCreated(
1700 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1702 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1703 scheduler_.OnLoadingStateChanged(
1704 kBackgroundChildId2, kBackgroundRouteId2, true);
1705 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1706 EXPECT_FALSE(scheduler_.active_clients_loaded());
1707 EXPECT_EQ(ResourceScheduler::THROTTLED,
1708 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1709 kBackgroundRouteId));
1710 EXPECT_EQ(ResourceScheduler::THROTTLED,
1711 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1712 kBackgroundRouteId2));
1713 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1714 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1715 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1716 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1718 scoped_ptr<TestRequest> high(
1719 NewBackgroundRequest("http://host/high", net::HIGHEST));
1720 scoped_ptr<TestRequest> low(
1721 NewBackgroundRequest("http://host/low", net::LOWEST));
1723 EXPECT_TRUE(high->started());
1724 EXPECT_FALSE(low->started());
1726 // 2 visible loaded, 1 hidden loading, 1 hidden loaded
1727 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1728 EXPECT_TRUE(scheduler_.active_clients_loaded());
1729 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1730 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1731 kBackgroundRouteId));
1732 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1733 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1734 kBackgroundRouteId2));
1735 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1736 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1737 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1738 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1739 // kBackgroundClientId unthrottling should unthrottle it's request.
1740 EXPECT_TRUE(low->started());
1742 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1743 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1744 EXPECT_FALSE(scheduler_.active_clients_loaded());
1745 EXPECT_EQ(ResourceScheduler::THROTTLED,
1746 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1747 kBackgroundRouteId));
1748 EXPECT_EQ(ResourceScheduler::THROTTLED,
1749 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1750 kBackgroundRouteId2));
1751 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1752 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1753 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1754 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1756 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1757 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1760 TEST_F(ResourceSchedulerTest,
1761 ActiveAndLoadingClientDeletedCorrectlyUnthrottle) {
1762 // TODO(aiolos): remove when throttling and coalescing have both landed
1763 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1764 false /* should_coalesce */);
1765 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1766 scheduler_.OnClientCreated(
1767 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1769 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1770 scheduler_.OnLoadingStateChanged(
1771 kBackgroundChildId2, kBackgroundRouteId2, true);
1772 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1773 EXPECT_FALSE(scheduler_.active_clients_loaded());
1774 EXPECT_EQ(ResourceScheduler::THROTTLED,
1775 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1776 kBackgroundRouteId));
1777 EXPECT_EQ(ResourceScheduler::THROTTLED,
1778 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1779 kBackgroundRouteId2));
1780 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1781 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1782 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1783 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1785 // 1 visible loaded, 1 hidden loading, 1 hidden loaded
1786 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1787 EXPECT_TRUE(scheduler_.active_clients_loaded());
1788 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1789 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1790 kBackgroundRouteId));
1791 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1792 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1793 kBackgroundRouteId2));
1794 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1795 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1797 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1798 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1799 EXPECT_FALSE(scheduler_.active_clients_loaded());
1800 EXPECT_EQ(ResourceScheduler::THROTTLED,
1801 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1802 kBackgroundRouteId));
1803 EXPECT_EQ(ResourceScheduler::THROTTLED,
1804 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1805 kBackgroundRouteId2));
1806 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1807 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1809 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1812 TEST_F(ResourceSchedulerTest, CoalescedClientCreationStartsTimer) {
1813 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1814 true /* should_coalesce */);
1815 EXPECT_FALSE(mock_timer_->IsRunning());
1816 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1817 EXPECT_FALSE(mock_timer_->IsRunning());
1818 scheduler_.OnLoadingStateChanged(
1819 kBackgroundChildId, kBackgroundRouteId, true);
1820 EXPECT_EQ(ResourceScheduler::COALESCED,
1821 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1822 kBackgroundRouteId));
1823 EXPECT_TRUE(mock_timer_->IsRunning());
1826 TEST_F(ResourceSchedulerTest, ActiveLoadingClientLoadedAndHiddenStartsTimer) {
1827 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1828 true /* should_coalesce */);
1829 EXPECT_FALSE(mock_timer_->IsRunning());
1830 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1831 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1832 EXPECT_EQ(ResourceScheduler::THROTTLED,
1833 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1834 kBackgroundRouteId));
1835 EXPECT_FALSE(mock_timer_->IsRunning());
1837 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1838 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1839 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1840 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1841 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1842 kBackgroundRouteId));
1843 EXPECT_FALSE(mock_timer_->IsRunning());
1845 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1846 EXPECT_EQ(ResourceScheduler::COALESCED,
1847 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1848 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1849 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1850 kBackgroundRouteId));
1851 EXPECT_TRUE(mock_timer_->IsRunning());
1854 TEST_F(ResourceSchedulerTest, ActiveLoadingClientHiddenAndLoadedStartsTimer) {
1855 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1856 true /* should_coalesce */);
1857 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1858 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1859 EXPECT_EQ(ResourceScheduler::THROTTLED,
1860 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1861 kBackgroundRouteId));
1862 EXPECT_FALSE(mock_timer_->IsRunning());
1864 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1865 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1866 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1867 EXPECT_FALSE(mock_timer_->IsRunning());
1869 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1870 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1871 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1872 kBackgroundRouteId));
1873 EXPECT_EQ(ResourceScheduler::COALESCED,
1874 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1875 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1876 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1877 kBackgroundRouteId));
1878 EXPECT_TRUE(mock_timer_->IsRunning());
1881 TEST_F(ResourceSchedulerTest, CoalescedClientBecomesAudibleStopsTimer) {
1882 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1883 true /* should_coalesce */);
1884 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1885 EXPECT_FALSE(mock_timer_->IsRunning());
1886 scheduler_.OnLoadingStateChanged(
1887 kBackgroundChildId, kBackgroundRouteId, true);
1888 EXPECT_EQ(ResourceScheduler::COALESCED,
1889 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1890 kBackgroundRouteId));
1891 EXPECT_TRUE(mock_timer_->IsRunning());
1893 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1894 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1895 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1896 kBackgroundRouteId));
1897 EXPECT_FALSE(mock_timer_->IsRunning());
1900 TEST_F(ResourceSchedulerTest, LastCoalescedClientDeletionStopsTimer) {
1901 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1902 true /* should_coalesce */);
1903 scheduler_.OnClientCreated(
1904 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1905 EXPECT_FALSE(mock_timer_->IsRunning());
1906 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1907 EXPECT_FALSE(mock_timer_->IsRunning());
1908 scheduler_.OnLoadingStateChanged(
1909 kBackgroundChildId, kBackgroundRouteId, true);
1910 EXPECT_EQ(ResourceScheduler::COALESCED,
1911 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1912 kBackgroundRouteId));
1913 scheduler_.OnLoadingStateChanged(
1914 kBackgroundChildId2, kBackgroundRouteId2, true);
1915 EXPECT_EQ(ResourceScheduler::COALESCED,
1916 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1917 kBackgroundRouteId2));
1918 EXPECT_TRUE(mock_timer_->IsRunning());
1920 scheduler_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
1921 EXPECT_TRUE(mock_timer_->IsRunning());
1923 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1924 EXPECT_FALSE(mock_timer_->IsRunning());
1926 // To avoid errors on test tear down.
1927 scheduler_.OnClientCreated(
1928 kBackgroundChildId, kBackgroundRouteId, false, false);
1931 TEST_F(ResourceSchedulerTest, LastCoalescedClientStartsLoadingStopsTimer) {
1932 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1933 true /* should_coalesce */);
1934 scheduler_.OnClientCreated(
1935 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1936 EXPECT_FALSE(mock_timer_->IsRunning());
1937 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1938 EXPECT_FALSE(mock_timer_->IsRunning());
1939 scheduler_.OnLoadingStateChanged(
1940 kBackgroundChildId, kBackgroundRouteId, true);
1941 EXPECT_EQ(ResourceScheduler::COALESCED,
1942 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1943 kBackgroundRouteId));
1944 scheduler_.OnLoadingStateChanged(
1945 kBackgroundChildId2, kBackgroundRouteId2, true);
1946 EXPECT_EQ(ResourceScheduler::COALESCED,
1947 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1948 kBackgroundRouteId2));
1949 EXPECT_TRUE(mock_timer_->IsRunning());
1951 scheduler_.OnLoadingStateChanged(
1952 kBackgroundChildId, kBackgroundRouteId, false);
1953 EXPECT_TRUE(mock_timer_->IsRunning());
1955 scheduler_.OnLoadingStateChanged(
1956 kBackgroundChildId2, kBackgroundRouteId2, false);
1957 EXPECT_FALSE(mock_timer_->IsRunning());
1959 // This is needed to avoid errors on test tear down.
1960 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1963 TEST_F(ResourceSchedulerTest, LastCoalescedClientBecomesVisibleStopsTimer) {
1964 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1965 true /* should_coalesce */);
1966 scheduler_.OnClientCreated(
1967 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1968 EXPECT_FALSE(mock_timer_->IsRunning());
1969 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1970 EXPECT_FALSE(mock_timer_->IsRunning());
1971 scheduler_.OnLoadingStateChanged(
1972 kBackgroundChildId, kBackgroundRouteId, true);
1973 EXPECT_EQ(ResourceScheduler::COALESCED,
1974 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1975 kBackgroundRouteId));
1976 scheduler_.OnLoadingStateChanged(
1977 kBackgroundChildId2, kBackgroundRouteId2, true);
1978 EXPECT_EQ(ResourceScheduler::COALESCED,
1979 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1980 kBackgroundRouteId2));
1981 EXPECT_TRUE(mock_timer_->IsRunning());
1983 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1984 EXPECT_TRUE(mock_timer_->IsRunning());
1986 scheduler_.OnVisibilityChanged(
1987 kBackgroundChildId2, kBackgroundRouteId2, true);
1988 EXPECT_FALSE(mock_timer_->IsRunning());
1990 // To avoid errors on test tear down.
1991 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1994 TEST_F(ResourceSchedulerTest,
1995 CoalescedClientBecomesLoadingAndVisibleStopsTimer) {
1996 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1997 true /* should_coalesce */);
1998 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1999 EXPECT_FALSE(mock_timer_->IsRunning());
2000 scheduler_.OnLoadingStateChanged(
2001 kBackgroundChildId, kBackgroundRouteId, true);
2002 EXPECT_EQ(ResourceScheduler::COALESCED,
2003 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2004 kBackgroundRouteId));
2005 EXPECT_TRUE(mock_timer_->IsRunning());
2007 scheduler_.OnLoadingStateChanged(
2008 kBackgroundChildId, kBackgroundRouteId, false);
2009 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
2010 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2011 kBackgroundRouteId));
2012 EXPECT_FALSE(mock_timer_->IsRunning());
2014 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
2015 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
2016 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2017 kBackgroundRouteId));
2018 EXPECT_FALSE(mock_timer_->IsRunning());
2021 TEST_F(ResourceSchedulerTest, CoalescedRequestsIssueOnTimer) {
2022 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2023 true /* should_coalesce */);
2024 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2025 scheduler_.OnLoadingStateChanged(
2026 kBackgroundChildId, kBackgroundRouteId, true);
2027 EXPECT_EQ(ResourceScheduler::COALESCED,
2028 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2029 kBackgroundRouteId));
2030 EXPECT_TRUE(scheduler_.active_clients_loaded());
2032 scoped_ptr<TestRequest> high(
2033 NewBackgroundRequest("http://host/high", net::HIGHEST));
2034 scoped_ptr<TestRequest> low(
2035 NewBackgroundRequest("http://host/low", net::LOWEST));
2036 EXPECT_FALSE(high->started());
2037 EXPECT_FALSE(low->started());
2039 FireCoalescingTimer();
2041 EXPECT_TRUE(high->started());
2042 EXPECT_TRUE(low->started());
2045 TEST_F(ResourceSchedulerTest, CoalescedRequestsUnthrottleCorrectlyOnTimer) {
2046 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2047 true /* should_coalesce */);
2048 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2049 scheduler_.OnLoadingStateChanged(
2050 kBackgroundChildId, kBackgroundRouteId, true);
2051 EXPECT_EQ(ResourceScheduler::COALESCED,
2052 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2053 kBackgroundRouteId));
2054 EXPECT_TRUE(scheduler_.active_clients_loaded());
2056 scoped_ptr<TestRequest> high(
2057 NewBackgroundRequest("http://host/high", net::HIGHEST));
2058 scoped_ptr<TestRequest> high2(
2059 NewBackgroundRequest("http://host/high", net::HIGHEST));
2060 scoped_ptr<TestRequest> high3(
2061 NewBackgroundRequest("http://host/high", net::HIGHEST));
2062 scoped_ptr<TestRequest> high4(
2063 NewBackgroundRequest("http://host/high", net::HIGHEST));
2064 scoped_ptr<TestRequest> low(
2065 NewBackgroundRequest("http://host/low", net::LOWEST));
2066 scoped_ptr<TestRequest> low2(
2067 NewBackgroundRequest("http://host/low", net::LOWEST));
2068 scoped_ptr<TestRequest> low3(
2069 NewBackgroundRequest("http://host/low", net::LOWEST));
2070 scoped_ptr<TestRequest> low4(
2071 NewBackgroundRequest("http://host/low", net::LOWEST));
2073 http_server_properties_.SetSupportsSpdy(net::HostPortPair("spdyhost", 443),
2074 true);
2075 scoped_ptr<TestRequest> low_spdy(
2076 NewBackgroundRequest("https://spdyhost/low", net::LOW));
2077 scoped_ptr<TestRequest> sync_request(
2078 NewBackgroundSyncRequest("http://host/req", net::LOW));
2079 scoped_ptr<TestRequest> non_http_request(
2080 NewBackgroundRequest("chrome-extension://req", net::LOW));
2082 // Sync requests should issue immediately.
2083 EXPECT_TRUE(sync_request->started());
2084 // Non-http(s) requests should issue immediately.
2085 EXPECT_TRUE(non_http_request->started());
2086 // Nothing else should issue without a timer fire.
2087 EXPECT_FALSE(high->started());
2088 EXPECT_FALSE(high2->started());
2089 EXPECT_FALSE(high3->started());
2090 EXPECT_FALSE(high4->started());
2091 EXPECT_FALSE(low->started());
2092 EXPECT_FALSE(low2->started());
2093 EXPECT_FALSE(low3->started());
2094 EXPECT_FALSE(low4->started());
2095 EXPECT_FALSE(low_spdy->started());
2097 FireCoalescingTimer();
2099 // All high priority requests should issue.
2100 EXPECT_TRUE(high->started());
2101 EXPECT_TRUE(high2->started());
2102 EXPECT_TRUE(high3->started());
2103 EXPECT_TRUE(high4->started());
2104 // There should only be one net::LOWEST priority request issued with
2105 // non-delayable requests in flight.
2106 EXPECT_TRUE(low->started());
2107 EXPECT_FALSE(low2->started());
2108 EXPECT_FALSE(low3->started());
2109 EXPECT_FALSE(low4->started());
2110 // Spdy-Enable requests should issue regardless of priority.
2111 EXPECT_TRUE(low_spdy->started());
2114 TEST_F(ResourceSchedulerTest, CoalescedRequestsWaitForNextTimer) {
2115 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2116 true /* should_coalesce */);
2117 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2118 scheduler_.OnLoadingStateChanged(
2119 kBackgroundChildId, kBackgroundRouteId, true);
2121 EXPECT_EQ(ResourceScheduler::COALESCED,
2122 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2123 kBackgroundRouteId));
2124 EXPECT_TRUE(scheduler_.active_clients_loaded());
2126 scoped_ptr<TestRequest> high(
2127 NewBackgroundRequest("http://host/high", net::HIGHEST));
2128 EXPECT_FALSE(high->started());
2130 FireCoalescingTimer();
2132 scoped_ptr<TestRequest> high2(
2133 NewBackgroundRequest("http://host/high2", net::HIGHEST));
2134 scoped_ptr<TestRequest> low(
2135 NewBackgroundRequest("http://host/low", net::LOWEST));
2137 EXPECT_TRUE(high->started());
2138 EXPECT_FALSE(high2->started());
2139 EXPECT_FALSE(low->started());
2141 FireCoalescingTimer();
2143 EXPECT_TRUE(high->started());
2144 EXPECT_TRUE(high2->started());
2145 EXPECT_TRUE(low->started());
2148 TEST_F(ResourceSchedulerTest, GetVisualSignalFromRenderViewHost) {
2149 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory;
2150 scoped_ptr<TestRenderViewHostFactory> render_view_host_factory;
2151 scoped_ptr<TestBrowserContext> browser_context;
2152 scoped_ptr<TestWebContents> web_contents_1;
2153 scoped_ptr<TestWebContents> web_contents_2;
2154 render_process_host_factory.reset(new MockRenderProcessHostFactory());
2155 render_view_host_factory.reset(
2156 new TestRenderViewHostFactory(render_process_host_factory.get()));
2158 browser_context.reset(new TestBrowserContext());
2159 scoped_refptr<SiteInstance> site_instance_1 =
2160 SiteInstance::Create(browser_context.get());
2161 scoped_refptr<SiteInstance> site_instance_2 =
2162 SiteInstance::Create(browser_context.get());
2163 SiteInstanceImpl::set_render_process_host_factory(
2164 render_process_host_factory.get());
2166 web_contents_1.reset(
2167 TestWebContents::Create(browser_context.get(), site_instance_1.get()));
2168 web_contents_2.reset(
2169 TestWebContents::Create(browser_context.get(), site_instance_2.get()));
2170 base::RunLoop().RunUntilIdle();
2172 RenderViewHostImpl* rvh1 = web_contents_1->GetRenderViewHost();
2173 RenderViewHostImpl* rvh2 = web_contents_2->GetRenderViewHost();
2174 ResourceScheduler* scheduler = ResourceDispatcherHostImpl::Get()->scheduler();
2176 // Check initial visibility is set correctly.
2177 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2178 rvh1->GetRoutingID()),
2179 !rvh1->is_hidden());
2180 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2181 rvh1->GetRoutingID()),
2182 !rvh2->is_hidden());
2184 // 1 visible, 1 hidden
2185 rvh1->WasShown(ui::LatencyInfo());
2186 rvh2->WasHidden();
2187 base::RunLoop().RunUntilIdle();
2189 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2190 rvh1->GetRoutingID()));
2191 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2192 rvh2->GetRoutingID()));
2194 // Flip the visibility and check again.
2195 rvh1->WasHidden();
2196 rvh2->WasShown(ui::LatencyInfo());
2197 base::RunLoop().RunUntilIdle();
2199 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2200 rvh1->GetRoutingID()));
2201 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2202 rvh2->GetRoutingID()));
2203 // Clean up.
2204 web_contents_1.reset();
2205 web_contents_2.reset();
2206 base::RunLoop().RunUntilIdle();
2208 browser_context.reset();
2209 render_process_host_factory.reset();
2212 } // unnamed namespace
2214 } // namespace content