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

Side by Side Diff: content/renderer/scheduler/renderer_scheduler_impl_unittest.cc

Issue 994833003: Prevent multiple pending UpdatePolicy tasks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tweak 2 Created 5 years, 9 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 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
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 }
64
65 bool RunUntilIdle() { return mock_task_runner_->RunUntilIdle(); }
60 66
61 void DoMainFrame() { 67 void DoMainFrame() {
62 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( 68 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create(
63 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), 69 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(),
64 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); 70 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL));
65 scheduler_->DidCommitFrameToCompositor(); 71 scheduler_->DidCommitFrameToCompositor();
66 } 72 }
67 73
68 void EnableIdleTasks() { DoMainFrame(); } 74 void EnableIdleTasks() { DoMainFrame(); }
69 75
70 Policy CurrentPolicy() { return scheduler_->current_policy_; } 76 Policy CurrentPolicy() { return scheduler_->current_policy_; }
71 77
78 void EnsureUgentPolicyUpdatePostedOnMainThread() {
79 base::AutoLock lock(scheduler_->incoming_signals_lock_);
80 scheduler_->EnsureUgentPolicyUpdatePostedOnMainThread(FROM_HERE);
81 }
82
83 void ScheduleDelayedPolicyUpdate(base::TimeDelta delay) {
84 scheduler_->delayed_update_policy_runner_.SetDeadline(
85 FROM_HERE, clock_->Now() + delay, clock_->Now());
86 }
87
72 // Helper for posting several tasks of specific types. |task_descriptor| is a 88 // Helper for posting several tasks of specific types. |task_descriptor| is a
73 // string with space delimited task identifiers. The first letter of each 89 // string with space delimited task identifiers. The first letter of each
74 // task identifier specifies the task type: 90 // task identifier specifies the task type:
75 // - 'D': Default task 91 // - 'D': Default task
76 // - 'C': Compositor task 92 // - 'C': Compositor task
77 // - 'L': Loading task 93 // - 'L': Loading task
78 // - 'I': Idle task 94 // - 'I': Idle task
79 void PostTestTasks(std::vector<std::string>* run_order, 95 void PostTestTasks(std::vector<std::string>* run_order,
80 const std::string& task_descriptor) { 96 const std::string& task_descriptor) {
81 std::istringstream stream(task_descriptor); 97 std::istringstream stream(task_descriptor);
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 RunUntilIdle(); 856 RunUntilIdle();
841 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy()); 857 EXPECT_EQ(Policy::COMPOSITOR_PRIORITY, CurrentPolicy());
842 858
843 // Process the input event with a new BeginMainFrame. 859 // Process the input event with a new BeginMainFrame.
844 DoMainFrame(); 860 DoMainFrame();
845 clock_->AdvanceNow(2 * priority_escalation_after_input_duration()); 861 clock_->AdvanceNow(2 * priority_escalation_after_input_duration());
846 RunUntilIdle(); 862 RunUntilIdle();
847 EXPECT_EQ(Policy::NORMAL, CurrentPolicy()); 863 EXPECT_EQ(Policy::NORMAL, CurrentPolicy());
848 } 864 }
849 865
866 class MockRendererSchedulerImpl : public RendererSchedulerImpl {
867 public:
868 MockRendererSchedulerImpl(
869 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner)
870 : RendererSchedulerImpl(main_task_runner), update_policy_count_(0) {}
871
872 void UpdatePolicyLocked() override {
873 update_policy_count_++;
874 RendererSchedulerImpl::UpdatePolicyLocked();
875 }
876
877 int update_policy_count_;
878 };
879
880 TEST_F(RendererSchedulerImplTest, OnlyOnePendingUrgentPolicyUpdatey) {
881 MockRendererSchedulerImpl* mock_scheduler =
882 new MockRendererSchedulerImpl(mock_task_runner_);
883 scheduler_.reset(mock_scheduler);
884
885 EnsureUgentPolicyUpdatePostedOnMainThread();
886 EnsureUgentPolicyUpdatePostedOnMainThread();
887 EnsureUgentPolicyUpdatePostedOnMainThread();
888 EnsureUgentPolicyUpdatePostedOnMainThread();
889
890 RunUntilIdle();
891
892 EXPECT_EQ(1, mock_scheduler->update_policy_count_);
893 }
894
895 TEST_F(RendererSchedulerImplTest, OnePendingDelayedAndOneUrgentUpdatePolicy) {
896 MockRendererSchedulerImpl* mock_scheduler =
897 new MockRendererSchedulerImpl(mock_task_runner_);
898 scheduler_.reset(mock_scheduler);
899 scheduler_->SetTimeSourceForTesting(clock_);
900 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
901
902 ScheduleDelayedPolicyUpdate(base::TimeDelta::FromMilliseconds(1));
903 EnsureUgentPolicyUpdatePostedOnMainThread();
904
905 RunUntilIdle();
906
907 // We expect both the urgent and the delayed updates to run.
908 EXPECT_EQ(2, mock_scheduler->update_policy_count_);
909 }
910
911 TEST_F(RendererSchedulerImplTest, OneUrgentAndOnePendingDelayedUpdatePolicy) {
912 MockRendererSchedulerImpl* mock_scheduler =
913 new MockRendererSchedulerImpl(mock_task_runner_);
914 scheduler_.reset(mock_scheduler);
915 scheduler_->SetTimeSourceForTesting(clock_);
916 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
917
918 EnsureUgentPolicyUpdatePostedOnMainThread();
919 ScheduleDelayedPolicyUpdate(base::TimeDelta::FromMilliseconds(1));
920
921 RunUntilIdle();
922
923 // We expect both the urgent and the delayed updates to run.
924 EXPECT_EQ(2, mock_scheduler->update_policy_count_);
925 }
926
927 TEST_F(RendererSchedulerImplTest, UpdatePolicyCountTriggeredByOneInputEvent) {
928 MockRendererSchedulerImpl* mock_scheduler =
929 new MockRendererSchedulerImpl(mock_task_runner_);
930 scheduler_.reset(mock_scheduler);
931 scheduler_->SetTimeSourceForTesting(clock_);
932 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
933
934 scheduler_->DidReceiveInputEventOnCompositorThread(
935 FakeInputEvent(blink::WebInputEvent::TouchStart));
936
937 RunUntilIdle();
938
939 EXPECT_EQ(2, mock_scheduler->update_policy_count_);
940 }
941
942 TEST_F(RendererSchedulerImplTest, UpdatePolicyCountTriggeredByTwoInputEvents) {
943 MockRendererSchedulerImpl* mock_scheduler =
944 new MockRendererSchedulerImpl(mock_task_runner_);
945 scheduler_.reset(mock_scheduler);
946 scheduler_->SetTimeSourceForTesting(clock_);
947 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
948
949 scheduler_->DidReceiveInputEventOnCompositorThread(
950 FakeInputEvent(blink::WebInputEvent::TouchStart));
951 scheduler_->DidReceiveInputEventOnCompositorThread(
952 FakeInputEvent(blink::WebInputEvent::TouchMove));
953
954 RunUntilIdle();
955
956 EXPECT_EQ(2, mock_scheduler->update_policy_count_);
957 }
958
959 TEST_F(RendererSchedulerImplTest, EnsureUpdatePolicyNotTriggeredTooOften) {
960 MockRendererSchedulerImpl* mock_scheduler =
961 new MockRendererSchedulerImpl(mock_task_runner_);
962 scheduler_.reset(mock_scheduler);
963 scheduler_->SetTimeSourceForTesting(clock_);
964 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true);
965
966 scheduler_->DidReceiveInputEventOnCompositorThread(
967 FakeInputEvent(blink::WebInputEvent::TouchStart));
968 scheduler_->DidReceiveInputEventOnCompositorThread(
969 FakeInputEvent(blink::WebInputEvent::TouchMove));
970
971 // We expect the first call to IsHighPriorityWorkAnticipated to be called
972 // after recieving an input event (but before the UpdateTask was processed) to
973 // call UpdatePolicy.
974 EXPECT_EQ(0, mock_scheduler->update_policy_count_);
975 scheduler_->IsHighPriorityWorkAnticipated();
976 EXPECT_EQ(1, mock_scheduler->update_policy_count_);
977 // Subsequent calls should not call UpdateTask.
978 scheduler_->IsHighPriorityWorkAnticipated();
979 scheduler_->IsHighPriorityWorkAnticipated();
980 scheduler_->IsHighPriorityWorkAnticipated();
981 scheduler_->ShouldYieldForHighPriorityWork();
982 scheduler_->ShouldYieldForHighPriorityWork();
983 scheduler_->ShouldYieldForHighPriorityWork();
984 scheduler_->ShouldYieldForHighPriorityWork();
985
986 EXPECT_EQ(1, mock_scheduler->update_policy_count_);
987
988 RunUntilIdle();
989 // We expect both the urgent and the delayed updates to run in addition to the
990 // earlier updated cause by IsHighPriorityWorkAnticipated.
991 EXPECT_EQ(3, mock_scheduler->update_policy_count_);
992 }
993
850 } // namespace content 994 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698