File: render_widget_host_view_android_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,411 bytes parent folder | download | duplicates (4)
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
// 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/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/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_java.h"
#include "ui/events/base_event_utils.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 time_ns = (ui::EventTimeForNow() - base::TimeTicks()).InNanoseconds();
  auto action = ui::MotionEvent::Action::DOWN;
  ui::MotionEventAndroidJava touch(
      env, nullptr, 1.f, 0, 0, 0, base::TimeTicks::FromJavaNanoTime(time_ns),
      ui::MotionEventAndroid::GetAndroidAction(action), 1, 0, 0, 0, 0, 0, 0, 0,
      0, 0, false, &p, nullptr);

  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);
}

}  // namespace content