Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / loader / resource_scheduler_unittest.cc
blobe8f3d4c42963528fd5f5d32e2341a8602a72f24e
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_web_contents.h"
26 #include "net/base/host_port_pair.h"
27 #include "net/base/request_priority.h"
28 #include "net/http/http_server_properties_impl.h"
29 #include "net/url_request/url_request.h"
30 #include "net/url_request/url_request_test_util.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "ui/events/latency_info.h"
34 namespace content {
36 namespace {
38 class TestRequestFactory;
40 const int kChildId = 30;
41 const int kRouteId = 75;
42 const int kChildId2 = 43;
43 const int kRouteId2 = 67;
44 const int kBackgroundChildId = 35;
45 const int kBackgroundRouteId = 43;
46 const int kBackgroundChildId2 = 54;
47 const int kBackgroundRouteId2 = 82;
49 class TestRequest : public ResourceController {
50 public:
51 TestRequest(scoped_ptr<ResourceThrottle> throttle,
52 scoped_ptr<net::URLRequest> url_request)
53 : started_(false),
54 throttle_(throttle.Pass()),
55 url_request_(url_request.Pass()) {
56 throttle_->set_controller_for_testing(this);
58 virtual ~TestRequest() {}
60 bool started() const { return started_; }
62 void Start() {
63 bool deferred = false;
64 throttle_->WillStartRequest(&deferred);
65 started_ = !deferred;
68 virtual void Cancel() OVERRIDE {
69 // Alert the scheduler that the request can be deleted.
70 throttle_.reset(0);
73 const net::URLRequest* url_request() const { return url_request_.get(); }
75 protected:
76 // ResourceController interface:
77 virtual void CancelAndIgnore() OVERRIDE {}
78 virtual void CancelWithError(int error_code) OVERRIDE {}
79 virtual void Resume() OVERRIDE { started_ = true; }
81 private:
82 bool started_;
83 scoped_ptr<ResourceThrottle> throttle_;
84 scoped_ptr<net::URLRequest> url_request_;
87 class CancelingTestRequest : public TestRequest {
88 public:
89 CancelingTestRequest(scoped_ptr<ResourceThrottle> throttle,
90 scoped_ptr<net::URLRequest> url_request)
91 : TestRequest(throttle.Pass(), url_request.Pass()) {}
93 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) {
94 request_to_cancel_ = request_to_cancel.Pass();
97 private:
98 virtual void Resume() OVERRIDE {
99 TestRequest::Resume();
100 request_to_cancel_.reset();
103 scoped_ptr<TestRequest> request_to_cancel_;
106 class FakeResourceContext : public ResourceContext {
107 private:
108 virtual net::HostResolver* GetHostResolver() OVERRIDE { return NULL; }
109 virtual net::URLRequestContext* GetRequestContext() OVERRIDE { return NULL; }
110 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { return false; }
111 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { return false; }
114 class FakeResourceMessageFilter : public ResourceMessageFilter {
115 public:
116 FakeResourceMessageFilter(int child_id)
117 : ResourceMessageFilter(
118 child_id,
119 PROCESS_TYPE_RENDERER,
120 NULL /* appcache_service */,
121 NULL /* blob_storage_context */,
122 NULL /* file_system_context */,
123 NULL /* service_worker_context */,
124 base::Bind(&FakeResourceMessageFilter::GetContexts,
125 base::Unretained(this))) {
128 private:
129 virtual ~FakeResourceMessageFilter() {}
131 void GetContexts(const ResourceHostMsg_Request& request,
132 ResourceContext** resource_context,
133 net::URLRequestContext** request_context) {
134 *resource_context = &context_;
135 *request_context = NULL;
138 FakeResourceContext context_;
141 class ResourceSchedulerTest : public testing::Test {
142 protected:
143 ResourceSchedulerTest()
144 : next_request_id_(0),
145 ui_thread_(BrowserThread::UI, &message_loop_),
146 io_thread_(BrowserThread::IO, &message_loop_),
147 mock_timer_(new base::MockTimer(true, true)) {
148 scheduler_.set_timer_for_testing(scoped_ptr<base::Timer>(mock_timer_));
150 // TODO(aiolos): Remove when throttling and coalescing have both landed.
151 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
152 false /* should_coalesce */);
154 scheduler_.OnClientCreated(kChildId, kRouteId, true);
155 scheduler_.OnClientCreated(kBackgroundChildId, kBackgroundRouteId, false);
156 context_.set_http_server_properties(http_server_properties_.GetWeakPtr());
159 virtual ~ResourceSchedulerTest() {
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 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 blink::WebReferrerPolicyDefault, // referrer_policy
191 blink::WebPageVisibilityStateVisible, // visibility_state
192 NULL, // context
193 base::WeakPtr<ResourceMessageFilter>(), // filter
194 is_async); // is_async
195 info->AssociateWithRequest(url_request.get());
196 return url_request.Pass();
199 scoped_ptr<net::URLRequest> NewURLRequest(const char* url,
200 net::RequestPriority priority) {
201 return NewURLRequestWithChildAndRoute(
202 url, priority, kChildId, kRouteId, true);
205 TestRequest* NewRequestWithRoute(const char* url,
206 net::RequestPriority priority,
207 int route_id) {
208 return NewRequestWithChildAndRoute(url, priority, route_id, kChildId);
211 TestRequest* NewRequestWithChildAndRoute(const char* url,
212 net::RequestPriority priority,
213 int child_id,
214 int route_id) {
215 return GetNewTestRequest(url, priority, child_id, route_id, true);
218 TestRequest* NewRequest(const char* url, net::RequestPriority priority) {
219 return NewRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
222 TestRequest* NewBackgroundRequest(const char* url,
223 net::RequestPriority priority) {
224 return NewRequestWithChildAndRoute(
225 url, priority, kBackgroundChildId, kBackgroundRouteId);
228 TestRequest* NewSyncRequest(const char* url, net::RequestPriority priority) {
229 return NewSyncRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
232 TestRequest* NewBackgroundSyncRequest(const char* url,
233 net::RequestPriority priority) {
234 return NewSyncRequestWithChildAndRoute(
235 url, priority, kBackgroundChildId, kBackgroundRouteId);
238 TestRequest* NewSyncRequestWithChildAndRoute(const char* url,
239 net::RequestPriority priority,
240 int child_id,
241 int route_id) {
242 return GetNewTestRequest(url, priority, child_id, route_id, false);
245 TestRequest* GetNewTestRequest(const char* url,
246 net::RequestPriority priority,
247 int child_id,
248 int route_id,
249 bool is_async) {
250 scoped_ptr<net::URLRequest> url_request(NewURLRequestWithChildAndRoute(
251 url, priority, child_id, route_id, is_async));
252 scoped_ptr<ResourceThrottle> throttle(
253 scheduler_.ScheduleRequest(child_id, route_id, url_request.get()));
254 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass());
255 request->Start();
256 return request;
260 void ChangeRequestPriority(TestRequest* request,
261 net::RequestPriority new_priority,
262 int intra_priority = 0) {
263 scoped_refptr<FakeResourceMessageFilter> filter(
264 new FakeResourceMessageFilter(kChildId));
265 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(
266 request->url_request());
267 const GlobalRequestID& id = info->GetGlobalRequestID();
268 ResourceHostMsg_DidChangePriority msg(id.request_id, new_priority,
269 intra_priority);
270 rdh_.OnMessageReceived(msg, filter.get());
273 void FireCoalescingTimer() {
274 EXPECT_TRUE(mock_timer_->IsRunning());
275 mock_timer_->Fire();
278 int next_request_id_;
279 base::MessageLoopForIO message_loop_;
280 BrowserThreadImpl ui_thread_;
281 BrowserThreadImpl io_thread_;
282 ResourceDispatcherHostImpl rdh_;
283 ResourceScheduler scheduler_;
284 base::MockTimer* mock_timer_;
285 net::HttpServerPropertiesImpl http_server_properties_;
286 net::TestURLRequestContext context_;
289 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
290 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST));
291 EXPECT_TRUE(request->started());
294 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilIdle) {
295 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
296 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
297 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
298 EXPECT_TRUE(high->started());
299 EXPECT_TRUE(low->started());
300 EXPECT_FALSE(low2->started());
301 high.reset();
302 EXPECT_TRUE(low2->started());
305 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInserted) {
306 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
307 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
308 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
309 EXPECT_TRUE(high->started());
310 EXPECT_TRUE(low->started());
311 EXPECT_FALSE(low2->started());
312 high.reset();
313 scheduler_.OnWillInsertBody(kChildId, kRouteId);
314 EXPECT_TRUE(low2->started());
317 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilCriticalComplete) {
318 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
319 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
320 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
321 EXPECT_TRUE(high->started());
322 EXPECT_TRUE(low->started());
323 EXPECT_FALSE(low2->started());
324 scheduler_.OnWillInsertBody(kChildId, kRouteId);
325 EXPECT_FALSE(low2->started());
326 high.reset();
327 EXPECT_TRUE(low2->started());
330 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) {
331 http_server_properties_.SetSupportsSpdy(
332 net::HostPortPair("spdyhost", 443), true);
333 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
334 scoped_ptr<TestRequest> low_spdy(
335 NewRequest("https://spdyhost/low", net::LOWEST));
336 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
337 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
338 EXPECT_TRUE(high->started());
339 EXPECT_TRUE(low_spdy->started());
340 EXPECT_TRUE(low->started());
341 EXPECT_FALSE(low2->started());
342 scheduler_.OnWillInsertBody(kChildId, kRouteId);
343 high.reset();
344 EXPECT_TRUE(low2->started());
347 TEST_F(ResourceSchedulerTest, NavigationResetsState) {
348 scheduler_.OnWillInsertBody(kChildId, kRouteId);
349 scheduler_.OnNavigate(kChildId, kRouteId);
350 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
351 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
352 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
353 EXPECT_TRUE(high->started());
354 EXPECT_TRUE(low->started());
355 EXPECT_FALSE(low2->started());
358 TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
359 const int route_id = 0; // Indicates a background request.
360 scoped_ptr<TestRequest> request(NewRequestWithRoute("http://host/1",
361 net::LOWEST, route_id));
362 EXPECT_TRUE(request->started());
365 TEST_F(ResourceSchedulerTest, StartMultipleLowRequestsWhenIdle) {
366 scoped_ptr<TestRequest> high1(NewRequest("http://host/high1", net::HIGHEST));
367 scoped_ptr<TestRequest> high2(NewRequest("http://host/high2", net::HIGHEST));
368 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
369 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
370 EXPECT_TRUE(high1->started());
371 EXPECT_TRUE(high2->started());
372 EXPECT_TRUE(low->started());
373 EXPECT_FALSE(low2->started());
374 high1.reset();
375 EXPECT_FALSE(low2->started());
376 high2.reset();
377 EXPECT_TRUE(low2->started());
380 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) {
381 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
382 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST));
384 scoped_ptr<net::URLRequest> url_request(
385 NewURLRequest("http://host/low2", net::LOWEST));
386 scoped_ptr<ResourceThrottle> throttle(
387 scheduler_.ScheduleRequest(kChildId, kRouteId, url_request.get()));
388 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest(
389 throttle.Pass(), url_request.Pass()));
390 low2->Start();
392 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST));
393 low2->set_request_to_cancel(low3.Pass());
394 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST));
396 EXPECT_TRUE(high->started());
397 EXPECT_FALSE(low2->started());
398 high.reset();
399 EXPECT_TRUE(low1->started());
400 EXPECT_TRUE(low2->started());
401 EXPECT_TRUE(low4->started());
404 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
405 // We only load low priority resources if there's a body.
406 scheduler_.OnWillInsertBody(kChildId, kRouteId);
408 // Throw in one high priority request to make sure that's not a factor.
409 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
410 EXPECT_TRUE(high->started());
412 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
413 const int kMaxNumDelayableRequestsPerHost = 6;
414 ScopedVector<TestRequest> lows_singlehost;
415 // Queue up to the per-host limit (we subtract the current high-pri request).
416 for (int i = 0; i < kMaxNumDelayableRequestsPerHost - 1; ++i) {
417 string url = "http://host/low" + base::IntToString(i);
418 lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
419 EXPECT_TRUE(lows_singlehost[i]->started());
422 scoped_ptr<TestRequest> second_last_singlehost(NewRequest("http://host/last",
423 net::LOWEST));
424 scoped_ptr<TestRequest> last_singlehost(NewRequest("http://host/s_last",
425 net::LOWEST));
427 EXPECT_FALSE(second_last_singlehost->started());
428 high.reset();
429 EXPECT_TRUE(second_last_singlehost->started());
430 EXPECT_FALSE(last_singlehost->started());
431 lows_singlehost.erase(lows_singlehost.begin());
432 EXPECT_TRUE(last_singlehost->started());
434 // Queue more requests from different hosts until we reach the total limit.
435 int expected_slots_left =
436 kMaxNumDelayableRequestsPerClient - kMaxNumDelayableRequestsPerHost;
437 EXPECT_GT(expected_slots_left, 0);
438 ScopedVector<TestRequest> lows_differenthosts;
439 for (int i = 0; i < expected_slots_left; ++i) {
440 string url = "http://host" + base::IntToString(i) + "/low";
441 lows_differenthosts.push_back(NewRequest(url.c_str(), net::LOWEST));
442 EXPECT_TRUE(lows_differenthosts[i]->started());
445 scoped_ptr<TestRequest> last_differenthost(NewRequest("http://host_new/last",
446 net::LOWEST));
447 EXPECT_FALSE(last_differenthost->started());
450 TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) {
451 // Dummies to enforce scheduling.
452 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
453 scoped_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
455 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
456 EXPECT_FALSE(request->started());
458 ChangeRequestPriority(request.get(), net::HIGHEST);
459 EXPECT_TRUE(request->started());
462 TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) {
463 // Dummies to enforce scheduling.
464 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
465 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
467 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
468 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
469 EXPECT_FALSE(request->started());
470 EXPECT_FALSE(idle->started());
472 ChangeRequestPriority(request.get(), net::LOWEST);
473 EXPECT_FALSE(request->started());
474 EXPECT_FALSE(idle->started());
476 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
477 ScopedVector<TestRequest> lows;
478 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
479 string url = "http://host/low" + base::IntToString(i);
480 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
483 scheduler_.OnWillInsertBody(kChildId, kRouteId);
484 high.reset();
486 EXPECT_TRUE(request->started());
487 EXPECT_FALSE(idle->started());
490 TEST_F(ResourceSchedulerTest, LowerPriority) {
491 // Dummies to enforce scheduling.
492 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
493 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
495 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
496 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
497 EXPECT_FALSE(request->started());
498 EXPECT_FALSE(idle->started());
500 ChangeRequestPriority(request.get(), net::IDLE);
501 EXPECT_FALSE(request->started());
502 EXPECT_FALSE(idle->started());
504 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
505 // 2 fewer filler requests: 1 for the "low" dummy at the start, and 1 for the
506 // one at the end, which will be tested.
507 const int kNumFillerRequests = kMaxNumDelayableRequestsPerClient - 2;
508 ScopedVector<TestRequest> lows;
509 for (int i = 0; i < kNumFillerRequests; ++i) {
510 string url = "http://host" + base::IntToString(i) + "/low";
511 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
514 scheduler_.OnWillInsertBody(kChildId, kRouteId);
515 high.reset();
517 EXPECT_FALSE(request->started());
518 EXPECT_TRUE(idle->started());
521 TEST_F(ResourceSchedulerTest, ReprioritizedRequestGoesToBackOfQueue) {
522 // Dummies to enforce scheduling.
523 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
524 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
526 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
527 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
528 EXPECT_FALSE(request->started());
529 EXPECT_FALSE(idle->started());
531 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
532 ScopedVector<TestRequest> lows;
533 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
534 string url = "http://host/low" + base::IntToString(i);
535 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
538 ChangeRequestPriority(request.get(), net::IDLE);
539 EXPECT_FALSE(request->started());
540 EXPECT_FALSE(idle->started());
542 ChangeRequestPriority(request.get(), net::LOWEST);
543 EXPECT_FALSE(request->started());
544 EXPECT_FALSE(idle->started());
546 scheduler_.OnWillInsertBody(kChildId, kRouteId);
547 EXPECT_FALSE(request->started());
548 EXPECT_FALSE(idle->started());
551 TEST_F(ResourceSchedulerTest, HigherIntraPriorityGoesToFrontOfQueue) {
552 // Dummies to enforce scheduling.
553 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
554 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
556 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
557 ScopedVector<TestRequest> lows;
558 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
559 string url = "http://host/low" + base::IntToString(i);
560 lows.push_back(NewRequest(url.c_str(), net::IDLE));
563 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
564 EXPECT_FALSE(request->started());
566 ChangeRequestPriority(request.get(), net::IDLE, 1);
567 EXPECT_FALSE(request->started());
569 scheduler_.OnWillInsertBody(kChildId, kRouteId);
570 high.reset();
571 EXPECT_TRUE(request->started());
574 TEST_F(ResourceSchedulerTest, NonHTTPSchedulesImmediately) {
575 // Dummies to enforce scheduling.
576 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
577 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
579 scoped_ptr<TestRequest> request(
580 NewRequest("chrome-extension://req", net::LOWEST));
581 EXPECT_TRUE(request->started());
584 TEST_F(ResourceSchedulerTest, ActiveLoadingSyncSchedulesImmediately) {
585 // TODO(aiolos): remove when throttling and coalescing have both landed
586 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
587 false /* should_coalesce */);
588 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
589 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
590 // Dummies to enforce scheduling.
591 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
592 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
594 scoped_ptr<TestRequest> request(
595 NewSyncRequest("http://host/req", net::LOWEST));
596 EXPECT_TRUE(request->started());
599 TEST_F(ResourceSchedulerTest, UnthrottledSyncSchedulesImmediately) {
600 // TODO(aiolos): remove when throttling and coalescing have both landed
601 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
602 false /* should_coalesce */);
603 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
604 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
605 scheduler_.GetClientStateForTesting(kBackgroundChildId,
606 kBackgroundRouteId));
607 // Dummies to enforce scheduling.
608 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
609 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
611 scoped_ptr<TestRequest> request(
612 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
613 EXPECT_TRUE(request->started());
616 TEST_F(ResourceSchedulerTest, SpdyProxySchedulesImmediately) {
617 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
618 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
620 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
621 EXPECT_FALSE(request->started());
623 scheduler_.OnReceivedSpdyProxiedHttpResponse(kChildId, kRouteId);
624 EXPECT_TRUE(request->started());
626 scoped_ptr<TestRequest> after(NewRequest("http://host/after", net::IDLE));
627 EXPECT_TRUE(after->started());
630 TEST_F(ResourceSchedulerTest, NewSpdyHostInDelayableRequests) {
631 scheduler_.OnWillInsertBody(kChildId, kRouteId);
632 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
634 scoped_ptr<TestRequest> low1_spdy(
635 NewRequest("http://spdyhost1:8080/low", net::LOWEST));
636 // Cancel a request after we learn the server supports SPDY.
637 ScopedVector<TestRequest> lows;
638 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
639 string url = "http://host" + base::IntToString(i) + "/low";
640 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
642 scoped_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
643 EXPECT_FALSE(low1->started());
644 http_server_properties_.SetSupportsSpdy(
645 net::HostPortPair("spdyhost1", 8080), true);
646 low1_spdy.reset();
647 EXPECT_TRUE(low1->started());
649 low1.reset();
650 scoped_ptr<TestRequest> low2_spdy(
651 NewRequest("http://spdyhost2:8080/low", net::IDLE));
652 // Reprioritize a request after we learn the server supports SPDY.
653 EXPECT_TRUE(low2_spdy->started());
654 http_server_properties_.SetSupportsSpdy(
655 net::HostPortPair("spdyhost2", 8080), true);
656 ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
657 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
658 EXPECT_TRUE(low2->started());
661 TEST_F(ResourceSchedulerTest, ThrottledClientCreation) {
662 // TODO(aiolos): remove when throttling and coalescing have both landed
663 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
664 false /* should_coalesce */);
665 EXPECT_TRUE(scheduler_.should_throttle());
666 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
668 EXPECT_EQ(ResourceScheduler::THROTTLED,
669 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
670 kBackgroundRouteId2));
671 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
674 TEST_F(ResourceSchedulerTest, ActiveClientThrottleUpdateOnLoadingChange) {
675 // TODO(aiolos): remove when throttling and coalescing have both landed
676 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
677 false /* should_coalesce */);
678 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
679 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
680 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
681 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
682 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
683 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
684 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
685 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
688 TEST_F(ResourceSchedulerTest, CoalesceBackgroundClientOnLoadCompletion) {
689 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
690 true /* should_coalesce */);
691 EXPECT_EQ(ResourceScheduler::THROTTLED,
692 scheduler_.GetClientStateForTesting(kBackgroundChildId,
693 kBackgroundRouteId));
694 scheduler_.OnLoadingStateChanged(
695 kBackgroundChildId, kBackgroundRouteId, true);
696 EXPECT_EQ(ResourceScheduler::THROTTLED,
697 scheduler_.GetClientStateForTesting(kBackgroundChildId,
698 kBackgroundRouteId));
699 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
700 EXPECT_EQ(ResourceScheduler::COALESCED,
701 scheduler_.GetClientStateForTesting(kBackgroundChildId,
702 kBackgroundRouteId));
705 TEST_F(ResourceSchedulerTest, UnthrottleBackgroundClientOnLoadingStarted) {
706 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
707 true /* should_coalesce */);
708 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
709 scheduler_.OnLoadingStateChanged(
710 kBackgroundChildId, kBackgroundRouteId, true);
711 EXPECT_EQ(ResourceScheduler::COALESCED,
712 scheduler_.GetClientStateForTesting(kBackgroundChildId,
713 kBackgroundRouteId));
715 scheduler_.OnLoadingStateChanged(
716 kBackgroundChildId, kBackgroundRouteId, false);
717 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
718 scheduler_.GetClientStateForTesting(kBackgroundChildId,
719 kBackgroundRouteId));
722 TEST_F(ResourceSchedulerTest, OneRequestPerThrottledClient) {
723 // TODO(aiolos): remove when throttling and coalescing have both landed
724 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
725 false /* should_coalesce */);
726 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
727 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
728 EXPECT_EQ(ResourceScheduler::THROTTLED,
729 scheduler_.GetClientStateForTesting(kBackgroundChildId,
730 kBackgroundRouteId));
731 scoped_ptr<TestRequest> high(
732 NewBackgroundRequest("http://host/high", net::HIGHEST));
733 scoped_ptr<TestRequest> request(
734 NewBackgroundRequest("http://host/req", net::IDLE));
736 EXPECT_TRUE(high->started());
737 EXPECT_FALSE(request->started());
740 TEST_F(ResourceSchedulerTest, UnthrottleNewlyVisibleClient) {
741 // TODO(aiolos): remove when throttling and coalescing have both landed
742 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
743 false /* should_coalesce */);
744 EXPECT_EQ(ResourceScheduler::THROTTLED,
745 scheduler_.GetClientStateForTesting(kBackgroundChildId,
746 kBackgroundRouteId));
747 scoped_ptr<TestRequest> high(
748 NewBackgroundRequest("http://host/high", net::HIGHEST));
749 scoped_ptr<TestRequest> request(
750 NewBackgroundRequest("http://host/req", net::IDLE));
751 EXPECT_FALSE(request->started());
753 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
754 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
755 scheduler_.GetClientStateForTesting(kBackgroundChildId,
756 kBackgroundRouteId));
757 EXPECT_TRUE(request->started());
760 TEST_F(ResourceSchedulerTest, UnthrottleNewlyAudibleClient) {
761 // TODO(aiolos): remove when throttling and coalescing have both landed
762 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
763 false /* should_coalesce */);
764 EXPECT_EQ(ResourceScheduler::THROTTLED,
765 scheduler_.GetClientStateForTesting(kBackgroundChildId,
766 kBackgroundRouteId));
767 scoped_ptr<TestRequest> high(
768 NewBackgroundRequest("http://host/high", net::HIGHEST));
769 scoped_ptr<TestRequest> request(
770 NewBackgroundRequest("http://host/req", net::IDLE));
771 EXPECT_FALSE(request->started());
773 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
774 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
775 scheduler_.GetClientStateForTesting(kBackgroundChildId,
776 kBackgroundRouteId));
777 EXPECT_TRUE(request->started());
780 TEST_F(ResourceSchedulerTest, VisibleClientStillUnthrottledOnAudabilityChange) {
781 // TODO(aiolos): remove when throttling and coalescing have both landed
782 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
783 false /* should_coalesce */);
784 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
785 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
786 EXPECT_EQ(ResourceScheduler::THROTTLED,
787 scheduler_.GetClientStateForTesting(kBackgroundChildId,
788 kBackgroundRouteId));
790 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
791 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
792 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
793 EXPECT_EQ(ResourceScheduler::THROTTLED,
794 scheduler_.GetClientStateForTesting(kBackgroundChildId,
795 kBackgroundRouteId));
797 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
798 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
799 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
800 EXPECT_EQ(ResourceScheduler::THROTTLED,
801 scheduler_.GetClientStateForTesting(kBackgroundChildId,
802 kBackgroundRouteId));
805 TEST_F(ResourceSchedulerTest, AudibleClientStillUnthrottledOnVisabilityChange) {
806 // TODO(aiolos): remove when throttling and coalescing have both landed
807 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
808 false /* should_coalesce */);
809 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
810 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
811 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
812 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
813 EXPECT_EQ(ResourceScheduler::THROTTLED,
814 scheduler_.GetClientStateForTesting(kBackgroundChildId,
815 kBackgroundRouteId));
817 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
818 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
819 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
820 EXPECT_EQ(ResourceScheduler::THROTTLED,
821 scheduler_.GetClientStateForTesting(kBackgroundChildId,
822 kBackgroundRouteId));
824 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
825 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
826 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
827 EXPECT_EQ(ResourceScheduler::THROTTLED,
828 scheduler_.GetClientStateForTesting(kBackgroundChildId,
829 kBackgroundRouteId));
832 TEST_F(ResourceSchedulerTest, ThrottledClientStartsNextHighestPriorityRequest) {
833 // TODO(aiolos): remove when throttling and coalescing have both landed
834 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
835 false /* should_coalesce */);
836 scoped_ptr<TestRequest> request(
837 NewBackgroundRequest("http://host/req", net::IDLE));
838 // Lower priority request started first to test request prioritizaton.
839 scoped_ptr<TestRequest> low(
840 NewBackgroundRequest("http://host/high", net::IDLE));
841 scoped_ptr<TestRequest> high(
842 NewBackgroundRequest("http://host/high", net::HIGHEST));
844 EXPECT_FALSE(low->started());
845 EXPECT_FALSE(high->started());
847 // request->CancelRequest();
848 request->Cancel();
849 EXPECT_TRUE(high->started());
850 EXPECT_FALSE(low->started());
853 TEST_F(ResourceSchedulerTest, ThrottledSpdyProxySchedulesImmediately) {
854 // TODO(aiolos): remove when throttling and coalescing have both landed
855 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
856 false /* should_coalesce */);
857 EXPECT_EQ(ResourceScheduler::THROTTLED,
858 scheduler_.GetClientStateForTesting(kBackgroundChildId,
859 kBackgroundRouteId));
860 scoped_ptr<TestRequest> high(
861 NewBackgroundRequest("http://host/high", net::HIGHEST));
862 scoped_ptr<TestRequest> request(
863 NewBackgroundRequest("http://host/req", net::IDLE));
865 EXPECT_FALSE(request->started());
867 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
868 kBackgroundRouteId);
869 EXPECT_TRUE(request->started());
871 scoped_ptr<TestRequest> after(
872 NewBackgroundRequest("http://host/after", net::IDLE));
873 EXPECT_TRUE(after->started());
876 TEST_F(ResourceSchedulerTest, CoalescedClientIssuesNoRequests) {
877 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
878 true /* should_coalesce */);
879 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
880 scheduler_.OnLoadingStateChanged(
881 kBackgroundChildId, kBackgroundRouteId, true);
882 EXPECT_EQ(ResourceScheduler::COALESCED,
883 scheduler_.GetClientStateForTesting(kBackgroundChildId,
884 kBackgroundRouteId));
885 scoped_ptr<TestRequest> high(
886 NewBackgroundRequest("http://host/high", net::HIGHEST));
887 scoped_ptr<TestRequest> request(
888 NewBackgroundRequest("http://host/req", net::IDLE));
890 EXPECT_FALSE(high->started());
891 EXPECT_FALSE(request->started());
893 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
894 kBackgroundRouteId);
895 EXPECT_FALSE(high->started());
897 scoped_ptr<TestRequest> after(
898 NewBackgroundRequest("http://host/after", net::HIGHEST));
899 EXPECT_FALSE(after->started());
902 TEST_F(ResourceSchedulerTest, CoalescedSpdyProxyWaits) {
903 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
904 true /* should_coalesce */);
905 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
906 scheduler_.OnLoadingStateChanged(
907 kBackgroundChildId, kBackgroundRouteId, true);
908 EXPECT_EQ(ResourceScheduler::COALESCED,
909 scheduler_.GetClientStateForTesting(kBackgroundChildId,
910 kBackgroundRouteId));
911 scoped_ptr<TestRequest> high(
912 NewBackgroundRequest("http://host/high", net::HIGHEST));
913 scoped_ptr<TestRequest> request(
914 NewBackgroundRequest("http://host/req", net::IDLE));
916 EXPECT_FALSE(request->started());
918 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
919 kBackgroundRouteId);
920 EXPECT_FALSE(request->started());
922 scoped_ptr<TestRequest> after(
923 NewBackgroundRequest("http://host/after", net::IDLE));
924 EXPECT_FALSE(after->started());
927 TEST_F(ResourceSchedulerTest, ThrottledNonHTTPSchedulesImmediately) {
928 // TODO(aiolos): remove when throttling and coalescing have both landed
929 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
930 false /* should_coalesce */);
931 // Dummies to enforce scheduling.
932 scoped_ptr<TestRequest> high(
933 NewBackgroundRequest("http://host/high", net::HIGHEST));
934 scoped_ptr<TestRequest> low(
935 NewBackgroundRequest("http://host/low", net::LOWEST));
937 scoped_ptr<TestRequest> request(
938 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
939 EXPECT_TRUE(request->started());
940 EXPECT_FALSE(low->started());
943 TEST_F(ResourceSchedulerTest, CoalescedNonHTTPSchedulesImmediately) {
944 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
945 true /* should_coalesce */);
946 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
947 scheduler_.OnLoadingStateChanged(
948 kBackgroundChildId, kBackgroundRouteId, true);
949 EXPECT_EQ(ResourceScheduler::COALESCED,
950 scheduler_.GetClientStateForTesting(kBackgroundChildId,
951 kBackgroundRouteId));
952 // Dummies to enforce scheduling.
953 scoped_ptr<TestRequest> high(
954 NewBackgroundRequest("http://host/high", net::HIGHEST));
955 scoped_ptr<TestRequest> low(
956 NewBackgroundRequest("http://host/low", net::LOWEST));
958 scoped_ptr<TestRequest> request(
959 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
960 EXPECT_TRUE(request->started());
961 EXPECT_FALSE(low->started());
964 TEST_F(ResourceSchedulerTest, ThrottledSyncSchedulesImmediately) {
965 // TODO(aiolos): remove when throttling and coalescing have both landed
966 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
967 false /* should_coalesce */);
968 // Dummies to enforce scheduling.
969 scoped_ptr<TestRequest> high(
970 NewBackgroundRequest("http://host/high", net::HIGHEST));
971 scoped_ptr<TestRequest> low(
972 NewBackgroundRequest("http://host/low", net::LOWEST));
974 scoped_ptr<TestRequest> request(
975 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
976 EXPECT_TRUE(request->started());
977 EXPECT_FALSE(low->started());
980 TEST_F(ResourceSchedulerTest, CoalescedSyncSchedulesImmediately) {
981 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
982 true /* should_coalesce */);
983 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
984 scheduler_.OnLoadingStateChanged(
985 kBackgroundChildId, kBackgroundRouteId, true);
986 EXPECT_EQ(ResourceScheduler::COALESCED,
987 scheduler_.GetClientStateForTesting(kBackgroundChildId,
988 kBackgroundRouteId));
989 // Dummies to enforce scheduling.
990 scoped_ptr<TestRequest> high(
991 NewBackgroundRequest("http://host/high", net::HIGHEST));
992 scoped_ptr<TestRequest> low(
993 NewBackgroundRequest("http://host/low", net::LOWEST));
995 scoped_ptr<TestRequest> request(
996 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
997 EXPECT_TRUE(request->started());
998 EXPECT_FALSE(low->started());
999 EXPECT_FALSE(high->started());
1002 TEST_F(ResourceSchedulerTest, AllBackgroundClientsUnthrottle) {
1003 // TODO(aiolos): remove when throttling and coalescing have both landed
1004 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1005 false /* should_coalesce */);
1006 EXPECT_EQ(ResourceScheduler::THROTTLED,
1007 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1008 kBackgroundRouteId));
1009 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1010 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1011 EXPECT_FALSE(scheduler_.active_clients_loaded());
1013 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1014 EXPECT_TRUE(scheduler_.active_clients_loaded());
1015 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1016 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1017 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1018 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1019 kBackgroundRouteId));
1021 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1022 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1023 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1024 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1025 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1026 kBackgroundRouteId));
1028 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
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 scheduler_.OnLoadingStateChanged(
1037 kBackgroundChildId, kBackgroundRouteId, true);
1038 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1039 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1040 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1041 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1042 kBackgroundRouteId));
1045 TEST_F(ResourceSchedulerTest,
1046 UnloadedClientVisibilityChangedCorrectlyUnthrottles) {
1047 // TODO(aiolos): remove when throttling and coalescing have both landed
1048 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1049 false /* should_coalesce */);
1050 scheduler_.OnClientCreated(kChildId2, kRouteId2, false);
1051 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1052 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1053 scheduler_.OnLoadingStateChanged(
1054 kBackgroundChildId2, kBackgroundRouteId2, true);
1056 // 1 visible, 3 hidden
1057 EXPECT_FALSE(scheduler_.active_clients_loaded());
1058 EXPECT_EQ(ResourceScheduler::THROTTLED,
1059 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1060 kBackgroundRouteId));
1061 EXPECT_EQ(ResourceScheduler::THROTTLED,
1062 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1063 kBackgroundRouteId2));
1064 EXPECT_EQ(ResourceScheduler::THROTTLED,
1065 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1066 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1067 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1069 // 2 visible, 2 hidden
1070 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1071 EXPECT_FALSE(scheduler_.active_clients_loaded());
1072 EXPECT_EQ(ResourceScheduler::THROTTLED,
1073 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1074 kBackgroundRouteId));
1075 EXPECT_EQ(ResourceScheduler::THROTTLED,
1076 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1077 kBackgroundRouteId2));
1078 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1079 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1080 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1081 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1083 // 1 visible, 3 hidden
1084 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1085 EXPECT_FALSE(scheduler_.active_clients_loaded());
1086 EXPECT_EQ(ResourceScheduler::THROTTLED,
1087 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1088 kBackgroundRouteId));
1089 EXPECT_EQ(ResourceScheduler::THROTTLED,
1090 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1091 kBackgroundRouteId2));
1092 EXPECT_EQ(ResourceScheduler::THROTTLED,
1093 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1094 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1095 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1097 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1098 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1101 TEST_F(ResourceSchedulerTest,
1102 UnloadedClientAudibilityChangedCorrectlyUnthrottles) {
1103 // TODO(aiolos): remove when throttling and coalescing have both landed
1104 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1105 false /* should_coalesce */);
1106 scheduler_.OnClientCreated(kChildId2, kRouteId2, false);
1107 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1108 scheduler_.OnLoadingStateChanged(
1109 kBackgroundChildId2, kBackgroundRouteId2, true);
1110 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1111 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1113 // 1 audible, 3 hidden
1114 EXPECT_FALSE(scheduler_.active_clients_loaded());
1115 EXPECT_EQ(ResourceScheduler::THROTTLED,
1116 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1117 kBackgroundRouteId));
1118 EXPECT_EQ(ResourceScheduler::THROTTLED,
1119 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1120 kBackgroundRouteId2));
1121 EXPECT_EQ(ResourceScheduler::THROTTLED,
1122 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1123 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1124 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1126 // 2 audible, 2 hidden
1127 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1128 EXPECT_FALSE(scheduler_.active_clients_loaded());
1129 EXPECT_EQ(ResourceScheduler::THROTTLED,
1130 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1131 kBackgroundRouteId));
1132 EXPECT_EQ(ResourceScheduler::THROTTLED,
1133 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1134 kBackgroundRouteId2));
1135 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1136 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1137 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1138 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1140 // 1 audible, 3 hidden
1141 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1142 EXPECT_FALSE(scheduler_.active_clients_loaded());
1143 EXPECT_EQ(ResourceScheduler::THROTTLED,
1144 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1145 kBackgroundRouteId));
1146 EXPECT_EQ(ResourceScheduler::THROTTLED,
1147 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1148 kBackgroundRouteId2));
1149 EXPECT_EQ(ResourceScheduler::THROTTLED,
1150 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1151 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1152 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1154 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1155 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1158 TEST_F(ResourceSchedulerTest,
1159 LoadedClientVisibilityChangedCorrectlyUnthrottles) {
1160 // TODO(aiolos): remove when throttling and coalescing have both landed
1161 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1162 false /* should_coalesce */);
1163 scheduler_.OnClientCreated(kChildId2, kRouteId2, false);
1164 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1165 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1166 scheduler_.OnLoadingStateChanged(
1167 kBackgroundChildId2, kBackgroundRouteId2, true);
1168 // 1 visible, 3 hidden
1169 EXPECT_FALSE(scheduler_.active_clients_loaded());
1170 EXPECT_EQ(ResourceScheduler::THROTTLED,
1171 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1172 kBackgroundRouteId));
1173 EXPECT_EQ(ResourceScheduler::THROTTLED,
1174 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1175 kBackgroundRouteId2));
1176 EXPECT_EQ(ResourceScheduler::THROTTLED,
1177 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1178 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1179 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1181 // 2 visible, 2 hidden
1182 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1183 EXPECT_FALSE(scheduler_.active_clients_loaded());
1184 EXPECT_EQ(ResourceScheduler::THROTTLED,
1185 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1186 kBackgroundRouteId));
1187 EXPECT_EQ(ResourceScheduler::THROTTLED,
1188 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1189 kBackgroundRouteId2));
1190 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1191 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1192 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1193 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1195 // 1 visible, 3 hidden
1196 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1197 EXPECT_FALSE(scheduler_.active_clients_loaded());
1198 EXPECT_EQ(ResourceScheduler::THROTTLED,
1199 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1200 kBackgroundRouteId));
1201 EXPECT_EQ(ResourceScheduler::THROTTLED,
1202 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1203 kBackgroundRouteId2));
1204 EXPECT_EQ(ResourceScheduler::THROTTLED,
1205 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1206 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1207 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1209 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1210 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1213 TEST_F(ResourceSchedulerTest,
1214 LoadedClientAudibilityChangedCorrectlyUnthrottles) {
1215 // TODO(aiolos): remove when throttling and coalescing have both landed
1216 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1217 false /* should_coalesce */);
1218 scheduler_.OnClientCreated(kChildId2, kRouteId2, false);
1219 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1220 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1221 scheduler_.OnLoadingStateChanged(
1222 kBackgroundChildId2, kBackgroundRouteId2, true);
1223 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1224 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1225 // 1 audible, 3 hidden
1226 EXPECT_FALSE(scheduler_.active_clients_loaded());
1227 EXPECT_EQ(ResourceScheduler::THROTTLED,
1228 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1229 kBackgroundRouteId));
1230 EXPECT_EQ(ResourceScheduler::THROTTLED,
1231 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1232 kBackgroundRouteId2));
1233 EXPECT_EQ(ResourceScheduler::THROTTLED,
1234 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1235 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1236 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1238 // 2 audible, 2 hidden
1239 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1240 EXPECT_FALSE(scheduler_.active_clients_loaded());
1241 EXPECT_EQ(ResourceScheduler::THROTTLED,
1242 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1243 kBackgroundRouteId));
1244 EXPECT_EQ(ResourceScheduler::THROTTLED,
1245 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1246 kBackgroundRouteId2));
1247 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1248 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1249 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1250 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1252 // 1 audible, 3 hidden
1253 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1254 EXPECT_FALSE(scheduler_.active_clients_loaded());
1255 EXPECT_EQ(ResourceScheduler::THROTTLED,
1256 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1257 kBackgroundRouteId));
1258 EXPECT_EQ(ResourceScheduler::THROTTLED,
1259 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1260 kBackgroundRouteId2));
1261 EXPECT_EQ(ResourceScheduler::THROTTLED,
1262 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1263 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1264 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1266 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1267 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1270 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesHiddenCorrectlyUnthrottles) {
1271 // TODO(aiolos): remove when throttling and coalescing have both landed
1272 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1273 false /* should_coalesce */);
1274 scheduler_.OnClientCreated(kChildId2, kRouteId2, true);
1275 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1276 scheduler_.OnLoadingStateChanged(
1277 kBackgroundChildId2, kBackgroundRouteId2, true);
1279 // 2 visible, 2 hidden
1280 EXPECT_FALSE(scheduler_.active_clients_loaded());
1281 EXPECT_EQ(ResourceScheduler::THROTTLED,
1282 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1283 kBackgroundRouteId));
1284 EXPECT_EQ(ResourceScheduler::THROTTLED,
1285 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1286 kBackgroundRouteId2));
1287 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1288 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1289 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1290 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1292 // 1 visible, 3 hidden
1293 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1294 EXPECT_FALSE(scheduler_.active_clients_loaded());
1295 EXPECT_EQ(ResourceScheduler::THROTTLED,
1296 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1297 kBackgroundRouteId));
1298 EXPECT_EQ(ResourceScheduler::THROTTLED,
1299 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1300 kBackgroundRouteId2));
1301 EXPECT_EQ(ResourceScheduler::THROTTLED,
1302 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1303 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1304 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1306 // 0 visible, 4 hidden
1307 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1308 EXPECT_TRUE(scheduler_.active_clients_loaded());
1309 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1310 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1311 kBackgroundRouteId));
1312 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1313 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1314 kBackgroundRouteId2));
1315 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1316 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1317 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1318 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1320 // 1 visible, 3 hidden
1321 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
1322 EXPECT_FALSE(scheduler_.active_clients_loaded());
1323 EXPECT_EQ(ResourceScheduler::THROTTLED,
1324 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1325 kBackgroundRouteId));
1326 EXPECT_EQ(ResourceScheduler::THROTTLED,
1327 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1328 kBackgroundRouteId2));
1329 EXPECT_EQ(ResourceScheduler::THROTTLED,
1330 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1331 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1332 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1334 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1335 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1338 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesSilentCorrectlyUnthrottles) {
1339 // TODO(aiolos): remove when throttling and coalescing have both landed
1340 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1341 false /* should_coalesce */);
1342 scheduler_.OnClientCreated(kChildId2, kRouteId2, false);
1343 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1344 scheduler_.OnLoadingStateChanged(
1345 kBackgroundChildId2, kBackgroundRouteId2, true);
1346 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1347 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1348 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1349 // 2 audible, 2 hidden
1350 EXPECT_FALSE(scheduler_.active_clients_loaded());
1351 EXPECT_EQ(ResourceScheduler::THROTTLED,
1352 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1353 kBackgroundRouteId));
1354 EXPECT_EQ(ResourceScheduler::THROTTLED,
1355 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1356 kBackgroundRouteId2));
1357 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1358 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1359 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1360 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1362 // 1 audible, 3 hidden
1363 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1364 EXPECT_FALSE(scheduler_.active_clients_loaded());
1365 EXPECT_EQ(ResourceScheduler::THROTTLED,
1366 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1367 kBackgroundRouteId));
1368 EXPECT_EQ(ResourceScheduler::THROTTLED,
1369 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1370 kBackgroundRouteId2));
1371 EXPECT_EQ(ResourceScheduler::THROTTLED,
1372 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1373 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1374 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1376 // 0 audible, 4 hidden
1377 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1378 EXPECT_TRUE(scheduler_.active_clients_loaded());
1379 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1380 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1381 kBackgroundRouteId));
1382 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1383 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1384 kBackgroundRouteId2));
1385 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1386 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1387 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1388 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1390 // 1 audible, 3 hidden
1391 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1392 EXPECT_FALSE(scheduler_.active_clients_loaded());
1393 EXPECT_EQ(ResourceScheduler::THROTTLED,
1394 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1395 kBackgroundRouteId));
1396 EXPECT_EQ(ResourceScheduler::THROTTLED,
1397 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1398 kBackgroundRouteId2));
1399 EXPECT_EQ(ResourceScheduler::THROTTLED,
1400 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1401 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1402 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1404 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1405 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1408 TEST_F(ResourceSchedulerTest, LoadedClientBecomesHiddenCorrectlyThrottles) {
1409 // TODO(aiolos): remove when throttling and coalescing have both landed
1410 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1411 false /* should_coalesce */);
1412 scheduler_.OnClientCreated(kChildId2, kRouteId2, true);
1413 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1414 scheduler_.OnLoadingStateChanged(
1415 kBackgroundChildId2, kBackgroundRouteId2, true);
1416 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1417 // 2 visible, 2 hidden
1418 EXPECT_FALSE(scheduler_.active_clients_loaded());
1419 EXPECT_EQ(ResourceScheduler::THROTTLED,
1420 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1421 kBackgroundRouteId));
1422 EXPECT_EQ(ResourceScheduler::THROTTLED,
1423 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1424 kBackgroundRouteId2));
1425 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1426 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1427 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1428 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1430 // 1 visible, 3 hidden
1431 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1432 EXPECT_FALSE(scheduler_.active_clients_loaded());
1433 EXPECT_EQ(ResourceScheduler::THROTTLED,
1434 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1435 kBackgroundRouteId));
1436 EXPECT_EQ(ResourceScheduler::THROTTLED,
1437 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1438 kBackgroundRouteId2));
1439 EXPECT_EQ(ResourceScheduler::THROTTLED,
1440 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1441 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1442 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1444 // 0 visible, 4 hidden
1445 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1446 EXPECT_TRUE(scheduler_.active_clients_loaded());
1447 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1448 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1449 kBackgroundRouteId));
1450 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1451 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1452 kBackgroundRouteId2));
1453 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1454 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1455 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1456 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1458 // 1 visible, 3 hidden
1459 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1460 EXPECT_TRUE(scheduler_.active_clients_loaded());
1461 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1462 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1463 kBackgroundRouteId));
1464 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1465 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1466 kBackgroundRouteId2));
1467 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1468 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1469 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1470 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1472 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1473 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1476 TEST_F(ResourceSchedulerTest, LoadedClientBecomesSilentCorrectlyThrottles) {
1477 // TODO(aiolos): remove when throttling and coalescing have both landed
1478 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1479 false /* should_coalesce */);
1480 scheduler_.OnClientCreated(kChildId2, kRouteId2, false);
1481 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1482 scheduler_.OnLoadingStateChanged(
1483 kBackgroundChildId2, kBackgroundRouteId2, true);
1484 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1485 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1486 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1487 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1488 // 2 audible, 2 hidden
1489 EXPECT_FALSE(scheduler_.active_clients_loaded());
1490 EXPECT_EQ(ResourceScheduler::THROTTLED,
1491 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1492 kBackgroundRouteId));
1493 EXPECT_EQ(ResourceScheduler::THROTTLED,
1494 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1495 kBackgroundRouteId2));
1496 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1497 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1498 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1499 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1501 // 1 audible, 3 hidden
1502 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1503 EXPECT_FALSE(scheduler_.active_clients_loaded());
1504 EXPECT_EQ(ResourceScheduler::THROTTLED,
1505 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1506 kBackgroundRouteId));
1507 EXPECT_EQ(ResourceScheduler::THROTTLED,
1508 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1509 kBackgroundRouteId2));
1510 EXPECT_EQ(ResourceScheduler::THROTTLED,
1511 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1512 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1513 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1515 // 0 audible, 4 hidden
1516 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1517 EXPECT_TRUE(scheduler_.active_clients_loaded());
1518 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1519 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1520 kBackgroundRouteId));
1521 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1522 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1523 kBackgroundRouteId2));
1524 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1525 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1526 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1527 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1529 // 1 audible, 3 hidden
1530 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1531 EXPECT_TRUE(scheduler_.active_clients_loaded());
1532 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1533 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1534 kBackgroundRouteId));
1535 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1536 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1537 kBackgroundRouteId2));
1538 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1539 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1540 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1541 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1543 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1544 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1547 TEST_F(ResourceSchedulerTest, HiddenLoadedChangesCorrectlyStayThrottled) {
1548 // TODO(aiolos): remove when throttling and coalescing have both landed
1549 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1550 false /* should_coalesce */);
1551 scheduler_.OnClientCreated(kChildId2, kRouteId2, true);
1552 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1554 // 1 visible and 2 hidden loading, 1 visible loaded
1555 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1556 EXPECT_FALSE(scheduler_.active_clients_loaded());
1557 EXPECT_EQ(ResourceScheduler::THROTTLED,
1558 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1559 kBackgroundRouteId));
1560 EXPECT_EQ(ResourceScheduler::THROTTLED,
1561 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1562 kBackgroundRouteId2));
1563 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1564 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1565 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1566 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1568 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1569 scheduler_.OnLoadingStateChanged(
1570 kBackgroundChildId2, kBackgroundRouteId2, true);
1571 EXPECT_FALSE(scheduler_.active_clients_loaded());
1572 EXPECT_EQ(ResourceScheduler::THROTTLED,
1573 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1574 kBackgroundRouteId));
1575 EXPECT_EQ(ResourceScheduler::THROTTLED,
1576 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1577 kBackgroundRouteId2));
1578 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1579 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1580 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1581 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1583 // 1 visible loading, 1 visible and 2 hidden loaded
1584 scheduler_.OnLoadingStateChanged(
1585 kBackgroundChildId, kBackgroundRouteId, true);
1586 EXPECT_FALSE(scheduler_.active_clients_loaded());
1587 EXPECT_EQ(ResourceScheduler::THROTTLED,
1588 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1589 kBackgroundRouteId));
1590 EXPECT_EQ(ResourceScheduler::THROTTLED,
1591 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1592 kBackgroundRouteId2));
1593 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1594 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1595 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1596 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1598 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1599 scheduler_.OnLoadingStateChanged(
1600 kBackgroundChildId2, kBackgroundRouteId2, true);
1601 EXPECT_FALSE(scheduler_.active_clients_loaded());
1602 EXPECT_EQ(ResourceScheduler::THROTTLED,
1603 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1604 kBackgroundRouteId));
1605 EXPECT_EQ(ResourceScheduler::THROTTLED,
1606 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1607 kBackgroundRouteId2));
1608 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1609 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1610 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1611 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1613 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1614 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1617 TEST_F(ResourceSchedulerTest, PartialVisibleClientLoadedDoesNotUnthrottle) {
1618 // TODO(aiolos): remove when throttling and coalescing have both landed
1619 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1620 false /* should_coalesce */);
1621 scheduler_.OnClientCreated(kChildId2, kRouteId2, true);
1622 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1624 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1625 scheduler_.OnLoadingStateChanged(
1626 kBackgroundChildId2, kBackgroundRouteId2, true);
1627 EXPECT_FALSE(scheduler_.active_clients_loaded());
1628 EXPECT_EQ(ResourceScheduler::THROTTLED,
1629 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1630 kBackgroundRouteId));
1631 EXPECT_EQ(ResourceScheduler::THROTTLED,
1632 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1633 kBackgroundRouteId2));
1634 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1635 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1636 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1637 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1639 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1640 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1641 EXPECT_FALSE(scheduler_.active_clients_loaded());
1642 EXPECT_EQ(ResourceScheduler::THROTTLED,
1643 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1644 kBackgroundRouteId));
1645 EXPECT_EQ(ResourceScheduler::THROTTLED,
1646 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1647 kBackgroundRouteId2));
1648 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1649 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1650 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1651 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1653 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1654 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1655 EXPECT_FALSE(scheduler_.active_clients_loaded());
1656 EXPECT_EQ(ResourceScheduler::THROTTLED,
1657 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1658 kBackgroundRouteId));
1659 EXPECT_EQ(ResourceScheduler::THROTTLED,
1660 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1661 kBackgroundRouteId2));
1662 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1663 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1664 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1665 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1667 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1668 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1671 TEST_F(ResourceSchedulerTest, FullVisibleLoadedCorrectlyUnthrottle) {
1672 // TODO(aiolos): remove when throttling and coalescing have both landed
1673 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1674 false /* should_coalesce */);
1675 scheduler_.OnClientCreated(kChildId2, kRouteId2, true);
1676 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1678 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1679 scheduler_.OnLoadingStateChanged(
1680 kBackgroundChildId2, kBackgroundRouteId2, true);
1681 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1682 EXPECT_FALSE(scheduler_.active_clients_loaded());
1683 EXPECT_EQ(ResourceScheduler::THROTTLED,
1684 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1685 kBackgroundRouteId));
1686 EXPECT_EQ(ResourceScheduler::THROTTLED,
1687 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1688 kBackgroundRouteId2));
1689 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1690 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1691 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1692 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1694 scoped_ptr<TestRequest> high(
1695 NewBackgroundRequest("http://host/high", net::HIGHEST));
1696 scoped_ptr<TestRequest> low(
1697 NewBackgroundRequest("http://host/low", net::LOWEST));
1699 EXPECT_TRUE(high->started());
1700 EXPECT_FALSE(low->started());
1702 // 2 visible loaded, 1 hidden loading, 1 hidden loaded
1703 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1704 EXPECT_TRUE(scheduler_.active_clients_loaded());
1705 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1706 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1707 kBackgroundRouteId));
1708 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1709 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1710 kBackgroundRouteId2));
1711 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1712 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1713 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1714 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1715 // kBackgroundClientId unthrottling should unthrottle it's request.
1716 EXPECT_TRUE(low->started());
1718 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1719 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1720 EXPECT_FALSE(scheduler_.active_clients_loaded());
1721 EXPECT_EQ(ResourceScheduler::THROTTLED,
1722 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1723 kBackgroundRouteId));
1724 EXPECT_EQ(ResourceScheduler::THROTTLED,
1725 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1726 kBackgroundRouteId2));
1727 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1728 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1729 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1730 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1732 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1733 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1736 TEST_F(ResourceSchedulerTest,
1737 ActiveAndLoadingClientDeletedCorrectlyUnthrottle) {
1738 // TODO(aiolos): remove when throttling and coalescing have both landed
1739 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1740 false /* should_coalesce */);
1741 scheduler_.OnClientCreated(kChildId2, kRouteId2, true);
1742 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1744 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1745 scheduler_.OnLoadingStateChanged(
1746 kBackgroundChildId2, kBackgroundRouteId2, true);
1747 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1748 EXPECT_FALSE(scheduler_.active_clients_loaded());
1749 EXPECT_EQ(ResourceScheduler::THROTTLED,
1750 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1751 kBackgroundRouteId));
1752 EXPECT_EQ(ResourceScheduler::THROTTLED,
1753 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1754 kBackgroundRouteId2));
1755 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1756 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1757 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1758 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1760 // 1 visible loaded, 1 hidden loading, 1 hidden loaded
1761 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1762 EXPECT_TRUE(scheduler_.active_clients_loaded());
1763 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1764 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1765 kBackgroundRouteId));
1766 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1767 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1768 kBackgroundRouteId2));
1769 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1770 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1772 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1773 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1774 EXPECT_FALSE(scheduler_.active_clients_loaded());
1775 EXPECT_EQ(ResourceScheduler::THROTTLED,
1776 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1777 kBackgroundRouteId));
1778 EXPECT_EQ(ResourceScheduler::THROTTLED,
1779 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1780 kBackgroundRouteId2));
1781 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1782 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1784 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1787 TEST_F(ResourceSchedulerTest, CoalescedClientCreationStartsTimer) {
1788 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1789 true /* should_coalesce */);
1790 EXPECT_FALSE(mock_timer_->IsRunning());
1791 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1792 EXPECT_FALSE(mock_timer_->IsRunning());
1793 scheduler_.OnLoadingStateChanged(
1794 kBackgroundChildId, kBackgroundRouteId, true);
1795 EXPECT_EQ(ResourceScheduler::COALESCED,
1796 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1797 kBackgroundRouteId));
1798 EXPECT_TRUE(mock_timer_->IsRunning());
1801 TEST_F(ResourceSchedulerTest, ActiveLoadingClientLoadedAndHiddenStartsTimer) {
1802 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1803 true /* should_coalesce */);
1804 EXPECT_FALSE(mock_timer_->IsRunning());
1805 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1806 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1807 EXPECT_EQ(ResourceScheduler::THROTTLED,
1808 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1809 kBackgroundRouteId));
1810 EXPECT_FALSE(mock_timer_->IsRunning());
1812 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1813 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1814 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1815 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1816 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1817 kBackgroundRouteId));
1818 EXPECT_FALSE(mock_timer_->IsRunning());
1820 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1821 EXPECT_EQ(ResourceScheduler::COALESCED,
1822 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1823 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1824 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1825 kBackgroundRouteId));
1826 EXPECT_TRUE(mock_timer_->IsRunning());
1829 TEST_F(ResourceSchedulerTest, ActiveLoadingClientHiddenAndLoadedStartsTimer) {
1830 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1831 true /* should_coalesce */);
1832 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1833 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1834 EXPECT_EQ(ResourceScheduler::THROTTLED,
1835 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1836 kBackgroundRouteId));
1837 EXPECT_FALSE(mock_timer_->IsRunning());
1839 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1840 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1841 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1842 EXPECT_FALSE(mock_timer_->IsRunning());
1844 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1845 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1846 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1847 kBackgroundRouteId));
1848 EXPECT_EQ(ResourceScheduler::COALESCED,
1849 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1850 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1851 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1852 kBackgroundRouteId));
1853 EXPECT_TRUE(mock_timer_->IsRunning());
1856 TEST_F(ResourceSchedulerTest, CoalescedClientBecomesAudibleStopsTimer) {
1857 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1858 true /* should_coalesce */);
1859 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1860 EXPECT_FALSE(mock_timer_->IsRunning());
1861 scheduler_.OnLoadingStateChanged(
1862 kBackgroundChildId, kBackgroundRouteId, true);
1863 EXPECT_EQ(ResourceScheduler::COALESCED,
1864 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1865 kBackgroundRouteId));
1866 EXPECT_TRUE(mock_timer_->IsRunning());
1868 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1869 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1870 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1871 kBackgroundRouteId));
1872 EXPECT_FALSE(mock_timer_->IsRunning());
1875 TEST_F(ResourceSchedulerTest, LastCoalescedClientDeletionStopsTimer) {
1876 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1877 true /* should_coalesce */);
1878 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1879 EXPECT_FALSE(mock_timer_->IsRunning());
1880 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1881 EXPECT_FALSE(mock_timer_->IsRunning());
1882 scheduler_.OnLoadingStateChanged(
1883 kBackgroundChildId, kBackgroundRouteId, true);
1884 EXPECT_EQ(ResourceScheduler::COALESCED,
1885 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1886 kBackgroundRouteId));
1887 scheduler_.OnLoadingStateChanged(
1888 kBackgroundChildId2, kBackgroundRouteId2, true);
1889 EXPECT_EQ(ResourceScheduler::COALESCED,
1890 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1891 kBackgroundRouteId2));
1892 EXPECT_TRUE(mock_timer_->IsRunning());
1894 scheduler_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
1895 EXPECT_TRUE(mock_timer_->IsRunning());
1897 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1898 EXPECT_FALSE(mock_timer_->IsRunning());
1900 // To avoid errors on test tear down.
1901 scheduler_.OnClientCreated(kBackgroundChildId, kBackgroundRouteId, false);
1904 TEST_F(ResourceSchedulerTest, LastCoalescedClientStartsLoadingStopsTimer) {
1905 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1906 true /* should_coalesce */);
1907 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1908 EXPECT_FALSE(mock_timer_->IsRunning());
1909 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1910 EXPECT_FALSE(mock_timer_->IsRunning());
1911 scheduler_.OnLoadingStateChanged(
1912 kBackgroundChildId, kBackgroundRouteId, true);
1913 EXPECT_EQ(ResourceScheduler::COALESCED,
1914 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1915 kBackgroundRouteId));
1916 scheduler_.OnLoadingStateChanged(
1917 kBackgroundChildId2, kBackgroundRouteId2, true);
1918 EXPECT_EQ(ResourceScheduler::COALESCED,
1919 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1920 kBackgroundRouteId2));
1921 EXPECT_TRUE(mock_timer_->IsRunning());
1923 scheduler_.OnLoadingStateChanged(
1924 kBackgroundChildId, kBackgroundRouteId, false);
1925 EXPECT_TRUE(mock_timer_->IsRunning());
1927 scheduler_.OnLoadingStateChanged(
1928 kBackgroundChildId2, kBackgroundRouteId2, false);
1929 EXPECT_FALSE(mock_timer_->IsRunning());
1931 // This is needed to avoid errors on test tear down.
1932 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1935 TEST_F(ResourceSchedulerTest, LastCoalescedClientBecomesVisibleStopsTimer) {
1936 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1937 true /* should_coalesce */);
1938 scheduler_.OnClientCreated(kBackgroundChildId2, kBackgroundRouteId2, false);
1939 EXPECT_FALSE(mock_timer_->IsRunning());
1940 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1941 EXPECT_FALSE(mock_timer_->IsRunning());
1942 scheduler_.OnLoadingStateChanged(
1943 kBackgroundChildId, kBackgroundRouteId, true);
1944 EXPECT_EQ(ResourceScheduler::COALESCED,
1945 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1946 kBackgroundRouteId));
1947 scheduler_.OnLoadingStateChanged(
1948 kBackgroundChildId2, kBackgroundRouteId2, true);
1949 EXPECT_EQ(ResourceScheduler::COALESCED,
1950 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1951 kBackgroundRouteId2));
1952 EXPECT_TRUE(mock_timer_->IsRunning());
1954 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1955 EXPECT_TRUE(mock_timer_->IsRunning());
1957 scheduler_.OnVisibilityChanged(
1958 kBackgroundChildId2, kBackgroundRouteId2, true);
1959 EXPECT_FALSE(mock_timer_->IsRunning());
1961 // To avoid errors on test tear down.
1962 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1965 TEST_F(ResourceSchedulerTest,
1966 CoalescedClientBecomesLoadingAndVisibleStopsTimer) {
1967 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1968 true /* should_coalesce */);
1969 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1970 EXPECT_FALSE(mock_timer_->IsRunning());
1971 scheduler_.OnLoadingStateChanged(
1972 kBackgroundChildId, kBackgroundRouteId, true);
1973 EXPECT_EQ(ResourceScheduler::COALESCED,
1974 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1975 kBackgroundRouteId));
1976 EXPECT_TRUE(mock_timer_->IsRunning());
1978 scheduler_.OnLoadingStateChanged(
1979 kBackgroundChildId, kBackgroundRouteId, false);
1980 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1981 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1982 kBackgroundRouteId));
1983 EXPECT_FALSE(mock_timer_->IsRunning());
1985 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1986 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1987 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1988 kBackgroundRouteId));
1989 EXPECT_FALSE(mock_timer_->IsRunning());
1992 TEST_F(ResourceSchedulerTest, CoalescedRequestsIssueOnTimer) {
1993 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1994 true /* should_coalesce */);
1995 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1996 scheduler_.OnLoadingStateChanged(
1997 kBackgroundChildId, kBackgroundRouteId, true);
1998 EXPECT_EQ(ResourceScheduler::COALESCED,
1999 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2000 kBackgroundRouteId));
2001 EXPECT_TRUE(scheduler_.active_clients_loaded());
2003 scoped_ptr<TestRequest> high(
2004 NewBackgroundRequest("http://host/high", net::HIGHEST));
2005 scoped_ptr<TestRequest> low(
2006 NewBackgroundRequest("http://host/low", net::LOWEST));
2007 EXPECT_FALSE(high->started());
2008 EXPECT_FALSE(low->started());
2010 FireCoalescingTimer();
2012 EXPECT_TRUE(high->started());
2013 EXPECT_TRUE(low->started());
2016 TEST_F(ResourceSchedulerTest, CoalescedRequestsUnthrottleCorrectlyOnTimer) {
2017 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2018 true /* should_coalesce */);
2019 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2020 scheduler_.OnLoadingStateChanged(
2021 kBackgroundChildId, kBackgroundRouteId, true);
2022 EXPECT_EQ(ResourceScheduler::COALESCED,
2023 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2024 kBackgroundRouteId));
2025 EXPECT_TRUE(scheduler_.active_clients_loaded());
2027 scoped_ptr<TestRequest> high(
2028 NewBackgroundRequest("http://host/high", net::HIGHEST));
2029 scoped_ptr<TestRequest> high2(
2030 NewBackgroundRequest("http://host/high", net::HIGHEST));
2031 scoped_ptr<TestRequest> high3(
2032 NewBackgroundRequest("http://host/high", net::HIGHEST));
2033 scoped_ptr<TestRequest> high4(
2034 NewBackgroundRequest("http://host/high", net::HIGHEST));
2035 scoped_ptr<TestRequest> low(
2036 NewBackgroundRequest("http://host/low", net::LOWEST));
2037 scoped_ptr<TestRequest> low2(
2038 NewBackgroundRequest("http://host/low", net::LOWEST));
2039 scoped_ptr<TestRequest> low3(
2040 NewBackgroundRequest("http://host/low", net::LOWEST));
2041 scoped_ptr<TestRequest> low4(
2042 NewBackgroundRequest("http://host/low", net::LOWEST));
2044 http_server_properties_.SetSupportsSpdy(net::HostPortPair("spdyhost", 443),
2045 true);
2046 scoped_ptr<TestRequest> low_spdy(
2047 NewBackgroundRequest("https://spdyhost/low", net::LOW));
2048 scoped_ptr<TestRequest> sync_request(
2049 NewBackgroundSyncRequest("http://host/req", net::LOW));
2050 scoped_ptr<TestRequest> non_http_request(
2051 NewBackgroundRequest("chrome-extension://req", net::LOW));
2053 // Sync requests should issue immediately.
2054 EXPECT_TRUE(sync_request->started());
2055 // Non-http(s) requests should issue immediately.
2056 EXPECT_TRUE(non_http_request->started());
2057 // Nothing else should issue without a timer fire.
2058 EXPECT_FALSE(high->started());
2059 EXPECT_FALSE(high2->started());
2060 EXPECT_FALSE(high3->started());
2061 EXPECT_FALSE(high4->started());
2062 EXPECT_FALSE(low->started());
2063 EXPECT_FALSE(low2->started());
2064 EXPECT_FALSE(low3->started());
2065 EXPECT_FALSE(low4->started());
2066 EXPECT_FALSE(low_spdy->started());
2068 FireCoalescingTimer();
2070 // All high priority requests should issue.
2071 EXPECT_TRUE(high->started());
2072 EXPECT_TRUE(high2->started());
2073 EXPECT_TRUE(high3->started());
2074 EXPECT_TRUE(high4->started());
2075 // There should only be one net::LOWEST priority request issued with
2076 // non-delayable requests in flight.
2077 EXPECT_TRUE(low->started());
2078 EXPECT_FALSE(low2->started());
2079 EXPECT_FALSE(low3->started());
2080 EXPECT_FALSE(low4->started());
2081 // Spdy-Enable requests should issue regardless of priority.
2082 EXPECT_TRUE(low_spdy->started());
2085 TEST_F(ResourceSchedulerTest, CoalescedRequestsWaitForNextTimer) {
2086 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2087 true /* should_coalesce */);
2088 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2089 scheduler_.OnLoadingStateChanged(
2090 kBackgroundChildId, kBackgroundRouteId, true);
2092 EXPECT_EQ(ResourceScheduler::COALESCED,
2093 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2094 kBackgroundRouteId));
2095 EXPECT_TRUE(scheduler_.active_clients_loaded());
2097 scoped_ptr<TestRequest> high(
2098 NewBackgroundRequest("http://host/high", net::HIGHEST));
2099 EXPECT_FALSE(high->started());
2101 FireCoalescingTimer();
2103 scoped_ptr<TestRequest> high2(
2104 NewBackgroundRequest("http://host/high2", net::HIGHEST));
2105 scoped_ptr<TestRequest> low(
2106 NewBackgroundRequest("http://host/low", net::LOWEST));
2108 EXPECT_TRUE(high->started());
2109 EXPECT_FALSE(high2->started());
2110 EXPECT_FALSE(low->started());
2112 FireCoalescingTimer();
2114 EXPECT_TRUE(high->started());
2115 EXPECT_TRUE(high2->started());
2116 EXPECT_TRUE(low->started());
2119 TEST_F(ResourceSchedulerTest, GetVisualSignalFromRenderViewHost) {
2120 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory;
2121 scoped_ptr<TestBrowserContext> browser_context;
2122 scoped_ptr<TestWebContents> web_contents_1;
2123 scoped_ptr<TestWebContents> web_contents_2;
2125 render_process_host_factory.reset(new MockRenderProcessHostFactory());
2126 browser_context.reset(new TestBrowserContext());
2127 scoped_refptr<SiteInstance> site_instance_1 =
2128 SiteInstance::Create(browser_context.get());
2129 scoped_refptr<SiteInstance> site_instance_2 =
2130 SiteInstance::Create(browser_context.get());
2131 SiteInstanceImpl::set_render_process_host_factory(
2132 render_process_host_factory.get());
2134 web_contents_1.reset(
2135 TestWebContents::Create(browser_context.get(), site_instance_1.get()));
2136 web_contents_2.reset(
2137 TestWebContents::Create(browser_context.get(), site_instance_2.get()));
2138 base::RunLoop().RunUntilIdle();
2140 RenderViewHostImpl* rvh1 = web_contents_1->GetRenderViewHost();
2141 RenderViewHostImpl* rvh2 = web_contents_2->GetRenderViewHost();
2142 ResourceScheduler* scheduler = ResourceDispatcherHostImpl::Get()->scheduler();
2144 // Check initial visibility is set correctly.
2145 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2146 rvh1->GetRoutingID()),
2147 !rvh1->is_hidden());
2148 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2149 rvh1->GetRoutingID()),
2150 !rvh2->is_hidden());
2152 // 1 visible, 1 hidden
2153 rvh1->WasShown(ui::LatencyInfo());
2154 rvh2->WasHidden();
2155 base::RunLoop().RunUntilIdle();
2157 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2158 rvh1->GetRoutingID()));
2159 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2160 rvh2->GetRoutingID()));
2162 // Flip the visibility and check again.
2163 rvh1->WasHidden();
2164 rvh2->WasShown(ui::LatencyInfo());
2165 base::RunLoop().RunUntilIdle();
2167 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2168 rvh1->GetRoutingID()));
2169 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2170 rvh2->GetRoutingID()));
2171 // Clean up.
2172 web_contents_1.reset();
2173 web_contents_2.reset();
2174 base::RunLoop().RunUntilIdle();
2176 browser_context.reset();
2177 render_process_host_factory.reset();
2180 } // unnamed namespace
2182 } // namespace content