File: fling_scheduler_android_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 (281 lines) | stat: -rw-r--r-- 11,402 bytes parent folder | download | duplicates (5)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/service/input/fling_scheduler_android.h"

#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "components/input/features.h"
#include "components/viz/service/frame_sinks/external_begin_frame_source_android.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/service/gl/mock_gpu_service_impl.h"
#include "components/viz/service/input/input_manager.h"
#include "components/viz/service/input/mock_input_manager.h"
#include "components/viz/test/begin_frame_args_test.h"
#include "components/viz/test/mock_compositor_frame_sink_client.h"
#include "components/viz/test/mock_display_client.h"
#include "components/viz/test/test_output_surface_provider.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace viz {
namespace {
constexpr FrameSinkId kFrameSinkIdRoot(1, 1);
constexpr FrameSinkId kFrameSinkIdA(2, 1);

// Holds the four interface objects needed to create a RootCompositorFrameSink.
struct RootCompositorFrameSinkData {
  mojom::RootCompositorFrameSinkParamsPtr BuildParams(
      const FrameSinkId& frame_sink_id) {
    auto params = mojom::RootCompositorFrameSinkParams::New();
    params->frame_sink_id = frame_sink_id;
    params->widget = gpu::kNullSurfaceHandle;
    params->compositor_frame_sink =
        compositor_frame_sink.BindNewEndpointAndPassReceiver();
    params->compositor_frame_sink_client =
        compositor_frame_sink_client.BindInterfaceRemote();
    params->display_private = display_private.BindNewEndpointAndPassReceiver();
    params->display_client = display_client.BindRemote();
    return params;
  }

  mojo::AssociatedRemote<mojom::CompositorFrameSink> compositor_frame_sink;
  MockCompositorFrameSinkClient compositor_frame_sink_client;
  mojo::AssociatedRemote<mojom::DisplayPrivate> display_private;
  MockDisplayClient display_client;
};

}  // namespace

class FlingSchedulerTest : public testing::Test,
                           public input::FlingControllerEventSenderClient {
 public:
  FlingSchedulerTest() {
    FrameSinkManagerImpl::InitParams init_params(&output_surface_provider_);
    init_params.gpu_service = &gpu_service_;
    frame_sink_manager_ =
        std::make_unique<FrameSinkManagerImpl>(std::move(init_params));
    scoped_feature_list_.InitWithFeatures(
        /* enabled_features */ {input::features::kInputOnViz},
        /* disabled_features */ {});
  }

  FlingSchedulerTest(const FlingSchedulerTest&) = delete;
  FlingSchedulerTest& operator=(const FlingSchedulerTest&) = delete;

  void SetUp() override {
    frame_sink_manager_->SetInputManagerForTesting(
        std::make_unique<MockInputManager>(frame_sink_manager_.get()));

    RootCompositorFrameSinkData root_data1;
    frame_sink_manager_->CreateRootCompositorFrameSink(
        root_data1.BuildParams(kFrameSinkIdRoot));
    EXPECT_TRUE(CompositorFrameSinkExists(kFrameSinkIdRoot));

    // Create a grouping id.
    base::UnguessableToken grouping_id = base::UnguessableToken::Create();
    // Create a CompositorFrameSinkImpl.
    frame_sink_manager_->RegisterFrameSinkId(kFrameSinkIdA,
                                             true /* report_activation */);
    CreateCompositorFrameSink(kFrameSinkIdA, CreateRIRConfig(grouping_id));

    // Set up initial hierarchy: root -> A.
    frame_sink_manager_->RegisterFrameSinkHierarchy(kFrameSinkIdRoot,
                                                    kFrameSinkIdA);
    ASSERT_EQ(frame_sink_manager_->GetOldestParentByChildFrameId(kFrameSinkIdA),
              kFrameSinkIdRoot);
  }

  void TearDown() override {
    // Cleanup hierarchy.
    frame_sink_manager_->UnregisterFrameSinkHierarchy(kFrameSinkIdRoot,
                                                      kFrameSinkIdA);
    frame_sink_manager_->InvalidateFrameSinkId(kFrameSinkIdRoot);
    // Invalidating should destroy the CompositorFrameSinkImpl's.
    frame_sink_manager_->InvalidateFrameSinkId(kFrameSinkIdA);

    fling_controller_.reset();
    // Make sure that all FrameSinkSourceMappings have been deleted.
    EXPECT_TRUE(frame_sink_manager_->frame_sink_source_map_.empty());
    // Make sure test cleans up all [Root]CompositorFrameSinkImpls.
    EXPECT_TRUE(frame_sink_manager_->support_map_.empty());
    // Make sure test has invalidated all registered FrameSinkIds.
    EXPECT_TRUE(frame_sink_manager_->frame_sink_data_.empty());
  }

  void SetupFlingController() {
    auto* rir =
        GetInputManager()->GetRenderInputRouterFromFrameSinkId(kFrameSinkIdA);
    fling_controller_ = std::make_unique<input::FlingController>(
        this, rir->GetFlingSchedulerForTesting(),
        input::FlingController::Config());
  }

  void SimulateFlingStart(const gfx::Vector2dF& velocity) {
    blink::WebGestureEvent fling_start(
        blink::WebInputEvent::Type::kGestureFlingStart, 0,
        base::TimeTicks::Now(), blink::WebGestureDevice::kTouchscreen);
    fling_start.data.fling_start.velocity_x = velocity.x();
    fling_start.data.fling_start.velocity_y = velocity.y();
    input::GestureEventWithLatencyInfo fling_start_with_latency(fling_start);
    fling_controller_->ObserveAndMaybeConsumeGestureEvent(
        fling_start_with_latency);
  }

  void SimulateFlingCancel() {
    blink::WebGestureEvent fling_cancel(
        blink::WebInputEvent::Type::kGestureFlingCancel, 0,
        base::TimeTicks::Now(), blink::WebGestureDevice::kTouchscreen);
    fling_cancel.data.fling_cancel.prevent_boosting = true;
    input::GestureEventWithLatencyInfo fling_cancel_with_latency(fling_cancel);
    fling_controller_->ObserveAndMaybeConsumeGestureEvent(
        fling_cancel_with_latency);
  }

  // FlingControllerEventSenderClient
  void SendGeneratedWheelEvent(
      const input::MouseWheelEventWithLatencyInfo& wheel_event) override {}
  void SendGeneratedGestureScrollEvents(
      const input::GestureEventWithLatencyInfo& gesture_event) override {}
  gfx::Size GetRootWidgetViewportSize() override {
    return gfx::Size(1920, 1080);
  }

  input::FlingController* fling_controller() { return fling_controller_.get(); }
  FlingSchedulerAndroid* fling_scheduler() {
    auto* rir =
        GetInputManager()->GetRenderInputRouterFromFrameSinkId(kFrameSinkIdA);
    return static_cast<FlingSchedulerAndroid*>(
        rir->GetFlingSchedulerForTesting());
  }

  bool InputManagerExists() { return GetInputManager(); }
  bool ExpectedInputManagerCreation() {
    return input::InputUtils::IsTransferInputToVizSupported();
  }

  InputManager* GetInputManager() {
    return frame_sink_manager_->GetInputManager();
  }

  const BeginFrameSource* GetRootBeginFrameSource() {
    auto* support = frame_sink_manager_->GetFrameSinkForId(kFrameSinkIdRoot);
    return support->begin_frame_source();
  }

  void InvalidateRootFrameSinkId() {
    frame_sink_manager_->InvalidateFrameSinkId(kFrameSinkIdRoot);
  }

 private:
  input::mojom::RenderInputRouterConfigPtr CreateRIRConfig(
      const base::UnguessableToken& grouping_id) {
    auto config = input::mojom::RenderInputRouterConfig::New();
    mojo::PendingReceiver<blink::mojom::RenderInputRouterClient>
        rir_client_receiver;
    config->rir_client = rir_client_receiver.InitWithNewPipeAndPassRemote();
    config->grouping_id = grouping_id;
    return config;
  }

  // Checks if a [Root]CompositorFrameSinkImpl exists for |frame_sink_id|.
  bool CompositorFrameSinkExists(const FrameSinkId& frame_sink_id) {
    return base::Contains(frame_sink_manager_->sink_map_, frame_sink_id) ||
           base::Contains(frame_sink_manager_->root_sink_map_, frame_sink_id);
  }

  // Creates a CompositorFrameSinkImpl.
  void CreateCompositorFrameSink(
      const FrameSinkId& frame_sink_id,
      input::mojom::RenderInputRouterConfigPtr config) {
    MockCompositorFrameSinkClient compositor_frame_sink_client;
    mojo::Remote<mojom::CompositorFrameSink> compositor_frame_sink;

    frame_sink_manager_->CreateCompositorFrameSink(
        frame_sink_id, /*bundle_id=*/std::nullopt,
        compositor_frame_sink.BindNewPipeAndPassReceiver(),
        compositor_frame_sink_client.BindInterfaceRemote(), std::move(config));
    EXPECT_TRUE(CompositorFrameSinkExists(frame_sink_id));
  }

  std::unique_ptr<input::FlingController> fling_controller_;
  TestOutputSurfaceProvider output_surface_provider_;
  MockGpuServiceImpl gpu_service_;
  std::unique_ptr<FrameSinkManagerImpl> frame_sink_manager_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

TEST_F(FlingSchedulerTest, ScheduleNextFlingProgress) {
  EXPECT_EQ(InputManagerExists(), ExpectedInputManagerCreation());
  if (!InputManagerExists()) {
    // FlingSchedulerAndroid's implementation depends on InputManager creation.
    // Exit if InputManager is not present.
    return;
  }

  SetupFlingController();
  ASSERT_EQ(fling_scheduler()->GetBeginFrameSource(),
            GetRootBeginFrameSource());

  SimulateFlingStart(gfx::Vector2dF(1000, 0));
  EXPECT_EQ(fling_controller(), fling_scheduler()->fling_controller_.get());
  EXPECT_TRUE(fling_scheduler()->observing_begin_frame_source_);
}

TEST_F(FlingSchedulerTest, FlingCancelled) {
  EXPECT_EQ(InputManagerExists(), ExpectedInputManagerCreation());
  if (!InputManagerExists()) {
    // FlingSchedulerAndroid's implementation depends on InputManager creation.
    // Exit if InputManager is not present.
    return;
  }

  SetupFlingController();
  ASSERT_EQ(fling_scheduler()->GetBeginFrameSource(),
            GetRootBeginFrameSource());

  SimulateFlingStart(gfx::Vector2dF(1000, 0));
  EXPECT_EQ(fling_controller(), fling_scheduler()->fling_controller_.get());
  EXPECT_TRUE(fling_scheduler()->observing_begin_frame_source_);

  SimulateFlingCancel();
  EXPECT_EQ(nullptr, fling_scheduler()->fling_controller_.get());
  EXPECT_FALSE(fling_scheduler()->observing_begin_frame_source_);
  EXPECT_EQ(fling_scheduler()->GetBeginFrameSource(),
            GetRootBeginFrameSource());
}

// This tests that FlingSchedulerAndroid stops observing BeginFrameSource
// when the RootCompositorFrameSinkImpl gets destroyed, addressing the UAF bug
// described in crbug.com/401501206.
TEST_F(FlingSchedulerTest, ResetStateOnBeginFrameSourceChange) {
  EXPECT_EQ(InputManagerExists(), ExpectedInputManagerCreation());
  if (!InputManagerExists()) {
    // FlingSchedulerAndroid's implementation depends on InputManager creation.
    // Exit if InputManager is not present.
    return;
  }

  SetupFlingController();
  ASSERT_EQ(fling_scheduler()->GetBeginFrameSource(),
            GetRootBeginFrameSource());

  base::TimeTicks progress_time = base::TimeTicks::Now();

  SimulateFlingStart(gfx::Vector2dF(1000, 0));
  EXPECT_EQ(fling_controller(), fling_scheduler()->fling_controller_.get());
  EXPECT_TRUE(fling_scheduler()->observing_begin_frame_source_);

  progress_time += base::Milliseconds(17);
  fling_controller()->ProgressFling(progress_time);
  EXPECT_TRUE(fling_controller()->fling_in_progress());

  // RootCompositorFrameSink gets invalidated.
  InvalidateRootFrameSinkId();

  EXPECT_FALSE(fling_scheduler()->observing_begin_frame_source_);
  EXPECT_EQ(nullptr, fling_scheduler()->GetBeginFrameSource());
}

}  // namespace viz