File: render_widget_host_view_android_browsertest.cc

package info (click to toggle)
chromium 142.0.7444.175-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,303,984 kB
  • sloc: cpp: 35,488,370; ansic: 7,479,680; javascript: 4,259,373; python: 1,466,844; xml: 757,444; asm: 710,716; pascal: 187,980; sh: 89,247; perl: 88,690; objc: 79,984; sql: 56,984; cs: 42,192; fortran: 24,137; makefile: 22,919; tcl: 15,277; php: 14,018; yacc: 9,005; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (222 lines) | stat: -rw-r--r-- 8,839 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
// 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 "content/browser/renderer_host/render_widget_host_view_android.h"

#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/test/test_trace_processor.h"
#include "components/input/features.h"
#include "components/input/utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/events/android/motion_event_android_factory.h"
#include "ui/events/android/motion_event_android_java.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/motionevent_jni_headers/MotionEvent_jni.h"

namespace content {

using ::testing::_;
using ::testing::Return;

namespace {

class MockJniDelegate : public InputTransferHandlerAndroid::JniDelegate {
 public:
  ~MockJniDelegate() override = default;

  MOCK_METHOD((int), MaybeTransferInputToViz, (int), (override));
  MOCK_METHOD((int), TransferInputToViz, (int), (override));
};

}  // namespace

class RenderWidgetHostViewAndroidBrowserTest : public ContentBrowserTest {};

class InputOnVizBrowserTest : public RenderWidgetHostViewAndroidBrowserTest {
 public:
  InputOnVizBrowserTest() {
    scoped_feature_list_.InitWithFeatureState(input::features::kInputOnViz,
                                              /* enabled= */ true);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(InputOnVizBrowserTest, TransfersStateOnTouchDown) {
  base::test::TestTraceProcessor ttp;
  ttp.StartTrace("input");
  RenderFrameSubmissionObserver render_frame_submission_observer(
      shell()->web_contents());

  EXPECT_TRUE(NavigateToURL(
      shell(), GURL("data:text/html,<!doctype html>"
                    "<body style='background-color: magenta;'></body>")));
  if (render_frame_submission_observer.render_frame_count() == 0) {
    render_frame_submission_observer.WaitForAnyFrameSubmission();
  }

  auto* view = static_cast<RenderWidgetHostViewAndroid*>(
      shell()->web_contents()->GetRenderWidgetHostView());
  ASSERT_NE(view, nullptr);
  auto* input_transfer_handler = view->GetInputTransferHandlerForTesting();
  ASSERT_EQ(!!input_transfer_handler,
            input::InputUtils::IsTransferInputToVizSupported());
  if (!input_transfer_handler) {
    return;
  }

  auto jni_delegate = std::unique_ptr<InputTransferHandlerAndroid::JniDelegate>(
      new MockJniDelegate());
  MockJniDelegate* mock_jni = static_cast<MockJniDelegate*>(jni_delegate.get());
  input_transfer_handler->set_jni_delegate_for_testing(std::move(jni_delegate));

  gfx::Point point(/*x=*/100, /*y=*/100);
  int tool_type = static_cast<int>(ui::MotionEvent::ToolType::FINGER);
  ui::MotionEventAndroid::Pointer p(0, point.x(), point.y(), 10, 0, 0, 0, 0,
                                    tool_type);
  JNIEnv* env = base::android::AttachCurrentThread();
  auto event_time = ui::EventTimeForNow();
  auto down_time_ms =
      base::TimeTicks::FromUptimeMillis(event_time.ToUptimeMillis());
  auto action = ui::MotionEvent::Action::DOWN;

  base::android::ScopedJavaLocalRef<jobject> obj =
      JNI_MotionEvent::Java_MotionEvent_obtain(
          env, /*downTime=*/0, /*eventTime=*/0, /*action=*/0, /*x=*/0, /*y=*/0,
          /*metaState=*/0);
  auto touch = ui::MotionEventAndroidFactory::CreateFromJava(
      env, obj,
      /*pix_to_dip=*/1.f,
      /*ticks_x=*/0,
      /*ticks_y=*/0,
      /*tick_multiplier=*/0,
      /*oldest_event_time=*/event_time,
      /*latest_event_time=*/event_time,
      /*down_time_ms=*/down_time_ms,
      /*android_action=*/ui::MotionEventAndroid::GetAndroidAction(action),
      /*pointer_count=*/1,
      /*history_size=*/0,
      /*action_index=*/0,
      /*android_action_button=*/0,
      /*android_gesture_classification=*/0,
      /*android_button_state=*/0,
      /*raw_offset_x_pixels=*/0,
      /*raw_offset_y_pixels=*/0,
      /*for_touch_handle=*/false,
      /*pointer0=*/&p,
      /*pointer1=*/nullptr,
      /*is_latest_event_time_resampled=*/false);

  int successfully_transferred =
      static_cast<int>(TransferInputToVizResult::kSuccessfullyTransferred);
  EXPECT_CALL(*mock_jni, MaybeTransferInputToViz(_))
      .WillOnce(Return(successfully_transferred));
  view->OnTouchEvent(*touch);

  absl::Status status = ttp.StopAndParseTrace();
  EXPECT_TRUE(status.ok()) << status.message();

  std::string query = R"(SELECT COUNT(*) as cnt
                     FROM slice
                     WHERE slice.name =
                       'RenderWidgetHostViewAndroid::StateOnTouchTransfer')";
  auto result = ttp.RunQuery(query);

  EXPECT_TRUE(result.has_value());

  // `result.value()` would look something like this: {{"cnt"}, {"<num>"}.
  EXPECT_EQ(result.value().size(), 2u);
  EXPECT_EQ(result.value()[1].size(), 1u);
  const std::string slice_count = result.value()[1][0];
  const std::string expected_count =
      input::InputUtils::IsTransferInputToVizSupported() ? "1" : "0";
  EXPECT_EQ(slice_count, expected_count);
}

class RenderWidgetHostViewAndroidFluidResizeBrowserTest
    : public RenderWidgetHostViewAndroidBrowserTest {
 public:
  RenderWidgetHostViewAndroidFluidResizeBrowserTest() {
    feature_list_.InitAndEnableFeature(features::kFluidResize);
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewAndroidFluidResizeBrowserTest,
                       ResizeDefersSynchronizationToNextFrame) {
  RenderFrameSubmissionObserver render_frame_submission_observer(
      shell()->web_contents());
  EXPECT_TRUE(NavigateToURL(
      shell(), GURL("data:text/html,<!doctype html>"
                    "<body style='background-color: magenta;'></body>")));
  if (render_frame_submission_observer.render_frame_count() == 0) {
    render_frame_submission_observer.WaitForAnyFrameSubmission();
  }

  auto* view = static_cast<RenderWidgetHostViewAndroid*>(
      shell()->web_contents()->GetRenderWidgetHostView());
  ASSERT_NE(view, nullptr);
  // A single call to Animate() may not be sufficient to settle visual
  // properties. Animate() triggers a synchronization with the renderer, and the
  // renderer's response can, in turn, trigger further visual property changes
  // in the browser, re-setting `visual_properties_update_pending_`. The loop
  // ensures this cycle is complete and the state is stable before the test
  // proceeds. On each iteration, it waits for a new frame and validates that
  // one was produced to ensure progress is being made.
  auto last_rfm = render_frame_submission_observer.LastRenderFrameMetadata();
  while (view->visual_properties_update_pending_) {
    view->Animate(base::TimeTicks::Now());
    render_frame_submission_observer.WaitForAnyFrameSubmission();
    if (view->visual_properties_update_pending_) {
      CHECK(last_rfm.local_surface_id !=
            render_frame_submission_observer.LastRenderFrameMetadata()
                .local_surface_id);
      last_rfm = render_frame_submission_observer.LastRenderFrameMetadata();
    }
  }

  EXPECT_FALSE(view->visual_properties_update_pending_);

  const gfx::Size current_size_dip = view->GetViewBounds().size();
  const float scale = view->GetDeviceScaleFactor();
  const gfx::Size current_size_px =
      gfx::ScaleToCeiledSize(current_size_dip, scale);
  const gfx::Size new_size(current_size_px.width() / 2,
                           current_size_px.height() / 2);
  // Ensure we're actually resizing.
  ASSERT_NE(new_size, current_size_px);
  view->screen_state_change_handler_.OnPhysicalBackingSizeChanged(new_size, 0);

  if (view->using_browser_compositor_) {
    EXPECT_TRUE(view->visual_properties_update_pending_);
    // Confirmed visual properties update is pending. We now wait for the
    // renderer to submit a frame acknowledging the resize.
    RenderFrameSubmissionObserver frame_observer(shell()->web_contents());
    frame_observer.WaitForAnyFrameSubmission();

    // The renderer has submitted a frame, but the browser's animation tick
    // that clears the pending flag might not have run yet. To avoid a race
    // condition, we manually call Animate() to process the update.
    view->Animate(base::TimeTicks::Now());

    EXPECT_FALSE(view->visual_properties_update_pending_);
  } else {
    // Confirmed no pending visual properties update.
    EXPECT_FALSE(view->visual_properties_update_pending_);
  }
}

}  // namespace content