Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / loader / resource_scheduler_unittest.cc
blob3aaf23d5977dbb3045b1c5c076ce44b1c6be9b75
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/metrics/field_trial.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/test/mock_entropy_provider.h"
13 #include "base/timer/mock_timer.h"
14 #include "base/timer/timer.h"
15 #include "content/browser/browser_thread_impl.h"
16 #include "content/browser/loader/resource_dispatcher_host_impl.h"
17 #include "content/public/browser/resource_context.h"
18 #include "content/public/browser/resource_controller.h"
19 #include "content/public/browser/resource_throttle.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/test/mock_render_process_host.h"
22 #include "content/public/test/test_browser_context.h"
23 #include "content/test/test_render_view_host_factory.h"
24 #include "content/test/test_web_contents.h"
25 #include "net/base/host_port_pair.h"
26 #include "net/base/request_priority.h"
27 #include "net/http/http_server_properties_impl.h"
28 #include "net/url_request/url_request.h"
29 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/events/latency_info.h"
33 using std::string;
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<net::URLRequest> url_request,
53 scoped_ptr<ResourceThrottle> throttle,
54 ResourceScheduler* scheduler)
55 : started_(false),
56 url_request_(url_request.Pass()),
57 throttle_(throttle.Pass()),
58 scheduler_(scheduler) {
59 throttle_->set_controller_for_testing(this);
61 ~TestRequest() override {
62 // The URLRequest must still be valid when the ScheduledResourceRequest is
63 // destroyed, so that it can unregister itself.
64 throttle_.reset();
67 bool started() const { return started_; }
69 void Start() {
70 bool deferred = false;
71 throttle_->WillStartRequest(&deferred);
72 started_ = !deferred;
75 void ChangePriority(net::RequestPriority new_priority, int intra_priority) {
76 scheduler_->ReprioritizeRequest(url_request_.get(), new_priority,
77 intra_priority);
80 void Cancel() override {
81 // Alert the scheduler that the request can be deleted.
82 throttle_.reset();
85 const net::URLRequest* url_request() const { return url_request_.get(); }
87 protected:
88 // ResourceController interface:
89 void CancelAndIgnore() override {}
90 void CancelWithError(int error_code) override {}
91 void Resume() override { started_ = true; }
93 private:
94 bool started_;
95 scoped_ptr<net::URLRequest> url_request_;
96 scoped_ptr<ResourceThrottle> throttle_;
97 ResourceScheduler* scheduler_;
100 class CancelingTestRequest : public TestRequest {
101 public:
102 CancelingTestRequest(scoped_ptr<net::URLRequest> url_request,
103 scoped_ptr<ResourceThrottle> throttle,
104 ResourceScheduler* scheduler)
105 : TestRequest(url_request.Pass(), throttle.Pass(), scheduler) {}
107 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) {
108 request_to_cancel_ = request_to_cancel.Pass();
111 private:
112 void Resume() override {
113 TestRequest::Resume();
114 request_to_cancel_.reset();
117 scoped_ptr<TestRequest> request_to_cancel_;
120 class FakeResourceContext : public ResourceContext {
121 private:
122 net::HostResolver* GetHostResolver() override { return NULL; }
123 net::URLRequestContext* GetRequestContext() override { return NULL; }
126 class ResourceSchedulerTest : public testing::Test {
127 protected:
128 ResourceSchedulerTest()
129 : ui_thread_(BrowserThread::UI, &message_loop_),
130 io_thread_(BrowserThread::IO, &message_loop_),
131 field_trial_list_(new base::MockEntropyProvider()) {
132 InitializeScheduler();
133 context_.set_http_server_properties(http_server_properties_.GetWeakPtr());
136 ~ResourceSchedulerTest() override {
137 CleanupScheduler();
140 // Done separately from construction to allow for modification of command
141 // line flags in tests.
142 void InitializeScheduler() {
143 CleanupScheduler();
145 // Destroys previous scheduler, also destroys any previously created
146 // mock_timer_.
147 scheduler_.reset(new ResourceScheduler());
149 mock_timer_ = new base::MockTimer(true, true);
150 scheduler_->set_timer_for_testing(scoped_ptr<base::Timer>(mock_timer_));
152 // TODO(aiolos): Remove when throttling and coalescing have both landed.
153 scheduler_->SetThrottleOptionsForTesting(true /* should_throttle */,
154 false /* should_coalesce */);
156 scheduler_->OnClientCreated(kChildId, kRouteId, true, false);
157 scheduler_->OnClientCreated(
158 kBackgroundChildId, kBackgroundRouteId, false, false);
161 void CleanupScheduler() {
162 if (scheduler_) {
163 scheduler_->OnClientDeleted(kChildId, kRouteId);
164 scheduler_->OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
168 // Create field trials based on the argument, which has the same format
169 // as the argument to kForceFieldTrials.
170 bool InitializeFieldTrials(const std::string& force_field_trial_argument) {
171 return base::FieldTrialList::CreateTrialsFromString(
172 force_field_trial_argument,
173 base::FieldTrialList::DONT_ACTIVATE_TRIALS,
174 std::set<std::string>());
177 scoped_ptr<net::URLRequest> NewURLRequestWithChildAndRoute(
178 const char* url,
179 net::RequestPriority priority,
180 int child_id,
181 int route_id) {
182 scoped_ptr<net::URLRequest> url_request(
183 context_.CreateRequest(GURL(url), priority, NULL));
184 return url_request.Pass();
187 scoped_ptr<net::URLRequest> NewURLRequest(const char* url,
188 net::RequestPriority priority) {
189 return NewURLRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
192 TestRequest* NewRequestWithRoute(const char* url,
193 net::RequestPriority priority,
194 int route_id) {
195 return NewRequestWithChildAndRoute(url, priority, kChildId, route_id);
198 TestRequest* NewRequestWithChildAndRoute(const char* url,
199 net::RequestPriority priority,
200 int child_id,
201 int route_id) {
202 return GetNewTestRequest(url, priority, child_id, route_id, true);
205 TestRequest* NewRequest(const char* url, net::RequestPriority priority) {
206 return NewRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
209 TestRequest* NewBackgroundRequest(const char* url,
210 net::RequestPriority priority) {
211 return NewRequestWithChildAndRoute(
212 url, priority, kBackgroundChildId, kBackgroundRouteId);
215 TestRequest* NewSyncRequest(const char* url, net::RequestPriority priority) {
216 return NewSyncRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
219 TestRequest* NewBackgroundSyncRequest(const char* url,
220 net::RequestPriority priority) {
221 return NewSyncRequestWithChildAndRoute(
222 url, priority, kBackgroundChildId, kBackgroundRouteId);
225 TestRequest* NewSyncRequestWithChildAndRoute(const char* url,
226 net::RequestPriority priority,
227 int child_id,
228 int route_id) {
229 return GetNewTestRequest(url, priority, child_id, route_id, false);
232 TestRequest* GetNewTestRequest(const char* url,
233 net::RequestPriority priority,
234 int child_id,
235 int route_id,
236 bool is_async) {
237 scoped_ptr<net::URLRequest> url_request(
238 NewURLRequestWithChildAndRoute(url, priority, child_id, route_id));
239 scoped_ptr<ResourceThrottle> throttle(scheduler_->ScheduleRequest(
240 child_id, route_id, is_async, url_request.get()));
241 TestRequest* request =
242 new TestRequest(url_request.Pass(), throttle.Pass(), scheduler());
243 request->Start();
244 return request;
247 void ChangeRequestPriority(TestRequest* request,
248 net::RequestPriority new_priority,
249 int intra_priority = 0) {
250 request->ChangePriority(new_priority, intra_priority);
253 void FireCoalescingTimer() {
254 EXPECT_TRUE(mock_timer_->IsRunning());
255 mock_timer_->Fire();
258 ResourceScheduler* scheduler() {
259 return scheduler_.get();
262 base::MessageLoopForIO message_loop_;
263 BrowserThreadImpl ui_thread_;
264 BrowserThreadImpl io_thread_;
265 ResourceDispatcherHostImpl rdh_;
266 scoped_ptr<ResourceScheduler> scheduler_;
267 base::FieldTrialList field_trial_list_;
268 base::MockTimer* mock_timer_;
269 net::HttpServerPropertiesImpl http_server_properties_;
270 net::TestURLRequestContext context_;
273 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
274 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST));
275 EXPECT_TRUE(request->started());
278 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilIdle) {
279 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
280 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
281 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
282 EXPECT_TRUE(high->started());
283 EXPECT_TRUE(low->started());
284 EXPECT_FALSE(low2->started());
285 high.reset();
286 EXPECT_TRUE(low2->started());
289 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInserted) {
290 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
291 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
292 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
293 EXPECT_TRUE(high->started());
294 EXPECT_TRUE(low->started());
295 EXPECT_FALSE(low2->started());
296 high.reset();
297 scheduler()->OnWillInsertBody(kChildId, kRouteId);
298 EXPECT_TRUE(low2->started());
301 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilCriticalComplete) {
302 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
303 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
304 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
305 EXPECT_TRUE(high->started());
306 EXPECT_TRUE(low->started());
307 EXPECT_FALSE(low2->started());
308 scheduler()->OnWillInsertBody(kChildId, kRouteId);
309 EXPECT_FALSE(low2->started());
310 high.reset();
311 EXPECT_TRUE(low2->started());
314 TEST_F(ResourceSchedulerTest, LowDoesNotBlockCriticalComplete) {
315 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
316 scoped_ptr<TestRequest> lowest(NewRequest("http://host/lowest", net::LOWEST));
317 scoped_ptr<TestRequest> lowest2(
318 NewRequest("http://host/lowest", net::LOWEST));
319 EXPECT_TRUE(low->started());
320 EXPECT_TRUE(lowest->started());
321 EXPECT_FALSE(lowest2->started());
322 scheduler()->OnWillInsertBody(kChildId, kRouteId);
323 EXPECT_TRUE(lowest2->started());
326 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) {
327 http_server_properties_.SetSupportsSpdy(
328 net::HostPortPair("spdyhost", 443), true);
329 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
330 scoped_ptr<TestRequest> low_spdy(
331 NewRequest("https://spdyhost/low", net::LOWEST));
332 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
333 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
334 EXPECT_TRUE(high->started());
335 EXPECT_TRUE(low_spdy->started());
336 EXPECT_TRUE(low->started());
337 EXPECT_FALSE(low2->started());
338 scheduler()->OnWillInsertBody(kChildId, kRouteId);
339 high.reset();
340 EXPECT_TRUE(low2->started());
343 TEST_F(ResourceSchedulerTest, NavigationResetsState) {
344 scheduler()->OnWillInsertBody(kChildId, kRouteId);
345 scheduler()->OnNavigate(kChildId, kRouteId);
346 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
347 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
348 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
349 EXPECT_TRUE(high->started());
350 EXPECT_TRUE(low->started());
351 EXPECT_FALSE(low2->started());
354 TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
355 const int route_id = 0; // Indicates a background request.
356 scoped_ptr<TestRequest> request(NewRequestWithRoute("http://host/1",
357 net::LOWEST, route_id));
358 EXPECT_TRUE(request->started());
361 TEST_F(ResourceSchedulerTest, StartMultipleLowRequestsWhenIdle) {
362 scoped_ptr<TestRequest> high1(NewRequest("http://host/high1", net::HIGHEST));
363 scoped_ptr<TestRequest> high2(NewRequest("http://host/high2", net::HIGHEST));
364 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
365 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
366 EXPECT_TRUE(high1->started());
367 EXPECT_TRUE(high2->started());
368 EXPECT_TRUE(low->started());
369 EXPECT_FALSE(low2->started());
370 high1.reset();
371 EXPECT_FALSE(low2->started());
372 high2.reset();
373 EXPECT_TRUE(low2->started());
376 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) {
377 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
378 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST));
380 scoped_ptr<net::URLRequest> url_request(
381 NewURLRequest("http://host/low2", net::LOWEST));
382 scoped_ptr<ResourceThrottle> throttle(scheduler()->ScheduleRequest(
383 kChildId, kRouteId, true, url_request.get()));
384 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest(
385 url_request.Pass(), throttle.Pass(), scheduler()));
386 low2->Start();
388 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST));
389 low2->set_request_to_cancel(low3.Pass());
390 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST));
392 EXPECT_TRUE(high->started());
393 EXPECT_FALSE(low2->started());
394 high.reset();
395 EXPECT_TRUE(low1->started());
396 EXPECT_TRUE(low2->started());
397 EXPECT_TRUE(low4->started());
400 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
401 // We only load low priority resources if there's a body.
402 scheduler()->OnWillInsertBody(kChildId, kRouteId);
404 // Throw in one high priority request to make sure that's not a factor.
405 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
406 EXPECT_TRUE(high->started());
408 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
409 const int kMaxNumDelayableRequestsPerHost = 6;
410 ScopedVector<TestRequest> lows_singlehost;
411 // Queue up to the per-host limit (we subtract the current high-pri request).
412 for (int i = 0; i < kMaxNumDelayableRequestsPerHost - 1; ++i) {
413 string url = "http://host/low" + base::IntToString(i);
414 lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
415 EXPECT_TRUE(lows_singlehost[i]->started());
418 scoped_ptr<TestRequest> second_last_singlehost(NewRequest("http://host/last",
419 net::LOWEST));
420 scoped_ptr<TestRequest> last_singlehost(NewRequest("http://host/s_last",
421 net::LOWEST));
423 EXPECT_FALSE(second_last_singlehost->started());
424 high.reset();
425 EXPECT_TRUE(second_last_singlehost->started());
426 EXPECT_FALSE(last_singlehost->started());
427 lows_singlehost.erase(lows_singlehost.begin());
428 EXPECT_TRUE(last_singlehost->started());
430 // Queue more requests from different hosts until we reach the total limit.
431 int expected_slots_left =
432 kMaxNumDelayableRequestsPerClient - kMaxNumDelayableRequestsPerHost;
433 EXPECT_GT(expected_slots_left, 0);
434 ScopedVector<TestRequest> lows_different_host;
435 for (int i = 0; i < expected_slots_left; ++i) {
436 string url = "http://host" + base::IntToString(i) + "/low";
437 lows_different_host.push_back(NewRequest(url.c_str(), net::LOWEST));
438 EXPECT_TRUE(lows_different_host[i]->started());
441 scoped_ptr<TestRequest> last_different_host(NewRequest("http://host_new/last",
442 net::LOWEST));
443 EXPECT_FALSE(last_different_host->started());
446 TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) {
447 // Dummies to enforce scheduling.
448 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
449 scoped_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
451 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
452 EXPECT_FALSE(request->started());
454 ChangeRequestPriority(request.get(), net::HIGHEST);
455 EXPECT_TRUE(request->started());
458 TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) {
459 // Dummies to enforce scheduling.
460 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
461 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
463 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
464 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
465 EXPECT_FALSE(request->started());
466 EXPECT_FALSE(idle->started());
468 ChangeRequestPriority(request.get(), net::LOWEST);
469 EXPECT_FALSE(request->started());
470 EXPECT_FALSE(idle->started());
472 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
473 ScopedVector<TestRequest> lows;
474 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
475 string url = "http://host/low" + base::IntToString(i);
476 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
479 scheduler()->OnWillInsertBody(kChildId, kRouteId);
480 high.reset();
482 EXPECT_TRUE(request->started());
483 EXPECT_FALSE(idle->started());
486 TEST_F(ResourceSchedulerTest, LowerPriority) {
487 // Dummies to enforce scheduling.
488 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
489 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
491 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
492 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
493 EXPECT_FALSE(request->started());
494 EXPECT_FALSE(idle->started());
496 ChangeRequestPriority(request.get(), net::IDLE);
497 EXPECT_FALSE(request->started());
498 EXPECT_FALSE(idle->started());
500 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
501 // 2 fewer filler requests: 1 for the "low" dummy at the start, and 1 for the
502 // one at the end, which will be tested.
503 const int kNumFillerRequests = kMaxNumDelayableRequestsPerClient - 2;
504 ScopedVector<TestRequest> lows;
505 for (int i = 0; i < kNumFillerRequests; ++i) {
506 string url = "http://host" + base::IntToString(i) + "/low";
507 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
510 scheduler()->OnWillInsertBody(kChildId, kRouteId);
511 high.reset();
513 EXPECT_FALSE(request->started());
514 EXPECT_TRUE(idle->started());
517 TEST_F(ResourceSchedulerTest, ReprioritizedRequestGoesToBackOfQueue) {
518 // Dummies to enforce scheduling.
519 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
520 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
522 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
523 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
524 EXPECT_FALSE(request->started());
525 EXPECT_FALSE(idle->started());
527 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
528 ScopedVector<TestRequest> lows;
529 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
530 string url = "http://host/low" + base::IntToString(i);
531 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
534 ChangeRequestPriority(request.get(), net::IDLE);
535 EXPECT_FALSE(request->started());
536 EXPECT_FALSE(idle->started());
538 ChangeRequestPriority(request.get(), net::LOWEST);
539 EXPECT_FALSE(request->started());
540 EXPECT_FALSE(idle->started());
542 scheduler()->OnWillInsertBody(kChildId, kRouteId);
543 EXPECT_FALSE(request->started());
544 EXPECT_FALSE(idle->started());
547 TEST_F(ResourceSchedulerTest, HigherIntraPriorityGoesToFrontOfQueue) {
548 // Dummies to enforce scheduling.
549 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
550 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
552 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
553 ScopedVector<TestRequest> lows;
554 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
555 string url = "http://host/low" + base::IntToString(i);
556 lows.push_back(NewRequest(url.c_str(), net::IDLE));
559 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
560 EXPECT_FALSE(request->started());
562 ChangeRequestPriority(request.get(), net::IDLE, 1);
563 EXPECT_FALSE(request->started());
565 scheduler()->OnWillInsertBody(kChildId, kRouteId);
566 high.reset();
567 EXPECT_TRUE(request->started());
570 TEST_F(ResourceSchedulerTest, NonHTTPSchedulesImmediately) {
571 // Dummies to enforce scheduling.
572 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
573 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
575 scoped_ptr<TestRequest> request(
576 NewRequest("chrome-extension://req", net::LOWEST));
577 EXPECT_TRUE(request->started());
580 TEST_F(ResourceSchedulerTest, ActiveLoadingSyncSchedulesImmediately) {
581 // TODO(aiolos): remove when throttling and coalescing have both landed
582 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
583 false /* should_coalesce */);
584 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
585 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
586 // Dummies to enforce scheduling.
587 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
588 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
590 scoped_ptr<TestRequest> request(
591 NewSyncRequest("http://host/req", net::LOWEST));
592 EXPECT_TRUE(request->started());
595 TEST_F(ResourceSchedulerTest, UnthrottledSyncSchedulesImmediately) {
596 // TODO(aiolos): remove when throttling and coalescing have both landed
597 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
598 false /* should_coalesce */);
599 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
600 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
601 scheduler()->GetClientStateForTesting(kBackgroundChildId,
602 kBackgroundRouteId));
603 // Dummies to enforce scheduling.
604 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
605 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
607 scoped_ptr<TestRequest> request(
608 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
609 EXPECT_TRUE(request->started());
612 TEST_F(ResourceSchedulerTest, SpdyProxySchedulesImmediately) {
613 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
614 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
616 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
617 EXPECT_FALSE(request->started());
619 scheduler()->OnReceivedSpdyProxiedHttpResponse(kChildId, kRouteId);
620 EXPECT_TRUE(request->started());
622 scoped_ptr<TestRequest> after(NewRequest("http://host/after", net::IDLE));
623 EXPECT_TRUE(after->started());
626 TEST_F(ResourceSchedulerTest, NewSpdyHostInDelayableRequests) {
627 scheduler()->OnWillInsertBody(kChildId, kRouteId);
628 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
630 scoped_ptr<TestRequest> low1_spdy(
631 NewRequest("http://spdyhost1:8080/low", net::LOWEST));
632 // Cancel a request after we learn the server supports SPDY.
633 ScopedVector<TestRequest> lows;
634 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
635 string url = "http://host" + base::IntToString(i) + "/low";
636 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
638 scoped_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
639 EXPECT_FALSE(low1->started());
640 http_server_properties_.SetSupportsSpdy(
641 net::HostPortPair("spdyhost1", 8080), true);
642 low1_spdy.reset();
643 EXPECT_TRUE(low1->started());
645 low1.reset();
646 scoped_ptr<TestRequest> low2_spdy(
647 NewRequest("http://spdyhost2:8080/low", net::IDLE));
648 // Reprioritize a request after we learn the server supports SPDY.
649 EXPECT_TRUE(low2_spdy->started());
650 http_server_properties_.SetSupportsSpdy(
651 net::HostPortPair("spdyhost2", 8080), true);
652 ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
653 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
654 EXPECT_TRUE(low2->started());
657 TEST_F(ResourceSchedulerTest, ThrottledClientCreation) {
658 // TODO(aiolos): remove when throttling and coalescing have both landed
659 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
660 false /* should_coalesce */);
661 EXPECT_TRUE(scheduler()->should_throttle());
662 scheduler()->OnClientCreated(
663 kBackgroundChildId2, kBackgroundRouteId2, false, false);
665 EXPECT_EQ(ResourceScheduler::THROTTLED,
666 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
667 kBackgroundRouteId2));
668 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
671 TEST_F(ResourceSchedulerTest, ActiveClientThrottleUpdateOnLoadingChange) {
672 // TODO(aiolos): remove when throttling and coalescing have both landed
673 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
674 false /* should_coalesce */);
675 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
676 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
677 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
678 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
679 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
680 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, false);
681 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
682 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
685 TEST_F(ResourceSchedulerTest, CoalesceBackgroundClientOnLoadCompletion) {
686 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
687 true /* should_coalesce */);
688 EXPECT_EQ(ResourceScheduler::THROTTLED,
689 scheduler()->GetClientStateForTesting(kBackgroundChildId,
690 kBackgroundRouteId));
691 scheduler()->OnLoadingStateChanged(
692 kBackgroundChildId, kBackgroundRouteId, true);
693 EXPECT_EQ(ResourceScheduler::THROTTLED,
694 scheduler()->GetClientStateForTesting(kBackgroundChildId,
695 kBackgroundRouteId));
696 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
697 EXPECT_EQ(ResourceScheduler::COALESCED,
698 scheduler()->GetClientStateForTesting(kBackgroundChildId,
699 kBackgroundRouteId));
702 TEST_F(ResourceSchedulerTest, UnthrottleBackgroundClientOnLoadingStarted) {
703 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
704 true /* should_coalesce */);
705 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
706 scheduler()->OnLoadingStateChanged(
707 kBackgroundChildId, kBackgroundRouteId, true);
708 EXPECT_EQ(ResourceScheduler::COALESCED,
709 scheduler()->GetClientStateForTesting(kBackgroundChildId,
710 kBackgroundRouteId));
712 scheduler()->OnLoadingStateChanged(
713 kBackgroundChildId, kBackgroundRouteId, false);
714 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
715 scheduler()->GetClientStateForTesting(kBackgroundChildId,
716 kBackgroundRouteId));
719 TEST_F(ResourceSchedulerTest, OneRequestPerThrottledClient) {
720 // TODO(aiolos): remove when throttling and coalescing have both landed
721 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
722 false /* should_coalesce */);
723 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
724 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
725 EXPECT_EQ(ResourceScheduler::THROTTLED,
726 scheduler()->GetClientStateForTesting(kBackgroundChildId,
727 kBackgroundRouteId));
728 scoped_ptr<TestRequest> high(
729 NewBackgroundRequest("http://host/high", net::HIGHEST));
730 scoped_ptr<TestRequest> request(
731 NewBackgroundRequest("http://host/req", net::IDLE));
733 EXPECT_TRUE(high->started());
734 EXPECT_FALSE(request->started());
737 TEST_F(ResourceSchedulerTest, UnthrottleNewlyVisibleClient) {
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::THROTTLED,
742 scheduler()->GetClientStateForTesting(kBackgroundChildId,
743 kBackgroundRouteId));
744 scoped_ptr<TestRequest> high(
745 NewBackgroundRequest("http://host/high", net::HIGHEST));
746 scoped_ptr<TestRequest> request(
747 NewBackgroundRequest("http://host/req", net::IDLE));
748 EXPECT_FALSE(request->started());
750 scheduler()->OnVisibilityChanged(
751 kBackgroundChildId, kBackgroundRouteId, true);
752 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
753 scheduler()->GetClientStateForTesting(kBackgroundChildId,
754 kBackgroundRouteId));
755 EXPECT_TRUE(request->started());
758 TEST_F(ResourceSchedulerTest, UnthrottleNewlyAudibleClient) {
759 // TODO(aiolos): remove when throttling and coalescing have both landed
760 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
761 false /* should_coalesce */);
762 EXPECT_EQ(ResourceScheduler::THROTTLED,
763 scheduler()->GetClientStateForTesting(kBackgroundChildId,
764 kBackgroundRouteId));
765 scoped_ptr<TestRequest> high(
766 NewBackgroundRequest("http://host/high", net::HIGHEST));
767 scoped_ptr<TestRequest> request(
768 NewBackgroundRequest("http://host/req", net::IDLE));
769 EXPECT_FALSE(request->started());
771 scheduler()->OnAudibilityChanged(
772 kBackgroundChildId, kBackgroundRouteId, true);
773 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
774 scheduler()->GetClientStateForTesting(kBackgroundChildId,
775 kBackgroundRouteId));
776 EXPECT_TRUE(request->started());
779 TEST_F(ResourceSchedulerTest, VisibleClientStillUnthrottledOnAudabilityChange) {
780 // TODO(aiolos): remove when throttling and coalescing have both landed
781 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
782 false /* should_coalesce */);
783 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
784 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
785 EXPECT_EQ(ResourceScheduler::THROTTLED,
786 scheduler()->GetClientStateForTesting(kBackgroundChildId,
787 kBackgroundRouteId));
789 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
790 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
791 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
792 EXPECT_EQ(ResourceScheduler::THROTTLED,
793 scheduler()->GetClientStateForTesting(kBackgroundChildId,
794 kBackgroundRouteId));
796 scheduler()->OnAudibilityChanged(kChildId, kRouteId, false);
797 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
798 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
799 EXPECT_EQ(ResourceScheduler::THROTTLED,
800 scheduler()->GetClientStateForTesting(kBackgroundChildId,
801 kBackgroundRouteId));
804 TEST_F(ResourceSchedulerTest, AudibleClientStillUnthrottledOnVisabilityChange) {
805 // TODO(aiolos): remove when throttling and coalescing have both landed
806 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
807 false /* should_coalesce */);
808 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
809 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
810 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
811 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
812 EXPECT_EQ(ResourceScheduler::THROTTLED,
813 scheduler()->GetClientStateForTesting(kBackgroundChildId,
814 kBackgroundRouteId));
816 scheduler()->OnVisibilityChanged(kChildId, kRouteId, true);
817 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
818 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
819 EXPECT_EQ(ResourceScheduler::THROTTLED,
820 scheduler()->GetClientStateForTesting(kBackgroundChildId,
821 kBackgroundRouteId));
823 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
824 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
825 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
826 EXPECT_EQ(ResourceScheduler::THROTTLED,
827 scheduler()->GetClientStateForTesting(kBackgroundChildId,
828 kBackgroundRouteId));
831 TEST_F(ResourceSchedulerTest, ThrottledClientStartsNextHighestPriorityRequest) {
832 // TODO(aiolos): remove when throttling and coalescing have both landed
833 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
834 false /* should_coalesce */);
835 scoped_ptr<TestRequest> request(
836 NewBackgroundRequest("http://host/req", net::IDLE));
837 // Lower priority request started first to test request prioritizaton.
838 scoped_ptr<TestRequest> low(
839 NewBackgroundRequest("http://host/high", net::IDLE));
840 scoped_ptr<TestRequest> high(
841 NewBackgroundRequest("http://host/high", net::HIGHEST));
843 EXPECT_FALSE(low->started());
844 EXPECT_FALSE(high->started());
846 // request->CancelRequest();
847 request->Cancel();
848 EXPECT_TRUE(high->started());
849 EXPECT_FALSE(low->started());
852 TEST_F(ResourceSchedulerTest, ThrottledSpdyProxySchedulesImmediately) {
853 // TODO(aiolos): remove when throttling and coalescing have both landed
854 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
855 false /* should_coalesce */);
856 EXPECT_EQ(ResourceScheduler::THROTTLED,
857 scheduler()->GetClientStateForTesting(kBackgroundChildId,
858 kBackgroundRouteId));
859 scoped_ptr<TestRequest> high(
860 NewBackgroundRequest("http://host/high", net::HIGHEST));
861 scoped_ptr<TestRequest> request(
862 NewBackgroundRequest("http://host/req", net::IDLE));
864 EXPECT_FALSE(request->started());
866 scheduler()->OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
867 kBackgroundRouteId);
868 EXPECT_TRUE(request->started());
870 scoped_ptr<TestRequest> after(
871 NewBackgroundRequest("http://host/after", net::IDLE));
872 EXPECT_TRUE(after->started());
875 TEST_F(ResourceSchedulerTest, CoalescedClientIssuesNoRequests) {
876 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
877 true /* should_coalesce */);
878 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
879 scheduler()->OnLoadingStateChanged(
880 kBackgroundChildId, kBackgroundRouteId, true);
881 EXPECT_EQ(ResourceScheduler::COALESCED,
882 scheduler()->GetClientStateForTesting(kBackgroundChildId,
883 kBackgroundRouteId));
884 scoped_ptr<TestRequest> high(
885 NewBackgroundRequest("http://host/high", net::HIGHEST));
886 scoped_ptr<TestRequest> request(
887 NewBackgroundRequest("http://host/req", net::IDLE));
889 EXPECT_FALSE(high->started());
890 EXPECT_FALSE(request->started());
892 scheduler()->OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
893 kBackgroundRouteId);
894 EXPECT_FALSE(high->started());
896 scoped_ptr<TestRequest> after(
897 NewBackgroundRequest("http://host/after", net::HIGHEST));
898 EXPECT_FALSE(after->started());
901 TEST_F(ResourceSchedulerTest, CoalescedSpdyProxyWaits) {
902 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
903 true /* should_coalesce */);
904 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
905 scheduler()->OnLoadingStateChanged(
906 kBackgroundChildId, kBackgroundRouteId, true);
907 EXPECT_EQ(ResourceScheduler::COALESCED,
908 scheduler()->GetClientStateForTesting(kBackgroundChildId,
909 kBackgroundRouteId));
910 scoped_ptr<TestRequest> high(
911 NewBackgroundRequest("http://host/high", net::HIGHEST));
912 scoped_ptr<TestRequest> request(
913 NewBackgroundRequest("http://host/req", net::IDLE));
915 EXPECT_FALSE(request->started());
917 scheduler()->OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
918 kBackgroundRouteId);
919 EXPECT_FALSE(request->started());
921 scoped_ptr<TestRequest> after(
922 NewBackgroundRequest("http://host/after", net::IDLE));
923 EXPECT_FALSE(after->started());
926 TEST_F(ResourceSchedulerTest, ThrottledNonHTTPSchedulesImmediately) {
927 // TODO(aiolos): remove when throttling and coalescing have both landed
928 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
929 false /* should_coalesce */);
930 // Dummies to enforce scheduling.
931 scoped_ptr<TestRequest> high(
932 NewBackgroundRequest("http://host/high", net::HIGHEST));
933 scoped_ptr<TestRequest> low(
934 NewBackgroundRequest("http://host/low", net::LOWEST));
936 scoped_ptr<TestRequest> request(
937 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
938 EXPECT_TRUE(request->started());
939 EXPECT_FALSE(low->started());
942 TEST_F(ResourceSchedulerTest, CoalescedNonHTTPSchedulesImmediately) {
943 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
944 true /* should_coalesce */);
945 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
946 scheduler()->OnLoadingStateChanged(
947 kBackgroundChildId, kBackgroundRouteId, true);
948 EXPECT_EQ(ResourceScheduler::COALESCED,
949 scheduler()->GetClientStateForTesting(kBackgroundChildId,
950 kBackgroundRouteId));
951 // Dummies to enforce scheduling.
952 scoped_ptr<TestRequest> high(
953 NewBackgroundRequest("http://host/high", net::HIGHEST));
954 scoped_ptr<TestRequest> low(
955 NewBackgroundRequest("http://host/low", net::LOWEST));
957 scoped_ptr<TestRequest> request(
958 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
959 EXPECT_TRUE(request->started());
960 EXPECT_FALSE(low->started());
963 TEST_F(ResourceSchedulerTest, ThrottledSyncSchedulesImmediately) {
964 // TODO(aiolos): remove when throttling and coalescing have both landed
965 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
966 false /* should_coalesce */);
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 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
975 EXPECT_TRUE(request->started());
976 EXPECT_FALSE(low->started());
979 TEST_F(ResourceSchedulerTest, CoalescedSyncSchedulesImmediately) {
980 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
981 true /* should_coalesce */);
982 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
983 scheduler()->OnLoadingStateChanged(
984 kBackgroundChildId, kBackgroundRouteId, true);
985 EXPECT_EQ(ResourceScheduler::COALESCED,
986 scheduler()->GetClientStateForTesting(kBackgroundChildId,
987 kBackgroundRouteId));
988 // Dummies to enforce scheduling.
989 scoped_ptr<TestRequest> high(
990 NewBackgroundRequest("http://host/high", net::HIGHEST));
991 scoped_ptr<TestRequest> low(
992 NewBackgroundRequest("http://host/low", net::LOWEST));
994 scoped_ptr<TestRequest> request(
995 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
996 EXPECT_TRUE(request->started());
997 EXPECT_FALSE(low->started());
998 EXPECT_FALSE(high->started());
1001 TEST_F(ResourceSchedulerTest, AllBackgroundClientsUnthrottle) {
1002 // TODO(aiolos): remove when throttling and coalescing have both landed
1003 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1004 false /* should_coalesce */);
1005 EXPECT_EQ(ResourceScheduler::THROTTLED,
1006 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1007 kBackgroundRouteId));
1008 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1009 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1010 EXPECT_FALSE(scheduler()->active_clients_loaded());
1012 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1013 EXPECT_TRUE(scheduler()->active_clients_loaded());
1014 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1015 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1016 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1017 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1018 kBackgroundRouteId));
1020 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1021 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1022 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1023 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1024 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1025 kBackgroundRouteId));
1027 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, false);
1028 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1029 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1030 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1031 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1032 kBackgroundRouteId));
1034 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1035 scheduler()->OnLoadingStateChanged(
1036 kBackgroundChildId, kBackgroundRouteId, true);
1037 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1038 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1039 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1040 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1041 kBackgroundRouteId));
1044 TEST_F(ResourceSchedulerTest,
1045 UnloadedClientVisibilityChangedCorrectlyUnthrottles) {
1046 // TODO(aiolos): remove when throttling and coalescing have both landed
1047 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1048 false /* should_coalesce */);
1049 scheduler()->OnClientCreated(kChildId2, kRouteId2, false, false);
1050 scheduler()->OnClientCreated(
1051 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1052 scheduler()->OnLoadingStateChanged(kChildId2, kRouteId2, true);
1053 scheduler()->OnLoadingStateChanged(
1054 kBackgroundChildId2, kBackgroundRouteId2, true);
1056 // 1 visible, 3 hidden
1057 EXPECT_FALSE(scheduler()->active_clients_loaded());
1058 EXPECT_EQ(ResourceScheduler::THROTTLED,
1059 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1060 kBackgroundRouteId));
1061 EXPECT_EQ(ResourceScheduler::THROTTLED,
1062 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1063 kBackgroundRouteId2));
1064 EXPECT_EQ(ResourceScheduler::THROTTLED,
1065 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1066 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1067 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1069 // 2 visible, 2 hidden
1070 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, true);
1071 EXPECT_FALSE(scheduler()->active_clients_loaded());
1072 EXPECT_EQ(ResourceScheduler::THROTTLED,
1073 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1074 kBackgroundRouteId));
1075 EXPECT_EQ(ResourceScheduler::THROTTLED,
1076 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1077 kBackgroundRouteId2));
1078 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1079 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1080 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1081 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1083 // 1 visible, 3 hidden
1084 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, false);
1085 EXPECT_FALSE(scheduler()->active_clients_loaded());
1086 EXPECT_EQ(ResourceScheduler::THROTTLED,
1087 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1088 kBackgroundRouteId));
1089 EXPECT_EQ(ResourceScheduler::THROTTLED,
1090 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1091 kBackgroundRouteId2));
1092 EXPECT_EQ(ResourceScheduler::THROTTLED,
1093 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1094 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1095 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1097 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1098 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1101 TEST_F(ResourceSchedulerTest,
1102 UnloadedClientAudibilityChangedCorrectlyUnthrottles) {
1103 // TODO(aiolos): remove when throttling and coalescing have both landed
1104 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1105 false /* should_coalesce */);
1106 scheduler()->OnClientCreated(kChildId2, kRouteId2, false, false);
1107 scheduler()->OnClientCreated(
1108 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1109 scheduler()->OnLoadingStateChanged(
1110 kBackgroundChildId2, kBackgroundRouteId2, true);
1111 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1112 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
1114 // 1 audible, 3 hidden
1115 EXPECT_FALSE(scheduler()->active_clients_loaded());
1116 EXPECT_EQ(ResourceScheduler::THROTTLED,
1117 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1118 kBackgroundRouteId));
1119 EXPECT_EQ(ResourceScheduler::THROTTLED,
1120 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1121 kBackgroundRouteId2));
1122 EXPECT_EQ(ResourceScheduler::THROTTLED,
1123 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1124 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1125 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1127 // 2 audible, 2 hidden
1128 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, true);
1129 EXPECT_FALSE(scheduler()->active_clients_loaded());
1130 EXPECT_EQ(ResourceScheduler::THROTTLED,
1131 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1132 kBackgroundRouteId));
1133 EXPECT_EQ(ResourceScheduler::THROTTLED,
1134 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1135 kBackgroundRouteId2));
1136 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1137 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1138 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1139 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1141 // 1 audible, 3 hidden
1142 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, false);
1143 EXPECT_FALSE(scheduler()->active_clients_loaded());
1144 EXPECT_EQ(ResourceScheduler::THROTTLED,
1145 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1146 kBackgroundRouteId));
1147 EXPECT_EQ(ResourceScheduler::THROTTLED,
1148 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1149 kBackgroundRouteId2));
1150 EXPECT_EQ(ResourceScheduler::THROTTLED,
1151 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1152 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1153 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1155 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1156 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1159 TEST_F(ResourceSchedulerTest,
1160 LoadedClientVisibilityChangedCorrectlyUnthrottles) {
1161 // TODO(aiolos): remove when throttling and coalescing have both landed
1162 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1163 false /* should_coalesce */);
1164 scheduler()->OnClientCreated(kChildId2, kRouteId2, false, false);
1165 scheduler()->OnClientCreated(
1166 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1167 scheduler()->OnLoadingStateChanged(kChildId2, kRouteId2, true);
1168 scheduler()->OnLoadingStateChanged(
1169 kBackgroundChildId2, kBackgroundRouteId2, true);
1170 // 1 visible, 3 hidden
1171 EXPECT_FALSE(scheduler()->active_clients_loaded());
1172 EXPECT_EQ(ResourceScheduler::THROTTLED,
1173 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1174 kBackgroundRouteId));
1175 EXPECT_EQ(ResourceScheduler::THROTTLED,
1176 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1177 kBackgroundRouteId2));
1178 EXPECT_EQ(ResourceScheduler::THROTTLED,
1179 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1180 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1181 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1183 // 2 visible, 2 hidden
1184 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, true);
1185 EXPECT_FALSE(scheduler()->active_clients_loaded());
1186 EXPECT_EQ(ResourceScheduler::THROTTLED,
1187 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1188 kBackgroundRouteId));
1189 EXPECT_EQ(ResourceScheduler::THROTTLED,
1190 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1191 kBackgroundRouteId2));
1192 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1193 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1194 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1195 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1197 // 1 visible, 3 hidden
1198 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, false);
1199 EXPECT_FALSE(scheduler()->active_clients_loaded());
1200 EXPECT_EQ(ResourceScheduler::THROTTLED,
1201 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1202 kBackgroundRouteId));
1203 EXPECT_EQ(ResourceScheduler::THROTTLED,
1204 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1205 kBackgroundRouteId2));
1206 EXPECT_EQ(ResourceScheduler::THROTTLED,
1207 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1208 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1209 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1211 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1212 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1215 TEST_F(ResourceSchedulerTest,
1216 LoadedClientAudibilityChangedCorrectlyUnthrottles) {
1217 // TODO(aiolos): remove when throttling and coalescing have both landed
1218 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1219 false /* should_coalesce */);
1220 scheduler()->OnClientCreated(kChildId2, kRouteId2, false, false);
1221 scheduler()->OnClientCreated(
1222 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1223 scheduler()->OnLoadingStateChanged(kChildId2, kRouteId2, true);
1224 scheduler()->OnLoadingStateChanged(
1225 kBackgroundChildId2, kBackgroundRouteId2, true);
1226 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1227 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
1228 // 1 audible, 3 hidden
1229 EXPECT_FALSE(scheduler()->active_clients_loaded());
1230 EXPECT_EQ(ResourceScheduler::THROTTLED,
1231 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1232 kBackgroundRouteId));
1233 EXPECT_EQ(ResourceScheduler::THROTTLED,
1234 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1235 kBackgroundRouteId2));
1236 EXPECT_EQ(ResourceScheduler::THROTTLED,
1237 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1238 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1239 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1241 // 2 audible, 2 hidden
1242 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, true);
1243 EXPECT_FALSE(scheduler()->active_clients_loaded());
1244 EXPECT_EQ(ResourceScheduler::THROTTLED,
1245 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1246 kBackgroundRouteId));
1247 EXPECT_EQ(ResourceScheduler::THROTTLED,
1248 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1249 kBackgroundRouteId2));
1250 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1251 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1252 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1253 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1255 // 1 audible, 3 hidden
1256 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, false);
1257 EXPECT_FALSE(scheduler()->active_clients_loaded());
1258 EXPECT_EQ(ResourceScheduler::THROTTLED,
1259 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1260 kBackgroundRouteId));
1261 EXPECT_EQ(ResourceScheduler::THROTTLED,
1262 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1263 kBackgroundRouteId2));
1264 EXPECT_EQ(ResourceScheduler::THROTTLED,
1265 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1266 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1267 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1269 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1270 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1273 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesHiddenCorrectlyUnthrottles) {
1274 // TODO(aiolos): remove when throttling and coalescing have both landed
1275 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1276 false /* should_coalesce */);
1277 scheduler()->OnClientCreated(kChildId2, kRouteId2, true, false);
1278 scheduler()->OnClientCreated(
1279 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1280 scheduler()->OnLoadingStateChanged(
1281 kBackgroundChildId2, kBackgroundRouteId2, true);
1283 // 2 visible, 2 hidden
1284 EXPECT_FALSE(scheduler()->active_clients_loaded());
1285 EXPECT_EQ(ResourceScheduler::THROTTLED,
1286 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1287 kBackgroundRouteId));
1288 EXPECT_EQ(ResourceScheduler::THROTTLED,
1289 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1290 kBackgroundRouteId2));
1291 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1292 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1293 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1294 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1296 // 1 visible, 3 hidden
1297 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, false);
1298 EXPECT_FALSE(scheduler()->active_clients_loaded());
1299 EXPECT_EQ(ResourceScheduler::THROTTLED,
1300 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1301 kBackgroundRouteId));
1302 EXPECT_EQ(ResourceScheduler::THROTTLED,
1303 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1304 kBackgroundRouteId2));
1305 EXPECT_EQ(ResourceScheduler::THROTTLED,
1306 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1307 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1308 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1310 // 0 visible, 4 hidden
1311 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1312 EXPECT_TRUE(scheduler()->active_clients_loaded());
1313 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1314 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1315 kBackgroundRouteId));
1316 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1317 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1318 kBackgroundRouteId2));
1319 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1320 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1321 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1322 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1324 // 1 visible, 3 hidden
1325 scheduler()->OnVisibilityChanged(kChildId, kRouteId, true);
1326 EXPECT_FALSE(scheduler()->active_clients_loaded());
1327 EXPECT_EQ(ResourceScheduler::THROTTLED,
1328 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1329 kBackgroundRouteId));
1330 EXPECT_EQ(ResourceScheduler::THROTTLED,
1331 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1332 kBackgroundRouteId2));
1333 EXPECT_EQ(ResourceScheduler::THROTTLED,
1334 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1335 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1336 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1338 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1339 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1342 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesSilentCorrectlyUnthrottles) {
1343 // TODO(aiolos): remove when throttling and coalescing have both landed
1344 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1345 false /* should_coalesce */);
1346 scheduler()->OnClientCreated(kChildId2, kRouteId2, false, true);
1347 scheduler()->OnClientCreated(
1348 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1349 scheduler()->OnLoadingStateChanged(
1350 kBackgroundChildId2, kBackgroundRouteId2, true);
1351 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
1352 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1353 // 2 audible, 2 hidden
1354 EXPECT_FALSE(scheduler()->active_clients_loaded());
1355 EXPECT_EQ(ResourceScheduler::THROTTLED,
1356 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1357 kBackgroundRouteId));
1358 EXPECT_EQ(ResourceScheduler::THROTTLED,
1359 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1360 kBackgroundRouteId2));
1361 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1362 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1363 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1364 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1366 // 1 audible, 3 hidden
1367 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, false);
1368 EXPECT_FALSE(scheduler()->active_clients_loaded());
1369 EXPECT_EQ(ResourceScheduler::THROTTLED,
1370 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1371 kBackgroundRouteId));
1372 EXPECT_EQ(ResourceScheduler::THROTTLED,
1373 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1374 kBackgroundRouteId2));
1375 EXPECT_EQ(ResourceScheduler::THROTTLED,
1376 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1377 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1378 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1380 // 0 audible, 4 hidden
1381 scheduler()->OnAudibilityChanged(kChildId, kRouteId, false);
1382 EXPECT_TRUE(scheduler()->active_clients_loaded());
1383 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1384 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1385 kBackgroundRouteId));
1386 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1387 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1388 kBackgroundRouteId2));
1389 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1390 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1391 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1392 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1394 // 1 audible, 3 hidden
1395 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
1396 EXPECT_FALSE(scheduler()->active_clients_loaded());
1397 EXPECT_EQ(ResourceScheduler::THROTTLED,
1398 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1399 kBackgroundRouteId));
1400 EXPECT_EQ(ResourceScheduler::THROTTLED,
1401 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1402 kBackgroundRouteId2));
1403 EXPECT_EQ(ResourceScheduler::THROTTLED,
1404 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1405 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1406 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1408 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1409 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1412 TEST_F(ResourceSchedulerTest, LoadedClientBecomesHiddenCorrectlyThrottles) {
1413 // TODO(aiolos): remove when throttling and coalescing have both landed
1414 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1415 false /* should_coalesce */);
1416 scheduler()->OnClientCreated(kChildId2, kRouteId2, true, false);
1417 scheduler()->OnClientCreated(
1418 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1419 scheduler()->OnLoadingStateChanged(
1420 kBackgroundChildId2, kBackgroundRouteId2, true);
1421 scheduler()->OnLoadingStateChanged(kChildId2, kRouteId2, true);
1422 // 2 visible, 2 hidden
1423 EXPECT_FALSE(scheduler()->active_clients_loaded());
1424 EXPECT_EQ(ResourceScheduler::THROTTLED,
1425 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1426 kBackgroundRouteId));
1427 EXPECT_EQ(ResourceScheduler::THROTTLED,
1428 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1429 kBackgroundRouteId2));
1430 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1431 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1432 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1433 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1435 // 1 visible, 3 hidden
1436 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, false);
1437 EXPECT_FALSE(scheduler()->active_clients_loaded());
1438 EXPECT_EQ(ResourceScheduler::THROTTLED,
1439 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1440 kBackgroundRouteId));
1441 EXPECT_EQ(ResourceScheduler::THROTTLED,
1442 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1443 kBackgroundRouteId2));
1444 EXPECT_EQ(ResourceScheduler::THROTTLED,
1445 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1446 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1447 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1449 // 0 visible, 4 hidden
1450 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1451 EXPECT_TRUE(scheduler()->active_clients_loaded());
1452 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1453 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1454 kBackgroundRouteId));
1455 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1456 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1457 kBackgroundRouteId2));
1458 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1459 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1460 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1461 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1463 // 1 visible, 3 hidden
1464 scheduler()->OnVisibilityChanged(kChildId2, kRouteId2, true);
1465 EXPECT_TRUE(scheduler()->active_clients_loaded());
1466 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1467 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1468 kBackgroundRouteId));
1469 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1470 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1471 kBackgroundRouteId2));
1472 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1473 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1474 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1475 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1477 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1478 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1481 TEST_F(ResourceSchedulerTest, LoadedClientBecomesSilentCorrectlyThrottles) {
1482 // TODO(aiolos): remove when throttling and coalescing have both landed
1483 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1484 false /* should_coalesce */);
1485 scheduler()->OnClientCreated(kChildId2, kRouteId2, false, true);
1486 scheduler()->OnClientCreated(
1487 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1488 scheduler()->OnLoadingStateChanged(
1489 kBackgroundChildId2, kBackgroundRouteId2, true);
1490 scheduler()->OnLoadingStateChanged(kChildId2, kRouteId2, true);
1491 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1492 scheduler()->OnAudibilityChanged(kChildId, kRouteId, true);
1493 // 2 audible, 2 hidden
1494 EXPECT_FALSE(scheduler()->active_clients_loaded());
1495 EXPECT_EQ(ResourceScheduler::THROTTLED,
1496 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1497 kBackgroundRouteId));
1498 EXPECT_EQ(ResourceScheduler::THROTTLED,
1499 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1500 kBackgroundRouteId2));
1501 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1502 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1503 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1504 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1506 // 1 audible, 3 hidden
1507 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, false);
1508 EXPECT_FALSE(scheduler()->active_clients_loaded());
1509 EXPECT_EQ(ResourceScheduler::THROTTLED,
1510 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1511 kBackgroundRouteId));
1512 EXPECT_EQ(ResourceScheduler::THROTTLED,
1513 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1514 kBackgroundRouteId2));
1515 EXPECT_EQ(ResourceScheduler::THROTTLED,
1516 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1517 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1518 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1520 // 0 audible, 4 hidden
1521 scheduler()->OnAudibilityChanged(kChildId, kRouteId, false);
1522 EXPECT_TRUE(scheduler()->active_clients_loaded());
1523 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1524 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1525 kBackgroundRouteId));
1526 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1527 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1528 kBackgroundRouteId2));
1529 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1530 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1531 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1532 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1534 // 1 audible, 3 hidden
1535 scheduler()->OnAudibilityChanged(kChildId2, kRouteId2, true);
1536 EXPECT_TRUE(scheduler()->active_clients_loaded());
1537 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1538 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1539 kBackgroundRouteId));
1540 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1541 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1542 kBackgroundRouteId2));
1543 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1544 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1545 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1546 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1548 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1549 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1552 TEST_F(ResourceSchedulerTest, HiddenLoadedChangesCorrectlyStayThrottled) {
1553 // TODO(aiolos): remove when throttling and coalescing have both landed
1554 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1555 false /* should_coalesce */);
1556 scheduler()->OnClientCreated(kChildId2, kRouteId2, true, false);
1557 scheduler()->OnClientCreated(
1558 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1560 // 1 visible and 2 hidden loading, 1 visible loaded
1561 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1562 EXPECT_FALSE(scheduler()->active_clients_loaded());
1563 EXPECT_EQ(ResourceScheduler::THROTTLED,
1564 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1565 kBackgroundRouteId));
1566 EXPECT_EQ(ResourceScheduler::THROTTLED,
1567 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1568 kBackgroundRouteId2));
1569 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1570 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1571 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1572 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1574 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1575 scheduler()->OnLoadingStateChanged(
1576 kBackgroundChildId2, kBackgroundRouteId2, true);
1577 EXPECT_FALSE(scheduler()->active_clients_loaded());
1578 EXPECT_EQ(ResourceScheduler::THROTTLED,
1579 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1580 kBackgroundRouteId));
1581 EXPECT_EQ(ResourceScheduler::THROTTLED,
1582 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1583 kBackgroundRouteId2));
1584 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1585 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1586 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1587 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1589 // 1 visible loading, 1 visible and 2 hidden loaded
1590 scheduler()->OnLoadingStateChanged(
1591 kBackgroundChildId, kBackgroundRouteId, true);
1592 EXPECT_FALSE(scheduler()->active_clients_loaded());
1593 EXPECT_EQ(ResourceScheduler::THROTTLED,
1594 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1595 kBackgroundRouteId));
1596 EXPECT_EQ(ResourceScheduler::THROTTLED,
1597 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1598 kBackgroundRouteId2));
1599 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1600 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1601 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1602 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1604 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1605 scheduler()->OnLoadingStateChanged(
1606 kBackgroundChildId2, kBackgroundRouteId2, true);
1607 EXPECT_FALSE(scheduler()->active_clients_loaded());
1608 EXPECT_EQ(ResourceScheduler::THROTTLED,
1609 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1610 kBackgroundRouteId));
1611 EXPECT_EQ(ResourceScheduler::THROTTLED,
1612 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1613 kBackgroundRouteId2));
1614 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1615 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1616 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1617 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1619 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1620 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1623 TEST_F(ResourceSchedulerTest, PartialVisibleClientLoadedDoesNotUnthrottle) {
1624 // TODO(aiolos): remove when throttling and coalescing have both landed
1625 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1626 false /* should_coalesce */);
1627 scheduler()->OnClientCreated(kChildId2, kRouteId2, true, false);
1628 scheduler()->OnClientCreated(
1629 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1631 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1632 scheduler()->OnLoadingStateChanged(
1633 kBackgroundChildId2, kBackgroundRouteId2, true);
1634 EXPECT_FALSE(scheduler()->active_clients_loaded());
1635 EXPECT_EQ(ResourceScheduler::THROTTLED,
1636 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1637 kBackgroundRouteId));
1638 EXPECT_EQ(ResourceScheduler::THROTTLED,
1639 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1640 kBackgroundRouteId2));
1641 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1642 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1643 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1644 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1646 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1647 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1648 EXPECT_FALSE(scheduler()->active_clients_loaded());
1649 EXPECT_EQ(ResourceScheduler::THROTTLED,
1650 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1651 kBackgroundRouteId));
1652 EXPECT_EQ(ResourceScheduler::THROTTLED,
1653 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1654 kBackgroundRouteId2));
1655 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1656 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1657 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1658 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1660 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1661 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, false);
1662 EXPECT_FALSE(scheduler()->active_clients_loaded());
1663 EXPECT_EQ(ResourceScheduler::THROTTLED,
1664 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1665 kBackgroundRouteId));
1666 EXPECT_EQ(ResourceScheduler::THROTTLED,
1667 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1668 kBackgroundRouteId2));
1669 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1670 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1671 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1672 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1674 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1675 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1678 TEST_F(ResourceSchedulerTest, FullVisibleLoadedCorrectlyUnthrottle) {
1679 // TODO(aiolos): remove when throttling and coalescing have both landed
1680 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1681 false /* should_coalesce */);
1682 scheduler()->OnClientCreated(kChildId2, kRouteId2, true, false);
1683 scheduler()->OnClientCreated(
1684 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1686 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1687 scheduler()->OnLoadingStateChanged(
1688 kBackgroundChildId2, kBackgroundRouteId2, true);
1689 scheduler()->OnLoadingStateChanged(kChildId2, kRouteId2, true);
1690 EXPECT_FALSE(scheduler()->active_clients_loaded());
1691 EXPECT_EQ(ResourceScheduler::THROTTLED,
1692 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1693 kBackgroundRouteId));
1694 EXPECT_EQ(ResourceScheduler::THROTTLED,
1695 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1696 kBackgroundRouteId2));
1697 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1698 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1699 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1700 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1702 scoped_ptr<TestRequest> high(
1703 NewBackgroundRequest("http://host/high", net::HIGHEST));
1704 scoped_ptr<TestRequest> low(
1705 NewBackgroundRequest("http://host/low", net::LOWEST));
1707 EXPECT_TRUE(high->started());
1708 EXPECT_FALSE(low->started());
1710 // 2 visible loaded, 1 hidden loading, 1 hidden loaded
1711 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1712 EXPECT_TRUE(scheduler()->active_clients_loaded());
1713 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1714 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1715 kBackgroundRouteId));
1716 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1717 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1718 kBackgroundRouteId2));
1719 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1720 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1721 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1722 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1723 // kBackgroundClientId unthrottling should unthrottle it's request.
1724 EXPECT_TRUE(low->started());
1726 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1727 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, false);
1728 EXPECT_FALSE(scheduler()->active_clients_loaded());
1729 EXPECT_EQ(ResourceScheduler::THROTTLED,
1730 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1731 kBackgroundRouteId));
1732 EXPECT_EQ(ResourceScheduler::THROTTLED,
1733 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1734 kBackgroundRouteId2));
1735 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1736 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1737 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1738 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1740 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1741 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1744 TEST_F(ResourceSchedulerTest,
1745 ActiveAndLoadingClientDeletedCorrectlyUnthrottle) {
1746 // TODO(aiolos): remove when throttling and coalescing have both landed
1747 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1748 false /* should_coalesce */);
1749 scheduler()->OnClientCreated(kChildId2, kRouteId2, true, false);
1750 scheduler()->OnClientCreated(
1751 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1753 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1754 scheduler()->OnLoadingStateChanged(
1755 kBackgroundChildId2, kBackgroundRouteId2, true);
1756 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1757 EXPECT_FALSE(scheduler()->active_clients_loaded());
1758 EXPECT_EQ(ResourceScheduler::THROTTLED,
1759 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1760 kBackgroundRouteId));
1761 EXPECT_EQ(ResourceScheduler::THROTTLED,
1762 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1763 kBackgroundRouteId2));
1764 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1765 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1766 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1767 scheduler()->GetClientStateForTesting(kChildId2, kRouteId2));
1769 // 1 visible loaded, 1 hidden loading, 1 hidden loaded
1770 scheduler()->OnClientDeleted(kChildId2, kRouteId2);
1771 EXPECT_TRUE(scheduler()->active_clients_loaded());
1772 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1773 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1774 kBackgroundRouteId));
1775 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1776 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1777 kBackgroundRouteId2));
1778 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1779 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1781 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1782 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, false);
1783 EXPECT_FALSE(scheduler()->active_clients_loaded());
1784 EXPECT_EQ(ResourceScheduler::THROTTLED,
1785 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1786 kBackgroundRouteId));
1787 EXPECT_EQ(ResourceScheduler::THROTTLED,
1788 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1789 kBackgroundRouteId2));
1790 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1791 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1793 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1796 TEST_F(ResourceSchedulerTest, CoalescedClientCreationStartsTimer) {
1797 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1798 true /* should_coalesce */);
1799 EXPECT_FALSE(mock_timer_->IsRunning());
1800 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1801 EXPECT_FALSE(mock_timer_->IsRunning());
1802 scheduler()->OnLoadingStateChanged(
1803 kBackgroundChildId, kBackgroundRouteId, true);
1804 EXPECT_EQ(ResourceScheduler::COALESCED,
1805 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1806 kBackgroundRouteId));
1807 EXPECT_TRUE(mock_timer_->IsRunning());
1810 TEST_F(ResourceSchedulerTest, ActiveLoadingClientLoadedAndHiddenStartsTimer) {
1811 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1812 true /* should_coalesce */);
1813 EXPECT_FALSE(mock_timer_->IsRunning());
1814 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1815 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1816 EXPECT_EQ(ResourceScheduler::THROTTLED,
1817 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1818 kBackgroundRouteId));
1819 EXPECT_FALSE(mock_timer_->IsRunning());
1821 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1822 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1823 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1824 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1825 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1826 kBackgroundRouteId));
1827 EXPECT_FALSE(mock_timer_->IsRunning());
1829 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1830 EXPECT_EQ(ResourceScheduler::COALESCED,
1831 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1832 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1833 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1834 kBackgroundRouteId));
1835 EXPECT_TRUE(mock_timer_->IsRunning());
1838 TEST_F(ResourceSchedulerTest, ActiveLoadingClientHiddenAndLoadedStartsTimer) {
1839 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1840 true /* should_coalesce */);
1841 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1842 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1843 EXPECT_EQ(ResourceScheduler::THROTTLED,
1844 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1845 kBackgroundRouteId));
1846 EXPECT_FALSE(mock_timer_->IsRunning());
1848 scheduler()->OnVisibilityChanged(kChildId, kRouteId, false);
1849 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1850 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1851 EXPECT_FALSE(mock_timer_->IsRunning());
1853 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1854 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1855 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1856 kBackgroundRouteId));
1857 EXPECT_EQ(ResourceScheduler::COALESCED,
1858 scheduler()->GetClientStateForTesting(kChildId, kRouteId));
1859 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1860 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1861 kBackgroundRouteId));
1862 EXPECT_TRUE(mock_timer_->IsRunning());
1865 TEST_F(ResourceSchedulerTest, CoalescedClientBecomesAudibleStopsTimer) {
1866 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1867 true /* should_coalesce */);
1868 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1869 EXPECT_FALSE(mock_timer_->IsRunning());
1870 scheduler()->OnLoadingStateChanged(
1871 kBackgroundChildId, kBackgroundRouteId, true);
1872 EXPECT_EQ(ResourceScheduler::COALESCED,
1873 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1874 kBackgroundRouteId));
1875 EXPECT_TRUE(mock_timer_->IsRunning());
1877 scheduler()->OnAudibilityChanged(
1878 kBackgroundChildId, kBackgroundRouteId, true);
1879 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1880 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1881 kBackgroundRouteId));
1882 EXPECT_FALSE(mock_timer_->IsRunning());
1885 TEST_F(ResourceSchedulerTest, LastCoalescedClientDeletionStopsTimer) {
1886 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1887 true /* should_coalesce */);
1888 scheduler()->OnClientCreated(
1889 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1890 EXPECT_FALSE(mock_timer_->IsRunning());
1891 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1892 EXPECT_FALSE(mock_timer_->IsRunning());
1893 scheduler()->OnLoadingStateChanged(
1894 kBackgroundChildId, kBackgroundRouteId, true);
1895 EXPECT_EQ(ResourceScheduler::COALESCED,
1896 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1897 kBackgroundRouteId));
1898 scheduler()->OnLoadingStateChanged(
1899 kBackgroundChildId2, kBackgroundRouteId2, true);
1900 EXPECT_EQ(ResourceScheduler::COALESCED,
1901 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1902 kBackgroundRouteId2));
1903 EXPECT_TRUE(mock_timer_->IsRunning());
1905 scheduler()->OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
1906 EXPECT_TRUE(mock_timer_->IsRunning());
1908 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1909 EXPECT_FALSE(mock_timer_->IsRunning());
1911 // To avoid errors on test tear down.
1912 scheduler()->OnClientCreated(
1913 kBackgroundChildId, kBackgroundRouteId, false, false);
1916 TEST_F(ResourceSchedulerTest, LastCoalescedClientStartsLoadingStopsTimer) {
1917 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1918 true /* should_coalesce */);
1919 scheduler()->OnClientCreated(
1920 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1921 EXPECT_FALSE(mock_timer_->IsRunning());
1922 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1923 EXPECT_FALSE(mock_timer_->IsRunning());
1924 scheduler()->OnLoadingStateChanged(
1925 kBackgroundChildId, kBackgroundRouteId, true);
1926 EXPECT_EQ(ResourceScheduler::COALESCED,
1927 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1928 kBackgroundRouteId));
1929 scheduler()->OnLoadingStateChanged(
1930 kBackgroundChildId2, kBackgroundRouteId2, true);
1931 EXPECT_EQ(ResourceScheduler::COALESCED,
1932 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1933 kBackgroundRouteId2));
1934 EXPECT_TRUE(mock_timer_->IsRunning());
1936 scheduler()->OnLoadingStateChanged(
1937 kBackgroundChildId, kBackgroundRouteId, false);
1938 EXPECT_TRUE(mock_timer_->IsRunning());
1940 scheduler()->OnLoadingStateChanged(
1941 kBackgroundChildId2, kBackgroundRouteId2, false);
1942 EXPECT_FALSE(mock_timer_->IsRunning());
1944 // This is needed to avoid errors on test tear down.
1945 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1948 TEST_F(ResourceSchedulerTest, LastCoalescedClientBecomesVisibleStopsTimer) {
1949 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1950 true /* should_coalesce */);
1951 scheduler()->OnClientCreated(
1952 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1953 EXPECT_FALSE(mock_timer_->IsRunning());
1954 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1955 EXPECT_FALSE(mock_timer_->IsRunning());
1956 scheduler()->OnLoadingStateChanged(
1957 kBackgroundChildId, kBackgroundRouteId, true);
1958 EXPECT_EQ(ResourceScheduler::COALESCED,
1959 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1960 kBackgroundRouteId));
1961 scheduler()->OnLoadingStateChanged(
1962 kBackgroundChildId2, kBackgroundRouteId2, true);
1963 EXPECT_EQ(ResourceScheduler::COALESCED,
1964 scheduler()->GetClientStateForTesting(kBackgroundChildId2,
1965 kBackgroundRouteId2));
1966 EXPECT_TRUE(mock_timer_->IsRunning());
1968 scheduler()->OnVisibilityChanged(
1969 kBackgroundChildId, kBackgroundRouteId, true);
1970 EXPECT_TRUE(mock_timer_->IsRunning());
1972 scheduler()->OnVisibilityChanged(
1973 kBackgroundChildId2, kBackgroundRouteId2, true);
1974 EXPECT_FALSE(mock_timer_->IsRunning());
1976 // To avoid errors on test tear down.
1977 scheduler()->OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1980 TEST_F(ResourceSchedulerTest,
1981 CoalescedClientBecomesLoadingAndVisibleStopsTimer) {
1982 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
1983 true /* should_coalesce */);
1984 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
1985 EXPECT_FALSE(mock_timer_->IsRunning());
1986 scheduler()->OnLoadingStateChanged(
1987 kBackgroundChildId, kBackgroundRouteId, true);
1988 EXPECT_EQ(ResourceScheduler::COALESCED,
1989 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1990 kBackgroundRouteId));
1991 EXPECT_TRUE(mock_timer_->IsRunning());
1993 scheduler()->OnLoadingStateChanged(
1994 kBackgroundChildId, kBackgroundRouteId, false);
1995 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1996 scheduler()->GetClientStateForTesting(kBackgroundChildId,
1997 kBackgroundRouteId));
1998 EXPECT_FALSE(mock_timer_->IsRunning());
2000 scheduler()->OnVisibilityChanged(
2001 kBackgroundChildId, kBackgroundRouteId, true);
2002 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
2003 scheduler()->GetClientStateForTesting(kBackgroundChildId,
2004 kBackgroundRouteId));
2005 EXPECT_FALSE(mock_timer_->IsRunning());
2008 TEST_F(ResourceSchedulerTest, CoalescedRequestsIssueOnTimer) {
2009 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2010 true /* should_coalesce */);
2011 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
2012 scheduler()->OnLoadingStateChanged(
2013 kBackgroundChildId, kBackgroundRouteId, true);
2014 EXPECT_EQ(ResourceScheduler::COALESCED,
2015 scheduler()->GetClientStateForTesting(kBackgroundChildId,
2016 kBackgroundRouteId));
2017 EXPECT_TRUE(scheduler()->active_clients_loaded());
2019 scoped_ptr<TestRequest> high(
2020 NewBackgroundRequest("http://host/high", net::HIGHEST));
2021 scoped_ptr<TestRequest> low(
2022 NewBackgroundRequest("http://host/low", net::LOWEST));
2023 EXPECT_FALSE(high->started());
2024 EXPECT_FALSE(low->started());
2026 FireCoalescingTimer();
2028 EXPECT_TRUE(high->started());
2029 EXPECT_TRUE(low->started());
2032 TEST_F(ResourceSchedulerTest, CoalescedRequestsUnthrottleCorrectlyOnTimer) {
2033 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2034 true /* should_coalesce */);
2035 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
2036 scheduler()->OnLoadingStateChanged(
2037 kBackgroundChildId, kBackgroundRouteId, true);
2038 EXPECT_EQ(ResourceScheduler::COALESCED,
2039 scheduler()->GetClientStateForTesting(kBackgroundChildId,
2040 kBackgroundRouteId));
2041 EXPECT_TRUE(scheduler()->active_clients_loaded());
2043 scoped_ptr<TestRequest> high(
2044 NewBackgroundRequest("http://host/high", net::HIGHEST));
2045 scoped_ptr<TestRequest> high2(
2046 NewBackgroundRequest("http://host/high", net::HIGHEST));
2047 scoped_ptr<TestRequest> high3(
2048 NewBackgroundRequest("http://host/high", net::HIGHEST));
2049 scoped_ptr<TestRequest> high4(
2050 NewBackgroundRequest("http://host/high", net::HIGHEST));
2051 scoped_ptr<TestRequest> low(
2052 NewBackgroundRequest("http://host/low", net::LOWEST));
2053 scoped_ptr<TestRequest> low2(
2054 NewBackgroundRequest("http://host/low", net::LOWEST));
2055 scoped_ptr<TestRequest> low3(
2056 NewBackgroundRequest("http://host/low", net::LOWEST));
2057 scoped_ptr<TestRequest> low4(
2058 NewBackgroundRequest("http://host/low", net::LOWEST));
2060 http_server_properties_.SetSupportsSpdy(net::HostPortPair("spdyhost", 443),
2061 true);
2062 scoped_ptr<TestRequest> low_spdy(
2063 NewBackgroundRequest("https://spdyhost/low", net::LOW));
2064 scoped_ptr<TestRequest> sync_request(
2065 NewBackgroundSyncRequest("http://host/req", net::LOW));
2066 scoped_ptr<TestRequest> non_http_request(
2067 NewBackgroundRequest("chrome-extension://req", net::LOW));
2069 // Sync requests should issue immediately.
2070 EXPECT_TRUE(sync_request->started());
2071 // Non-http(s) requests should issue immediately.
2072 EXPECT_TRUE(non_http_request->started());
2073 // Nothing else should issue without a timer fire.
2074 EXPECT_FALSE(high->started());
2075 EXPECT_FALSE(high2->started());
2076 EXPECT_FALSE(high3->started());
2077 EXPECT_FALSE(high4->started());
2078 EXPECT_FALSE(low->started());
2079 EXPECT_FALSE(low2->started());
2080 EXPECT_FALSE(low3->started());
2081 EXPECT_FALSE(low4->started());
2082 EXPECT_FALSE(low_spdy->started());
2084 FireCoalescingTimer();
2086 // All high priority requests should issue.
2087 EXPECT_TRUE(high->started());
2088 EXPECT_TRUE(high2->started());
2089 EXPECT_TRUE(high3->started());
2090 EXPECT_TRUE(high4->started());
2091 // There should only be one net::LOWEST priority request issued with
2092 // non-delayable requests in flight.
2093 EXPECT_TRUE(low->started());
2094 EXPECT_FALSE(low2->started());
2095 EXPECT_FALSE(low3->started());
2096 EXPECT_FALSE(low4->started());
2097 // Spdy-Enable requests should issue regardless of priority.
2098 EXPECT_TRUE(low_spdy->started());
2101 TEST_F(ResourceSchedulerTest, CoalescedRequestsWaitForNextTimer) {
2102 scheduler()->SetThrottleOptionsForTesting(true /* should_throttle */,
2103 true /* should_coalesce */);
2104 scheduler()->OnLoadingStateChanged(kChildId, kRouteId, true);
2105 scheduler()->OnLoadingStateChanged(
2106 kBackgroundChildId, kBackgroundRouteId, true);
2108 EXPECT_EQ(ResourceScheduler::COALESCED,
2109 scheduler()->GetClientStateForTesting(kBackgroundChildId,
2110 kBackgroundRouteId));
2111 EXPECT_TRUE(scheduler()->active_clients_loaded());
2113 scoped_ptr<TestRequest> high(
2114 NewBackgroundRequest("http://host/high", net::HIGHEST));
2115 EXPECT_FALSE(high->started());
2117 FireCoalescingTimer();
2119 scoped_ptr<TestRequest> high2(
2120 NewBackgroundRequest("http://host/high2", net::HIGHEST));
2121 scoped_ptr<TestRequest> low(
2122 NewBackgroundRequest("http://host/low", net::LOWEST));
2124 EXPECT_TRUE(high->started());
2125 EXPECT_FALSE(high2->started());
2126 EXPECT_FALSE(low->started());
2128 FireCoalescingTimer();
2130 EXPECT_TRUE(high->started());
2131 EXPECT_TRUE(high2->started());
2132 EXPECT_TRUE(low->started());
2135 TEST_F(ResourceSchedulerTest, GetVisualSignalFromRenderViewHost) {
2136 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory;
2137 scoped_ptr<TestRenderViewHostFactory> render_view_host_factory;
2138 scoped_ptr<TestBrowserContext> browser_context;
2139 scoped_ptr<TestWebContents> web_contents_1;
2140 scoped_ptr<TestWebContents> web_contents_2;
2141 render_process_host_factory.reset(new MockRenderProcessHostFactory());
2142 render_view_host_factory.reset(
2143 new TestRenderViewHostFactory(render_process_host_factory.get()));
2145 browser_context.reset(new TestBrowserContext());
2146 scoped_refptr<SiteInstance> site_instance_1 =
2147 SiteInstance::Create(browser_context.get());
2148 scoped_refptr<SiteInstance> site_instance_2 =
2149 SiteInstance::Create(browser_context.get());
2150 SiteInstanceImpl::set_render_process_host_factory(
2151 render_process_host_factory.get());
2153 web_contents_1.reset(
2154 TestWebContents::Create(browser_context.get(), site_instance_1.get()));
2155 web_contents_2.reset(
2156 TestWebContents::Create(browser_context.get(), site_instance_2.get()));
2157 base::RunLoop().RunUntilIdle();
2159 RenderViewHostImpl* rvh1 = web_contents_1->GetRenderViewHost();
2160 RenderViewHostImpl* rvh2 = web_contents_2->GetRenderViewHost();
2161 ResourceScheduler* scheduler = ResourceDispatcherHostImpl::Get()->scheduler();
2163 // Check initial visibility is set correctly.
2164 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2165 rvh1->GetRoutingID()),
2166 !rvh1->is_hidden());
2167 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2168 rvh1->GetRoutingID()),
2169 !rvh2->is_hidden());
2171 // 1 visible, 1 hidden
2172 rvh1->WasShown(ui::LatencyInfo());
2173 rvh2->WasHidden();
2174 base::RunLoop().RunUntilIdle();
2176 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2177 rvh1->GetRoutingID()));
2178 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2179 rvh2->GetRoutingID()));
2181 // Flip the visibility and check again.
2182 rvh1->WasHidden();
2183 rvh2->WasShown(ui::LatencyInfo());
2184 base::RunLoop().RunUntilIdle();
2186 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2187 rvh1->GetRoutingID()));
2188 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2189 rvh2->GetRoutingID()));
2190 // Clean up.
2191 web_contents_1.reset();
2192 web_contents_2.reset();
2193 base::RunLoop().RunUntilIdle();
2195 browser_context.reset();
2196 render_process_host_factory.reset();
2199 TEST_F(ResourceSchedulerTest, OustandingRequestLimitEnforced) {
2200 const int kRequestLimit = 3;
2201 ASSERT_TRUE(InitializeFieldTrials(
2202 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/",
2203 kRequestLimit)));
2204 InitializeScheduler();
2206 // Throw in requests up to the above limit; make sure they are started.
2207 ScopedVector<TestRequest> requests;
2208 for (int i = 0; i < kRequestLimit; ++i) {
2209 string url = "http://host/medium";
2210 requests.push_back(NewRequest(url.c_str(), net::MEDIUM));
2211 EXPECT_TRUE(requests[i]->started());
2214 // Confirm that another request will indeed fail.
2215 string url = "http://host/medium";
2216 requests.push_back(NewRequest(url.c_str(), net::MEDIUM));
2217 EXPECT_FALSE(requests[kRequestLimit]->started());
2220 // Confirm that outstanding requests limits apply to requests to hosts
2221 // that support request priority.
2222 TEST_F(ResourceSchedulerTest,
2223 OutstandingRequestsLimitsEnforcedForRequestPriority) {
2224 const int kRequestLimit = 3;
2225 ASSERT_TRUE(InitializeFieldTrials(
2226 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/",
2227 kRequestLimit)));
2228 InitializeScheduler();
2230 http_server_properties_.SetSupportsSpdy(
2231 net::HostPortPair("spdyhost", 443), true);
2233 // Throw in requests up to the above limit; make sure they are started.
2234 ScopedVector<TestRequest> requests;
2235 for (int i = 0; i < kRequestLimit; ++i) {
2236 string url = "http://spdyhost/medium";
2237 requests.push_back(NewRequest(url.c_str(), net::MEDIUM));
2238 EXPECT_TRUE(requests[i]->started());
2241 // Confirm that another request will indeed fail.
2242 string url = "http://spdyhost/medium";
2243 requests.push_back(NewRequest(url.c_str(), net::MEDIUM));
2244 EXPECT_FALSE(requests[kRequestLimit]->started());
2247 TEST_F(ResourceSchedulerTest, OutstandingRequestLimitDelays) {
2248 const int kRequestLimit = 3;
2249 ASSERT_TRUE(InitializeFieldTrials(
2250 base::StringPrintf("OutstandingRequestLimiting/Limit=%d/",
2251 kRequestLimit)));
2253 InitializeScheduler();
2254 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
2255 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
2256 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
2257 EXPECT_TRUE(high->started());
2258 EXPECT_FALSE(low->started());
2259 EXPECT_FALSE(low2->started());
2260 high.reset();
2261 EXPECT_TRUE(low->started());
2262 EXPECT_TRUE(low2->started());
2265 // Async revalidations which are not started when the tab is closed must be
2266 // started at some point, or they will hang around forever and prevent other
2267 // async revalidations to the same URL from being issued.
2268 TEST_F(ResourceSchedulerTest, RequestStartedAfterClientDeleted) {
2269 scheduler_->OnClientCreated(kChildId2, kRouteId2, false, false);
2270 scoped_ptr<TestRequest> high(NewRequestWithChildAndRoute(
2271 "http://host/high", net::HIGHEST, kChildId2, kRouteId2));
2272 scoped_ptr<TestRequest> lowest1(NewRequestWithChildAndRoute(
2273 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2));
2274 scoped_ptr<TestRequest> lowest2(NewRequestWithChildAndRoute(
2275 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2));
2276 EXPECT_FALSE(lowest2->started());
2277 scheduler_->OnClientDeleted(kChildId2, kRouteId2);
2278 high.reset();
2279 lowest1.reset();
2280 EXPECT_TRUE(lowest2->started());
2283 // The ResourceScheduler::Client destructor calls
2284 // LoadAnyStartablePendingRequests(), which may start some pending requests.
2285 // This test is to verify that requests will be started at some point
2286 // even if they were not started by the destructor.
2287 TEST_F(ResourceSchedulerTest, RequestStartedAfterClientDeletedManyDelayable) {
2288 scheduler_->OnClientCreated(kChildId2, kRouteId2, false, false);
2289 scoped_ptr<TestRequest> high(NewRequestWithChildAndRoute(
2290 "http://host/high", net::HIGHEST, kChildId2, kRouteId2));
2291 const int kMaxNumDelayableRequestsPerClient = 10;
2292 ScopedVector<TestRequest> delayable_requests;
2293 for (int i = 0; i < kMaxNumDelayableRequestsPerClient + 1; ++i) {
2294 delayable_requests.push_back(NewRequestWithChildAndRoute(
2295 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2));
2297 scoped_ptr<TestRequest> lowest(NewRequestWithChildAndRoute(
2298 "http://host/lowest", net::LOWEST, kChildId2, kRouteId2));
2299 EXPECT_FALSE(lowest->started());
2300 scheduler_->OnClientDeleted(kChildId2, kRouteId2);
2301 high.reset();
2302 delayable_requests.clear();
2303 EXPECT_TRUE(lowest->started());
2306 TEST_F(ResourceSchedulerTest, DefaultLayoutBlockingPriority) {
2307 const int kDeferLateScripts = 0;
2308 const int kIncreaseFontPriority = 0;
2309 const int kIncreaseAsyncScriptPriority = 0;
2310 const int kEnablePriorityIncrease = 0;
2311 const int kEnableLayoutBlockingThreshold = 0;
2312 const int kLayoutBlockingThreshold = 0;
2313 const int kMaxNumDelayableWhileLayoutBlocking = 1;
2314 const int kMaxNumDelayableRequestsPerClient = 10;
2315 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2316 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2317 kDeferLateScripts,
2318 kIncreaseFontPriority,
2319 kIncreaseAsyncScriptPriority,
2320 kEnablePriorityIncrease,
2321 kEnableLayoutBlockingThreshold,
2322 kLayoutBlockingThreshold,
2323 kMaxNumDelayableWhileLayoutBlocking,
2324 kMaxNumDelayableRequestsPerClient)));
2325 InitializeScheduler();
2326 scoped_ptr<TestRequest> high(
2327 NewRequest("http://hosthigh/high", net::HIGHEST));
2328 scoped_ptr<TestRequest> high2(
2329 NewRequest("http://hosthigh/high", net::HIGHEST));
2330 scoped_ptr<TestRequest> medium(
2331 NewRequest("http://hostmedium/medium", net::MEDIUM));
2332 scoped_ptr<TestRequest> medium2(
2333 NewRequest("http://hostmedium/medium", net::MEDIUM));
2334 scoped_ptr<TestRequest> low(NewRequest("http://hostlow/low", net::LOW));
2335 scoped_ptr<TestRequest> low2(NewRequest("http://hostlow/low", net::LOW));
2336 scoped_ptr<TestRequest> lowest(NewRequest("http://hostlowest/lowest", net::LOWEST));
2337 scoped_ptr<TestRequest> lowest2(
2338 NewRequest("http://hostlowest/lowest", net::LOWEST));
2339 EXPECT_TRUE(high->started());
2340 EXPECT_TRUE(high2->started());
2341 EXPECT_TRUE(medium->started());
2342 EXPECT_TRUE(medium2->started());
2343 EXPECT_TRUE(low->started());
2344 EXPECT_TRUE(low2->started());
2345 EXPECT_TRUE(lowest->started());
2346 EXPECT_FALSE(lowest2->started());
2347 lowest.reset();
2348 EXPECT_TRUE(lowest2->started());
2351 TEST_F(ResourceSchedulerTest, IncreaseLayoutBlockingPriority) {
2352 // Changes the level of priorities that are allowed during layout-blocking
2353 // from net::LOWEST to net::LOW.
2354 const int kDeferLateScripts = 0;
2355 const int kIncreaseFontPriority = 0;
2356 const int kIncreaseAsyncScriptPriority = 0;
2357 const int kEnablePriorityIncrease = 1;
2358 const int kEnableLayoutBlockingThreshold = 0;
2359 const int kLayoutBlockingThreshold = 0;
2360 const int kMaxNumDelayableWhileLayoutBlocking = 1;
2361 const int kMaxNumDelayableRequestsPerClient = 10;
2362 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2363 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2364 kDeferLateScripts,
2365 kIncreaseFontPriority,
2366 kIncreaseAsyncScriptPriority,
2367 kEnablePriorityIncrease,
2368 kEnableLayoutBlockingThreshold,
2369 kLayoutBlockingThreshold,
2370 kMaxNumDelayableWhileLayoutBlocking,
2371 kMaxNumDelayableRequestsPerClient)));
2372 InitializeScheduler();
2373 scoped_ptr<TestRequest> high(
2374 NewRequest("http://hosthigh/high", net::HIGHEST));
2375 scoped_ptr<TestRequest> high2(
2376 NewRequest("http://hosthigh/high", net::HIGHEST));
2377 scoped_ptr<TestRequest> medium(
2378 NewRequest("http://hostmedium/medium", net::MEDIUM));
2379 scoped_ptr<TestRequest> medium2(
2380 NewRequest("http://hostmedium/medium", net::MEDIUM));
2381 scoped_ptr<TestRequest> low(NewRequest("http://hostlow/low", net::LOW));
2382 scoped_ptr<TestRequest> low2(NewRequest("http://hostlow/low", net::LOW));
2383 scoped_ptr<TestRequest> lowest(NewRequest("http://hostlowest/lowest", net::LOWEST));
2384 scoped_ptr<TestRequest> lowest2(
2385 NewRequest("http://hostlowest/lowest", net::LOWEST));
2386 EXPECT_TRUE(high->started());
2387 EXPECT_TRUE(high2->started());
2388 EXPECT_TRUE(medium->started());
2389 EXPECT_TRUE(medium2->started());
2390 EXPECT_TRUE(low->started());
2391 EXPECT_FALSE(low2->started());
2392 EXPECT_FALSE(lowest->started());
2393 EXPECT_FALSE(lowest2->started());
2394 low.reset();
2395 EXPECT_TRUE(low2->started());
2396 EXPECT_FALSE(lowest->started());
2397 EXPECT_FALSE(lowest2->started());
2398 low2.reset();
2399 EXPECT_TRUE(lowest->started());
2400 EXPECT_FALSE(lowest2->started());
2401 lowest.reset();
2402 EXPECT_TRUE(lowest2->started());
2405 TEST_F(ResourceSchedulerTest, UseLayoutBlockingThresholdOne) {
2406 // Prevents any low priority requests from starting while more than
2407 // N high priority requests are pending (before body).
2408 const int kDeferLateScripts = 0;
2409 const int kIncreaseFontPriority = 0;
2410 const int kIncreaseAsyncScriptPriority = 0;
2411 const int kEnablePriorityIncrease = 0;
2412 const int kEnableLayoutBlockingThreshold = 1;
2413 const int kLayoutBlockingThreshold = 1;
2414 const int kMaxNumDelayableWhileLayoutBlocking = 1;
2415 const int kMaxNumDelayableRequestsPerClient = 10;
2416 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2417 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2418 kDeferLateScripts,
2419 kIncreaseFontPriority,
2420 kIncreaseAsyncScriptPriority,
2421 kEnablePriorityIncrease,
2422 kEnableLayoutBlockingThreshold,
2423 kLayoutBlockingThreshold,
2424 kMaxNumDelayableWhileLayoutBlocking,
2425 kMaxNumDelayableRequestsPerClient)));
2426 InitializeScheduler();
2427 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
2428 scoped_ptr<TestRequest> high2(NewRequest("http://host/high", net::HIGHEST));
2429 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
2430 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
2431 EXPECT_TRUE(high->started());
2432 EXPECT_TRUE(high2->started());
2433 EXPECT_FALSE(low->started());
2434 EXPECT_FALSE(low2->started());
2435 high.reset();
2436 EXPECT_TRUE(low->started());
2437 EXPECT_FALSE(low2->started());
2438 high2.reset();
2439 EXPECT_FALSE(low2->started());
2440 scheduler()->OnWillInsertBody(kChildId, kRouteId);
2441 EXPECT_TRUE(low2->started());
2444 TEST_F(ResourceSchedulerTest, UseLayoutBlockingThresholdTwo) {
2445 // Prevents any low priority requests from starting while more than
2446 // N high priority requests are pending (before body).
2447 const int kDeferLateScripts = 0;
2448 const int kIncreaseFontPriority = 0;
2449 const int kIncreaseAsyncScriptPriority = 0;
2450 const int kEnablePriorityIncrease = 0;
2451 const int kEnableLayoutBlockingThreshold = 1;
2452 const int kLayoutBlockingThreshold = 2;
2453 const int kMaxNumDelayableWhileLayoutBlocking = 1;
2454 const int kMaxNumDelayableRequestsPerClient = 10;
2455 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2456 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2457 kDeferLateScripts,
2458 kIncreaseFontPriority,
2459 kIncreaseAsyncScriptPriority,
2460 kEnablePriorityIncrease,
2461 kEnableLayoutBlockingThreshold,
2462 kLayoutBlockingThreshold,
2463 kMaxNumDelayableWhileLayoutBlocking,
2464 kMaxNumDelayableRequestsPerClient)));
2465 InitializeScheduler();
2466 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
2467 scoped_ptr<TestRequest> high2(NewRequest("http://host/high", net::HIGHEST));
2468 scoped_ptr<TestRequest> high3(NewRequest("http://host/high", net::HIGHEST));
2469 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
2470 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
2471 EXPECT_TRUE(high->started());
2472 EXPECT_TRUE(high2->started());
2473 EXPECT_TRUE(high3->started());
2474 EXPECT_FALSE(low->started());
2475 EXPECT_FALSE(low2->started());
2476 high.reset();
2477 EXPECT_TRUE(low->started());
2478 EXPECT_FALSE(low2->started());
2479 high2.reset();
2480 high3.reset();
2481 EXPECT_FALSE(low2->started());
2482 scheduler()->OnWillInsertBody(kChildId, kRouteId);
2483 EXPECT_TRUE(low2->started());
2486 TEST_F(ResourceSchedulerTest, TwoDelayableLoadsUntilBodyInserted) {
2487 // Allow for two low priority requests to be in flight at any point in time
2488 // during the layout-blocking phase of loading.
2489 const int kDeferLateScripts = 0;
2490 const int kIncreaseFontPriority = 0;
2491 const int kIncreaseAsyncScriptPriority = 0;
2492 const int kEnablePriorityIncrease = 0;
2493 const int kEnableLayoutBlockingThreshold = 0;
2494 const int kLayoutBlockingThreshold = 0;
2495 const int kMaxNumDelayableWhileLayoutBlocking = 2;
2496 const int kMaxNumDelayableRequestsPerClient = 10;
2497 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2498 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2499 kDeferLateScripts,
2500 kIncreaseFontPriority,
2501 kIncreaseAsyncScriptPriority,
2502 kEnablePriorityIncrease,
2503 kEnableLayoutBlockingThreshold,
2504 kLayoutBlockingThreshold,
2505 kMaxNumDelayableWhileLayoutBlocking,
2506 kMaxNumDelayableRequestsPerClient)));
2507 InitializeScheduler();
2508 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
2509 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
2510 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
2511 scoped_ptr<TestRequest> low3(NewRequest("http://host/low", net::LOWEST));
2512 EXPECT_TRUE(high->started());
2513 EXPECT_TRUE(low->started());
2514 EXPECT_TRUE(low2->started());
2515 EXPECT_FALSE(low3->started());
2516 high.reset();
2517 scheduler()->OnWillInsertBody(kChildId, kRouteId);
2518 EXPECT_TRUE(low3->started());
2521 TEST_F(ResourceSchedulerTest,
2522 UseLayoutBlockingThresholdOneAndTwoDelayableLoadsUntilBodyInserted) {
2523 // Allow for two low priority requests to be in flight during the
2524 // layout-blocking phase of loading but only when there is not more than one
2525 // in-flight high priority request.
2526 const int kDeferLateScripts = 0;
2527 const int kIncreaseFontPriority = 0;
2528 const int kIncreaseAsyncScriptPriority = 0;
2529 const int kEnablePriorityIncrease = 0;
2530 const int kEnableLayoutBlockingThreshold = 1;
2531 const int kLayoutBlockingThreshold = 1;
2532 const int kMaxNumDelayableWhileLayoutBlocking = 2;
2533 const int kMaxNumDelayableRequestsPerClient = 10;
2534 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2535 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2536 kDeferLateScripts,
2537 kIncreaseFontPriority,
2538 kIncreaseAsyncScriptPriority,
2539 kEnablePriorityIncrease,
2540 kEnableLayoutBlockingThreshold,
2541 kLayoutBlockingThreshold,
2542 kMaxNumDelayableWhileLayoutBlocking,
2543 kMaxNumDelayableRequestsPerClient)));
2544 InitializeScheduler();
2545 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
2546 scoped_ptr<TestRequest> high2(NewRequest("http://host/high", net::HIGHEST));
2547 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
2548 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
2549 scoped_ptr<TestRequest> low3(NewRequest("http://host/low", net::LOWEST));
2550 EXPECT_TRUE(high->started());
2551 EXPECT_TRUE(high2->started());
2552 EXPECT_FALSE(low->started());
2553 EXPECT_FALSE(low2->started());
2554 EXPECT_FALSE(low3->started());
2555 high.reset();
2556 EXPECT_TRUE(low->started());
2557 EXPECT_TRUE(low2->started());
2558 EXPECT_FALSE(low3->started());
2559 high2.reset();
2560 scheduler()->OnWillInsertBody(kChildId, kRouteId);
2561 EXPECT_TRUE(low3->started());
2564 TEST_F(ResourceSchedulerTest, TwentyMaxNumDelayableRequestsPerClient) {
2565 // Do not exceed 20 low-priority requests to be in flight across all hosts
2566 // at any point in time.
2567 const int kDeferLateScripts = 0;
2568 const int kIncreaseFontPriority = 0;
2569 const int kIncreaseAsyncScriptPriority = 0;
2570 const int kEnablePriorityIncrease = 0;
2571 const int kEnableLayoutBlockingThreshold = 0;
2572 const int kLayoutBlockingThreshold = 0;
2573 const int kMaxNumDelayableWhileLayoutBlocking = 1;
2574 const int kMaxNumDelayableRequestsPerClient = 20;
2575 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2576 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2577 kDeferLateScripts,
2578 kIncreaseFontPriority,
2579 kIncreaseAsyncScriptPriority,
2580 kEnablePriorityIncrease,
2581 kEnableLayoutBlockingThreshold,
2582 kLayoutBlockingThreshold,
2583 kMaxNumDelayableWhileLayoutBlocking,
2584 kMaxNumDelayableRequestsPerClient)));
2585 InitializeScheduler();
2587 // Only load low priority resources if there's a body.
2588 scheduler()->OnWillInsertBody(kChildId, kRouteId);
2590 // Queue requests from different hosts until the total limit is reached.
2591 ScopedVector<TestRequest> lows_different_host;
2592 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
2593 string url = "http://host" + base::IntToString(i) + "/low";
2594 lows_different_host.push_back(NewRequest(url.c_str(), net::LOWEST));
2595 EXPECT_TRUE(lows_different_host[i]->started());
2598 scoped_ptr<TestRequest> last_different_host(NewRequest("http://host_new/last",
2599 net::LOWEST));
2600 EXPECT_FALSE(last_different_host->started());
2603 TEST_F(ResourceSchedulerTest,
2604 TwentyMaxNumDelayableRequestsPerClientWithEverythingEnabled) {
2605 // Do not exceed 20 low-priority requests to be in flight across all hosts
2606 // at any point in time and make sure it still works correctly when the other
2607 // options are toggled.
2608 const int kDeferLateScripts = 1;
2609 const int kIncreaseFontPriority = 1;
2610 const int kIncreaseAsyncScriptPriority = 1;
2611 const int kEnablePriorityIncrease = 1;
2612 const int kEnableLayoutBlockingThreshold = 1;
2613 const int kLayoutBlockingThreshold = 1;
2614 const int kMaxNumDelayableWhileLayoutBlocking = 1;
2615 const int kMaxNumDelayableRequestsPerClient = 20;
2616 ASSERT_TRUE(InitializeFieldTrials(base::StringPrintf(
2617 "ResourcePriorities/LayoutBlocking_%d%d%d%d%d_%d_%d_%d/",
2618 kDeferLateScripts,
2619 kIncreaseFontPriority,
2620 kIncreaseAsyncScriptPriority,
2621 kEnablePriorityIncrease,
2622 kEnableLayoutBlockingThreshold,
2623 kLayoutBlockingThreshold,
2624 kMaxNumDelayableWhileLayoutBlocking,
2625 kMaxNumDelayableRequestsPerClient)));
2626 InitializeScheduler();
2628 // Only load low priority resources if there's a body.
2629 scheduler()->OnWillInsertBody(kChildId, kRouteId);
2631 // Queue requests from different hosts until the total limit is reached.
2632 ScopedVector<TestRequest> lows_different_host;
2633 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
2634 string url = "http://host" + base::IntToString(i) + "/low";
2635 lows_different_host.push_back(NewRequest(url.c_str(), net::LOWEST));
2636 EXPECT_TRUE(lows_different_host[i]->started());
2639 scoped_ptr<TestRequest> last_different_host(NewRequest("http://host_new/last",
2640 net::LOWEST));
2641 EXPECT_FALSE(last_different_host->started());
2644 } // unnamed namespace
2646 } // namespace content