1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/metrics/jank_injector.h"
9 #include "base/test/scoped_feature_list.h"
10 #include "base/test/test_simple_task_runner.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 class JankInjectorTest : public testing::Test {
19 JankInjectorTest() = default;
20 ~JankInjectorTest() override = default;
23 TEST_F(JankInjectorTest, Basic) {
24 base::test::ScopedFeatureList features;
25 features.InitFromCommandLine("JankInjectionAblation:cluster/4/percent/10",
28 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
29 new base::TestSimpleTaskRunner());
31 ScopedJankInjectionEnabler enable_jank;
32 JankInjector injector;
33 const auto& config = injector.config();
34 EXPECT_EQ(config.target_dropped_frames_percent, 10u);
35 EXPECT_EQ(config.dropped_frame_cluster_size, 4u);
37 const uint32_t kSourceId = 1;
38 uint32_t sequence_number = 1;
39 constexpr base::TimeDelta kInterval = base::Milliseconds(16);
40 base::TimeTicks frame_time = base::TimeTicks::Now();
41 base::TimeTicks deadline = frame_time + kInterval;
43 auto args = viz::BeginFrameArgs::Create(
44 BEGINFRAME_FROM_HERE, kSourceId, ++sequence_number, frame_time, deadline,
45 kInterval, viz::BeginFrameArgs::NORMAL);
46 // For the first frame, no janks scheduled.
47 injector.ScheduleJankIfNeeded(args, task_runner.get());
48 EXPECT_FALSE(task_runner->HasPendingTask());
50 // Generate over 100 frames. This should cause jank to be injected 3 times.
51 for (uint32_t count = 0; count < 100; ++count) {
52 args.frame_time += kInterval;
53 args.deadline += kInterval;
54 ++args.frame_id.sequence_number;
55 injector.ScheduleJankIfNeeded(args, task_runner.get());
58 // Jank should be injected 3 times for the 100 frames.
59 EXPECT_EQ(task_runner->NumPendingTasks(), 3u);