Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1363)

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/child/idle_helper_unittest.cc

Issue 2967013002: Be explicit about namespace testing to not mix it with blink::testing (Closed)
Patch Set: Dropped mojo parts that need another review. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/scheduler/child/idle_helper.h" 5 #include "platform/scheduler/child/idle_helper.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/test/simple_test_tick_clock.h" 15 #include "base/test/simple_test_tick_clock.h"
16 #include "cc/test/ordered_simple_task_runner.h" 16 #include "cc/test/ordered_simple_task_runner.h"
17 #include "platform/scheduler/base/real_time_domain.h" 17 #include "platform/scheduler/base/real_time_domain.h"
18 #include "platform/scheduler/base/task_queue.h" 18 #include "platform/scheduler/base/task_queue.h"
19 #include "platform/scheduler/base/task_queue_manager.h" 19 #include "platform/scheduler/base/task_queue_manager.h"
20 #include "platform/scheduler/base/test_time_source.h" 20 #include "platform/scheduler/base/test_time_source.h"
21 #include "platform/scheduler/child/scheduler_helper.h" 21 #include "platform/scheduler/child/scheduler_helper.h"
22 #include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h" 22 #include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h"
23 #include "platform/scheduler/child/scheduler_tqm_delegate_impl.h" 23 #include "platform/scheduler/child/scheduler_tqm_delegate_impl.h"
24 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 using testing::_; 27 using ::testing::_;
28 using testing::AnyNumber; 28 using ::testing::AnyNumber;
29 using testing::AtLeast; 29 using ::testing::AtLeast;
30 using testing::Exactly; 30 using ::testing::Exactly;
31 using testing::Invoke; 31 using ::testing::Invoke;
32 using testing::Return; 32 using ::testing::Return;
33 33
34 namespace blink { 34 namespace blink {
35 namespace scheduler { 35 namespace scheduler {
36 36
37 namespace { 37 namespace {
38 void AppendToVectorTestTask(std::vector<std::string>* vector, 38 void AppendToVectorTestTask(std::vector<std::string>* vector,
39 std::string value) { 39 std::string value) {
40 vector->push_back(value); 40 vector->push_back(value);
41 } 41 }
42 42
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 MOCK_METHOD2(CanEnterLongIdlePeriod, 170 MOCK_METHOD2(CanEnterLongIdlePeriod,
171 bool(base::TimeTicks now, 171 bool(base::TimeTicks now,
172 base::TimeDelta* next_long_idle_period_delay_out)); 172 base::TimeDelta* next_long_idle_period_delay_out));
173 173
174 MOCK_METHOD0(IsNotQuiescent, void()); 174 MOCK_METHOD0(IsNotQuiescent, void());
175 MOCK_METHOD0(OnIdlePeriodStarted, void()); 175 MOCK_METHOD0(OnIdlePeriodStarted, void());
176 MOCK_METHOD0(OnIdlePeriodEnded, void()); 176 MOCK_METHOD0(OnIdlePeriodEnded, void());
177 MOCK_METHOD1(OnPendingTasksChanged, void(bool has_tasks)); 177 MOCK_METHOD1(OnPendingTasksChanged, void(bool has_tasks));
178 }; 178 };
179 179
180 class BaseIdleHelperTest : public testing::Test { 180 class BaseIdleHelperTest : public ::testing::Test {
181 public: 181 public:
182 BaseIdleHelperTest( 182 BaseIdleHelperTest(
183 base::MessageLoop* message_loop, 183 base::MessageLoop* message_loop,
184 base::TimeDelta required_quiescence_duration_before_long_idle_period) 184 base::TimeDelta required_quiescence_duration_before_long_idle_period)
185 : clock_(new base::SimpleTestTickClock()), 185 : clock_(new base::SimpleTestTickClock()),
186 mock_task_runner_( 186 mock_task_runner_(
187 message_loop 187 message_loop
188 ? nullptr 188 ? nullptr
189 : new cc::OrderedSimpleTaskRunner(clock_.get(), false)), 189 : new cc::OrderedSimpleTaskRunner(clock_.get(), false)),
190 message_loop_(message_loop), 190 message_loop_(message_loop),
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 420
421 TaskQueueManager* task_queue_manager() const { 421 TaskQueueManager* task_queue_manager() const {
422 return scheduler_helper_->GetTaskQueueManagerForTesting(); 422 return scheduler_helper_->GetTaskQueueManagerForTesting();
423 } 423 }
424 424
425 void ExpectIdlePeriodStartsButNeverEnds() { 425 void ExpectIdlePeriodStartsButNeverEnds() {
426 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(1); 426 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(1);
427 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(0); 427 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(0);
428 } 428 }
429 429
430 void ExpectIdlePeriodStartsAndEnds(const testing::Cardinality& cardinality) { 430 void ExpectIdlePeriodStartsAndEnds(
431 const ::testing::Cardinality& cardinality) {
431 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(cardinality); 432 EXPECT_CALL(*idle_helper_, OnIdlePeriodStarted()).Times(cardinality);
432 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(cardinality); 433 EXPECT_CALL(*idle_helper_, OnIdlePeriodEnded()).Times(cardinality);
433 } 434 }
434 435
435 private: 436 private:
436 DISALLOW_COPY_AND_ASSIGN(IdleHelperTestWithIdlePeriodObserver); 437 DISALLOW_COPY_AND_ASSIGN(IdleHelperTestWithIdlePeriodObserver);
437 }; 438 };
438 439
439 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestEnterButNotExitIdlePeriod) { 440 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestEnterButNotExitIdlePeriod) {
440 ExpectIdlePeriodStartsButNeverEnds(); 441 ExpectIdlePeriodStartsButNeverEnds();
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 FROM_HERE, 518 FROM_HERE,
518 base::Bind(&IdleHelperWithMessageLoopTest::PostFromNestedRunloop, 519 base::Bind(&IdleHelperWithMessageLoopTest::PostFromNestedRunloop,
519 base::Unretained(this), 520 base::Unretained(this),
520 base::Unretained(&tasks_to_post_from_nested_loop))); 521 base::Unretained(&tasks_to_post_from_nested_loop)));
521 522
522 idle_helper_->StartIdlePeriod( 523 idle_helper_->StartIdlePeriod(
523 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), 524 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
524 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10)); 525 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(10));
525 RunUntilIdle(); 526 RunUntilIdle();
526 // Note we expect task 3 to run last because it's non-nestable. 527 // Note we expect task 3 to run last because it's non-nestable.
527 EXPECT_THAT(order, testing::ElementsAre(std::string("1"), std::string("2"), 528 EXPECT_THAT(order, ::testing::ElementsAre(std::string("1"), std::string("2"),
528 std::string("4"), std::string("5"), 529 std::string("4"), std::string("5"),
529 std::string("3"))); 530 std::string("3")));
530 } 531 }
531 532
532 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestLongIdlePeriod) { 533 TEST_F(IdleHelperTestWithIdlePeriodObserver, TestLongIdlePeriod) {
533 base::TimeTicks expected_deadline = 534 base::TimeTicks expected_deadline =
534 clock_->NowTicks() + maximum_idle_period_duration(); 535 clock_->NowTicks() + maximum_idle_period_duration();
535 base::TimeTicks deadline_in_task; 536 base::TimeTicks deadline_in_task;
536 int run_count = 0; 537 int run_count = 0;
537 538
538 idle_task_runner_->PostIdleTask( 539 idle_task_runner_->PostIdleTask(
539 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 540 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 base::Bind(&RepostingUpdateClockIdleTestTask, 613 base::Bind(&RepostingUpdateClockIdleTestTask,
613 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), 614 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(),
614 idle_task_runtime, &actual_deadlines)); 615 idle_task_runtime, &actual_deadlines));
615 616
616 // Check each idle task runs in their own idle period. 617 // Check each idle task runs in their own idle period.
617 idle_helper_->EnableLongIdlePeriod(); 618 idle_helper_->EnableLongIdlePeriod();
618 RunUntilIdle(); 619 RunUntilIdle();
619 EXPECT_EQ(3, run_count); 620 EXPECT_EQ(3, run_count);
620 EXPECT_THAT( 621 EXPECT_THAT(
621 actual_deadlines, 622 actual_deadlines,
622 testing::ElementsAre( 623 ::testing::ElementsAre(
623 clock_before + maximum_idle_period_duration(), 624 clock_before + maximum_idle_period_duration(),
624 clock_before + idle_task_runtime + maximum_idle_period_duration(), 625 clock_before + idle_task_runtime + maximum_idle_period_duration(),
625 clock_before + (2 * idle_task_runtime) + 626 clock_before + (2 * idle_task_runtime) +
626 maximum_idle_period_duration())); 627 maximum_idle_period_duration()));
627 628
628 g_max_idle_task_reposts = 5; 629 g_max_idle_task_reposts = 5;
629 idle_task_runner_->PostIdleTask( 630 idle_task_runner_->PostIdleTask(
630 FROM_HERE, 631 FROM_HERE,
631 base::Bind(&RepostingUpdateClockIdleTestTask, 632 base::Bind(&RepostingUpdateClockIdleTestTask,
632 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), 633 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(),
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 FROM_HERE, 695 FROM_HERE,
695 base::Bind(&RepostingUpdateClockIdleTestTask, 696 base::Bind(&RepostingUpdateClockIdleTestTask,
696 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), 697 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(),
697 idle_task_runtime, &actual_deadlines)); 698 idle_task_runtime, &actual_deadlines));
698 699
699 idle_helper_->EnableLongIdlePeriod(); 700 idle_helper_->EnableLongIdlePeriod();
700 RunUntilIdle(); 701 RunUntilIdle();
701 EXPECT_EQ(2, run_count); 702 EXPECT_EQ(2, run_count);
702 EXPECT_THAT( 703 EXPECT_THAT(
703 actual_deadlines, 704 actual_deadlines,
704 testing::ElementsAre( 705 ::testing::ElementsAre(
705 clock_before + maximum_idle_period_duration(), 706 clock_before + maximum_idle_period_duration(),
706 clock_before + idle_task_runtime + maximum_idle_period_duration())); 707 clock_before + idle_task_runtime + maximum_idle_period_duration()));
707 } 708 }
708 709
709 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) { 710 TEST_F(IdleHelperTest, TestLongIdlePeriodRestartWaitsIfNotMaxDeadline) {
710 base::TimeTicks actual_deadline; 711 base::TimeTicks actual_deadline;
711 int run_count = 0; 712 int run_count = 0;
712 713
713 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20)); 714 base::TimeDelta pending_task_delay(base::TimeDelta::FromMilliseconds(20));
714 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10)); 715 base::TimeDelta idle_task_duration(base::TimeDelta::FromMilliseconds(10));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); 766 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10));
766 idle_task_runner_->PostIdleTask( 767 idle_task_runner_->PostIdleTask(
767 FROM_HERE, 768 FROM_HERE,
768 base::Bind(&RepostingUpdateClockIdleTestTask, 769 base::Bind(&RepostingUpdateClockIdleTestTask,
769 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(), 770 base::RetainedRef(idle_task_runner_), &run_count, clock_.get(),
770 idle_task_runtime, &actual_deadlines)); 771 idle_task_runtime, &actual_deadlines));
771 RunUntilIdle(); 772 RunUntilIdle();
772 EXPECT_EQ(2, run_count); 773 EXPECT_EQ(2, run_count);
773 EXPECT_THAT( 774 EXPECT_THAT(
774 actual_deadlines, 775 actual_deadlines,
775 testing::ElementsAre( 776 ::testing::ElementsAre(
776 clock_before + maximum_idle_period_duration(), 777 clock_before + maximum_idle_period_duration(),
777 clock_before + idle_task_runtime + maximum_idle_period_duration())); 778 clock_before + idle_task_runtime + maximum_idle_period_duration()));
778 779
779 // Once all task have been run we should go back to the paused state. 780 // Once all task have been run we should go back to the paused state.
780 CheckIdlePeriodStateIs("in_long_idle_period_paused"); 781 CheckIdlePeriodStateIs("in_long_idle_period_paused");
781 EXPECT_FALSE(scheduler_helper_->real_time_domain()->NextScheduledRunTime( 782 EXPECT_FALSE(scheduler_helper_->real_time_domain()->NextScheduledRunTime(
782 &next_pending_delayed_task)); 783 &next_pending_delayed_task));
783 784
784 idle_helper_->EndIdlePeriod(); 785 idle_helper_->EndIdlePeriod();
785 CheckIdlePeriodStateIs("not_in_idle_period"); 786 CheckIdlePeriodStateIs("not_in_idle_period");
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
1119 1120
1120 // Tests that the OnPendingTasksChanged callback is called once when the idle 1121 // Tests that the OnPendingTasksChanged callback is called once when the idle
1121 // queue becomes non-empty and again when it becomes empty. 1122 // queue becomes non-empty and again when it becomes empty.
1122 TEST_F(IdleHelperTest, OnPendingTasksChanged) { 1123 TEST_F(IdleHelperTest, OnPendingTasksChanged) {
1123 int run_count = 0; 1124 int run_count = 0;
1124 base::TimeTicks expected_deadline = 1125 base::TimeTicks expected_deadline =
1125 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300); 1126 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300);
1126 base::TimeTicks deadline_in_task; 1127 base::TimeTicks deadline_in_task;
1127 1128
1128 { 1129 {
1129 testing::InSequence dummy; 1130 ::testing::InSequence dummy;
1130 // This will be called once. I.e when the one and only task is posted. 1131 // This will be called once. I.e when the one and only task is posted.
1131 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(true)).Times(1); 1132 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(true)).Times(1);
1132 // This will be called once. I.e when the one and only task completes. 1133 // This will be called once. I.e when the one and only task completes.
1133 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(false)).Times(1); 1134 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(false)).Times(1);
1134 } 1135 }
1135 1136
1136 clock_->Advance(base::TimeDelta::FromMilliseconds(100)); 1137 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
1137 idle_task_runner_->PostIdleTask( 1138 idle_task_runner_->PostIdleTask(
1138 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1139 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1139 1140
(...skipping 10 matching lines...) Expand all
1150 1151
1151 // Tests that the OnPendingTasksChanged callback is still only called once 1152 // Tests that the OnPendingTasksChanged callback is still only called once
1152 // with false despite there being two idle tasks posted. 1153 // with false despite there being two idle tasks posted.
1153 TEST_F(IdleHelperTest, OnPendingTasksChanged_TwoTasksAtTheSameTime) { 1154 TEST_F(IdleHelperTest, OnPendingTasksChanged_TwoTasksAtTheSameTime) {
1154 int run_count = 0; 1155 int run_count = 0;
1155 base::TimeTicks expected_deadline = 1156 base::TimeTicks expected_deadline =
1156 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300); 1157 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(2300);
1157 base::TimeTicks deadline_in_task; 1158 base::TimeTicks deadline_in_task;
1158 1159
1159 { 1160 {
1160 testing::InSequence dummy; 1161 ::testing::InSequence dummy;
1161 // This will be called 3 times. I.e when T1 and T2 are posted and when T1 1162 // This will be called 3 times. I.e when T1 and T2 are posted and when T1
1162 // completes. 1163 // completes.
1163 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(true)).Times(3); 1164 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(true)).Times(3);
1164 // This will be called once. I.e when T2 completes. 1165 // This will be called once. I.e when T2 completes.
1165 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(false)).Times(1); 1166 EXPECT_CALL(*idle_helper_, OnPendingTasksChanged(false)).Times(1);
1166 } 1167 }
1167 1168
1168 clock_->Advance(base::TimeDelta::FromMilliseconds(100)); 1169 clock_->Advance(base::TimeDelta::FromMilliseconds(100));
1169 idle_task_runner_->PostIdleTask( 1170 idle_task_runner_->PostIdleTask(
1170 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1171 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1171 idle_task_runner_->PostIdleTask( 1172 idle_task_runner_->PostIdleTask(
1172 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task)); 1173 FROM_HERE, base::Bind(&IdleTestTask, &run_count, &deadline_in_task));
1173 1174
1174 RunUntilIdle(); 1175 RunUntilIdle();
1175 EXPECT_EQ(0, run_count); 1176 EXPECT_EQ(0, run_count);
1176 1177
1177 idle_helper_->StartIdlePeriod( 1178 idle_helper_->StartIdlePeriod(
1178 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(), 1179 IdleHelper::IdlePeriodState::IN_SHORT_IDLE_PERIOD, clock_->NowTicks(),
1179 expected_deadline); 1180 expected_deadline);
1180 RunUntilIdle(); 1181 RunUntilIdle();
1181 EXPECT_EQ(2, run_count); 1182 EXPECT_EQ(2, run_count);
1182 EXPECT_EQ(expected_deadline, deadline_in_task); 1183 EXPECT_EQ(expected_deadline, deadline_in_task);
1183 } 1184 }
1184 1185
1185 } // namespace scheduler 1186 } // namespace scheduler
1186 } // namespace blink 1187 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698