OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/renderer/scheduler/renderer_scheduler_impl.h" | 5 #include "content/renderer/scheduler/renderer_scheduler_impl.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "cc/output/begin_frame_args.h" | 8 #include "cc/output/begin_frame_args.h" |
9 #include "cc/test/ordered_simple_task_runner.h" | 9 #include "cc/test/ordered_simple_task_runner.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, false)), | 49 mock_task_runner_(new cc::OrderedSimpleTaskRunner(clock_, false)), |
50 scheduler_(new RendererSchedulerImpl(mock_task_runner_)), | 50 scheduler_(new RendererSchedulerImpl(mock_task_runner_)), |
51 default_task_runner_(scheduler_->DefaultTaskRunner()), | 51 default_task_runner_(scheduler_->DefaultTaskRunner()), |
52 compositor_task_runner_(scheduler_->CompositorTaskRunner()), | 52 compositor_task_runner_(scheduler_->CompositorTaskRunner()), |
53 loading_task_runner_(scheduler_->LoadingTaskRunner()), | 53 loading_task_runner_(scheduler_->LoadingTaskRunner()), |
54 idle_task_runner_(scheduler_->IdleTaskRunner()) { | 54 idle_task_runner_(scheduler_->IdleTaskRunner()) { |
55 scheduler_->SetTimeSourceForTesting(clock_); | 55 scheduler_->SetTimeSourceForTesting(clock_); |
56 } | 56 } |
57 ~RendererSchedulerImplTest() override {} | 57 ~RendererSchedulerImplTest() override {} |
58 | 58 |
59 void RunUntilIdle() { mock_task_runner_->RunUntilIdle(); } | 59 void TearDown() override { |
60 // Check that all tests stop posting tasks. | |
61 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
62 while (RunUntilIdle()) | |
63 ; | |
rmcilroy
2015/03/12 22:09:34
nit - this would be clearer as an empty '{ }' rath
alex clarke (OOO till 29th)
2015/03/13 09:53:03
Done.
| |
64 } | |
65 | |
66 bool RunUntilIdle() { return mock_task_runner_->RunUntilIdle(); } | |
60 | 67 |
61 void DoMainFrame() { | 68 void DoMainFrame() { |
62 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | 69 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( |
63 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | 70 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), |
64 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | 71 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); |
65 scheduler_->DidCommitFrameToCompositor(); | 72 scheduler_->DidCommitFrameToCompositor(); |
66 } | 73 } |
67 | 74 |
68 void EnableIdleTasks() { DoMainFrame(); } | 75 void EnableIdleTasks() { DoMainFrame(); } |
69 | 76 |
70 Policy CurrentPolicy() { return scheduler_->current_policy_; } | 77 Policy CurrentPolicy() { return scheduler_->current_policy_; } |
71 | 78 |
79 void EnsureUrgentPolicyUpdatePostedOnMainThread() { | |
80 base::AutoLock lock(scheduler_->incoming_signals_lock_); | |
81 scheduler_->EnsureUrgentPolicyUpdatePostedOnMainThread(FROM_HERE); | |
82 } | |
83 | |
84 void ScheduleDelayedPolicyUpdate(base::TimeDelta delay) { | |
85 scheduler_->delayed_update_policy_runner_.SetDeadline(FROM_HERE, delay, | |
86 clock_->Now()); | |
87 } | |
88 | |
72 // Helper for posting several tasks of specific types. |task_descriptor| is a | 89 // Helper for posting several tasks of specific types. |task_descriptor| is a |
73 // string with space delimited task identifiers. The first letter of each | 90 // string with space delimited task identifiers. The first letter of each |
74 // task identifier specifies the task type: | 91 // task identifier specifies the task type: |
75 // - 'D': Default task | 92 // - 'D': Default task |
76 // - 'C': Compositor task | 93 // - 'C': Compositor task |
77 // - 'L': Loading task | 94 // - 'L': Loading task |
78 // - 'I': Idle task | 95 // - 'I': Idle task |
79 void PostTestTasks(std::vector<std::string>* run_order, | 96 void PostTestTasks(std::vector<std::string>* run_order, |
80 const std::string& task_descriptor) { | 97 const std::string& task_descriptor) { |
81 std::istringstream stream(task_descriptor); | 98 std::istringstream stream(task_descriptor); |
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
840 RunUntilIdle(); | 857 RunUntilIdle(); |
841 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); | 858 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); |
842 | 859 |
843 // Process the input event with a new BeginMainFrame. | 860 // Process the input event with a new BeginMainFrame. |
844 DoMainFrame(); | 861 DoMainFrame(); |
845 clock_->AdvanceNow(2 * priority_escalation_after_input_duration()); | 862 clock_->AdvanceNow(2 * priority_escalation_after_input_duration()); |
846 RunUntilIdle(); | 863 RunUntilIdle(); |
847 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); | 864 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); |
848 } | 865 } |
849 | 866 |
867 class MockRendererSchedulerImpl : public RendererSchedulerImpl { | |
rmcilroy
2015/03/12 22:09:34
naming nit - this isn't really a mock, RenderSched
alex clarke (OOO till 29th)
2015/03/13 09:53:03
Done.
| |
868 public: | |
869 MockRendererSchedulerImpl( | |
870 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner) | |
871 : RendererSchedulerImpl(main_task_runner), update_policy_count_(0) {} | |
872 | |
873 void UpdatePolicyLocked() override { | |
874 update_policy_count_++; | |
875 RendererSchedulerImpl::UpdatePolicyLocked(); | |
876 } | |
877 | |
878 int update_policy_count_; | |
879 }; | |
880 | |
881 TEST_F(RendererSchedulerImplTest, OnlyOnePendingUrgentPolicyUpdatey) { | |
882 MockRendererSchedulerImpl* mock_scheduler = | |
883 new MockRendererSchedulerImpl(mock_task_runner_); | |
884 scheduler_.reset(mock_scheduler); | |
885 | |
886 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
887 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
888 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
889 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
890 | |
891 RunUntilIdle(); | |
892 | |
893 EXPECT_EQ(1, mock_scheduler->update_policy_count_); | |
894 } | |
895 | |
896 TEST_F(RendererSchedulerImplTest, OnePendingDelayedAndOneUrgentUpdatePolicy) { | |
897 MockRendererSchedulerImpl* mock_scheduler = | |
898 new MockRendererSchedulerImpl(mock_task_runner_); | |
899 scheduler_.reset(mock_scheduler); | |
900 scheduler_->SetTimeSourceForTesting(clock_); | |
901 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
902 | |
903 ScheduleDelayedPolicyUpdate(base::TimeDelta::FromMilliseconds(1)); | |
904 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
905 | |
906 RunUntilIdle(); | |
907 | |
908 // We expect both the urgent and the delayed updates to run. | |
909 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
910 } | |
911 | |
912 TEST_F(RendererSchedulerImplTest, OneUrgentAndOnePendingDelayedUpdatePolicy) { | |
913 MockRendererSchedulerImpl* mock_scheduler = | |
914 new MockRendererSchedulerImpl(mock_task_runner_); | |
915 scheduler_.reset(mock_scheduler); | |
916 scheduler_->SetTimeSourceForTesting(clock_); | |
917 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
918 | |
919 EnsureUrgentPolicyUpdatePostedOnMainThread(); | |
920 ScheduleDelayedPolicyUpdate(base::TimeDelta::FromMilliseconds(1)); | |
921 | |
922 RunUntilIdle(); | |
923 | |
924 // We expect both the urgent and the delayed updates to run. | |
925 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
926 } | |
927 | |
928 TEST_F(RendererSchedulerImplTest, UpdatePolicyCountTriggeredByOneInputEvent) { | |
929 MockRendererSchedulerImpl* mock_scheduler = | |
930 new MockRendererSchedulerImpl(mock_task_runner_); | |
931 scheduler_.reset(mock_scheduler); | |
932 scheduler_->SetTimeSourceForTesting(clock_); | |
933 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
934 | |
935 scheduler_->DidReceiveInputEventOnCompositorThread( | |
936 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
937 | |
938 RunUntilIdle(); | |
939 | |
940 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
941 } | |
942 | |
943 TEST_F(RendererSchedulerImplTest, UpdatePolicyCountTriggeredByTwoInputEvents) { | |
944 MockRendererSchedulerImpl* mock_scheduler = | |
945 new MockRendererSchedulerImpl(mock_task_runner_); | |
946 scheduler_.reset(mock_scheduler); | |
947 scheduler_->SetTimeSourceForTesting(clock_); | |
948 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
949 | |
950 scheduler_->DidReceiveInputEventOnCompositorThread( | |
951 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
952 scheduler_->DidReceiveInputEventOnCompositorThread( | |
953 FakeInputEvent(blink::WebInputEvent::TouchMove)); | |
954 | |
955 RunUntilIdle(); | |
956 | |
957 EXPECT_EQ(2, mock_scheduler->update_policy_count_); | |
958 } | |
959 | |
960 TEST_F(RendererSchedulerImplTest, EnsureUpdatePolicyNotTriggeredTooOften) { | |
961 MockRendererSchedulerImpl* mock_scheduler = | |
962 new MockRendererSchedulerImpl(mock_task_runner_); | |
963 scheduler_.reset(mock_scheduler); | |
964 scheduler_->SetTimeSourceForTesting(clock_); | |
965 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | |
966 | |
967 scheduler_->DidReceiveInputEventOnCompositorThread( | |
968 FakeInputEvent(blink::WebInputEvent::TouchStart)); | |
969 scheduler_->DidReceiveInputEventOnCompositorThread( | |
970 FakeInputEvent(blink::WebInputEvent::TouchMove)); | |
971 | |
972 // We expect the first call to IsHighPriorityWorkAnticipated to be called | |
973 // after recieving an input event (but before the UpdateTask was processed) to | |
974 // call UpdatePolicy. | |
975 EXPECT_EQ(0, mock_scheduler->update_policy_count_); | |
976 scheduler_->IsHighPriorityWorkAnticipated(); | |
977 EXPECT_EQ(1, mock_scheduler->update_policy_count_); | |
978 // Subsequent calls should not call UpdatePolicy. | |
979 scheduler_->IsHighPriorityWorkAnticipated(); | |
980 scheduler_->IsHighPriorityWorkAnticipated(); | |
981 scheduler_->IsHighPriorityWorkAnticipated(); | |
982 scheduler_->ShouldYieldForHighPriorityWork(); | |
983 scheduler_->ShouldYieldForHighPriorityWork(); | |
984 scheduler_->ShouldYieldForHighPriorityWork(); | |
985 scheduler_->ShouldYieldForHighPriorityWork(); | |
986 | |
987 EXPECT_EQ(1, mock_scheduler->update_policy_count_); | |
988 | |
989 RunUntilIdle(); | |
990 // We expect both the urgent and the delayed updates to run in addition to the | |
991 // earlier updated cause by IsHighPriorityWorkAnticipated. | |
992 EXPECT_EQ(3, mock_scheduler->update_policy_count_); | |
993 } | |
rmcilroy
2015/03/12 22:09:34
Nice tests, thanks!
alex clarke (OOO till 29th)
2015/03/13 09:53:03
Acknowledged.
| |
994 | |
850 } // namespace content | 995 } // namespace content |
OLD | NEW |