Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / loader / resource_scheduler_unittest.cc
blob2847fbf31919531190105ab908ab684b39b576b7
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/loader/resource_scheduler.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/timer/mock_timer.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/loader/resource_dispatcher_host_impl.h"
15 #include "content/browser/loader/resource_message_filter.h"
16 #include "content/browser/loader/resource_request_info_impl.h"
17 #include "content/common/resource_messages.h"
18 #include "content/public/browser/resource_context.h"
19 #include "content/public/browser/resource_controller.h"
20 #include "content/public/browser/resource_throttle.h"
21 #include "content/public/common/process_type.h"
22 #include "content/public/common/resource_type.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/test/test_render_view_host_factory.h"
26 #include "content/test/test_web_contents.h"
27 #include "net/base/host_port_pair.h"
28 #include "net/base/request_priority.h"
29 #include "net/http/http_server_properties_impl.h"
30 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "ui/events/latency_info.h"
35 using std::string;
37 namespace content {
39 namespace {
41 class TestRequestFactory;
43 const int kChildId = 30;
44 const int kRouteId = 75;
45 const int kChildId2 = 43;
46 const int kRouteId2 = 67;
47 const int kBackgroundChildId = 35;
48 const int kBackgroundRouteId = 43;
49 const int kBackgroundChildId2 = 54;
50 const int kBackgroundRouteId2 = 82;
52 class TestRequest : public ResourceController {
53 public:
54 TestRequest(scoped_ptr<ResourceThrottle> throttle,
55 scoped_ptr<net::URLRequest> url_request)
56 : started_(false),
57 throttle_(throttle.Pass()),
58 url_request_(url_request.Pass()) {
59 throttle_->set_controller_for_testing(this);
61 ~TestRequest() override {}
63 bool started() const { return started_; }
65 void Start() {
66 bool deferred = false;
67 throttle_->WillStartRequest(&deferred);
68 started_ = !deferred;
71 void Cancel() override {
72 // Alert the scheduler that the request can be deleted.
73 throttle_.reset(0);
76 const net::URLRequest* url_request() const { return url_request_.get(); }
78 protected:
79 // ResourceController interface:
80 void CancelAndIgnore() override {}
81 void CancelWithError(int error_code) override {}
82 void Resume() override { started_ = true; }
84 private:
85 bool started_;
86 scoped_ptr<ResourceThrottle> throttle_;
87 scoped_ptr<net::URLRequest> url_request_;
90 class CancelingTestRequest : public TestRequest {
91 public:
92 CancelingTestRequest(scoped_ptr<ResourceThrottle> throttle,
93 scoped_ptr<net::URLRequest> url_request)
94 : TestRequest(throttle.Pass(), url_request.Pass()) {}
96 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) {
97 request_to_cancel_ = request_to_cancel.Pass();
100 private:
101 void Resume() override {
102 TestRequest::Resume();
103 request_to_cancel_.reset();
106 scoped_ptr<TestRequest> request_to_cancel_;
109 class FakeResourceContext : public ResourceContext {
110 private:
111 net::HostResolver* GetHostResolver() override { return NULL; }
112 net::URLRequestContext* GetRequestContext() override { return NULL; }
115 class FakeResourceMessageFilter : public ResourceMessageFilter {
116 public:
117 FakeResourceMessageFilter(int child_id)
118 : ResourceMessageFilter(
119 child_id,
120 PROCESS_TYPE_RENDERER,
121 NULL /* appcache_service */,
122 NULL /* blob_storage_context */,
123 NULL /* file_system_context */,
124 NULL /* service_worker_context */,
125 NULL /* host_zoom_level_context */,
126 base::Bind(&FakeResourceMessageFilter::GetContexts,
127 base::Unretained(this))) {
130 private:
131 ~FakeResourceMessageFilter() override {}
133 void GetContexts(const ResourceHostMsg_Request& request,
134 ResourceContext** resource_context,
135 net::URLRequestContext** request_context) {
136 *resource_context = &context_;
137 *request_context = NULL;
140 FakeResourceContext context_;
143 class ResourceSchedulerTest : public testing::Test {
144 protected:
145 ResourceSchedulerTest()
146 : next_request_id_(0),
147 ui_thread_(BrowserThread::UI, &message_loop_),
148 io_thread_(BrowserThread::IO, &message_loop_),
149 mock_timer_(new base::MockTimer(true, true)) {
150 scheduler_.set_timer_for_testing(scoped_ptr<base::Timer>(mock_timer_));
152 // TODO(aiolos): Remove when throttling and coalescing have both landed.
153 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
154 false /* should_coalesce */);
156 scheduler_.OnClientCreated(kChildId, kRouteId, true, false);
157 scheduler_.OnClientCreated(
158 kBackgroundChildId, kBackgroundRouteId, false, false);
159 context_.set_http_server_properties(http_server_properties_.GetWeakPtr());
162 ~ResourceSchedulerTest() override {
163 scheduler_.OnClientDeleted(kChildId, kRouteId);
164 scheduler_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
167 scoped_ptr<net::URLRequest> NewURLRequestWithChildAndRoute(
168 const char* url,
169 net::RequestPriority priority,
170 int child_id,
171 int route_id,
172 bool is_async) {
173 scoped_ptr<net::URLRequest> url_request(
174 context_.CreateRequest(GURL(url), priority, NULL, NULL));
175 ResourceRequestInfoImpl* info = new ResourceRequestInfoImpl(
176 PROCESS_TYPE_RENDERER, // process_type
177 child_id, // child_id
178 route_id, // route_id
179 0, // origin_pid
180 ++next_request_id_, // request_id
181 MSG_ROUTING_NONE, // render_frame_id
182 false, // is_main_frame
183 false, // parent_is_main_frame
184 0, // parent_render_frame_id
185 RESOURCE_TYPE_SUB_RESOURCE, // resource_type
186 ui::PAGE_TRANSITION_LINK, // transition_type
187 false, // should_replace_current_entry
188 false, // is_download
189 false, // is_stream
190 true, // allow_download
191 false, // has_user_gesture
192 false, // enable_load_timing
193 false, // enable_upload_progress
194 false, // do_not_prompt_for_login
195 blink::WebReferrerPolicyDefault, // referrer_policy
196 blink::WebPageVisibilityStateVisible, // visibility_state
197 NULL, // context
198 base::WeakPtr<ResourceMessageFilter>(), // filter
199 is_async); // is_async
200 info->AssociateWithRequest(url_request.get());
201 return url_request.Pass();
204 scoped_ptr<net::URLRequest> NewURLRequest(const char* url,
205 net::RequestPriority priority) {
206 return NewURLRequestWithChildAndRoute(
207 url, priority, kChildId, kRouteId, true);
210 TestRequest* NewRequestWithRoute(const char* url,
211 net::RequestPriority priority,
212 int route_id) {
213 return NewRequestWithChildAndRoute(url, priority, route_id, kChildId);
216 TestRequest* NewRequestWithChildAndRoute(const char* url,
217 net::RequestPriority priority,
218 int child_id,
219 int route_id) {
220 return GetNewTestRequest(url, priority, child_id, route_id, true);
223 TestRequest* NewRequest(const char* url, net::RequestPriority priority) {
224 return NewRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
227 TestRequest* NewBackgroundRequest(const char* url,
228 net::RequestPriority priority) {
229 return NewRequestWithChildAndRoute(
230 url, priority, kBackgroundChildId, kBackgroundRouteId);
233 TestRequest* NewSyncRequest(const char* url, net::RequestPriority priority) {
234 return NewSyncRequestWithChildAndRoute(url, priority, kChildId, kRouteId);
237 TestRequest* NewBackgroundSyncRequest(const char* url,
238 net::RequestPriority priority) {
239 return NewSyncRequestWithChildAndRoute(
240 url, priority, kBackgroundChildId, kBackgroundRouteId);
243 TestRequest* NewSyncRequestWithChildAndRoute(const char* url,
244 net::RequestPriority priority,
245 int child_id,
246 int route_id) {
247 return GetNewTestRequest(url, priority, child_id, route_id, false);
250 TestRequest* GetNewTestRequest(const char* url,
251 net::RequestPriority priority,
252 int child_id,
253 int route_id,
254 bool is_async) {
255 scoped_ptr<net::URLRequest> url_request(NewURLRequestWithChildAndRoute(
256 url, priority, child_id, route_id, is_async));
257 scoped_ptr<ResourceThrottle> throttle(
258 scheduler_.ScheduleRequest(child_id, route_id, url_request.get()));
259 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass());
260 request->Start();
261 return request;
265 void ChangeRequestPriority(TestRequest* request,
266 net::RequestPriority new_priority,
267 int intra_priority = 0) {
268 scoped_refptr<FakeResourceMessageFilter> filter(
269 new FakeResourceMessageFilter(kChildId));
270 const ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(
271 request->url_request());
272 const GlobalRequestID& id = info->GetGlobalRequestID();
273 ResourceHostMsg_DidChangePriority msg(id.request_id, new_priority,
274 intra_priority);
275 rdh_.OnMessageReceived(msg, filter.get());
278 void FireCoalescingTimer() {
279 EXPECT_TRUE(mock_timer_->IsRunning());
280 mock_timer_->Fire();
283 int next_request_id_;
284 base::MessageLoopForIO message_loop_;
285 BrowserThreadImpl ui_thread_;
286 BrowserThreadImpl io_thread_;
287 ResourceDispatcherHostImpl rdh_;
288 ResourceScheduler scheduler_;
289 base::MockTimer* mock_timer_;
290 net::HttpServerPropertiesImpl http_server_properties_;
291 net::TestURLRequestContext context_;
294 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
295 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOWEST));
296 EXPECT_TRUE(request->started());
299 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilIdle) {
300 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
301 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
302 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
303 EXPECT_TRUE(high->started());
304 EXPECT_TRUE(low->started());
305 EXPECT_FALSE(low2->started());
306 high.reset();
307 EXPECT_TRUE(low2->started());
310 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInserted) {
311 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
312 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
313 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
314 EXPECT_TRUE(high->started());
315 EXPECT_TRUE(low->started());
316 EXPECT_FALSE(low2->started());
317 high.reset();
318 scheduler_.OnWillInsertBody(kChildId, kRouteId);
319 EXPECT_TRUE(low2->started());
322 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilCriticalComplete) {
323 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
324 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
325 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
326 EXPECT_TRUE(high->started());
327 EXPECT_TRUE(low->started());
328 EXPECT_FALSE(low2->started());
329 scheduler_.OnWillInsertBody(kChildId, kRouteId);
330 EXPECT_FALSE(low2->started());
331 high.reset();
332 EXPECT_TRUE(low2->started());
335 TEST_F(ResourceSchedulerTest, LowDoesNotBlockCriticalComplete) {
336 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
337 scoped_ptr<TestRequest> lowest(NewRequest("http://host/lowest", net::LOWEST));
338 scoped_ptr<TestRequest> lowest2(
339 NewRequest("http://host/lowest", net::LOWEST));
340 EXPECT_TRUE(low->started());
341 EXPECT_TRUE(lowest->started());
342 EXPECT_FALSE(lowest2->started());
343 scheduler_.OnWillInsertBody(kChildId, kRouteId);
344 EXPECT_TRUE(lowest2->started());
347 TEST_F(ResourceSchedulerTest, OneLowLoadsUntilBodyInsertedExceptSpdy) {
348 http_server_properties_.SetSupportsSpdy(
349 net::HostPortPair("spdyhost", 443), true);
350 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
351 scoped_ptr<TestRequest> low_spdy(
352 NewRequest("https://spdyhost/low", net::LOWEST));
353 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
354 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
355 EXPECT_TRUE(high->started());
356 EXPECT_TRUE(low_spdy->started());
357 EXPECT_TRUE(low->started());
358 EXPECT_FALSE(low2->started());
359 scheduler_.OnWillInsertBody(kChildId, kRouteId);
360 high.reset();
361 EXPECT_TRUE(low2->started());
364 TEST_F(ResourceSchedulerTest, NavigationResetsState) {
365 scheduler_.OnWillInsertBody(kChildId, kRouteId);
366 scheduler_.OnNavigate(kChildId, kRouteId);
367 scoped_ptr<TestRequest> high(NewRequest("http://host/high", 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(high->started());
371 EXPECT_TRUE(low->started());
372 EXPECT_FALSE(low2->started());
375 TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
376 const int route_id = 0; // Indicates a background request.
377 scoped_ptr<TestRequest> request(NewRequestWithRoute("http://host/1",
378 net::LOWEST, route_id));
379 EXPECT_TRUE(request->started());
382 TEST_F(ResourceSchedulerTest, StartMultipleLowRequestsWhenIdle) {
383 scoped_ptr<TestRequest> high1(NewRequest("http://host/high1", net::HIGHEST));
384 scoped_ptr<TestRequest> high2(NewRequest("http://host/high2", net::HIGHEST));
385 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
386 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
387 EXPECT_TRUE(high1->started());
388 EXPECT_TRUE(high2->started());
389 EXPECT_TRUE(low->started());
390 EXPECT_FALSE(low2->started());
391 high1.reset();
392 EXPECT_FALSE(low2->started());
393 high2.reset();
394 EXPECT_TRUE(low2->started());
397 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) {
398 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
399 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOWEST));
401 scoped_ptr<net::URLRequest> url_request(
402 NewURLRequest("http://host/low2", net::LOWEST));
403 scoped_ptr<ResourceThrottle> throttle(
404 scheduler_.ScheduleRequest(kChildId, kRouteId, url_request.get()));
405 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest(
406 throttle.Pass(), url_request.Pass()));
407 low2->Start();
409 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOWEST));
410 low2->set_request_to_cancel(low3.Pass());
411 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOWEST));
413 EXPECT_TRUE(high->started());
414 EXPECT_FALSE(low2->started());
415 high.reset();
416 EXPECT_TRUE(low1->started());
417 EXPECT_TRUE(low2->started());
418 EXPECT_TRUE(low4->started());
421 TEST_F(ResourceSchedulerTest, LimitedNumberOfDelayableRequestsInFlight) {
422 // We only load low priority resources if there's a body.
423 scheduler_.OnWillInsertBody(kChildId, kRouteId);
425 // Throw in one high priority request to make sure that's not a factor.
426 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
427 EXPECT_TRUE(high->started());
429 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
430 const int kMaxNumDelayableRequestsPerHost = 6;
431 ScopedVector<TestRequest> lows_singlehost;
432 // Queue up to the per-host limit (we subtract the current high-pri request).
433 for (int i = 0; i < kMaxNumDelayableRequestsPerHost - 1; ++i) {
434 string url = "http://host/low" + base::IntToString(i);
435 lows_singlehost.push_back(NewRequest(url.c_str(), net::LOWEST));
436 EXPECT_TRUE(lows_singlehost[i]->started());
439 scoped_ptr<TestRequest> second_last_singlehost(NewRequest("http://host/last",
440 net::LOWEST));
441 scoped_ptr<TestRequest> last_singlehost(NewRequest("http://host/s_last",
442 net::LOWEST));
444 EXPECT_FALSE(second_last_singlehost->started());
445 high.reset();
446 EXPECT_TRUE(second_last_singlehost->started());
447 EXPECT_FALSE(last_singlehost->started());
448 lows_singlehost.erase(lows_singlehost.begin());
449 EXPECT_TRUE(last_singlehost->started());
451 // Queue more requests from different hosts until we reach the total limit.
452 int expected_slots_left =
453 kMaxNumDelayableRequestsPerClient - kMaxNumDelayableRequestsPerHost;
454 EXPECT_GT(expected_slots_left, 0);
455 ScopedVector<TestRequest> lows_differenthosts;
456 for (int i = 0; i < expected_slots_left; ++i) {
457 string url = "http://host" + base::IntToString(i) + "/low";
458 lows_differenthosts.push_back(NewRequest(url.c_str(), net::LOWEST));
459 EXPECT_TRUE(lows_differenthosts[i]->started());
462 scoped_ptr<TestRequest> last_differenthost(NewRequest("http://host_new/last",
463 net::LOWEST));
464 EXPECT_FALSE(last_differenthost->started());
467 TEST_F(ResourceSchedulerTest, RaisePriorityAndStart) {
468 // Dummies to enforce scheduling.
469 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
470 scoped_ptr<TestRequest> low(NewRequest("http://host/req", net::LOWEST));
472 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
473 EXPECT_FALSE(request->started());
475 ChangeRequestPriority(request.get(), net::HIGHEST);
476 EXPECT_TRUE(request->started());
479 TEST_F(ResourceSchedulerTest, RaisePriorityInQueue) {
480 // Dummies to enforce scheduling.
481 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
482 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
484 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
485 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
486 EXPECT_FALSE(request->started());
487 EXPECT_FALSE(idle->started());
489 ChangeRequestPriority(request.get(), net::LOWEST);
490 EXPECT_FALSE(request->started());
491 EXPECT_FALSE(idle->started());
493 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
494 ScopedVector<TestRequest> lows;
495 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
496 string url = "http://host/low" + base::IntToString(i);
497 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
500 scheduler_.OnWillInsertBody(kChildId, kRouteId);
501 high.reset();
503 EXPECT_TRUE(request->started());
504 EXPECT_FALSE(idle->started());
507 TEST_F(ResourceSchedulerTest, LowerPriority) {
508 // Dummies to enforce scheduling.
509 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
510 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
512 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
513 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
514 EXPECT_FALSE(request->started());
515 EXPECT_FALSE(idle->started());
517 ChangeRequestPriority(request.get(), net::IDLE);
518 EXPECT_FALSE(request->started());
519 EXPECT_FALSE(idle->started());
521 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
522 // 2 fewer filler requests: 1 for the "low" dummy at the start, and 1 for the
523 // one at the end, which will be tested.
524 const int kNumFillerRequests = kMaxNumDelayableRequestsPerClient - 2;
525 ScopedVector<TestRequest> lows;
526 for (int i = 0; i < kNumFillerRequests; ++i) {
527 string url = "http://host" + base::IntToString(i) + "/low";
528 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
531 scheduler_.OnWillInsertBody(kChildId, kRouteId);
532 high.reset();
534 EXPECT_FALSE(request->started());
535 EXPECT_TRUE(idle->started());
538 TEST_F(ResourceSchedulerTest, ReprioritizedRequestGoesToBackOfQueue) {
539 // Dummies to enforce scheduling.
540 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
541 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
543 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::LOWEST));
544 scoped_ptr<TestRequest> idle(NewRequest("http://host/idle", net::IDLE));
545 EXPECT_FALSE(request->started());
546 EXPECT_FALSE(idle->started());
548 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
549 ScopedVector<TestRequest> lows;
550 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
551 string url = "http://host/low" + base::IntToString(i);
552 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
555 ChangeRequestPriority(request.get(), net::IDLE);
556 EXPECT_FALSE(request->started());
557 EXPECT_FALSE(idle->started());
559 ChangeRequestPriority(request.get(), net::LOWEST);
560 EXPECT_FALSE(request->started());
561 EXPECT_FALSE(idle->started());
563 scheduler_.OnWillInsertBody(kChildId, kRouteId);
564 EXPECT_FALSE(request->started());
565 EXPECT_FALSE(idle->started());
568 TEST_F(ResourceSchedulerTest, HigherIntraPriorityGoesToFrontOfQueue) {
569 // Dummies to enforce scheduling.
570 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
571 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
573 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
574 ScopedVector<TestRequest> lows;
575 for (int i = 0; i < kMaxNumDelayableRequestsPerClient; ++i) {
576 string url = "http://host/low" + base::IntToString(i);
577 lows.push_back(NewRequest(url.c_str(), net::IDLE));
580 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
581 EXPECT_FALSE(request->started());
583 ChangeRequestPriority(request.get(), net::IDLE, 1);
584 EXPECT_FALSE(request->started());
586 scheduler_.OnWillInsertBody(kChildId, kRouteId);
587 high.reset();
588 EXPECT_TRUE(request->started());
591 TEST_F(ResourceSchedulerTest, NonHTTPSchedulesImmediately) {
592 // Dummies to enforce scheduling.
593 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
594 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
596 scoped_ptr<TestRequest> request(
597 NewRequest("chrome-extension://req", net::LOWEST));
598 EXPECT_TRUE(request->started());
601 TEST_F(ResourceSchedulerTest, ActiveLoadingSyncSchedulesImmediately) {
602 // TODO(aiolos): remove when throttling and coalescing have both landed
603 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
604 false /* should_coalesce */);
605 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
606 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
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 NewSyncRequest("http://host/req", net::LOWEST));
613 EXPECT_TRUE(request->started());
616 TEST_F(ResourceSchedulerTest, UnthrottledSyncSchedulesImmediately) {
617 // TODO(aiolos): remove when throttling and coalescing have both landed
618 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
619 false /* should_coalesce */);
620 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
621 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
622 scheduler_.GetClientStateForTesting(kBackgroundChildId,
623 kBackgroundRouteId));
624 // Dummies to enforce scheduling.
625 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
626 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
628 scoped_ptr<TestRequest> request(
629 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
630 EXPECT_TRUE(request->started());
633 TEST_F(ResourceSchedulerTest, SpdyProxySchedulesImmediately) {
634 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
635 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOWEST));
637 scoped_ptr<TestRequest> request(NewRequest("http://host/req", net::IDLE));
638 EXPECT_FALSE(request->started());
640 scheduler_.OnReceivedSpdyProxiedHttpResponse(kChildId, kRouteId);
641 EXPECT_TRUE(request->started());
643 scoped_ptr<TestRequest> after(NewRequest("http://host/after", net::IDLE));
644 EXPECT_TRUE(after->started());
647 TEST_F(ResourceSchedulerTest, NewSpdyHostInDelayableRequests) {
648 scheduler_.OnWillInsertBody(kChildId, kRouteId);
649 const int kMaxNumDelayableRequestsPerClient = 10; // Should match the .cc.
651 scoped_ptr<TestRequest> low1_spdy(
652 NewRequest("http://spdyhost1:8080/low", net::LOWEST));
653 // Cancel a request after we learn the server supports SPDY.
654 ScopedVector<TestRequest> lows;
655 for (int i = 0; i < kMaxNumDelayableRequestsPerClient - 1; ++i) {
656 string url = "http://host" + base::IntToString(i) + "/low";
657 lows.push_back(NewRequest(url.c_str(), net::LOWEST));
659 scoped_ptr<TestRequest> low1(NewRequest("http://host/low", net::LOWEST));
660 EXPECT_FALSE(low1->started());
661 http_server_properties_.SetSupportsSpdy(
662 net::HostPortPair("spdyhost1", 8080), true);
663 low1_spdy.reset();
664 EXPECT_TRUE(low1->started());
666 low1.reset();
667 scoped_ptr<TestRequest> low2_spdy(
668 NewRequest("http://spdyhost2:8080/low", net::IDLE));
669 // Reprioritize a request after we learn the server supports SPDY.
670 EXPECT_TRUE(low2_spdy->started());
671 http_server_properties_.SetSupportsSpdy(
672 net::HostPortPair("spdyhost2", 8080), true);
673 ChangeRequestPriority(low2_spdy.get(), net::LOWEST);
674 scoped_ptr<TestRequest> low2(NewRequest("http://host/low", net::LOWEST));
675 EXPECT_TRUE(low2->started());
678 TEST_F(ResourceSchedulerTest, ThrottledClientCreation) {
679 // TODO(aiolos): remove when throttling and coalescing have both landed
680 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
681 false /* should_coalesce */);
682 EXPECT_TRUE(scheduler_.should_throttle());
683 scheduler_.OnClientCreated(
684 kBackgroundChildId2, kBackgroundRouteId2, false, false);
686 EXPECT_EQ(ResourceScheduler::THROTTLED,
687 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
688 kBackgroundRouteId2));
689 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
692 TEST_F(ResourceSchedulerTest, ActiveClientThrottleUpdateOnLoadingChange) {
693 // TODO(aiolos): remove when throttling and coalescing have both landed
694 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
695 false /* should_coalesce */);
696 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
697 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
698 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
699 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
700 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
701 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
702 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
703 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
706 TEST_F(ResourceSchedulerTest, CoalesceBackgroundClientOnLoadCompletion) {
707 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
708 true /* should_coalesce */);
709 EXPECT_EQ(ResourceScheduler::THROTTLED,
710 scheduler_.GetClientStateForTesting(kBackgroundChildId,
711 kBackgroundRouteId));
712 scheduler_.OnLoadingStateChanged(
713 kBackgroundChildId, kBackgroundRouteId, true);
714 EXPECT_EQ(ResourceScheduler::THROTTLED,
715 scheduler_.GetClientStateForTesting(kBackgroundChildId,
716 kBackgroundRouteId));
717 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
718 EXPECT_EQ(ResourceScheduler::COALESCED,
719 scheduler_.GetClientStateForTesting(kBackgroundChildId,
720 kBackgroundRouteId));
723 TEST_F(ResourceSchedulerTest, UnthrottleBackgroundClientOnLoadingStarted) {
724 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
725 true /* should_coalesce */);
726 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
727 scheduler_.OnLoadingStateChanged(
728 kBackgroundChildId, kBackgroundRouteId, true);
729 EXPECT_EQ(ResourceScheduler::COALESCED,
730 scheduler_.GetClientStateForTesting(kBackgroundChildId,
731 kBackgroundRouteId));
733 scheduler_.OnLoadingStateChanged(
734 kBackgroundChildId, kBackgroundRouteId, false);
735 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
736 scheduler_.GetClientStateForTesting(kBackgroundChildId,
737 kBackgroundRouteId));
740 TEST_F(ResourceSchedulerTest, OneRequestPerThrottledClient) {
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::ACTIVE_AND_LOADING,
745 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
746 EXPECT_EQ(ResourceScheduler::THROTTLED,
747 scheduler_.GetClientStateForTesting(kBackgroundChildId,
748 kBackgroundRouteId));
749 scoped_ptr<TestRequest> high(
750 NewBackgroundRequest("http://host/high", net::HIGHEST));
751 scoped_ptr<TestRequest> request(
752 NewBackgroundRequest("http://host/req", net::IDLE));
754 EXPECT_TRUE(high->started());
755 EXPECT_FALSE(request->started());
758 TEST_F(ResourceSchedulerTest, UnthrottleNewlyVisibleClient) {
759 // TODO(aiolos): remove when throttling and coalescing have both landed
760 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
761 false /* should_coalesce */);
762 EXPECT_EQ(ResourceScheduler::THROTTLED,
763 scheduler_.GetClientStateForTesting(kBackgroundChildId,
764 kBackgroundRouteId));
765 scoped_ptr<TestRequest> high(
766 NewBackgroundRequest("http://host/high", net::HIGHEST));
767 scoped_ptr<TestRequest> request(
768 NewBackgroundRequest("http://host/req", net::IDLE));
769 EXPECT_FALSE(request->started());
771 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
772 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
773 scheduler_.GetClientStateForTesting(kBackgroundChildId,
774 kBackgroundRouteId));
775 EXPECT_TRUE(request->started());
778 TEST_F(ResourceSchedulerTest, UnthrottleNewlyAudibleClient) {
779 // TODO(aiolos): remove when throttling and coalescing have both landed
780 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
781 false /* should_coalesce */);
782 EXPECT_EQ(ResourceScheduler::THROTTLED,
783 scheduler_.GetClientStateForTesting(kBackgroundChildId,
784 kBackgroundRouteId));
785 scoped_ptr<TestRequest> high(
786 NewBackgroundRequest("http://host/high", net::HIGHEST));
787 scoped_ptr<TestRequest> request(
788 NewBackgroundRequest("http://host/req", net::IDLE));
789 EXPECT_FALSE(request->started());
791 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
792 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
793 scheduler_.GetClientStateForTesting(kBackgroundChildId,
794 kBackgroundRouteId));
795 EXPECT_TRUE(request->started());
798 TEST_F(ResourceSchedulerTest, VisibleClientStillUnthrottledOnAudabilityChange) {
799 // TODO(aiolos): remove when throttling and coalescing have both landed
800 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
801 false /* should_coalesce */);
802 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
803 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
804 EXPECT_EQ(ResourceScheduler::THROTTLED,
805 scheduler_.GetClientStateForTesting(kBackgroundChildId,
806 kBackgroundRouteId));
808 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
809 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
810 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
811 EXPECT_EQ(ResourceScheduler::THROTTLED,
812 scheduler_.GetClientStateForTesting(kBackgroundChildId,
813 kBackgroundRouteId));
815 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
816 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
817 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
818 EXPECT_EQ(ResourceScheduler::THROTTLED,
819 scheduler_.GetClientStateForTesting(kBackgroundChildId,
820 kBackgroundRouteId));
823 TEST_F(ResourceSchedulerTest, AudibleClientStillUnthrottledOnVisabilityChange) {
824 // TODO(aiolos): remove when throttling and coalescing have both landed
825 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
826 false /* should_coalesce */);
827 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
828 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
829 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
830 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
831 EXPECT_EQ(ResourceScheduler::THROTTLED,
832 scheduler_.GetClientStateForTesting(kBackgroundChildId,
833 kBackgroundRouteId));
835 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
836 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
837 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
838 EXPECT_EQ(ResourceScheduler::THROTTLED,
839 scheduler_.GetClientStateForTesting(kBackgroundChildId,
840 kBackgroundRouteId));
842 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
843 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
844 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
845 EXPECT_EQ(ResourceScheduler::THROTTLED,
846 scheduler_.GetClientStateForTesting(kBackgroundChildId,
847 kBackgroundRouteId));
850 TEST_F(ResourceSchedulerTest, ThrottledClientStartsNextHighestPriorityRequest) {
851 // TODO(aiolos): remove when throttling and coalescing have both landed
852 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
853 false /* should_coalesce */);
854 scoped_ptr<TestRequest> request(
855 NewBackgroundRequest("http://host/req", net::IDLE));
856 // Lower priority request started first to test request prioritizaton.
857 scoped_ptr<TestRequest> low(
858 NewBackgroundRequest("http://host/high", net::IDLE));
859 scoped_ptr<TestRequest> high(
860 NewBackgroundRequest("http://host/high", net::HIGHEST));
862 EXPECT_FALSE(low->started());
863 EXPECT_FALSE(high->started());
865 // request->CancelRequest();
866 request->Cancel();
867 EXPECT_TRUE(high->started());
868 EXPECT_FALSE(low->started());
871 TEST_F(ResourceSchedulerTest, ThrottledSpdyProxySchedulesImmediately) {
872 // TODO(aiolos): remove when throttling and coalescing have both landed
873 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
874 false /* should_coalesce */);
875 EXPECT_EQ(ResourceScheduler::THROTTLED,
876 scheduler_.GetClientStateForTesting(kBackgroundChildId,
877 kBackgroundRouteId));
878 scoped_ptr<TestRequest> high(
879 NewBackgroundRequest("http://host/high", net::HIGHEST));
880 scoped_ptr<TestRequest> request(
881 NewBackgroundRequest("http://host/req", net::IDLE));
883 EXPECT_FALSE(request->started());
885 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
886 kBackgroundRouteId);
887 EXPECT_TRUE(request->started());
889 scoped_ptr<TestRequest> after(
890 NewBackgroundRequest("http://host/after", net::IDLE));
891 EXPECT_TRUE(after->started());
894 TEST_F(ResourceSchedulerTest, CoalescedClientIssuesNoRequests) {
895 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
896 true /* should_coalesce */);
897 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
898 scheduler_.OnLoadingStateChanged(
899 kBackgroundChildId, kBackgroundRouteId, true);
900 EXPECT_EQ(ResourceScheduler::COALESCED,
901 scheduler_.GetClientStateForTesting(kBackgroundChildId,
902 kBackgroundRouteId));
903 scoped_ptr<TestRequest> high(
904 NewBackgroundRequest("http://host/high", net::HIGHEST));
905 scoped_ptr<TestRequest> request(
906 NewBackgroundRequest("http://host/req", net::IDLE));
908 EXPECT_FALSE(high->started());
909 EXPECT_FALSE(request->started());
911 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
912 kBackgroundRouteId);
913 EXPECT_FALSE(high->started());
915 scoped_ptr<TestRequest> after(
916 NewBackgroundRequest("http://host/after", net::HIGHEST));
917 EXPECT_FALSE(after->started());
920 TEST_F(ResourceSchedulerTest, CoalescedSpdyProxyWaits) {
921 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
922 true /* should_coalesce */);
923 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
924 scheduler_.OnLoadingStateChanged(
925 kBackgroundChildId, kBackgroundRouteId, true);
926 EXPECT_EQ(ResourceScheduler::COALESCED,
927 scheduler_.GetClientStateForTesting(kBackgroundChildId,
928 kBackgroundRouteId));
929 scoped_ptr<TestRequest> high(
930 NewBackgroundRequest("http://host/high", net::HIGHEST));
931 scoped_ptr<TestRequest> request(
932 NewBackgroundRequest("http://host/req", net::IDLE));
934 EXPECT_FALSE(request->started());
936 scheduler_.OnReceivedSpdyProxiedHttpResponse(kBackgroundChildId,
937 kBackgroundRouteId);
938 EXPECT_FALSE(request->started());
940 scoped_ptr<TestRequest> after(
941 NewBackgroundRequest("http://host/after", net::IDLE));
942 EXPECT_FALSE(after->started());
945 TEST_F(ResourceSchedulerTest, ThrottledNonHTTPSchedulesImmediately) {
946 // TODO(aiolos): remove when throttling and coalescing have both landed
947 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
948 false /* should_coalesce */);
949 // Dummies to enforce scheduling.
950 scoped_ptr<TestRequest> high(
951 NewBackgroundRequest("http://host/high", net::HIGHEST));
952 scoped_ptr<TestRequest> low(
953 NewBackgroundRequest("http://host/low", net::LOWEST));
955 scoped_ptr<TestRequest> request(
956 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
957 EXPECT_TRUE(request->started());
958 EXPECT_FALSE(low->started());
961 TEST_F(ResourceSchedulerTest, CoalescedNonHTTPSchedulesImmediately) {
962 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
963 true /* should_coalesce */);
964 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
965 scheduler_.OnLoadingStateChanged(
966 kBackgroundChildId, kBackgroundRouteId, true);
967 EXPECT_EQ(ResourceScheduler::COALESCED,
968 scheduler_.GetClientStateForTesting(kBackgroundChildId,
969 kBackgroundRouteId));
970 // Dummies to enforce scheduling.
971 scoped_ptr<TestRequest> high(
972 NewBackgroundRequest("http://host/high", net::HIGHEST));
973 scoped_ptr<TestRequest> low(
974 NewBackgroundRequest("http://host/low", net::LOWEST));
976 scoped_ptr<TestRequest> request(
977 NewBackgroundRequest("chrome-extension://req", net::LOWEST));
978 EXPECT_TRUE(request->started());
979 EXPECT_FALSE(low->started());
982 TEST_F(ResourceSchedulerTest, ThrottledSyncSchedulesImmediately) {
983 // TODO(aiolos): remove when throttling and coalescing have both landed
984 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
985 false /* should_coalesce */);
986 // Dummies to enforce scheduling.
987 scoped_ptr<TestRequest> high(
988 NewBackgroundRequest("http://host/high", net::HIGHEST));
989 scoped_ptr<TestRequest> low(
990 NewBackgroundRequest("http://host/low", net::LOWEST));
992 scoped_ptr<TestRequest> request(
993 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
994 EXPECT_TRUE(request->started());
995 EXPECT_FALSE(low->started());
998 TEST_F(ResourceSchedulerTest, CoalescedSyncSchedulesImmediately) {
999 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1000 true /* should_coalesce */);
1001 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1002 scheduler_.OnLoadingStateChanged(
1003 kBackgroundChildId, kBackgroundRouteId, true);
1004 EXPECT_EQ(ResourceScheduler::COALESCED,
1005 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1006 kBackgroundRouteId));
1007 // Dummies to enforce scheduling.
1008 scoped_ptr<TestRequest> high(
1009 NewBackgroundRequest("http://host/high", net::HIGHEST));
1010 scoped_ptr<TestRequest> low(
1011 NewBackgroundRequest("http://host/low", net::LOWEST));
1013 scoped_ptr<TestRequest> request(
1014 NewBackgroundSyncRequest("http://host/req", net::LOWEST));
1015 EXPECT_TRUE(request->started());
1016 EXPECT_FALSE(low->started());
1017 EXPECT_FALSE(high->started());
1020 TEST_F(ResourceSchedulerTest, AllBackgroundClientsUnthrottle) {
1021 // TODO(aiolos): remove when throttling and coalescing have both landed
1022 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1023 false /* should_coalesce */);
1024 EXPECT_EQ(ResourceScheduler::THROTTLED,
1025 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1026 kBackgroundRouteId));
1027 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1028 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1029 EXPECT_FALSE(scheduler_.active_clients_loaded());
1031 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1032 EXPECT_TRUE(scheduler_.active_clients_loaded());
1033 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1034 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1035 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1036 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1037 kBackgroundRouteId));
1039 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1040 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1041 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1042 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1043 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1044 kBackgroundRouteId));
1046 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1047 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1048 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1049 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1050 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1051 kBackgroundRouteId));
1053 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1054 scheduler_.OnLoadingStateChanged(
1055 kBackgroundChildId, kBackgroundRouteId, true);
1056 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1057 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1058 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1059 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1060 kBackgroundRouteId));
1063 TEST_F(ResourceSchedulerTest,
1064 UnloadedClientVisibilityChangedCorrectlyUnthrottles) {
1065 // TODO(aiolos): remove when throttling and coalescing have both landed
1066 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1067 false /* should_coalesce */);
1068 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1069 scheduler_.OnClientCreated(
1070 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1071 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1072 scheduler_.OnLoadingStateChanged(
1073 kBackgroundChildId2, kBackgroundRouteId2, true);
1075 // 1 visible, 3 hidden
1076 EXPECT_FALSE(scheduler_.active_clients_loaded());
1077 EXPECT_EQ(ResourceScheduler::THROTTLED,
1078 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1079 kBackgroundRouteId));
1080 EXPECT_EQ(ResourceScheduler::THROTTLED,
1081 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1082 kBackgroundRouteId2));
1083 EXPECT_EQ(ResourceScheduler::THROTTLED,
1084 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1085 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1086 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1088 // 2 visible, 2 hidden
1089 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1090 EXPECT_FALSE(scheduler_.active_clients_loaded());
1091 EXPECT_EQ(ResourceScheduler::THROTTLED,
1092 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1093 kBackgroundRouteId));
1094 EXPECT_EQ(ResourceScheduler::THROTTLED,
1095 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1096 kBackgroundRouteId2));
1097 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1098 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1099 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1100 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1102 // 1 visible, 3 hidden
1103 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1104 EXPECT_FALSE(scheduler_.active_clients_loaded());
1105 EXPECT_EQ(ResourceScheduler::THROTTLED,
1106 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1107 kBackgroundRouteId));
1108 EXPECT_EQ(ResourceScheduler::THROTTLED,
1109 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1110 kBackgroundRouteId2));
1111 EXPECT_EQ(ResourceScheduler::THROTTLED,
1112 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1113 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1114 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1116 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1117 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1120 TEST_F(ResourceSchedulerTest,
1121 UnloadedClientAudibilityChangedCorrectlyUnthrottles) {
1122 // TODO(aiolos): remove when throttling and coalescing have both landed
1123 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1124 false /* should_coalesce */);
1125 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1126 scheduler_.OnClientCreated(
1127 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1128 scheduler_.OnLoadingStateChanged(
1129 kBackgroundChildId2, kBackgroundRouteId2, true);
1130 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1131 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1133 // 1 audible, 3 hidden
1134 EXPECT_FALSE(scheduler_.active_clients_loaded());
1135 EXPECT_EQ(ResourceScheduler::THROTTLED,
1136 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1137 kBackgroundRouteId));
1138 EXPECT_EQ(ResourceScheduler::THROTTLED,
1139 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1140 kBackgroundRouteId2));
1141 EXPECT_EQ(ResourceScheduler::THROTTLED,
1142 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1143 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1144 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1146 // 2 audible, 2 hidden
1147 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1148 EXPECT_FALSE(scheduler_.active_clients_loaded());
1149 EXPECT_EQ(ResourceScheduler::THROTTLED,
1150 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1151 kBackgroundRouteId));
1152 EXPECT_EQ(ResourceScheduler::THROTTLED,
1153 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1154 kBackgroundRouteId2));
1155 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1156 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1157 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1158 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1160 // 1 audible, 3 hidden
1161 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1162 EXPECT_FALSE(scheduler_.active_clients_loaded());
1163 EXPECT_EQ(ResourceScheduler::THROTTLED,
1164 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1165 kBackgroundRouteId));
1166 EXPECT_EQ(ResourceScheduler::THROTTLED,
1167 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1168 kBackgroundRouteId2));
1169 EXPECT_EQ(ResourceScheduler::THROTTLED,
1170 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1171 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1172 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1174 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1175 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1178 TEST_F(ResourceSchedulerTest,
1179 LoadedClientVisibilityChangedCorrectlyUnthrottles) {
1180 // TODO(aiolos): remove when throttling and coalescing have both landed
1181 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1182 false /* should_coalesce */);
1183 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1184 scheduler_.OnClientCreated(
1185 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1186 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1187 scheduler_.OnLoadingStateChanged(
1188 kBackgroundChildId2, kBackgroundRouteId2, true);
1189 // 1 visible, 3 hidden
1190 EXPECT_FALSE(scheduler_.active_clients_loaded());
1191 EXPECT_EQ(ResourceScheduler::THROTTLED,
1192 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1193 kBackgroundRouteId));
1194 EXPECT_EQ(ResourceScheduler::THROTTLED,
1195 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1196 kBackgroundRouteId2));
1197 EXPECT_EQ(ResourceScheduler::THROTTLED,
1198 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1199 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1200 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1202 // 2 visible, 2 hidden
1203 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1204 EXPECT_FALSE(scheduler_.active_clients_loaded());
1205 EXPECT_EQ(ResourceScheduler::THROTTLED,
1206 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1207 kBackgroundRouteId));
1208 EXPECT_EQ(ResourceScheduler::THROTTLED,
1209 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1210 kBackgroundRouteId2));
1211 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1212 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1213 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1214 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1216 // 1 visible, 3 hidden
1217 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1218 EXPECT_FALSE(scheduler_.active_clients_loaded());
1219 EXPECT_EQ(ResourceScheduler::THROTTLED,
1220 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1221 kBackgroundRouteId));
1222 EXPECT_EQ(ResourceScheduler::THROTTLED,
1223 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1224 kBackgroundRouteId2));
1225 EXPECT_EQ(ResourceScheduler::THROTTLED,
1226 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1227 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1228 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1230 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1231 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1234 TEST_F(ResourceSchedulerTest,
1235 LoadedClientAudibilityChangedCorrectlyUnthrottles) {
1236 // TODO(aiolos): remove when throttling and coalescing have both landed
1237 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1238 false /* should_coalesce */);
1239 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, false);
1240 scheduler_.OnClientCreated(
1241 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1242 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1243 scheduler_.OnLoadingStateChanged(
1244 kBackgroundChildId2, kBackgroundRouteId2, true);
1245 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1246 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1247 // 1 audible, 3 hidden
1248 EXPECT_FALSE(scheduler_.active_clients_loaded());
1249 EXPECT_EQ(ResourceScheduler::THROTTLED,
1250 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1251 kBackgroundRouteId));
1252 EXPECT_EQ(ResourceScheduler::THROTTLED,
1253 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1254 kBackgroundRouteId2));
1255 EXPECT_EQ(ResourceScheduler::THROTTLED,
1256 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1257 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1258 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1260 // 2 audible, 2 hidden
1261 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1262 EXPECT_FALSE(scheduler_.active_clients_loaded());
1263 EXPECT_EQ(ResourceScheduler::THROTTLED,
1264 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1265 kBackgroundRouteId));
1266 EXPECT_EQ(ResourceScheduler::THROTTLED,
1267 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1268 kBackgroundRouteId2));
1269 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1270 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1271 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1272 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1274 // 1 audible, 3 hidden
1275 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1276 EXPECT_FALSE(scheduler_.active_clients_loaded());
1277 EXPECT_EQ(ResourceScheduler::THROTTLED,
1278 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1279 kBackgroundRouteId));
1280 EXPECT_EQ(ResourceScheduler::THROTTLED,
1281 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1282 kBackgroundRouteId2));
1283 EXPECT_EQ(ResourceScheduler::THROTTLED,
1284 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1285 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1286 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1288 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1289 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1292 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesHiddenCorrectlyUnthrottles) {
1293 // TODO(aiolos): remove when throttling and coalescing have both landed
1294 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1295 false /* should_coalesce */);
1296 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1297 scheduler_.OnClientCreated(
1298 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1299 scheduler_.OnLoadingStateChanged(
1300 kBackgroundChildId2, kBackgroundRouteId2, true);
1302 // 2 visible, 2 hidden
1303 EXPECT_FALSE(scheduler_.active_clients_loaded());
1304 EXPECT_EQ(ResourceScheduler::THROTTLED,
1305 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1306 kBackgroundRouteId));
1307 EXPECT_EQ(ResourceScheduler::THROTTLED,
1308 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1309 kBackgroundRouteId2));
1310 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1311 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1312 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1313 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1315 // 1 visible, 3 hidden
1316 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1317 EXPECT_FALSE(scheduler_.active_clients_loaded());
1318 EXPECT_EQ(ResourceScheduler::THROTTLED,
1319 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1320 kBackgroundRouteId));
1321 EXPECT_EQ(ResourceScheduler::THROTTLED,
1322 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1323 kBackgroundRouteId2));
1324 EXPECT_EQ(ResourceScheduler::THROTTLED,
1325 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1326 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1327 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1329 // 0 visible, 4 hidden
1330 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1331 EXPECT_TRUE(scheduler_.active_clients_loaded());
1332 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1333 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1334 kBackgroundRouteId));
1335 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1336 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1337 kBackgroundRouteId2));
1338 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1339 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1340 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1341 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1343 // 1 visible, 3 hidden
1344 scheduler_.OnVisibilityChanged(kChildId, kRouteId, true);
1345 EXPECT_FALSE(scheduler_.active_clients_loaded());
1346 EXPECT_EQ(ResourceScheduler::THROTTLED,
1347 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1348 kBackgroundRouteId));
1349 EXPECT_EQ(ResourceScheduler::THROTTLED,
1350 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1351 kBackgroundRouteId2));
1352 EXPECT_EQ(ResourceScheduler::THROTTLED,
1353 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1354 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1355 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1357 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1358 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1361 TEST_F(ResourceSchedulerTest, UnloadedClientBecomesSilentCorrectlyUnthrottles) {
1362 // TODO(aiolos): remove when throttling and coalescing have both landed
1363 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1364 false /* should_coalesce */);
1365 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, true);
1366 scheduler_.OnClientCreated(
1367 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1368 scheduler_.OnLoadingStateChanged(
1369 kBackgroundChildId2, kBackgroundRouteId2, true);
1370 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1371 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1372 // 2 audible, 2 hidden
1373 EXPECT_FALSE(scheduler_.active_clients_loaded());
1374 EXPECT_EQ(ResourceScheduler::THROTTLED,
1375 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1376 kBackgroundRouteId));
1377 EXPECT_EQ(ResourceScheduler::THROTTLED,
1378 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1379 kBackgroundRouteId2));
1380 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1381 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1382 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1383 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1385 // 1 audible, 3 hidden
1386 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1387 EXPECT_FALSE(scheduler_.active_clients_loaded());
1388 EXPECT_EQ(ResourceScheduler::THROTTLED,
1389 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1390 kBackgroundRouteId));
1391 EXPECT_EQ(ResourceScheduler::THROTTLED,
1392 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1393 kBackgroundRouteId2));
1394 EXPECT_EQ(ResourceScheduler::THROTTLED,
1395 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1396 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1397 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1399 // 0 audible, 4 hidden
1400 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1401 EXPECT_TRUE(scheduler_.active_clients_loaded());
1402 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1403 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1404 kBackgroundRouteId));
1405 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1406 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1407 kBackgroundRouteId2));
1408 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1409 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1410 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1411 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1413 // 1 audible, 3 hidden
1414 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1415 EXPECT_FALSE(scheduler_.active_clients_loaded());
1416 EXPECT_EQ(ResourceScheduler::THROTTLED,
1417 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1418 kBackgroundRouteId));
1419 EXPECT_EQ(ResourceScheduler::THROTTLED,
1420 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1421 kBackgroundRouteId2));
1422 EXPECT_EQ(ResourceScheduler::THROTTLED,
1423 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1424 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1425 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1427 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1428 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1431 TEST_F(ResourceSchedulerTest, LoadedClientBecomesHiddenCorrectlyThrottles) {
1432 // TODO(aiolos): remove when throttling and coalescing have both landed
1433 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1434 false /* should_coalesce */);
1435 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1436 scheduler_.OnClientCreated(
1437 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1438 scheduler_.OnLoadingStateChanged(
1439 kBackgroundChildId2, kBackgroundRouteId2, true);
1440 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1441 // 2 visible, 2 hidden
1442 EXPECT_FALSE(scheduler_.active_clients_loaded());
1443 EXPECT_EQ(ResourceScheduler::THROTTLED,
1444 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1445 kBackgroundRouteId));
1446 EXPECT_EQ(ResourceScheduler::THROTTLED,
1447 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1448 kBackgroundRouteId2));
1449 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1450 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1451 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1452 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1454 // 1 visible, 3 hidden
1455 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, false);
1456 EXPECT_FALSE(scheduler_.active_clients_loaded());
1457 EXPECT_EQ(ResourceScheduler::THROTTLED,
1458 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1459 kBackgroundRouteId));
1460 EXPECT_EQ(ResourceScheduler::THROTTLED,
1461 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1462 kBackgroundRouteId2));
1463 EXPECT_EQ(ResourceScheduler::THROTTLED,
1464 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1465 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1466 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1468 // 0 visible, 4 hidden
1469 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1470 EXPECT_TRUE(scheduler_.active_clients_loaded());
1471 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1472 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1473 kBackgroundRouteId));
1474 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1475 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1476 kBackgroundRouteId2));
1477 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1478 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1479 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1480 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1482 // 1 visible, 3 hidden
1483 scheduler_.OnVisibilityChanged(kChildId2, kRouteId2, true);
1484 EXPECT_TRUE(scheduler_.active_clients_loaded());
1485 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1486 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1487 kBackgroundRouteId));
1488 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1489 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1490 kBackgroundRouteId2));
1491 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1492 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1493 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1494 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1496 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1497 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1500 TEST_F(ResourceSchedulerTest, LoadedClientBecomesSilentCorrectlyThrottles) {
1501 // TODO(aiolos): remove when throttling and coalescing have both landed
1502 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1503 false /* should_coalesce */);
1504 scheduler_.OnClientCreated(kChildId2, kRouteId2, false, true);
1505 scheduler_.OnClientCreated(
1506 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1507 scheduler_.OnLoadingStateChanged(
1508 kBackgroundChildId2, kBackgroundRouteId2, true);
1509 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1510 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1511 scheduler_.OnAudibilityChanged(kChildId, kRouteId, true);
1512 // 2 audible, 2 hidden
1513 EXPECT_FALSE(scheduler_.active_clients_loaded());
1514 EXPECT_EQ(ResourceScheduler::THROTTLED,
1515 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1516 kBackgroundRouteId));
1517 EXPECT_EQ(ResourceScheduler::THROTTLED,
1518 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1519 kBackgroundRouteId2));
1520 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1521 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1522 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1523 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1525 // 1 audible, 3 hidden
1526 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, false);
1527 EXPECT_FALSE(scheduler_.active_clients_loaded());
1528 EXPECT_EQ(ResourceScheduler::THROTTLED,
1529 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1530 kBackgroundRouteId));
1531 EXPECT_EQ(ResourceScheduler::THROTTLED,
1532 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1533 kBackgroundRouteId2));
1534 EXPECT_EQ(ResourceScheduler::THROTTLED,
1535 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1536 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1537 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1539 // 0 audible, 4 hidden
1540 scheduler_.OnAudibilityChanged(kChildId, kRouteId, false);
1541 EXPECT_TRUE(scheduler_.active_clients_loaded());
1542 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1543 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1544 kBackgroundRouteId));
1545 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1546 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1547 kBackgroundRouteId2));
1548 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1549 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1550 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1551 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1553 // 1 audible, 3 hidden
1554 scheduler_.OnAudibilityChanged(kChildId2, kRouteId2, true);
1555 EXPECT_TRUE(scheduler_.active_clients_loaded());
1556 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1557 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1558 kBackgroundRouteId));
1559 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1560 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1561 kBackgroundRouteId2));
1562 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1563 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1564 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1565 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1567 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1568 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1571 TEST_F(ResourceSchedulerTest, HiddenLoadedChangesCorrectlyStayThrottled) {
1572 // TODO(aiolos): remove when throttling and coalescing have both landed
1573 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1574 false /* should_coalesce */);
1575 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1576 scheduler_.OnClientCreated(
1577 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1579 // 1 visible and 2 hidden loading, 1 visible loaded
1580 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1581 EXPECT_FALSE(scheduler_.active_clients_loaded());
1582 EXPECT_EQ(ResourceScheduler::THROTTLED,
1583 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1584 kBackgroundRouteId));
1585 EXPECT_EQ(ResourceScheduler::THROTTLED,
1586 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1587 kBackgroundRouteId2));
1588 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1589 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1590 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1591 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1593 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1594 scheduler_.OnLoadingStateChanged(
1595 kBackgroundChildId2, kBackgroundRouteId2, true);
1596 EXPECT_FALSE(scheduler_.active_clients_loaded());
1597 EXPECT_EQ(ResourceScheduler::THROTTLED,
1598 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1599 kBackgroundRouteId));
1600 EXPECT_EQ(ResourceScheduler::THROTTLED,
1601 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1602 kBackgroundRouteId2));
1603 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1604 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1605 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1606 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1608 // 1 visible loading, 1 visible and 2 hidden loaded
1609 scheduler_.OnLoadingStateChanged(
1610 kBackgroundChildId, kBackgroundRouteId, true);
1611 EXPECT_FALSE(scheduler_.active_clients_loaded());
1612 EXPECT_EQ(ResourceScheduler::THROTTLED,
1613 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1614 kBackgroundRouteId));
1615 EXPECT_EQ(ResourceScheduler::THROTTLED,
1616 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1617 kBackgroundRouteId2));
1618 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1619 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1620 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1621 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1623 // 1 visible and 1 hidden loading, 1 visible and 1 hidden loaded
1624 scheduler_.OnLoadingStateChanged(
1625 kBackgroundChildId2, kBackgroundRouteId2, true);
1626 EXPECT_FALSE(scheduler_.active_clients_loaded());
1627 EXPECT_EQ(ResourceScheduler::THROTTLED,
1628 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1629 kBackgroundRouteId));
1630 EXPECT_EQ(ResourceScheduler::THROTTLED,
1631 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1632 kBackgroundRouteId2));
1633 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1634 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1635 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1636 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1638 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1639 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1642 TEST_F(ResourceSchedulerTest, PartialVisibleClientLoadedDoesNotUnthrottle) {
1643 // TODO(aiolos): remove when throttling and coalescing have both landed
1644 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1645 false /* should_coalesce */);
1646 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1647 scheduler_.OnClientCreated(
1648 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1650 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1651 scheduler_.OnLoadingStateChanged(
1652 kBackgroundChildId2, kBackgroundRouteId2, true);
1653 EXPECT_FALSE(scheduler_.active_clients_loaded());
1654 EXPECT_EQ(ResourceScheduler::THROTTLED,
1655 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1656 kBackgroundRouteId));
1657 EXPECT_EQ(ResourceScheduler::THROTTLED,
1658 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1659 kBackgroundRouteId2));
1660 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1661 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1662 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1663 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1665 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1666 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1667 EXPECT_FALSE(scheduler_.active_clients_loaded());
1668 EXPECT_EQ(ResourceScheduler::THROTTLED,
1669 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1670 kBackgroundRouteId));
1671 EXPECT_EQ(ResourceScheduler::THROTTLED,
1672 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1673 kBackgroundRouteId2));
1674 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1675 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1676 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1677 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1679 // 2 visible loading, 1 hidden loading, 1 hidden loaded
1680 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1681 EXPECT_FALSE(scheduler_.active_clients_loaded());
1682 EXPECT_EQ(ResourceScheduler::THROTTLED,
1683 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1684 kBackgroundRouteId));
1685 EXPECT_EQ(ResourceScheduler::THROTTLED,
1686 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1687 kBackgroundRouteId2));
1688 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1689 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1690 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1691 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1693 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1694 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1697 TEST_F(ResourceSchedulerTest, FullVisibleLoadedCorrectlyUnthrottle) {
1698 // TODO(aiolos): remove when throttling and coalescing have both landed
1699 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1700 false /* should_coalesce */);
1701 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1702 scheduler_.OnClientCreated(
1703 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1705 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1706 scheduler_.OnLoadingStateChanged(
1707 kBackgroundChildId2, kBackgroundRouteId2, true);
1708 scheduler_.OnLoadingStateChanged(kChildId2, kRouteId2, true);
1709 EXPECT_FALSE(scheduler_.active_clients_loaded());
1710 EXPECT_EQ(ResourceScheduler::THROTTLED,
1711 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1712 kBackgroundRouteId));
1713 EXPECT_EQ(ResourceScheduler::THROTTLED,
1714 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1715 kBackgroundRouteId2));
1716 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1717 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1718 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1719 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1721 scoped_ptr<TestRequest> high(
1722 NewBackgroundRequest("http://host/high", net::HIGHEST));
1723 scoped_ptr<TestRequest> low(
1724 NewBackgroundRequest("http://host/low", net::LOWEST));
1726 EXPECT_TRUE(high->started());
1727 EXPECT_FALSE(low->started());
1729 // 2 visible loaded, 1 hidden loading, 1 hidden loaded
1730 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1731 EXPECT_TRUE(scheduler_.active_clients_loaded());
1732 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1733 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1734 kBackgroundRouteId));
1735 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1736 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1737 kBackgroundRouteId2));
1738 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1739 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1740 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1741 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1742 // kBackgroundClientId unthrottling should unthrottle it's request.
1743 EXPECT_TRUE(low->started());
1745 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1746 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1747 EXPECT_FALSE(scheduler_.active_clients_loaded());
1748 EXPECT_EQ(ResourceScheduler::THROTTLED,
1749 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1750 kBackgroundRouteId));
1751 EXPECT_EQ(ResourceScheduler::THROTTLED,
1752 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1753 kBackgroundRouteId2));
1754 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1755 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1756 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1757 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1759 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1760 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1763 TEST_F(ResourceSchedulerTest,
1764 ActiveAndLoadingClientDeletedCorrectlyUnthrottle) {
1765 // TODO(aiolos): remove when throttling and coalescing have both landed
1766 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1767 false /* should_coalesce */);
1768 scheduler_.OnClientCreated(kChildId2, kRouteId2, true, false);
1769 scheduler_.OnClientCreated(
1770 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1772 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1773 scheduler_.OnLoadingStateChanged(
1774 kBackgroundChildId2, kBackgroundRouteId2, true);
1775 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1776 EXPECT_FALSE(scheduler_.active_clients_loaded());
1777 EXPECT_EQ(ResourceScheduler::THROTTLED,
1778 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1779 kBackgroundRouteId));
1780 EXPECT_EQ(ResourceScheduler::THROTTLED,
1781 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1782 kBackgroundRouteId2));
1783 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1784 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1785 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1786 scheduler_.GetClientStateForTesting(kChildId2, kRouteId2));
1788 // 1 visible loaded, 1 hidden loading, 1 hidden loaded
1789 scheduler_.OnClientDeleted(kChildId2, kRouteId2);
1790 EXPECT_TRUE(scheduler_.active_clients_loaded());
1791 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1792 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1793 kBackgroundRouteId));
1794 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1795 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1796 kBackgroundRouteId2));
1797 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1798 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1800 // 1 visible and 1 hidden loaded, 1 visible and 1 hidden loading
1801 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, false);
1802 EXPECT_FALSE(scheduler_.active_clients_loaded());
1803 EXPECT_EQ(ResourceScheduler::THROTTLED,
1804 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1805 kBackgroundRouteId));
1806 EXPECT_EQ(ResourceScheduler::THROTTLED,
1807 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1808 kBackgroundRouteId2));
1809 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1810 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1812 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1815 TEST_F(ResourceSchedulerTest, CoalescedClientCreationStartsTimer) {
1816 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1817 true /* should_coalesce */);
1818 EXPECT_FALSE(mock_timer_->IsRunning());
1819 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1820 EXPECT_FALSE(mock_timer_->IsRunning());
1821 scheduler_.OnLoadingStateChanged(
1822 kBackgroundChildId, kBackgroundRouteId, true);
1823 EXPECT_EQ(ResourceScheduler::COALESCED,
1824 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1825 kBackgroundRouteId));
1826 EXPECT_TRUE(mock_timer_->IsRunning());
1829 TEST_F(ResourceSchedulerTest, ActiveLoadingClientLoadedAndHiddenStartsTimer) {
1830 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1831 true /* should_coalesce */);
1832 EXPECT_FALSE(mock_timer_->IsRunning());
1833 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1834 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1835 EXPECT_EQ(ResourceScheduler::THROTTLED,
1836 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1837 kBackgroundRouteId));
1838 EXPECT_FALSE(mock_timer_->IsRunning());
1840 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1841 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1842 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1843 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1844 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1845 kBackgroundRouteId));
1846 EXPECT_FALSE(mock_timer_->IsRunning());
1848 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1849 EXPECT_EQ(ResourceScheduler::COALESCED,
1850 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1851 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1852 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1853 kBackgroundRouteId));
1854 EXPECT_TRUE(mock_timer_->IsRunning());
1857 TEST_F(ResourceSchedulerTest, ActiveLoadingClientHiddenAndLoadedStartsTimer) {
1858 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1859 true /* should_coalesce */);
1860 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
1861 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1862 EXPECT_EQ(ResourceScheduler::THROTTLED,
1863 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1864 kBackgroundRouteId));
1865 EXPECT_FALSE(mock_timer_->IsRunning());
1867 scheduler_.OnVisibilityChanged(kChildId, kRouteId, false);
1868 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1869 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1870 EXPECT_FALSE(mock_timer_->IsRunning());
1872 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1873 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1874 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1875 kBackgroundRouteId));
1876 EXPECT_EQ(ResourceScheduler::COALESCED,
1877 scheduler_.GetClientStateForTesting(kChildId, kRouteId));
1878 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1879 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1880 kBackgroundRouteId));
1881 EXPECT_TRUE(mock_timer_->IsRunning());
1884 TEST_F(ResourceSchedulerTest, CoalescedClientBecomesAudibleStopsTimer) {
1885 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1886 true /* should_coalesce */);
1887 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1888 EXPECT_FALSE(mock_timer_->IsRunning());
1889 scheduler_.OnLoadingStateChanged(
1890 kBackgroundChildId, kBackgroundRouteId, true);
1891 EXPECT_EQ(ResourceScheduler::COALESCED,
1892 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1893 kBackgroundRouteId));
1894 EXPECT_TRUE(mock_timer_->IsRunning());
1896 scheduler_.OnAudibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1897 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
1898 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1899 kBackgroundRouteId));
1900 EXPECT_FALSE(mock_timer_->IsRunning());
1903 TEST_F(ResourceSchedulerTest, LastCoalescedClientDeletionStopsTimer) {
1904 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1905 true /* should_coalesce */);
1906 scheduler_.OnClientCreated(
1907 kBackgroundChildId2, kBackgroundRouteId2, false, 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_.OnClientDeleted(kBackgroundChildId, kBackgroundRouteId);
1924 EXPECT_TRUE(mock_timer_->IsRunning());
1926 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1927 EXPECT_FALSE(mock_timer_->IsRunning());
1929 // To avoid errors on test tear down.
1930 scheduler_.OnClientCreated(
1931 kBackgroundChildId, kBackgroundRouteId, false, false);
1934 TEST_F(ResourceSchedulerTest, LastCoalescedClientStartsLoadingStopsTimer) {
1935 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1936 true /* should_coalesce */);
1937 scheduler_.OnClientCreated(
1938 kBackgroundChildId2, kBackgroundRouteId2, false, 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_.OnLoadingStateChanged(
1955 kBackgroundChildId, kBackgroundRouteId, false);
1956 EXPECT_TRUE(mock_timer_->IsRunning());
1958 scheduler_.OnLoadingStateChanged(
1959 kBackgroundChildId2, kBackgroundRouteId2, false);
1960 EXPECT_FALSE(mock_timer_->IsRunning());
1962 // This is needed to avoid errors on test tear down.
1963 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1966 TEST_F(ResourceSchedulerTest, LastCoalescedClientBecomesVisibleStopsTimer) {
1967 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
1968 true /* should_coalesce */);
1969 scheduler_.OnClientCreated(
1970 kBackgroundChildId2, kBackgroundRouteId2, false, false);
1971 EXPECT_FALSE(mock_timer_->IsRunning());
1972 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
1973 EXPECT_FALSE(mock_timer_->IsRunning());
1974 scheduler_.OnLoadingStateChanged(
1975 kBackgroundChildId, kBackgroundRouteId, true);
1976 EXPECT_EQ(ResourceScheduler::COALESCED,
1977 scheduler_.GetClientStateForTesting(kBackgroundChildId,
1978 kBackgroundRouteId));
1979 scheduler_.OnLoadingStateChanged(
1980 kBackgroundChildId2, kBackgroundRouteId2, true);
1981 EXPECT_EQ(ResourceScheduler::COALESCED,
1982 scheduler_.GetClientStateForTesting(kBackgroundChildId2,
1983 kBackgroundRouteId2));
1984 EXPECT_TRUE(mock_timer_->IsRunning());
1986 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
1987 EXPECT_TRUE(mock_timer_->IsRunning());
1989 scheduler_.OnVisibilityChanged(
1990 kBackgroundChildId2, kBackgroundRouteId2, true);
1991 EXPECT_FALSE(mock_timer_->IsRunning());
1993 // To avoid errors on test tear down.
1994 scheduler_.OnClientDeleted(kBackgroundChildId2, kBackgroundRouteId2);
1997 TEST_F(ResourceSchedulerTest,
1998 CoalescedClientBecomesLoadingAndVisibleStopsTimer) {
1999 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2000 true /* should_coalesce */);
2001 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2002 EXPECT_FALSE(mock_timer_->IsRunning());
2003 scheduler_.OnLoadingStateChanged(
2004 kBackgroundChildId, kBackgroundRouteId, true);
2005 EXPECT_EQ(ResourceScheduler::COALESCED,
2006 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2007 kBackgroundRouteId));
2008 EXPECT_TRUE(mock_timer_->IsRunning());
2010 scheduler_.OnLoadingStateChanged(
2011 kBackgroundChildId, kBackgroundRouteId, false);
2012 EXPECT_EQ(ResourceScheduler::UNTHROTTLED,
2013 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2014 kBackgroundRouteId));
2015 EXPECT_FALSE(mock_timer_->IsRunning());
2017 scheduler_.OnVisibilityChanged(kBackgroundChildId, kBackgroundRouteId, true);
2018 EXPECT_EQ(ResourceScheduler::ACTIVE_AND_LOADING,
2019 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2020 kBackgroundRouteId));
2021 EXPECT_FALSE(mock_timer_->IsRunning());
2024 TEST_F(ResourceSchedulerTest, CoalescedRequestsIssueOnTimer) {
2025 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2026 true /* should_coalesce */);
2027 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2028 scheduler_.OnLoadingStateChanged(
2029 kBackgroundChildId, kBackgroundRouteId, true);
2030 EXPECT_EQ(ResourceScheduler::COALESCED,
2031 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2032 kBackgroundRouteId));
2033 EXPECT_TRUE(scheduler_.active_clients_loaded());
2035 scoped_ptr<TestRequest> high(
2036 NewBackgroundRequest("http://host/high", net::HIGHEST));
2037 scoped_ptr<TestRequest> low(
2038 NewBackgroundRequest("http://host/low", net::LOWEST));
2039 EXPECT_FALSE(high->started());
2040 EXPECT_FALSE(low->started());
2042 FireCoalescingTimer();
2044 EXPECT_TRUE(high->started());
2045 EXPECT_TRUE(low->started());
2048 TEST_F(ResourceSchedulerTest, CoalescedRequestsUnthrottleCorrectlyOnTimer) {
2049 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2050 true /* should_coalesce */);
2051 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2052 scheduler_.OnLoadingStateChanged(
2053 kBackgroundChildId, kBackgroundRouteId, true);
2054 EXPECT_EQ(ResourceScheduler::COALESCED,
2055 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2056 kBackgroundRouteId));
2057 EXPECT_TRUE(scheduler_.active_clients_loaded());
2059 scoped_ptr<TestRequest> high(
2060 NewBackgroundRequest("http://host/high", net::HIGHEST));
2061 scoped_ptr<TestRequest> high2(
2062 NewBackgroundRequest("http://host/high", net::HIGHEST));
2063 scoped_ptr<TestRequest> high3(
2064 NewBackgroundRequest("http://host/high", net::HIGHEST));
2065 scoped_ptr<TestRequest> high4(
2066 NewBackgroundRequest("http://host/high", net::HIGHEST));
2067 scoped_ptr<TestRequest> low(
2068 NewBackgroundRequest("http://host/low", net::LOWEST));
2069 scoped_ptr<TestRequest> low2(
2070 NewBackgroundRequest("http://host/low", net::LOWEST));
2071 scoped_ptr<TestRequest> low3(
2072 NewBackgroundRequest("http://host/low", net::LOWEST));
2073 scoped_ptr<TestRequest> low4(
2074 NewBackgroundRequest("http://host/low", net::LOWEST));
2076 http_server_properties_.SetSupportsSpdy(net::HostPortPair("spdyhost", 443),
2077 true);
2078 scoped_ptr<TestRequest> low_spdy(
2079 NewBackgroundRequest("https://spdyhost/low", net::LOW));
2080 scoped_ptr<TestRequest> sync_request(
2081 NewBackgroundSyncRequest("http://host/req", net::LOW));
2082 scoped_ptr<TestRequest> non_http_request(
2083 NewBackgroundRequest("chrome-extension://req", net::LOW));
2085 // Sync requests should issue immediately.
2086 EXPECT_TRUE(sync_request->started());
2087 // Non-http(s) requests should issue immediately.
2088 EXPECT_TRUE(non_http_request->started());
2089 // Nothing else should issue without a timer fire.
2090 EXPECT_FALSE(high->started());
2091 EXPECT_FALSE(high2->started());
2092 EXPECT_FALSE(high3->started());
2093 EXPECT_FALSE(high4->started());
2094 EXPECT_FALSE(low->started());
2095 EXPECT_FALSE(low2->started());
2096 EXPECT_FALSE(low3->started());
2097 EXPECT_FALSE(low4->started());
2098 EXPECT_FALSE(low_spdy->started());
2100 FireCoalescingTimer();
2102 // All high priority requests should issue.
2103 EXPECT_TRUE(high->started());
2104 EXPECT_TRUE(high2->started());
2105 EXPECT_TRUE(high3->started());
2106 EXPECT_TRUE(high4->started());
2107 // There should only be one net::LOWEST priority request issued with
2108 // non-delayable requests in flight.
2109 EXPECT_TRUE(low->started());
2110 EXPECT_FALSE(low2->started());
2111 EXPECT_FALSE(low3->started());
2112 EXPECT_FALSE(low4->started());
2113 // Spdy-Enable requests should issue regardless of priority.
2114 EXPECT_TRUE(low_spdy->started());
2117 TEST_F(ResourceSchedulerTest, CoalescedRequestsWaitForNextTimer) {
2118 scheduler_.SetThrottleOptionsForTesting(true /* should_throttle */,
2119 true /* should_coalesce */);
2120 scheduler_.OnLoadingStateChanged(kChildId, kRouteId, true);
2121 scheduler_.OnLoadingStateChanged(
2122 kBackgroundChildId, kBackgroundRouteId, true);
2124 EXPECT_EQ(ResourceScheduler::COALESCED,
2125 scheduler_.GetClientStateForTesting(kBackgroundChildId,
2126 kBackgroundRouteId));
2127 EXPECT_TRUE(scheduler_.active_clients_loaded());
2129 scoped_ptr<TestRequest> high(
2130 NewBackgroundRequest("http://host/high", net::HIGHEST));
2131 EXPECT_FALSE(high->started());
2133 FireCoalescingTimer();
2135 scoped_ptr<TestRequest> high2(
2136 NewBackgroundRequest("http://host/high2", net::HIGHEST));
2137 scoped_ptr<TestRequest> low(
2138 NewBackgroundRequest("http://host/low", net::LOWEST));
2140 EXPECT_TRUE(high->started());
2141 EXPECT_FALSE(high2->started());
2142 EXPECT_FALSE(low->started());
2144 FireCoalescingTimer();
2146 EXPECT_TRUE(high->started());
2147 EXPECT_TRUE(high2->started());
2148 EXPECT_TRUE(low->started());
2151 TEST_F(ResourceSchedulerTest, GetVisualSignalFromRenderViewHost) {
2152 scoped_ptr<MockRenderProcessHostFactory> render_process_host_factory;
2153 scoped_ptr<TestRenderViewHostFactory> render_view_host_factory;
2154 scoped_ptr<TestBrowserContext> browser_context;
2155 scoped_ptr<TestWebContents> web_contents_1;
2156 scoped_ptr<TestWebContents> web_contents_2;
2157 render_process_host_factory.reset(new MockRenderProcessHostFactory());
2158 render_view_host_factory.reset(
2159 new TestRenderViewHostFactory(render_process_host_factory.get()));
2161 browser_context.reset(new TestBrowserContext());
2162 scoped_refptr<SiteInstance> site_instance_1 =
2163 SiteInstance::Create(browser_context.get());
2164 scoped_refptr<SiteInstance> site_instance_2 =
2165 SiteInstance::Create(browser_context.get());
2166 SiteInstanceImpl::set_render_process_host_factory(
2167 render_process_host_factory.get());
2169 web_contents_1.reset(
2170 TestWebContents::Create(browser_context.get(), site_instance_1.get()));
2171 web_contents_2.reset(
2172 TestWebContents::Create(browser_context.get(), site_instance_2.get()));
2173 base::RunLoop().RunUntilIdle();
2175 RenderViewHostImpl* rvh1 = web_contents_1->GetRenderViewHost();
2176 RenderViewHostImpl* rvh2 = web_contents_2->GetRenderViewHost();
2177 ResourceScheduler* scheduler = ResourceDispatcherHostImpl::Get()->scheduler();
2179 // Check initial visibility is set correctly.
2180 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2181 rvh1->GetRoutingID()),
2182 !rvh1->is_hidden());
2183 EXPECT_EQ(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2184 rvh1->GetRoutingID()),
2185 !rvh2->is_hidden());
2187 // 1 visible, 1 hidden
2188 rvh1->WasShown(ui::LatencyInfo());
2189 rvh2->WasHidden();
2190 base::RunLoop().RunUntilIdle();
2192 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2193 rvh1->GetRoutingID()));
2194 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2195 rvh2->GetRoutingID()));
2197 // Flip the visibility and check again.
2198 rvh1->WasHidden();
2199 rvh2->WasShown(ui::LatencyInfo());
2200 base::RunLoop().RunUntilIdle();
2202 EXPECT_FALSE(scheduler->IsClientVisibleForTesting(rvh1->GetProcess()->GetID(),
2203 rvh1->GetRoutingID()));
2204 EXPECT_TRUE(scheduler->IsClientVisibleForTesting(rvh2->GetProcess()->GetID(),
2205 rvh2->GetRoutingID()));
2206 // Clean up.
2207 web_contents_1.reset();
2208 web_contents_2.reset();
2209 base::RunLoop().RunUntilIdle();
2211 browser_context.reset();
2212 render_process_host_factory.reset();
2215 } // unnamed namespace
2217 } // namespace content