File: compositor_animation_runner_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (212 lines) | stat: -rw-r--r-- 6,815 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/animation/compositor_animation_runner.h"

#include "base/test/bind.h"
#include "base/timer/timer.h"
#include "ui/compositor/compositor_metrics_tracker.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/views/animation/animation_delegate_views.h"
#include "ui/views/buildflags.h"
#include "ui/views/test/widget_test.h"

namespace views::test {
namespace {
constexpr base::TimeDelta kDuration = base::Milliseconds(100);
}

using CompositorAnimationRunnerTest = WidgetTest;

TEST_F(CompositorAnimationRunnerTest, BasicCoverageTest) {
  WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
  widget->Show();

  AnimationDelegateViews delegate(widget->GetContentsView());
  gfx::LinearAnimation animation(
      kDuration, gfx::LinearAnimation::kDefaultFrameRate, &delegate);

  base::RepeatingTimer interval_timer;
  base::RunLoop run_loop;

  animation.Start();
  EXPECT_TRUE(animation.is_animating());
  EXPECT_TRUE(delegate.container()->has_custom_animation_runner());

  interval_timer.Start(FROM_HERE, kDuration, base::BindLambdaForTesting([&]() {
                         if (animation.is_animating()) {
                           return;
                         }

                         interval_timer.Stop();
                         run_loop.Quit();
                       }));

  run_loop.Run();

  // Verifies that AnimationDelegateViews carries location of call sites
  // instead of implementation.
  EXPECT_STREQ(base::Location::Current().file_name(),
               delegate.location_for_test().file_name());
}

namespace {

// Test AnimationDelegateView which has a non-zero expected animation duration
// time, which is required for getting smoothness reports.
class TestAnimationDelegateViews : public AnimationDelegateViews {
 public:
  explicit TestAnimationDelegateViews(View* view)
      : AnimationDelegateViews(view) {}
  TestAnimationDelegateViews(TestAnimationDelegateViews&) = delete;
  TestAnimationDelegateViews& operator=(TestAnimationDelegateViews&) = delete;
  ~TestAnimationDelegateViews() override = default;

  // AnimationDelegateViews:
  base::TimeDelta GetAnimationDurationForReporting() const override {
    return kDuration;
  }
};

}  // namespace

#if BUILDFLAG(IS_CHROMEOS)
// Tests that ui::CompositorMetricsTracker will report for gfx::Animation. Only
// supported on ChromeOS.
TEST_F(CompositorAnimationRunnerTest, ThroughputTracker) {
  WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
  widget->Show();

  ui::DrawWaiterForTest::WaitForCompositingStarted(widget->GetCompositor());

  int report_count = 0;
  int report_count2 = 0;

  TestAnimationDelegateViews delegate(widget->GetContentsView());

  gfx::LinearAnimation animation(
      kDuration, gfx::LinearAnimation::kDefaultFrameRate, &delegate);

  base::RepeatingTimer interval_timer;
  base::RunLoop run_loop;

  ui::CompositorMetricsTracker tracker1 =
      widget->GetCompositor()->RequestNewCompositorMetricsTracker();
  tracker1.Start(base::BindLambdaForTesting(
      [&](const cc::FrameSequenceMetrics::CustomReportData& data) {
        ++report_count;
        run_loop.Quit();
      }));

  animation.Start();
  EXPECT_TRUE(animation.is_animating());
  EXPECT_TRUE(delegate.container()->has_custom_animation_runner());

  interval_timer.Start(FROM_HERE, kDuration, base::BindLambdaForTesting([&]() {
                         if (animation.is_animating()) {
                           return;
                         }

                         interval_timer.Stop();
                         tracker1.Stop();
                       }));
  run_loop.Run();
  EXPECT_EQ(1, report_count);
  EXPECT_EQ(0, report_count2);

  // Tests that switching metrics reporters for the next animation works as
  // expected.
  base::RunLoop run_loop2;

  ui::CompositorMetricsTracker tracker2 =
      widget->GetCompositor()->RequestNewCompositorMetricsTracker();
  tracker2.Start(base::BindLambdaForTesting(
      [&](const cc::FrameSequenceMetrics::CustomReportData& data) {
        ++report_count2;
        run_loop2.Quit();
      }));

  animation.Start();
  EXPECT_TRUE(animation.is_animating());

  interval_timer.Start(FROM_HERE, kDuration, base::BindLambdaForTesting([&]() {
                         if (animation.is_animating()) {
                           return;
                         }

                         interval_timer.Stop();
                         tracker2.Stop();
                       }));
  run_loop2.Run();
  EXPECT_EQ(1, report_count);
  EXPECT_EQ(1, report_count2);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

// No DesktopAura on ChromeOS.
// Each widget on MACOSX has its own ui::Compositor.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
using CompositorAnimationRunnerDesktopTest = DesktopWidgetTest;

TEST_F(CompositorAnimationRunnerDesktopTest, SwitchCompositor) {
  WidgetAutoclosePtr widget1(CreateTopLevelNativeWidget());
  widget1->Show();

  WidgetAutoclosePtr widget2(CreateTopLevelNativeWidget());
  widget2->Show();

  ASSERT_NE(widget1->GetCompositor(), widget2->GetCompositor());

  Widget* child = CreateChildNativeWidgetWithParent(widget1.get());
  child->Show();
  AnimationDelegateViews delegate(child->GetContentsView());
  gfx::LinearAnimation animation(
      kDuration, gfx::LinearAnimation::kDefaultFrameRate, &delegate);

  base::RepeatingTimer interval_timer;

  animation.Start();
  EXPECT_TRUE(animation.is_animating());
  EXPECT_TRUE(delegate.container()->has_custom_animation_runner());
  {
    base::RunLoop run_loop;
    interval_timer.Start(FROM_HERE, kDuration,
                         base::BindLambdaForTesting([&]() {
                           if (animation.is_animating()) {
                             return;
                           }
                           interval_timer.Stop();
                           run_loop.Quit();
                         }));
    run_loop.Run();
  }

  EXPECT_FALSE(animation.is_animating());

  Widget::ReparentNativeView(child->GetNativeView(), widget2->GetNativeView());
  widget1.reset();

  animation.Start();
  EXPECT_TRUE(animation.is_animating());
  EXPECT_TRUE(delegate.container()->has_custom_animation_runner());

  {
    base::RunLoop run_loop;
    interval_timer.Start(FROM_HERE, kDuration,
                         base::BindLambdaForTesting([&]() {
                           if (animation.is_animating()) {
                             return;
                           }

                           interval_timer.Stop();
                           run_loop.Quit();
                         }));

    run_loop.Run();
  }
}
#endif

}  // namespace views::test