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
|
// 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 "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.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"
namespace input {
class InputBrowserTest : public content::ContentBrowserTest {
public:
bool IsRenderInputRouterCreatedOnViz() {
base::test::TestTraceProcessor ttp;
ttp.StartTrace("input");
content::RenderFrameSubmissionObserver render_frame_submission_observer(
shell()->web_contents());
EXPECT_TRUE(content::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();
}
absl::Status status = ttp.StopAndParseTrace();
EXPECT_TRUE(status.ok()) << status.message();
std::string query = R"(SELECT COUNT(*) as cnt
FROM slice
JOIN thread_track ON slice.track_id = thread_track.id
JOIN thread USING(utid)
WHERE slice.name = 'RenderInputRouter::RenderInputRouter'
AND thread.name = 'VizCompositorThread'
)";
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];
return slice_count == "1";
}
};
#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(InputBrowserTest,
RenderInputRouterNotCreatedOnNonAndroid) {
EXPECT_FALSE(IsRenderInputRouterCreatedOnViz());
}
#else
class AndroidInputBrowserTest : public InputBrowserTest,
public ::testing::WithParamInterface<bool> {
public:
AndroidInputBrowserTest() {
scoped_feature_list_.InitWithFeatureState(features::kInputOnViz,
/* enabled= */ GetParam());
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(AndroidInputBrowserTest, RenderInputRouterCreation) {
const bool expected_creation = InputUtils::IsTransferInputToVizSupported();
EXPECT_EQ(IsRenderInputRouterCreatedOnViz(), expected_creation);
}
// Tests whether RenderWidgetHostInputEventRouter was created with different
// grouping id's for different WebContents.
IN_PROC_BROWSER_TEST_P(AndroidInputBrowserTest,
RenderWidgetHostInputEventRouterCreated) {
base::test::TestTraceProcessor ttp;
ttp.StartTrace("viz");
GURL url = GURL(
"data:text/html,<!doctype html>"
"<body style='background-color: magenta;'></body>");
// Loads a URL in two separate web contents.
{
content::RenderFrameSubmissionObserver render_frame_submission_observer(
shell()->web_contents());
ASSERT_TRUE(NavigateToURL(shell(), url));
if (render_frame_submission_observer.render_frame_count() == 0) {
render_frame_submission_observer.WaitForAnyFrameSubmission();
}
}
{
auto* second_shell = content::Shell::CreateNewWindow(
shell()->web_contents()->GetBrowserContext(), url, nullptr, {800, 600});
content::RenderFrameSubmissionObserver render_frame_submission_observer(
second_shell->web_contents());
if (render_frame_submission_observer.render_frame_count() == 0) {
render_frame_submission_observer.WaitForAnyFrameSubmission();
}
}
absl::Status status = ttp.StopAndParseTrace();
EXPECT_TRUE(status.ok()) << status.message();
// Select the count of distinct grouping_id's.
std::string query = R"(
SELECT COUNT(DISTINCT EXTRACT_ARG(arg_set_id, 'debug.grouping_id'))
AS distinct_grouping_ids
FROM slice
WHERE slice.name = 'RenderWidgetHostInputEventRouterCreated'
)";
auto result = ttp.RunQuery(query);
EXPECT_TRUE(result.has_value());
// `result.value()` would look something like this: {{"id"}, {"<num>"},
// {"<num>"}}.
EXPECT_EQ(result.value()[0].size(), 1u);
if (InputUtils::IsTransferInputToVizSupported()) {
EXPECT_EQ(result.value().size(), 2u);
// Expect the distinct grouping_id count to be 2 for different WebContents.
EXPECT_THAT(
result.value(),
testing::ElementsAre(testing::ElementsAre("distinct_grouping_ids"),
testing::ElementsAre("2")));
} else {
EXPECT_EQ(result.value().size(), 2u);
EXPECT_THAT(
result.value(),
testing::ElementsAre(testing::ElementsAre("distinct_grouping_ids"),
testing::ElementsAre("0")));
}
}
IN_PROC_BROWSER_TEST_P(AndroidInputBrowserTest, AndroidInputReceiverCreated) {
base::HistogramTester histogram_tester;
content::RenderFrameSubmissionObserver render_frame_submission_observer(
shell()->web_contents());
EXPECT_TRUE(content::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();
}
// By this point a root compositor frame sink should have been created as
// well, and if an input receiver were to be created it should have been
// since it happens when root compositor frame sink is created.
const int expected_count =
InputUtils::IsTransferInputToVizSupported() ? 1 : 0;
content::FetchHistogramsFromChildProcesses();
histogram_tester.ExpectUniqueSample(
"Android.InputOnViz.InputReceiverCreationResult",
/*kSuccessfullyCreated*/ 0, expected_count);
}
IN_PROC_BROWSER_TEST_P(AndroidInputBrowserTest,
VizInputTransferTokenReceivedOnBrowser) {
base::test::TestTraceProcessor ttp;
ttp.StartTrace("input,Java");
content::RenderFrameSubmissionObserver render_frame_submission_observer(
shell()->web_contents());
EXPECT_TRUE(content::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();
}
// By this point a root compositor frame sink should have been created as
// well, and if an input receiver were to be created it should have been
// since it happens when root compositor frame sink is created.
absl::Status status = ttp.StopAndParseTrace();
EXPECT_TRUE(status.ok()) << status.message();
std::string query = R"(SELECT COUNT(*) as cnt
FROM slice
WHERE slice.name = 'Storing InputTransferToken')";
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 =
InputUtils::IsTransferInputToVizSupported() ? "1" : "0";
EXPECT_EQ(slice_count, expected_count);
}
// TODO(crbug.com/381039758): Re-enable after investigation.
IN_PROC_BROWSER_TEST_P(AndroidInputBrowserTest,
DISABLED_ReuseSingleInputReceiver) {
base::HistogramTester histogram_tester;
GURL url = GURL(
"data:text/html,<!doctype html>"
"<body style='background-color: magenta;'></body>");
{
content::RenderFrameSubmissionObserver render_frame_submission_observer(
shell()->web_contents());
EXPECT_TRUE(content::NavigateToURL(shell(), url));
if (render_frame_submission_observer.render_frame_count() == 0) {
render_frame_submission_observer.WaitForAnyFrameSubmission();
}
}
auto* second_shell = content::Shell::CreateNewWindow(
shell()->web_contents()->GetBrowserContext(), url, nullptr, {800, 600});
{
content::RenderFrameSubmissionObserver render_frame_submission_observer(
second_shell->web_contents());
if (render_frame_submission_observer.render_frame_count() == 0) {
render_frame_submission_observer.WaitForAnyFrameSubmission();
}
}
const int expected_count =
InputUtils::IsTransferInputToVizSupported() ? 1 : 0;
content::FetchHistogramsFromChildProcesses();
histogram_tester.ExpectBucketCount(
"Android.InputOnViz.InputReceiverCreationResult",
/*kReuseExistingInputReceiver*/ 7, expected_count);
}
INSTANTIATE_TEST_SUITE_P(All,
AndroidInputBrowserTest,
::testing::Bool(),
[](auto& info) {
return info.param ? "InputOnViz_Enabled"
: "InputOnViz_Disabled";
});
#endif // !BUILDFLAG(IS_ANDROID)
} // namespace input
|