Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / loader / resource_scheduler_unittest.cc
blob2d1b0fe34bd1b356ea567051d7613efac68dfca3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/loader/resource_scheduler.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/timer/mock_timer.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/loader/resource_dispatcher_host_impl.h"
15 #include "content/browser/loader/resource_message_filter.h"
16 #include "content/browser/loader/resource_request_info_impl.h"
17 #include "content/common/resource_messages.h"
18 #include "content/public/browser/resource_context.h"
19 #include "content/public/browser/resource_controller.h"
20 #include "content/public/browser/resource_throttle.h"
21 #include "content/public/common/process_type.h"
22 #include "content/public/common/resource_type.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/test/test_render_view_host_factory.h"
26 #include "content/test/test_web_contents.h"
27 #include "net/base/host_port_pair.h"
28 #include "net/base/request_priority.h"
29 #include "net/http/http_server_properties_impl.h"
30 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "ui/events/latency_info.h"
35 namespace content {
37 namespace {
39 class TestRequestFactory;
41 const int kChildId = 30;
42 const int kRouteId = 75;
43 const int kChildId2 = 43;
44 const int kRouteId2 = 67;
45 const int kBackgroundChildId = 35;
46 const int kBackgroundRouteId = 43;
47 const int kBackgroundChildId2 = 54;
48 const int kBackgroundRouteId2 = 82;
50 class TestRequest : public ResourceController {
51 public:
52 TestRequest(scoped_ptr<ResourceThrottle> throttle,
53 scoped_ptr<net::URLRequest> url_request)
54 : started_(false),
55 throttle_(throttle.Pass()),
56 url_request_(url_request.Pass()) {
57 throttle_->set_controller_for_testing(this);
59 ~TestRequest() override {}
61 bool started() const { return started_; }
63 void Start() {
64 bool deferred = false;
65 throttle_->WillStartRequest(&deferred);
66 started_ = !deferred;
69 void Cancel() override {
70 // Alert the scheduler that the request can be deleted.
71 throttle_.reset(0);
74 const net::URLRequest* url_request() const { return url_request_.get(); }
76 protected:
77 // ResourceController interface:
78 void CancelAndIgnore() override {}
79 void CancelWithError(int error_code) override {}
80 void Resume() override { started_ = true; }
82 private:
83 bool started_;
84 scoped_ptr<ResourceThrottle> throttle_;
85 scoped_ptr<net::URLRequest> url_request_;
88 class CancelingTestRequest : public TestRequest {
89 public:
90 CancelingTestRequest(scoped_ptr<ResourceThrottle> throttle,
91 scoped_ptr<net::URLRequest> url_request)
92 : TestRequest(throttle.Pass(), url_request.Pass()) {}
94 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) {
95 request_to_cancel_ = request_to_cancel.Pass();
98 private:
99 void Resume() override {
100 TestRequest::Resume();
101 request_to_cancel_.reset();
104 scoped_ptr<TestRequest> request_to_cancel_;
107 class FakeResourceContext : public ResourceContext {
108 private:
109 net::HostResolver* GetHostResolver() override { return NULL; }
110 net::URLRequestContext* GetRequestContext() override { return NULL; }
113 class FakeResourceMessageFilter : public ResourceMessageFilter {
114 public:
115 FakeResourceMessageFilter(int child_id)
116 : ResourceMessageFilter(
117 child_id,
118 PROCESS_TYPE_RENDERER,
119 NULL /* appcache_service */,
120 NULL /* blob_storage_context */,
121 NULL /* file_system_context */,
122 NULL /* service_worker_context */,
123 base::Bind(&FakeResourceMessageFilter::GetContexts,
124 base::Unretained(this))) {
127 private:
128 ~FakeResourceMessageFilter() override {}
130 void GetContexts(const ResourceHostMsg_Request& request,
131 ResourceContext** resource_context,
132 net::URLRequestContext** request_context) {
133 *resource_context = &context_;
134 *request_context = NULL;
137 FakeResourceContext context_;
140 class ResourceSchedulerTest : public testing::Test {
141 protected:
142 ResourceSchedulerTest()
143 : next_request_id_(0),
144 ui_thread_(BrowserThread::UI, &message_loop_),
145 io_thread_(BrowserThread::IO, &message_loop_),
146 mock_timer_(new base::MockTimer(true, true)) {
147 scheduler_.set_timer_for_testing(scoped_ptr<base::Timer>(mock_timer_));
149 // TODO(aiolos): Remove when throttling and coalescing have both landed.
150 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
151 false /* should_coalesce */);
153 scheduler_.OnClientCreated(kChildId, kRouteId, true, false);
154 scheduler_.OnClientCreated(
155 kBackgroundChildId, kBackgroundRouteId, false, false);
156 context_.set_http_server_properties(http_server_properties_.GetWeakPtr());
159 ~ResourceSchedulerTest() override {
160 scheduler_.OnClientDeleted(kChildId, kRouteId);
161 scheduler_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
164 scoped_ptr<net::URLRequest> NewURLRequestWithChildAndRoute(
165 const char* url,
166 net::RequestPriority priority,
167 int child_id,
168 int route_id,
169 bool is_async) {
170 scoped_ptr<net::URLRequest> url_request(
171 context_.CreateRequest(GURL(url), priority, NULL, NULL));
172 ResourceRequestInfoImpl* info = new ResourceRequestInfoImpl(
173 PROCESS_TYPE_RENDERER, // process_type
174 child_id, // child_id
175 route_id, // route_id
176 0, // origin_pid
177 ++next_request_id_, // request_id
178 MSG_ROUTING_NONE, // render_frame_id
179 false, // is_main_frame
180 false, // parent_is_main_frame
181 0, // parent_render_frame_id
182 RESOURCE_TYPE_SUB_RESOURCE, // resource_type
183 ui::PAGE_TRANSITION_LINK, // transition_type
184 false, // should_replace_current_entry
185 false, // is_download
186 false, // is_stream
187 true, // allow_download
188 false, // has_user_gesture
189 false, // enable_load_timing
190 false, // enable_upload_progress
191 blink::WebReferrerPolicyDefault, // referrer_policy
192 blink::WebPageVisibilityStateVisible, // visibility_state
193 NULL, // context
194 base::WeakPtr<ResourceMessageFilter>(), // filter
195 is_async); // is_async
196 info->AssociateWithRequest(url_request.get());
197 return url_request.Pass();
200 scoped_ptr<net::URLRequest> NewURLRequest(const char* url,
201 net::RequestPriority priority) {
202 return NewURLRequestWithChildAndRoute(
203 url, priority, kChildId, kRouteId, true);
206 TestRequest* NewRequestWithRoute(const char* url,
207 net::RequestPriority priority,
208 int route_id) {
209 return NewRequestWithChildAndRoute(url, priority, route_id, kChildId);
212 TestRequest* NewRequestWithChildAndRoute(const char* url,
213 net::RequestPriority priority,
214 int child_id,
215 int route_id) {
216 return GetNewTestRequest(url, priority, child_id, route_id, true);
219 TestRequest* NewRequest(const char* url, net::RequestPriority priority) {
220 return NewRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
223 TestRequest* NewBackgroundRequest(const char* url,
224 net::RequestPriority priority) {
225 return NewRequestWithChildAndRoute(
226 url, priority, kBackgroundChildId, kBackgroundRouteId);
229 TestRequest* NewSyncRequest(const char* url, net::RequestPriority priority) {
230 return NewSyncRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
233 TestRequest* NewBackgroundSyncRequest(const char* url,
234 net::RequestPriority priority) {
235 return NewSyncRequestWithChildAndRoute(
236 url, priority, kBackgroundChildId, kBackgroundRouteId);
239 TestRequest* NewSyncRequestWithChildAndRoute(const char* url,
240 net::RequestPriority priority,
241 int child_id,
242 int route_id) {
243 return GetNewTestRequest(url, priority, child_id, route_id, false);
246 TestRequest* GetNewTestRequest(const char* url,
247 net::RequestPriority priority,
248 int child_id,
249 int route_id,
250 bool is_async) {
251 scoped_ptr<net::URLRequest> url_request(NewURLRequestWithChildAndRoute(
252 url, priority, child_id, route_id, is_async));
253 scoped_ptr<ResourceThrottle> throttle(
254 scheduler_.ScheduleRequest(child_id, route_id, url_request.get()));
255 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass());
256 request->Start();
257 return request;
261 void ChangeRequestPriority(TestRequest* request,
262 net::RequestPriority new_priority,
263 int intra_priority = 0) {
264 scoped_refptr<FakeResourceMessageFilter> filter(
265 new FakeResourceMessageFilter(kChildId));
266 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(
267 request->url_request());
268 const GlobalRequestID& id = info->GetGlobalRequestID();
269 ResourceHostMsg_DidChangePriority msg(id.request_id, new_priority,
270 intra_priority);
271 rdh_.OnMessageReceived(msg, filter.get());
274 void FireCoalescingTimer() {
275 EXPECT_TRUE(mock_timer_->IsRunning());
276 mock_timer_->Fire();
279 int next_request_id_;
280 base::MessageLoopForIO message_loop_;
281 BrowserThreadImpl ui_thread_;
282 BrowserThreadImpl io_thread_;
283 ResourceDispatcherHostImpl rdh_;
284 ResourceScheduler scheduler_;
285 base::MockTimer* mock_timer_;
286 net::HttpServerPropertiesImpl http_server_properties_;
287 net::TestURLRequestContext context_;
290 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
291 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST));
292 EXPECT_TRUE(request->started());
295 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilIdle) {
296 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
297 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
298 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
299 EXPECT_TRUE(high->started());
300 EXPECT_TRUE(low->started());
301 EXPECT_FALSE(low2->started());
302 high.reset();
303 EXPECT_TRUE(low2->started());
306 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInserted) {
307 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
308 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
309 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
310 EXPECT_TRUE(high->started());
311 EXPECT_TRUE(low->started());
312 EXPECT_FALSE(low2->started());
313 high.reset();
314 scheduler_.OnWillInsertBody(kChildId, kRouteId);
315 EXPECT_TRUE(low2->started());
318 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilCriticalComplete) {
319 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
320 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
321 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
322 EXPECT_TRUE(high->started());
323 EXPECT_TRUE(low->started());
324 EXPECT_FALSE(low2->started());
325 scheduler_.OnWillInsertBody(kChildId, kRouteId);
326 EXPECT_FALSE(low2->started());
327 high.reset();
328 EXPECT_TRUE(low2->started());
331 TEST_F(ResourceSchedulerTest, LowDoesNotBlockCriticalComplete) {
332 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
333 scoped_ptr<TestRequest> lowest(NewRequest("http://host/lowest", net::LOWEST));
334 scoped_ptr<TestRequest> lowest2(
335 NewRequest("http://host/lowest", net::LOWEST));
336 EXPECT_TRUE(low->started());
337 EXPECT_TRUE(lowest->started());
338 EXPECT_FALSE(lowest2->started());
339 scheduler_.OnWillInsertBody(kChildId, kRouteId);
340 EXPECT_TRUE(lowest2->started());
343 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) {
344 http_server_properties_.SetSupportsSpdy(
345 net::HostPortPair("spdyhost", 443), true);
346 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
347 scoped_ptr<TestRequest> low_spdy(
348 NewRequest("https://spdyhost/low", net::LOWEST));
349 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
350 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
351 EXPECT_TRUE(high->started());
352 EXPECT_TRUE(low_spdy->started());
353 EXPECT_TRUE(low->started());
354 EXPECT_FALSE(low2->started());
355 scheduler_.OnWillInsertBody(kChildId, kRouteId);
356 high.reset();
357 EXPECT_TRUE(low2->started());
360 TEST_F(ResourceSchedulerTest, NavigationResetsState) {
361 scheduler_.OnWillInsertBody(kChildId, kRouteId);
362 scheduler_.OnNavigate(kChildId, kRouteId);
363 scoped_ptr<TestRequest> high(NewRequest("http://host/high", 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(high->started());
367 EXPECT_TRUE(low->started());
368 EXPECT_FALSE(low2->started());
371 TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
372 const int route_id = 0; // Indicates a background request.
373 scoped_ptr<TestRequest> request(NewRequestWithRoute("http://host/1",
374 net::LOWEST, route_id));
375 EXPECT_TRUE(request->started());
378 TEST_F(ResourceSchedulerTest, StartMultipleLowRequestsWhenIdle) {
379 scoped_ptr<TestRequest> high1(NewRequest("http://host/high1", net::HIGHEST));
380 scoped_ptr<TestRequest> high2(NewRequest("http://host/high2", net::HIGHEST));
381 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
382 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
383 EXPECT_TRUE(high1->started());
384 EXPECT_TRUE(high2->started());
385 EXPECT_TRUE(low->started());
386 EXPECT_FALSE(low2->started());
387 high1.reset();
388 EXPECT_FALSE(low2->started());
389 high2.reset();
390 EXPECT_TRUE(low2->started());
393 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) {
394 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
395 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST));
397 scoped_ptr<net::URLRequest> url_request(
398 NewURLRequest("http://host/low2", net::LOWEST));
399 scoped_ptr<ResourceThrottle> throttle(
400 scheduler_.ScheduleRequest(kChildId, kRouteId, url_request.get()));
401 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest(
402 throttle.Pass(), url_request.Pass()));
403 low2->Start();
405 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST));
406 low2->set_request_to_cancel(low3.Pass());
407 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST));
409 EXPECT_TRUE(high->started());
410 EXPECT_FALSE(low2->started());
411 high.reset();
412 EXPECT_TRUE(low1->started());
413 EXPECT_TRUE(low2->started());
414 EXPECT_TRUE(low4->started());
417 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
418 // We only load low priority resources if there's a body.
419 scheduler_.OnWillInsertBody(kChildId, kRouteId);
421 // Throw in one high priority request to make sure that's not a factor.
422 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
423 EXPECT_TRUE(high->started());
425 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
426 const int kMaxNumDelayableRequestsPerHost = 6;
427 ScopedVector<TestRequest> lows_singlehost;
428 // Queue up to the per-host limit (we subtract the current high-pri request).
429 for (int i = 0; i < kMaxNumDelayableRequestsPerHost - 1; ++i) {
430 string url = "http://host/low" + base::IntToString(i);
431 lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
432 EXPECT_TRUE(lows_singlehost[i]->started());
435 scoped_ptr<TestRequest> second_last_singlehost(NewRequest("http://host/last",
436 net::LOWEST));
437 scoped_ptr<TestRequest> last_singlehost(NewRequest("http://host/s_last",
438 net::LOWEST));
440 EXPECT_FALSE(second_last_singlehost->started());
441 high.reset();
442 EXPECT_TRUE(second_last_singlehost->started());
443 EXPECT_FALSE(last_singlehost->started());
444 lows_singlehost.erase(lows_singlehost.begin());
445 EXPECT_TRUE(last_singlehost->started());
447 // Queue more requests from different hosts until we reach the total limit.
448 int expected_slots_left =
449 kMaxNumDelayableRequestsPerClient - kMaxNumDelayableRequestsPerHost;
450 EXPECT_GT(expected_slots_left, 0);
451 ScopedVector<TestRequest> lows_differenthosts;
452 for (int i = 0; i < expected_slots_left; ++i) {
453 string url = "http://host" + base::IntToString(i) + "/low";
454 lows_differenthosts.push_back(NewRequest(url.c_str(), net::LOWEST));
455 EXPECT_TRUE(lows_differenthosts[i]->started());
458 scoped_ptr<TestRequest> last_differenthost(NewRequest("http://host_new/last",
459 net::LOWEST));
460 EXPECT_FALSE(last_differenthost->started());
463 TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) {
464 // Dummies to enforce scheduling.
465 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
466 scoped_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
468 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
469 EXPECT_FALSE(request->started());
471 ChangeRequestPriority(request.get(), net::HIGHEST);
472 EXPECT_TRUE(request->started());
475 TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) {
476 // Dummies to enforce scheduling.
477 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
478 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
480 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
481 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
482 EXPECT_FALSE(request->started());
483 EXPECT_FALSE(idle->started());
485 ChangeRequestPriority(request.get(), net::LOWEST);
486 EXPECT_FALSE(request->started());
487 EXPECT_FALSE(idle->started());
489 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
490 ScopedVector<TestRequest> lows;
491 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
492 string url = "http://host/low" + base::IntToString(i);
493 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
496 scheduler_.OnWillInsertBody(kChildId, kRouteId);
497 high.reset();
499 EXPECT_TRUE(request->started());
500 EXPECT_FALSE(idle->started());
503 TEST_F(ResourceSchedulerTest, LowerPriority) {
504 // Dummies to enforce scheduling.
505 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
506 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
508 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
509 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
510 EXPECT_FALSE(request->started());
511 EXPECT_FALSE(idle->started());
513 ChangeRequestPriority(request.get(), net::IDLE);
514 EXPECT_FALSE(request->started());
515 EXPECT_FALSE(idle->started());
517 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
518 // 2 fewer filler requests: 1 for the "low" dummy at the start, and 1 for the
519 // one at the end, which will be tested.
520 const int kNumFillerRequests = kMaxNumDelayableRequestsPerClient - 2;
521 ScopedVector<TestRequest> lows;
522 for (int i = 0; i < kNumFillerRequests; ++i) {
523 string url = "http://host" + base::IntToString(i) + "/low";
524 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
527 scheduler_.OnWillInsertBody(kChildId, kRouteId);
528 high.reset();
530 EXPECT_FALSE(request->started());
531 EXPECT_TRUE(idle->started());
534 TEST_F(ResourceSchedulerTest, ReprioritizedRequestGoesToBackOfQueue) {
535 // Dummies to enforce scheduling.
536 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
537 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
539 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
540 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
541 EXPECT_FALSE(request->started());
542 EXPECT_FALSE(idle->started());
544 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
545 ScopedVector<TestRequest> lows;
546 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
547 string url = "http://host/low" + base::IntToString(i);
548 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
551 ChangeRequestPriority(request.get(), net::IDLE);
552 EXPECT_FALSE(request->started());
553 EXPECT_FALSE(idle->started());
555 ChangeRequestPriority(request.get(), net::LOWEST);
556 EXPECT_FALSE(request->started());
557 EXPECT_FALSE(idle->started());
559 scheduler_.OnWillInsertBody(kChildId, kRouteId);
560 EXPECT_FALSE(request->started());
561 EXPECT_FALSE(idle->started());
564 TEST_F(ResourceSchedulerTest, HigherIntraPriorityGoesToFrontOfQueue) {
565 // Dummies to enforce scheduling.
566 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
567 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
569 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
570 ScopedVector<TestRequest> lows;
571 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
572 string url = "http://host/low" + base::IntToString(i);
573 lows.push_back(NewRequest(url.c_str(), net::IDLE));
576 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
577 EXPECT_FALSE(request->started());
579 ChangeRequestPriority(request.get(), net::IDLE, 1);
580 EXPECT_FALSE(request->started());
582 scheduler_.OnWillInsertBody(kChildId, kRouteId);
583 high.reset();
584 EXPECT_TRUE(request->started());
587 TEST_F(ResourceSchedulerTest, NonHTTPSchedulesImmediately) {
588 // Dummies to enforce scheduling.
589 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
590 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
592 scoped_ptr<TestRequest> request(
593 NewRequest("chrome-extension://req", net::LOWEST));
594 EXPECT_TRUE(request->started());
597 TEST_F(ResourceSchedulerTest, ActiveLoadingSyncSchedulesImmediately) {
598 // TODO(aiolos): remove when throttling and coalescing have both landed
599 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
600 false /* should_coalesce */);
601 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
602 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
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 NewSyncRequest("http://host/req", net::LOWEST));
609 EXPECT_TRUE(request->started());
612 TEST_F(ResourceSchedulerTest, UnthrottledSyncSchedulesImmediately) {
613 // TODO(aiolos): remove when throttling and coalescing have both landed
614 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
615 false /* should_coalesce */);
616 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
617 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
618 scheduler_.GetClientStateForTesting(kBackgroundChildId,
619 kBackgroundRouteId));
620 // Dummies to enforce scheduling.
621 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
622 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
624 scoped_ptr<TestRequest> request(
625 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
626 EXPECT_TRUE(request->started());
629 TEST_F(ResourceSchedulerTest, SpdyProxySchedulesImmediately) {
630 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
631 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
633 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
634 EXPECT_FALSE(request->started());
636 scheduler_.OnReceivedSpdyProxiedHttpResponse(kChildId, kRouteId);
637 EXPECT_TRUE(request->started());
639 scoped_ptr<TestRequest> after(NewRequest("http://host/after", net::IDLE));
640 EXPECT_TRUE(after->started());
643 TEST_F(ResourceSchedulerTest, NewSpdyHostInDelayableRequests) {
644 scheduler_.OnWillInsertBody(kChildId, kRouteId);
645 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
647 scoped_ptr<TestRequest> low1_spdy(
648 NewRequest("http://spdyhost1:8080/low", net::LOWEST));
649 // Cancel a request after we learn the server supports SPDY.
650 ScopedVector<TestRequest> lows;
651 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
652 string url = "http://host" + base::IntToString(i) + "/low";
653 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
655 scoped_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
656 EXPECT_FALSE(low1->started());
657 http_server_properties_.SetSupportsSpdy(
658 net::HostPortPair("spdyhost1", 8080), true);
659 low1_spdy.reset();
660 EXPECT_TRUE(low1->started());
662 low1.reset();
663 scoped_ptr<TestRequest> low2_spdy(
664 NewRequest("http://spdyhost2:8080/low", net::IDLE));
665 // Reprioritize a request after we learn the server supports SPDY.
666 EXPECT_TRUE(low2_spdy->started());
667 http_server_properties_.SetSupportsSpdy(
668 net::HostPortPair("spdyhost2", 8080), true);
669 ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
670 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
671 EXPECT_TRUE(low2->started());
674 TEST_F(ResourceSchedulerTest, ThrottledClientCreation) {
675 // TODO(aiolos): remove when throttling and coalescing have both landed
676 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
677 false /* should_coalesce */);
678 EXPECT_TRUE(scheduler_.should_throttle());
679 scheduler_.OnClientCreated(
680 kBackgroundChildId2, kBackgroundRouteId2, false, false);
682 EXPECT_EQ(ResourceScheduler::THROTTLED,
683 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
684 kBackgroundRouteId2));
685 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
688 TEST_F(ResourceSchedulerTest, ActiveClientThrottleUpdateOnLoadingChange) {
689 // TODO(aiolos): remove when throttling and coalescing have both landed
690 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
691 false /* should_coalesce */);
692 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
693 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
694 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
695 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
696 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
697 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
698 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
699 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
702 TEST_F(ResourceSchedulerTest, CoalesceBackgroundClientOnLoadCompletion) {
703 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
704 true /* should_coalesce */);
705 EXPECT_EQ(ResourceScheduler::THROTTLED,
706 scheduler_.GetClientStateForTesting(kBackgroundChildId,
707 kBackgroundRouteId));
708 scheduler_.OnLoadingStateChanged(
709 kBackgroundChildId, kBackgroundRouteId, true);
710 EXPECT_EQ(ResourceScheduler::THROTTLED,
711 scheduler_.GetClientStateForTesting(kBackgroundChildId,
712 kBackgroundRouteId));
713 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
714 EXPECT_EQ(ResourceScheduler::COALESCED,
715 scheduler_.GetClientStateForTesting(kBackgroundChildId,
716 kBackgroundRouteId));
719 TEST_F(ResourceSchedulerTest, UnthrottleBackgroundClientOnLoadingStarted) {
720 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
721 true /* should_coalesce */);
722 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
723 scheduler_.OnLoadingStateChanged(
724 kBackgroundChildId, kBackgroundRouteId, true);
725 EXPECT_EQ(ResourceScheduler::COALESCED,
726 scheduler_.GetClientStateForTesting(kBackgroundChildId,
727 kBackgroundRouteId));
729 scheduler_.OnLoadingStateChanged(
730 kBackgroundChildId, kBackgroundRouteId, false);
731 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
732 scheduler_.GetClientStateForTesting(kBackgroundChildId,
733 kBackgroundRouteId));
736 TEST_F(ResourceSchedulerTest, OneRequestPerThrottledClient) {
737 // TODO(aiolos): remove when throttling and coalescing have both landed
738 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
739 false /* should_coalesce */);
740 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
741 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
742 EXPECT_EQ(ResourceScheduler::THROTTLED,
743 scheduler_.GetClientStateForTesting(kBackgroundChildId,
744 kBackgroundRouteId));
745 scoped_ptr<TestRequest> high(
746 NewBackgroundRequest("http://host/high", net::HIGHEST));
747 scoped_ptr<TestRequest> request(
748 NewBackgroundRequest("http://host/req", net::IDLE));
750 EXPECT_TRUE(high->started());
751 EXPECT_FALSE(request->started());
754 TEST_F(ResourceSchedulerTest, UnthrottleNewlyVisibleClient) {
755 // TODO(aiolos): remove when throttling and coalescing have both landed
756 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
757 false /* should_coalesce */);
758 EXPECT_EQ(ResourceScheduler::THROTTLED,
759 scheduler_.GetClientStateForTesting(kBackgroundChildId,
760 kBackgroundRouteId));
761 scoped_ptr<TestRequest> high(
762 NewBackgroundRequest("http://host/high", net::HIGHEST));
763 scoped_ptr<TestRequest> request(
764 NewBackgroundRequest("http://host/req", net::IDLE));
765 EXPECT_FALSE(request->started());
767 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
768 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
769 scheduler_.GetClientStateForTesting(kBackgroundChildId,
770 kBackgroundRouteId));
771 EXPECT_TRUE(request->started());
774 TEST_F(ResourceSchedulerTest, UnthrottleNewlyAudibleClient) {
775 // TODO(aiolos): remove when throttling and coalescing have both landed
776 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
777 false /* should_coalesce */);
778 EXPECT_EQ(ResourceScheduler::THROTTLED,
779 scheduler_.GetClientStateForTesting(kBackgroundChildId,
780 kBackgroundRouteId));
781 scoped_ptr<TestRequest> high(
782 NewBackgroundRequest("http://host/high", net::HIGHEST));
783 scoped_ptr<TestRequest> request(
784 NewBackgroundRequest("http://host/req", net::IDLE));
785 EXPECT_FALSE(request->started());
787 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
788 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
789 scheduler_.GetClientStateForTesting(kBackgroundChildId,
790 kBackgroundRouteId));
791 EXPECT_TRUE(request->started());
794 TEST_F(ResourceSchedulerTest, VisibleClientStillUnthrottledOnAudabilityChange) {
795 // TODO(aiolos): remove when throttling and coalescing have both landed
796 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
797 false /* should_coalesce */);
798 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
799 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
800 EXPECT_EQ(ResourceScheduler::THROTTLED,
801 scheduler_.GetClientStateForTesting(kBackgroundChildId,
802 kBackgroundRouteId));
804 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
805 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
806 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
807 EXPECT_EQ(ResourceScheduler::THROTTLED,
808 scheduler_.GetClientStateForTesting(kBackgroundChildId,
809 kBackgroundRouteId));
811 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
812 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
813 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
814 EXPECT_EQ(ResourceScheduler::THROTTLED,
815 scheduler_.GetClientStateForTesting(kBackgroundChildId,
816 kBackgroundRouteId));
819 TEST_F(ResourceSchedulerTest, AudibleClientStillUnthrottledOnVisabilityChange) {
820 // TODO(aiolos): remove when throttling and coalescing have both landed
821 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
822 false /* should_coalesce */);
823 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
824 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
825 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
826 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
827 EXPECT_EQ(ResourceScheduler::THROTTLED,
828 scheduler_.GetClientStateForTesting(kBackgroundChildId,
829 kBackgroundRouteId));
831 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
832 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
833 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
834 EXPECT_EQ(ResourceScheduler::THROTTLED,
835 scheduler_.GetClientStateForTesting(kBackgroundChildId,
836 kBackgroundRouteId));
838 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
839 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
840 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
841 EXPECT_EQ(ResourceScheduler::THROTTLED,
842 scheduler_.GetClientStateForTesting(kBackgroundChildId,
843 kBackgroundRouteId));
846 TEST_F(ResourceSchedulerTest, ThrottledClientStartsNextHighestPriorityRequest) {
847 // TODO(aiolos): remove when throttling and coalescing have both landed
848 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
849 false /* should_coalesce */);
850 scoped_ptr<TestRequest> request(
851 NewBackgroundRequest("http://host/req", net::IDLE));
852 // Lower priority request started first to test request prioritizaton.
853 scoped_ptr<TestRequest> low(
854 NewBackgroundRequest("http://host/high", net::IDLE));
855 scoped_ptr<TestRequest> high(
856 NewBackgroundRequest("http://host/high", net::HIGHEST));
858 EXPECT_FALSE(low->started());
859 EXPECT_FALSE(high->started());
861 // request->CancelRequest();
862 request->Cancel();
863 EXPECT_TRUE(high->started());
864 EXPECT_FALSE(low->started());
867 TEST_F(ResourceSchedulerTest, ThrottledSpdyProxySchedulesImmediately) {
868 // TODO(aiolos): remove when throttling and coalescing have both landed
869 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
870 false /* should_coalesce */);
871 EXPECT_EQ(ResourceScheduler::THROTTLED,
872 scheduler_.GetClientStateForTesting(kBackgroundChildId,
873 kBackgroundRouteId));
874 scoped_ptr<TestRequest> high(
875 NewBackgroundRequest("http://host/high", net::HIGHEST));
876 scoped_ptr<TestRequest> request(
877 NewBackgroundRequest("http://host/req", net::IDLE));
879 EXPECT_FALSE(request->started());
881 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
882 kBackgroundRouteId);
883 EXPECT_TRUE(request->started());
885 scoped_ptr<TestRequest> after(
886 NewBackgroundRequest("http://host/after", net::IDLE));
887 EXPECT_TRUE(after->started());
890 TEST_F(ResourceSchedulerTest, CoalescedClientIssuesNoRequests) {
891 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
892 true /* should_coalesce */);
893 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
894 scheduler_.OnLoadingStateChanged(
895 kBackgroundChildId, kBackgroundRouteId, true);
896 EXPECT_EQ(ResourceScheduler::COALESCED,
897 scheduler_.GetClientStateForTesting(kBackgroundChildId,
898 kBackgroundRouteId));
899 scoped_ptr<TestRequest> high(
900 NewBackgroundRequest("http://host/high", net::HIGHEST));
901 scoped_ptr<TestRequest> request(
902 NewBackgroundRequest("http://host/req", net::IDLE));
904 EXPECT_FALSE(high->started());
905 EXPECT_FALSE(request->started());
907 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
908 kBackgroundRouteId);
909 EXPECT_FALSE(high->started());
911 scoped_ptr<TestRequest> after(
912 NewBackgroundRequest("http://host/after", net::HIGHEST));
913 EXPECT_FALSE(after->started());
916 TEST_F(ResourceSchedulerTest, CoalescedSpdyProxyWaits) {
917 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
918 true /* should_coalesce */);
919 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
920 scheduler_.OnLoadingStateChanged(
921 kBackgroundChildId, kBackgroundRouteId, true);
922 EXPECT_EQ(ResourceScheduler::COALESCED,
923 scheduler_.GetClientStateForTesting(kBackgroundChildId,
924 kBackgroundRouteId));
925 scoped_ptr<TestRequest> high(
926 NewBackgroundRequest("http://host/high", net::HIGHEST));
927 scoped_ptr<TestRequest> request(
928 NewBackgroundRequest("http://host/req", net::IDLE));
930 EXPECT_FALSE(request->started());
932 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
933 kBackgroundRouteId);
934 EXPECT_FALSE(request->started());
936 scoped_ptr<TestRequest> after(
937 NewBackgroundRequest("http://host/after", net::IDLE));
938 EXPECT_FALSE(after->started());
941 TEST_F(ResourceSchedulerTest, ThrottledNonHTTPSchedulesImmediately) {
942 // TODO(aiolos): remove when throttling and coalescing have both landed
943 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
944 false /* should_coalesce */);
945 // Dummies to enforce scheduling.
946 scoped_ptr<TestRequest> high(
947 NewBackgroundRequest("http://host/high", net::HIGHEST));
948 scoped_ptr<TestRequest> low(
949 NewBackgroundRequest("http://host/low", net::LOWEST));
951 scoped_ptr<TestRequest> request(
952 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
953 EXPECT_TRUE(request->started());
954 EXPECT_FALSE(low->started());
957 TEST_F(ResourceSchedulerTest, CoalescedNonHTTPSchedulesImmediately) {
958 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
959 true /* should_coalesce */);
960 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
961 scheduler_.OnLoadingStateChanged(
962 kBackgroundChildId, kBackgroundRouteId, true);
963 EXPECT_EQ(ResourceScheduler::COALESCED,
964 scheduler_.GetClientStateForTesting(kBackgroundChildId,
965 kBackgroundRouteId));
966 // Dummies to enforce scheduling.
967 scoped_ptr<TestRequest> high(
968 NewBackgroundRequest("http://host/high", net::HIGHEST));
969 scoped_ptr<TestRequest> low(
970 NewBackgroundRequest("http://host/low", net::LOWEST));
972 scoped_ptr<TestRequest> request(
973 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
974 EXPECT_TRUE(request->started());
975 EXPECT_FALSE(low->started());
978 TEST_F(ResourceSchedulerTest, ThrottledSyncSchedulesImmediately) {
979 // TODO(aiolos): remove when throttling and coalescing have both landed
980 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
981 false /* should_coalesce */);
982 // Dummies to enforce scheduling.
983 scoped_ptr<TestRequest> high(
984 NewBackgroundRequest("http://host/high", net::HIGHEST));
985 scoped_ptr<TestRequest> low(
986 NewBackgroundRequest("http://host/low", net::LOWEST));
988 scoped_ptr<TestRequest> request(
989 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
990 EXPECT_TRUE(request->started());
991 EXPECT_FALSE(low->started());
994 TEST_F(ResourceSchedulerTest, CoalescedSyncSchedulesImmediately) {
995 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
996 true /* should_coalesce */);
997 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
998 scheduler_.OnLoadingStateChanged(
999 kBackgroundChildId, kBackgroundRouteId, true);
1000 EXPECT_EQ(ResourceScheduler::COALESCED,
1001 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1002 kBackgroundRouteId));
1003 // Dummies to enforce scheduling.
1004 scoped_ptr<TestRequest> high(
1005 NewBackgroundRequest("http://host/high", net::HIGHEST));
1006 scoped_ptr<TestRequest> low(
1007 NewBackgroundRequest("http://host/low", net::LOWEST));
1009 scoped_ptr<TestRequest> request(
1010 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
1011 EXPECT_TRUE(request->started());
1012 EXPECT_FALSE(low->started());
1013 EXPECT_FALSE(high->started());
1016 TEST_F(ResourceSchedulerTest, AllBackgroundClientsUnthrottle) {
1017 // TODO(aiolos): remove when throttling and coalescing have both landed
1018 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1019 false /* should_coalesce */);
1020 EXPECT_EQ(ResourceScheduler::THROTTLED,
1021 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1022 kBackgroundRouteId));
1023 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1024 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1025 EXPECT_FALSE(scheduler_.active_clients_loaded());
1027 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1028 EXPECT_TRUE(scheduler_.active_clients_loaded());
1029 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1030 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1031 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1032 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1033 kBackgroundRouteId));
1035 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1036 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1037 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1038 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1039 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1040 kBackgroundRouteId));
1042 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1043 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1044 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1045 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1046 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1047 kBackgroundRouteId));
1049 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1050 scheduler_.OnLoadingStateChanged(
1051 kBackgroundChildId, kBackgroundRouteId, true);
1052 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1053 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1054 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1055 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1056 kBackgroundRouteId));
1059 TEST_F(ResourceSchedulerTest,
1060 UnloadedClientVisibilityChangedCorrectlyUnthrottles) {
1061 // TODO(aiolos): remove when throttling and coalescing have both landed
1062 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1063 false /* should_coalesce */);
1064 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1065 scheduler_.OnClientCreated(
1066 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1067 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1068 scheduler_.OnLoadingStateChanged(
1069 kBackgroundChildId2, kBackgroundRouteId2, true);
1071 // 1 visible, 3 hidden
1072 EXPECT_FALSE(scheduler_.active_clients_loaded());
1073 EXPECT_EQ(ResourceScheduler::THROTTLED,
1074 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1075 kBackgroundRouteId));
1076 EXPECT_EQ(ResourceScheduler::THROTTLED,
1077 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1078 kBackgroundRouteId2));
1079 EXPECT_EQ(ResourceScheduler::THROTTLED,
1080 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1081 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1082 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1084 // 2 visible, 2 hidden
1085 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1086 EXPECT_FALSE(scheduler_.active_clients_loaded());
1087 EXPECT_EQ(ResourceScheduler::THROTTLED,
1088 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1089 kBackgroundRouteId));
1090 EXPECT_EQ(ResourceScheduler::THROTTLED,
1091 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1092 kBackgroundRouteId2));
1093 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1094 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1095 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1096 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1098 // 1 visible, 3 hidden
1099 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1100 EXPECT_FALSE(scheduler_.active_clients_loaded());
1101 EXPECT_EQ(ResourceScheduler::THROTTLED,
1102 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1103 kBackgroundRouteId));
1104 EXPECT_EQ(ResourceScheduler::THROTTLED,
1105 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1106 kBackgroundRouteId2));
1107 EXPECT_EQ(ResourceScheduler::THROTTLED,
1108 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1109 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1110 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1112 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1113 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1116 TEST_F(ResourceSchedulerTest,
1117 UnloadedClientAudibilityChangedCorrectlyUnthrottles) {
1118 // TODO(aiolos): remove when throttling and coalescing have both landed
1119 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1120 false /* should_coalesce */);
1121 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1122 scheduler_.OnClientCreated(
1123 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1124 scheduler_.OnLoadingStateChanged(
1125 kBackgroundChildId2, kBackgroundRouteId2, true);
1126 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1127 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1129 // 1 audible, 3 hidden
1130 EXPECT_FALSE(scheduler_.active_clients_loaded());
1131 EXPECT_EQ(ResourceScheduler::THROTTLED,
1132 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1133 kBackgroundRouteId));
1134 EXPECT_EQ(ResourceScheduler::THROTTLED,
1135 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1136 kBackgroundRouteId2));
1137 EXPECT_EQ(ResourceScheduler::THROTTLED,
1138 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1139 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1140 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1142 // 2 audible, 2 hidden
1143 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1144 EXPECT_FALSE(scheduler_.active_clients_loaded());
1145 EXPECT_EQ(ResourceScheduler::THROTTLED,
1146 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1147 kBackgroundRouteId));
1148 EXPECT_EQ(ResourceScheduler::THROTTLED,
1149 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1150 kBackgroundRouteId2));
1151 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1152 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1153 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1154 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1156 // 1 audible, 3 hidden
1157 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1158 EXPECT_FALSE(scheduler_.active_clients_loaded());
1159 EXPECT_EQ(ResourceScheduler::THROTTLED,
1160 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1161 kBackgroundRouteId));
1162 EXPECT_EQ(ResourceScheduler::THROTTLED,
1163 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1164 kBackgroundRouteId2));
1165 EXPECT_EQ(ResourceScheduler::THROTTLED,
1166 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1167 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1168 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1170 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1171 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1174 TEST_F(ResourceSchedulerTest,
1175 LoadedClientVisibilityChangedCorrectlyUnthrottles) {
1176 // TODO(aiolos): remove when throttling and coalescing have both landed
1177 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1178 false /* should_coalesce */);
1179 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1180 scheduler_.OnClientCreated(
1181 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1182 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1183 scheduler_.OnLoadingStateChanged(
1184 kBackgroundChildId2, kBackgroundRouteId2, true);
1185 // 1 visible, 3 hidden
1186 EXPECT_FALSE(scheduler_.active_clients_loaded());
1187 EXPECT_EQ(ResourceScheduler::THROTTLED,
1188 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1189 kBackgroundRouteId));
1190 EXPECT_EQ(ResourceScheduler::THROTTLED,
1191 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1192 kBackgroundRouteId2));
1193 EXPECT_EQ(ResourceScheduler::THROTTLED,
1194 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1195 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1196 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1198 // 2 visible, 2 hidden
1199 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1200 EXPECT_FALSE(scheduler_.active_clients_loaded());
1201 EXPECT_EQ(ResourceScheduler::THROTTLED,
1202 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1203 kBackgroundRouteId));
1204 EXPECT_EQ(ResourceScheduler::THROTTLED,
1205 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1206 kBackgroundRouteId2));
1207 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1208 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1209 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1210 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1212 // 1 visible, 3 hidden
1213 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1214 EXPECT_FALSE(scheduler_.active_clients_loaded());
1215 EXPECT_EQ(ResourceScheduler::THROTTLED,
1216 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1217 kBackgroundRouteId));
1218 EXPECT_EQ(ResourceScheduler::THROTTLED,
1219 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1220 kBackgroundRouteId2));
1221 EXPECT_EQ(ResourceScheduler::THROTTLED,
1222 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1223 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1224 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1226 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1227 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1230 TEST_F(ResourceSchedulerTest,
1231 LoadedClientAudibilityChangedCorrectlyUnthrottles) {
1232 // TODO(aiolos): remove when throttling and coalescing have both landed
1233 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1234 false /* should_coalesce */);
1235 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1236 scheduler_.OnClientCreated(
1237 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1238 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1239 scheduler_.OnLoadingStateChanged(
1240 kBackgroundChildId2, kBackgroundRouteId2, true);
1241 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1242 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1243 // 1 audible, 3 hidden
1244 EXPECT_FALSE(scheduler_.active_clients_loaded());
1245 EXPECT_EQ(ResourceScheduler::THROTTLED,
1246 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1247 kBackgroundRouteId));
1248 EXPECT_EQ(ResourceScheduler::THROTTLED,
1249 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1250 kBackgroundRouteId2));
1251 EXPECT_EQ(ResourceScheduler::THROTTLED,
1252 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1253 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1254 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1256 // 2 audible, 2 hidden
1257 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1258 EXPECT_FALSE(scheduler_.active_clients_loaded());
1259 EXPECT_EQ(ResourceScheduler::THROTTLED,
1260 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1261 kBackgroundRouteId));
1262 EXPECT_EQ(ResourceScheduler::THROTTLED,
1263 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1264 kBackgroundRouteId2));
1265 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1266 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1267 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1268 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1270 // 1 audible, 3 hidden
1271 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1272 EXPECT_FALSE(scheduler_.active_clients_loaded());
1273 EXPECT_EQ(ResourceScheduler::THROTTLED,
1274 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1275 kBackgroundRouteId));
1276 EXPECT_EQ(ResourceScheduler::THROTTLED,
1277 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1278 kBackgroundRouteId2));
1279 EXPECT_EQ(ResourceScheduler::THROTTLED,
1280 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1281 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1282 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1284 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1285 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1288 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesHiddenCorrectlyUnthrottles) {
1289 // TODO(aiolos): remove when throttling and coalescing have both landed
1290 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1291 false /* should_coalesce */);
1292 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1293 scheduler_.OnClientCreated(
1294 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1295 scheduler_.OnLoadingStateChanged(
1296 kBackgroundChildId2, kBackgroundRouteId2, true);
1298 // 2 visible, 2 hidden
1299 EXPECT_FALSE(scheduler_.active_clients_loaded());
1300 EXPECT_EQ(ResourceScheduler::THROTTLED,
1301 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1302 kBackgroundRouteId));
1303 EXPECT_EQ(ResourceScheduler::THROTTLED,
1304 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1305 kBackgroundRouteId2));
1306 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1307 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1308 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1309 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1311 // 1 visible, 3 hidden
1312 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1313 EXPECT_FALSE(scheduler_.active_clients_loaded());
1314 EXPECT_EQ(ResourceScheduler::THROTTLED,
1315 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1316 kBackgroundRouteId));
1317 EXPECT_EQ(ResourceScheduler::THROTTLED,
1318 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1319 kBackgroundRouteId2));
1320 EXPECT_EQ(ResourceScheduler::THROTTLED,
1321 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1322 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1323 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1325 // 0 visible, 4 hidden
1326 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1327 EXPECT_TRUE(scheduler_.active_clients_loaded());
1328 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1329 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1330 kBackgroundRouteId));
1331 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1332 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1333 kBackgroundRouteId2));
1334 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1335 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1336 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1337 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1339 // 1 visible, 3 hidden
1340 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
1341 EXPECT_FALSE(scheduler_.active_clients_loaded());
1342 EXPECT_EQ(ResourceScheduler::THROTTLED,
1343 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1344 kBackgroundRouteId));
1345 EXPECT_EQ(ResourceScheduler::THROTTLED,
1346 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1347 kBackgroundRouteId2));
1348 EXPECT_EQ(ResourceScheduler::THROTTLED,
1349 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1350 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1351 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1353 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1354 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1357 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesSilentCorrectlyUnthrottles) {
1358 // TODO(aiolos): remove when throttling and coalescing have both landed
1359 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1360 false /* should_coalesce */);
1361 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, true);
1362 scheduler_.OnClientCreated(
1363 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1364 scheduler_.OnLoadingStateChanged(
1365 kBackgroundChildId2, kBackgroundRouteId2, true);
1366 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1367 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1368 // 2 audible, 2 hidden
1369 EXPECT_FALSE(scheduler_.active_clients_loaded());
1370 EXPECT_EQ(ResourceScheduler::THROTTLED,
1371 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1372 kBackgroundRouteId));
1373 EXPECT_EQ(ResourceScheduler::THROTTLED,
1374 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1375 kBackgroundRouteId2));
1376 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1377 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1378 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1379 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1381 // 1 audible, 3 hidden
1382 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1383 EXPECT_FALSE(scheduler_.active_clients_loaded());
1384 EXPECT_EQ(ResourceScheduler::THROTTLED,
1385 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1386 kBackgroundRouteId));
1387 EXPECT_EQ(ResourceScheduler::THROTTLED,
1388 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1389 kBackgroundRouteId2));
1390 EXPECT_EQ(ResourceScheduler::THROTTLED,
1391 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1392 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1393 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1395 // 0 audible, 4 hidden
1396 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1397 EXPECT_TRUE(scheduler_.active_clients_loaded());
1398 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1399 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1400 kBackgroundRouteId));
1401 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1402 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1403 kBackgroundRouteId2));
1404 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1405 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1406 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1407 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1409 // 1 audible, 3 hidden
1410 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1411 EXPECT_FALSE(scheduler_.active_clients_loaded());
1412 EXPECT_EQ(ResourceScheduler::THROTTLED,
1413 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1414 kBackgroundRouteId));
1415 EXPECT_EQ(ResourceScheduler::THROTTLED,
1416 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1417 kBackgroundRouteId2));
1418 EXPECT_EQ(ResourceScheduler::THROTTLED,
1419 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1420 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1421 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1423 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1424 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1427 TEST_F(ResourceSchedulerTest, LoadedClientBecomesHiddenCorrectlyThrottles) {
1428 // TODO(aiolos): remove when throttling and coalescing have both landed
1429 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1430 false /* should_coalesce */);
1431 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1432 scheduler_.OnClientCreated(
1433 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1434 scheduler_.OnLoadingStateChanged(
1435 kBackgroundChildId2, kBackgroundRouteId2, true);
1436 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1437 // 2 visible, 2 hidden
1438 EXPECT_FALSE(scheduler_.active_clients_loaded());
1439 EXPECT_EQ(ResourceScheduler::THROTTLED,
1440 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1441 kBackgroundRouteId));
1442 EXPECT_EQ(ResourceScheduler::THROTTLED,
1443 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1444 kBackgroundRouteId2));
1445 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1446 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1447 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1448 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1450 // 1 visible, 3 hidden
1451 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1452 EXPECT_FALSE(scheduler_.active_clients_loaded());
1453 EXPECT_EQ(ResourceScheduler::THROTTLED,
1454 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1455 kBackgroundRouteId));
1456 EXPECT_EQ(ResourceScheduler::THROTTLED,
1457 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1458 kBackgroundRouteId2));
1459 EXPECT_EQ(ResourceScheduler::THROTTLED,
1460 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1461 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1462 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1464 // 0 visible, 4 hidden
1465 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1466 EXPECT_TRUE(scheduler_.active_clients_loaded());
1467 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1468 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1469 kBackgroundRouteId));
1470 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1471 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1472 kBackgroundRouteId2));
1473 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1474 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1475 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1476 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1478 // 1 visible, 3 hidden
1479 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1480 EXPECT_TRUE(scheduler_.active_clients_loaded());
1481 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1482 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1483 kBackgroundRouteId));
1484 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1485 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1486 kBackgroundRouteId2));
1487 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1488 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1489 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1490 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1492 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1493 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1496 TEST_F(ResourceSchedulerTest, LoadedClientBecomesSilentCorrectlyThrottles) {
1497 // TODO(aiolos): remove when throttling and coalescing have both landed
1498 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1499 false /* should_coalesce */);
1500 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, true);
1501 scheduler_.OnClientCreated(
1502 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1503 scheduler_.OnLoadingStateChanged(
1504 kBackgroundChildId2, kBackgroundRouteId2, true);
1505 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1506 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1507 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1508 // 2 audible, 2 hidden
1509 EXPECT_FALSE(scheduler_.active_clients_loaded());
1510 EXPECT_EQ(ResourceScheduler::THROTTLED,
1511 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1512 kBackgroundRouteId));
1513 EXPECT_EQ(ResourceScheduler::THROTTLED,
1514 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1515 kBackgroundRouteId2));
1516 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1517 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1518 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1519 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1521 // 1 audible, 3 hidden
1522 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1523 EXPECT_FALSE(scheduler_.active_clients_loaded());
1524 EXPECT_EQ(ResourceScheduler::THROTTLED,
1525 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1526 kBackgroundRouteId));
1527 EXPECT_EQ(ResourceScheduler::THROTTLED,
1528 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1529 kBackgroundRouteId2));
1530 EXPECT_EQ(ResourceScheduler::THROTTLED,
1531 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1532 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1533 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1535 // 0 audible, 4 hidden
1536 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1537 EXPECT_TRUE(scheduler_.active_clients_loaded());
1538 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1539 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1540 kBackgroundRouteId));
1541 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1542 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1543 kBackgroundRouteId2));
1544 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1545 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1546 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1547 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1549 // 1 audible, 3 hidden
1550 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1551 EXPECT_TRUE(scheduler_.active_clients_loaded());
1552 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1553 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1554 kBackgroundRouteId));
1555 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1556 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1557 kBackgroundRouteId2));
1558 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1559 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1560 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1561 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1563 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1564 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1567 TEST_F(ResourceSchedulerTest, HiddenLoadedChangesCorrectlyStayThrottled) {
1568 // TODO(aiolos): remove when throttling and coalescing have both landed
1569 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1570 false /* should_coalesce */);
1571 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1572 scheduler_.OnClientCreated(
1573 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1575 // 1 visible and 2 hidden loading, 1 visible loaded
1576 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, 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 and 1 hidden loading, 1 visible and 1 hidden loaded
1590 scheduler_.OnLoadingStateChanged(
1591 kBackgroundChildId2, kBackgroundRouteId2, 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 loading, 1 visible and 2 hidden loaded
1605 scheduler_.OnLoadingStateChanged(
1606 kBackgroundChildId, kBackgroundRouteId, 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 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1620 scheduler_.OnLoadingStateChanged(
1621 kBackgroundChildId2, kBackgroundRouteId2, true);
1622 EXPECT_FALSE(scheduler_.active_clients_loaded());
1623 EXPECT_EQ(ResourceScheduler::THROTTLED,
1624 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1625 kBackgroundRouteId));
1626 EXPECT_EQ(ResourceScheduler::THROTTLED,
1627 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1628 kBackgroundRouteId2));
1629 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1630 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1631 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1632 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1634 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1635 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1638 TEST_F(ResourceSchedulerTest, PartialVisibleClientLoadedDoesNotUnthrottle) {
1639 // TODO(aiolos): remove when throttling and coalescing have both landed
1640 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1641 false /* should_coalesce */);
1642 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1643 scheduler_.OnClientCreated(
1644 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1646 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1647 scheduler_.OnLoadingStateChanged(
1648 kBackgroundChildId2, kBackgroundRouteId2, true);
1649 EXPECT_FALSE(scheduler_.active_clients_loaded());
1650 EXPECT_EQ(ResourceScheduler::THROTTLED,
1651 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1652 kBackgroundRouteId));
1653 EXPECT_EQ(ResourceScheduler::THROTTLED,
1654 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1655 kBackgroundRouteId2));
1656 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1657 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1658 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1659 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1661 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1662 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1663 EXPECT_FALSE(scheduler_.active_clients_loaded());
1664 EXPECT_EQ(ResourceScheduler::THROTTLED,
1665 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1666 kBackgroundRouteId));
1667 EXPECT_EQ(ResourceScheduler::THROTTLED,
1668 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1669 kBackgroundRouteId2));
1670 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1671 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1672 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1673 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1675 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1676 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1677 EXPECT_FALSE(scheduler_.active_clients_loaded());
1678 EXPECT_EQ(ResourceScheduler::THROTTLED,
1679 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1680 kBackgroundRouteId));
1681 EXPECT_EQ(ResourceScheduler::THROTTLED,
1682 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1683 kBackgroundRouteId2));
1684 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1685 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1686 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1687 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1689 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1690 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1693 TEST_F(ResourceSchedulerTest, FullVisibleLoadedCorrectlyUnthrottle) {
1694 // TODO(aiolos): remove when throttling and coalescing have both landed
1695 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1696 false /* should_coalesce */);
1697 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1698 scheduler_.OnClientCreated(
1699 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1701 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1702 scheduler_.OnLoadingStateChanged(
1703 kBackgroundChildId2, kBackgroundRouteId2, true);
1704 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1705 EXPECT_FALSE(scheduler_.active_clients_loaded());
1706 EXPECT_EQ(ResourceScheduler::THROTTLED,
1707 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1708 kBackgroundRouteId));
1709 EXPECT_EQ(ResourceScheduler::THROTTLED,
1710 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1711 kBackgroundRouteId2));
1712 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1713 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1714 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1715 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1717 scoped_ptr<TestRequest> high(
1718 NewBackgroundRequest("http://host/high", net::HIGHEST));
1719 scoped_ptr<TestRequest> low(
1720 NewBackgroundRequest("http://host/low", net::LOWEST));
1722 EXPECT_TRUE(high->started());
1723 EXPECT_FALSE(low->started());
1725 // 2 visible loaded, 1 hidden loading, 1 hidden loaded
1726 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1727 EXPECT_TRUE(scheduler_.active_clients_loaded());
1728 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1729 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1730 kBackgroundRouteId));
1731 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1732 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1733 kBackgroundRouteId2));
1734 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1735 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1736 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1737 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1738 // kBackgroundClientId unthrottling should unthrottle it's request.
1739 EXPECT_TRUE(low->started());
1741 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1742 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1743 EXPECT_FALSE(scheduler_.active_clients_loaded());
1744 EXPECT_EQ(ResourceScheduler::THROTTLED,
1745 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1746 kBackgroundRouteId));
1747 EXPECT_EQ(ResourceScheduler::THROTTLED,
1748 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1749 kBackgroundRouteId2));
1750 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1751 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1752 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1753 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1755 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1756 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1759 TEST_F(ResourceSchedulerTest,
1760 ActiveAndLoadingClientDeletedCorrectlyUnthrottle) {
1761 // TODO(aiolos): remove when throttling and coalescing have both landed
1762 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1763 false /* should_coalesce */);
1764 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1765 scheduler_.OnClientCreated(
1766 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1768 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1769 scheduler_.OnLoadingStateChanged(
1770 kBackgroundChildId2, kBackgroundRouteId2, true);
1771 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1772 EXPECT_FALSE(scheduler_.active_clients_loaded());
1773 EXPECT_EQ(ResourceScheduler::THROTTLED,
1774 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1775 kBackgroundRouteId));
1776 EXPECT_EQ(ResourceScheduler::THROTTLED,
1777 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1778 kBackgroundRouteId2));
1779 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1780 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1781 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1782 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1784 // 1 visible loaded, 1 hidden loading, 1 hidden loaded
1785 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1786 EXPECT_TRUE(scheduler_.active_clients_loaded());
1787 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1788 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1789 kBackgroundRouteId));
1790 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1791 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1792 kBackgroundRouteId2));
1793 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1794 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1796 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1797 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1798 EXPECT_FALSE(scheduler_.active_clients_loaded());
1799 EXPECT_EQ(ResourceScheduler::THROTTLED,
1800 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1801 kBackgroundRouteId));
1802 EXPECT_EQ(ResourceScheduler::THROTTLED,
1803 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1804 kBackgroundRouteId2));
1805 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1806 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1808 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1811 TEST_F(ResourceSchedulerTest, CoalescedClientCreationStartsTimer) {
1812 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1813 true /* should_coalesce */);
1814 EXPECT_FALSE(mock_timer_->IsRunning());
1815 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1816 EXPECT_FALSE(mock_timer_->IsRunning());
1817 scheduler_.OnLoadingStateChanged(
1818 kBackgroundChildId, kBackgroundRouteId, true);
1819 EXPECT_EQ(ResourceScheduler::COALESCED,
1820 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1821 kBackgroundRouteId));
1822 EXPECT_TRUE(mock_timer_->IsRunning());
1825 TEST_F(ResourceSchedulerTest, ActiveLoadingClientLoadedAndHiddenStartsTimer) {
1826 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1827 true /* should_coalesce */);
1828 EXPECT_FALSE(mock_timer_->IsRunning());
1829 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1830 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1831 EXPECT_EQ(ResourceScheduler::THROTTLED,
1832 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1833 kBackgroundRouteId));
1834 EXPECT_FALSE(mock_timer_->IsRunning());
1836 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1837 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1838 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1839 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1840 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1841 kBackgroundRouteId));
1842 EXPECT_FALSE(mock_timer_->IsRunning());
1844 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1845 EXPECT_EQ(ResourceScheduler::COALESCED,
1846 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1847 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1848 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1849 kBackgroundRouteId));
1850 EXPECT_TRUE(mock_timer_->IsRunning());
1853 TEST_F(ResourceSchedulerTest, ActiveLoadingClientHiddenAndLoadedStartsTimer) {
1854 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1855 true /* should_coalesce */);
1856 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1857 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1858 EXPECT_EQ(ResourceScheduler::THROTTLED,
1859 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1860 kBackgroundRouteId));
1861 EXPECT_FALSE(mock_timer_->IsRunning());
1863 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1864 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1865 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1866 EXPECT_FALSE(mock_timer_->IsRunning());
1868 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1869 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1870 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1871 kBackgroundRouteId));
1872 EXPECT_EQ(ResourceScheduler::COALESCED,
1873 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1874 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1875 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1876 kBackgroundRouteId));
1877 EXPECT_TRUE(mock_timer_->IsRunning());
1880 TEST_F(ResourceSchedulerTest, CoalescedClientBecomesAudibleStopsTimer) {
1881 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1882 true /* should_coalesce */);
1883 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1884 EXPECT_FALSE(mock_timer_->IsRunning());
1885 scheduler_.OnLoadingStateChanged(
1886 kBackgroundChildId, kBackgroundRouteId, true);
1887 EXPECT_EQ(ResourceScheduler::COALESCED,
1888 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1889 kBackgroundRouteId));
1890 EXPECT_TRUE(mock_timer_->IsRunning());
1892 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1893 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1894 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1895 kBackgroundRouteId));
1896 EXPECT_FALSE(mock_timer_->IsRunning());
1899 TEST_F(ResourceSchedulerTest, LastCoalescedClientDeletionStopsTimer) {
1900 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1901 true /* should_coalesce */);
1902 scheduler_.OnClientCreated(
1903 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1904 EXPECT_FALSE(mock_timer_->IsRunning());
1905 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1906 EXPECT_FALSE(mock_timer_->IsRunning());
1907 scheduler_.OnLoadingStateChanged(
1908 kBackgroundChildId, kBackgroundRouteId, true);
1909 EXPECT_EQ(ResourceScheduler::COALESCED,
1910 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1911 kBackgroundRouteId));
1912 scheduler_.OnLoadingStateChanged(
1913 kBackgroundChildId2, kBackgroundRouteId2, true);
1914 EXPECT_EQ(ResourceScheduler::COALESCED,
1915 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1916 kBackgroundRouteId2));
1917 EXPECT_TRUE(mock_timer_->IsRunning());
1919 scheduler_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
1920 EXPECT_TRUE(mock_timer_->IsRunning());
1922 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1923 EXPECT_FALSE(mock_timer_->IsRunning());
1925 // To avoid errors on test tear down.
1926 scheduler_.OnClientCreated(
1927 kBackgroundChildId, kBackgroundRouteId, false, false);
1930 TEST_F(ResourceSchedulerTest, LastCoalescedClientStartsLoadingStopsTimer) {
1931 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1932 true /* should_coalesce */);
1933 scheduler_.OnClientCreated(
1934 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1935 EXPECT_FALSE(mock_timer_->IsRunning());
1936 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1937 EXPECT_FALSE(mock_timer_->IsRunning());
1938 scheduler_.OnLoadingStateChanged(
1939 kBackgroundChildId, kBackgroundRouteId, true);
1940 EXPECT_EQ(ResourceScheduler::COALESCED,
1941 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1942 kBackgroundRouteId));
1943 scheduler_.OnLoadingStateChanged(
1944 kBackgroundChildId2, kBackgroundRouteId2, true);
1945 EXPECT_EQ(ResourceScheduler::COALESCED,
1946 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1947 kBackgroundRouteId2));
1948 EXPECT_TRUE(mock_timer_->IsRunning());
1950 scheduler_.OnLoadingStateChanged(
1951 kBackgroundChildId, kBackgroundRouteId, false);
1952 EXPECT_TRUE(mock_timer_->IsRunning());
1954 scheduler_.OnLoadingStateChanged(
1955 kBackgroundChildId2, kBackgroundRouteId2, false);
1956 EXPECT_FALSE(mock_timer_->IsRunning());
1958 // This is needed to avoid errors on test tear down.
1959 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1962 TEST_F(ResourceSchedulerTest, LastCoalescedClientBecomesVisibleStopsTimer) {
1963 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1964 true /* should_coalesce */);
1965 scheduler_.OnClientCreated(
1966 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1967 EXPECT_FALSE(mock_timer_->IsRunning());
1968 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1969 EXPECT_FALSE(mock_timer_->IsRunning());
1970 scheduler_.OnLoadingStateChanged(
1971 kBackgroundChildId, kBackgroundRouteId, true);
1972 EXPECT_EQ(ResourceScheduler::COALESCED,
1973 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1974 kBackgroundRouteId));
1975 scheduler_.OnLoadingStateChanged(
1976 kBackgroundChildId2, kBackgroundRouteId2, true);
1977 EXPECT_EQ(ResourceScheduler::COALESCED,
1978 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1979 kBackgroundRouteId2));
1980 EXPECT_TRUE(mock_timer_->IsRunning());
1982 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1983 EXPECT_TRUE(mock_timer_->IsRunning());
1985 scheduler_.OnVisibilityChanged(
1986 kBackgroundChildId2, kBackgroundRouteId2, true);
1987 EXPECT_FALSE(mock_timer_->IsRunning());
1989 // To avoid errors on test tear down.
1990 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1993 TEST_F(ResourceSchedulerTest,
1994 CoalescedClientBecomesLoadingAndVisibleStopsTimer) {
1995 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1996 true /* should_coalesce */);
1997 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1998 EXPECT_FALSE(mock_timer_->IsRunning());
1999 scheduler_.OnLoadingStateChanged(
2000 kBackgroundChildId, kBackgroundRouteId, true);
2001 EXPECT_EQ(ResourceScheduler::COALESCED,
2002 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2003 kBackgroundRouteId));
2004 EXPECT_TRUE(mock_timer_->IsRunning());
2006 scheduler_.OnLoadingStateChanged(
2007 kBackgroundChildId, kBackgroundRouteId, false);
2008 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
2009 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2010 kBackgroundRouteId));
2011 EXPECT_FALSE(mock_timer_->IsRunning());
2013 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
2014 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
2015 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2016 kBackgroundRouteId));
2017 EXPECT_FALSE(mock_timer_->IsRunning());
2020 TEST_F(ResourceSchedulerTest, CoalescedRequestsIssueOnTimer) {
2021 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2022 true /* should_coalesce */);
2023 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2024 scheduler_.OnLoadingStateChanged(
2025 kBackgroundChildId, kBackgroundRouteId, true);
2026 EXPECT_EQ(ResourceScheduler::COALESCED,
2027 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2028 kBackgroundRouteId));
2029 EXPECT_TRUE(scheduler_.active_clients_loaded());
2031 scoped_ptr<TestRequest> high(
2032 NewBackgroundRequest("http://host/high", net::HIGHEST));
2033 scoped_ptr<TestRequest> low(
2034 NewBackgroundRequest("http://host/low", net::LOWEST));
2035 EXPECT_FALSE(high->started());
2036 EXPECT_FALSE(low->started());
2038 FireCoalescingTimer();
2040 EXPECT_TRUE(high->started());
2041 EXPECT_TRUE(low->started());
2044 TEST_F(ResourceSchedulerTest, CoalescedRequestsUnthrottleCorrectlyOnTimer) {
2045 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2046 true /* should_coalesce */);
2047 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2048 scheduler_.OnLoadingStateChanged(
2049 kBackgroundChildId, kBackgroundRouteId, true);
2050 EXPECT_EQ(ResourceScheduler::COALESCED,
2051 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2052 kBackgroundRouteId));
2053 EXPECT_TRUE(scheduler_.active_clients_loaded());
2055 scoped_ptr<TestRequest> high(
2056 NewBackgroundRequest("http://host/high", net::HIGHEST));
2057 scoped_ptr<TestRequest> high2(
2058 NewBackgroundRequest("http://host/high", net::HIGHEST));
2059 scoped_ptr<TestRequest> high3(
2060 NewBackgroundRequest("http://host/high", net::HIGHEST));
2061 scoped_ptr<TestRequest> high4(
2062 NewBackgroundRequest("http://host/high", net::HIGHEST));
2063 scoped_ptr<TestRequest> low(
2064 NewBackgroundRequest("http://host/low", net::LOWEST));
2065 scoped_ptr<TestRequest> low2(
2066 NewBackgroundRequest("http://host/low", net::LOWEST));
2067 scoped_ptr<TestRequest> low3(
2068 NewBackgroundRequest("http://host/low", net::LOWEST));
2069 scoped_ptr<TestRequest> low4(
2070 NewBackgroundRequest("http://host/low", net::LOWEST));
2072 http_server_properties_.SetSupportsSpdy(net::HostPortPair("spdyhost", 443),
2073 true);
2074 scoped_ptr<TestRequest> low_spdy(
2075 NewBackgroundRequest("https://spdyhost/low", net::LOW));
2076 scoped_ptr<TestRequest> sync_request(
2077 NewBackgroundSyncRequest("http://host/req", net::LOW));
2078 scoped_ptr<TestRequest> non_http_request(
2079 NewBackgroundRequest("chrome-extension://req", net::LOW));
2081 // Sync requests should issue immediately.
2082 EXPECT_TRUE(sync_request->started());
2083 // Non-http(s) requests should issue immediately.
2084 EXPECT_TRUE(non_http_request->started());
2085 // Nothing else should issue without a timer fire.
2086 EXPECT_FALSE(high->started());
2087 EXPECT_FALSE(high2->started());
2088 EXPECT_FALSE(high3->started());
2089 EXPECT_FALSE(high4->started());
2090 EXPECT_FALSE(low->started());
2091 EXPECT_FALSE(low2->started());
2092 EXPECT_FALSE(low3->started());
2093 EXPECT_FALSE(low4->started());
2094 EXPECT_FALSE(low_spdy->started());
2096 FireCoalescingTimer();
2098 // All high priority requests should issue.
2099 EXPECT_TRUE(high->started());
2100 EXPECT_TRUE(high2->started());
2101 EXPECT_TRUE(high3->started());
2102 EXPECT_TRUE(high4->started());
2103 // There should only be one net::LOWEST priority request issued with
2104 // non-delayable requests in flight.
2105 EXPECT_TRUE(low->started());
2106 EXPECT_FALSE(low2->started());
2107 EXPECT_FALSE(low3->started());
2108 EXPECT_FALSE(low4->started());
2109 // Spdy-Enable requests should issue regardless of priority.
2110 EXPECT_TRUE(low_spdy->started());
2113 TEST_F(ResourceSchedulerTest, CoalescedRequestsWaitForNextTimer) {
2114 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2115 true /* should_coalesce */);
2116 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2117 scheduler_.OnLoadingStateChanged(
2118 kBackgroundChildId, kBackgroundRouteId, true);
2120 EXPECT_EQ(ResourceScheduler::COALESCED,
2121 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2122 kBackgroundRouteId));
2123 EXPECT_TRUE(scheduler_.active_clients_loaded());
2125 scoped_ptr<TestRequest> high(
2126 NewBackgroundRequest("http://host/high", net::HIGHEST));
2127 EXPECT_FALSE(high->started());
2129 FireCoalescingTimer();
2131 scoped_ptr<TestRequest> high2(
2132 NewBackgroundRequest("http://host/high2", net::HIGHEST));
2133 scoped_ptr<TestRequest> low(
2134 NewBackgroundRequest("http://host/low", net::LOWEST));
2136 EXPECT_TRUE(high->started());
2137 EXPECT_FALSE(high2->started());
2138 EXPECT_FALSE(low->started());
2140 FireCoalescingTimer();
2142 EXPECT_TRUE(high->started());
2143 EXPECT_TRUE(high2->started());
2144 EXPECT_TRUE(low->started());
2147 TEST_F(ResourceSchedulerTest, GetVisualSignalFromRenderViewHost) {
2148 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory;
2149 scoped_ptr<TestRenderViewHostFactory> render_view_host_factory;
2150 scoped_ptr<TestBrowserContext> browser_context;
2151 scoped_ptr<TestWebContents> web_contents_1;
2152 scoped_ptr<TestWebContents> web_contents_2;
2153 render_process_host_factory.reset(new MockRenderProcessHostFactory());
2154 render_view_host_factory.reset(
2155 new TestRenderViewHostFactory(render_process_host_factory.get()));
2157 browser_context.reset(new TestBrowserContext());
2158 scoped_refptr<SiteInstance> site_instance_1 =
2159 SiteInstance::Create(browser_context.get());
2160 scoped_refptr<SiteInstance> site_instance_2 =
2161 SiteInstance::Create(browser_context.get());
2162 SiteInstanceImpl::set_render_process_host_factory(
2163 render_process_host_factory.get());
2165 web_contents_1.reset(
2166 TestWebContents::Create(browser_context.get(), site_instance_1.get()));
2167 web_contents_2.reset(
2168 TestWebContents::Create(browser_context.get(), site_instance_2.get()));
2169 base::RunLoop().RunUntilIdle();
2171 RenderViewHostImpl* rvh1 = web_contents_1->GetRenderViewHost();
2172 RenderViewHostImpl* rvh2 = web_contents_2->GetRenderViewHost();
2173 ResourceScheduler* scheduler = ResourceDispatcherHostImpl::Get()->scheduler();
2175 // Check initial visibility is set correctly.
2176 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2177 rvh1->GetRoutingID()),
2178 !rvh1->is_hidden());
2179 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2180 rvh1->GetRoutingID()),
2181 !rvh2->is_hidden());
2183 // 1 visible, 1 hidden
2184 rvh1->WasShown(ui::LatencyInfo());
2185 rvh2->WasHidden();
2186 base::RunLoop().RunUntilIdle();
2188 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2189 rvh1->GetRoutingID()));
2190 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2191 rvh2->GetRoutingID()));
2193 // Flip the visibility and check again.
2194 rvh1->WasHidden();
2195 rvh2->WasShown(ui::LatencyInfo());
2196 base::RunLoop().RunUntilIdle();
2198 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2199 rvh1->GetRoutingID()));
2200 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2201 rvh2->GetRoutingID()));
2202 // Clean up.
2203 web_contents_1.reset();
2204 web_contents_2.reset();
2205 base::RunLoop().RunUntilIdle();
2207 browser_context.reset();
2208 render_process_host_factory.reset();
2211 } // unnamed namespace
2213 } // namespace content