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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "chrome/browser/page_load_metrics/integration_tests/metric_integration_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
class EventCountsBrowserTest : public MetricIntegrationTest {
public:
~EventCountsBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
MetricIntegrationTest::SetUpCommandLine(command_line);
// content::SimulateTouchEventAt can only be used in Aura, however
// chrome.gpuBenchmarking.tap can be used on all platforms.
command_line->AppendSwitch(switches::kEnableGpuBenchmarking);
}
};
IN_PROC_BROWSER_TEST_F(EventCountsBrowserTest, EventCountsForMouseClick) {
LoadHTML(R"HTML(
<script type="text/javascript">
let eventCounts = {mouseup: 0, pointerup: 0, click: 0};
const loadPromise = new Promise(resolve => {
window.addEventListener("load", () => {
resolve(true);
});
});
let eventPromise;
checkLoad = async () => {
await loadPromise;
eventPromise = new Promise(resolve => {
for (let evt in eventCounts) {
window.addEventListener(evt, function(e) {
eventCounts[e.type]++;
if (eventCounts.click == 2 &&
eventCounts.pointerup == 2 &&
eventCounts.mouseup == 2) {
resolve(true);
}
});
}
});
return true;
};
runtest = async () => {
return await eventPromise;
};
</script>
)HTML");
// Make sure the page is fully loaded.
ASSERT_TRUE(EvalJs(web_contents(), "checkLoad()").ExtractBool());
// We should wait for the main frame's hit-test data to be ready before
// sending the click event below to avoid flakiness.
content::WaitForHitTestData(web_contents()->GetPrimaryMainFrame());
// Ensure the compositor thread is aware of the event listener.
content::MainThreadFrameObserver frame_observer(GetRenderWidgetHost());
frame_observer.Wait();
// Simulate clicks.
content::SimulateMouseClick(web_contents(), 0,
blink::WebMouseEvent::Button::kLeft);
content::SimulateMouseClick(web_contents(), 0,
blink::WebMouseEvent::Button::kLeft);
ASSERT_TRUE(EvalJs(web_contents(), "runtest()").ExtractBool());
// Check event counts.
int expected_pointerdown =
EvalJs(web_contents(),
"window.performance.eventCounts.get('pointerdown')")
.ExtractInt();
int expected_pointerup =
EvalJs(web_contents(), "window.performance.eventCounts.get('pointerup')")
.ExtractInt();
int expected_mousedown =
EvalJs(web_contents(), "window.performance.eventCounts.get('mousedown')")
.ExtractInt();
int expected_mouseup =
EvalJs(web_contents(), "window.performance.eventCounts.get('mouseup')")
.ExtractInt();
int expected_click =
EvalJs(web_contents(), "window.performance.eventCounts.get('click')")
.ExtractInt();
EXPECT_EQ(expected_pointerdown, 2);
EXPECT_EQ(expected_pointerup, 2);
EXPECT_EQ(expected_mousedown, 2);
EXPECT_EQ(expected_mouseup, 2);
EXPECT_EQ(expected_click, 2);
}
IN_PROC_BROWSER_TEST_F(EventCountsBrowserTest, EventCountsForTouchTap) {
LoadHTML(R"HTML(
<script type="text/javascript">
let eventCounts = {touchend: 0, pointerup: 0, click: 0};
const loadPromise = new Promise(resolve => {
window.addEventListener("load", () => {
resolve(true);
});
});
let eventPromise;
checkLoad = async () => {
await loadPromise;
eventPromise = new Promise(resolve => {
for (let evt in eventCounts) {
window.addEventListener(evt, function(e) {
eventCounts[e.type]++;
if (eventCounts.click == 1 &&
eventCounts.pointerup == 1 &&
eventCounts.touchend == 1) {
resolve(true);
}
});
}
});
return true;
};
runtest = async () => {
return await eventPromise;
};
</script>
)HTML");
// Make sure the page is fully loaded.
ASSERT_TRUE(EvalJs(web_contents(), "checkLoad()").ExtractBool());
// We should wait for the main frame's hit-test data to be ready before
// sending the touch events below to avoid flakiness.
content::WaitForHitTestData(web_contents()->GetPrimaryMainFrame());
// Ensure the compositor thread is aware of the event listener.
content::MainThreadFrameObserver frame_observer(GetRenderWidgetHost());
frame_observer.Wait();
// Simulate touch tap which will generate touch, pointer and mouse events and
// a click event.
EXPECT_TRUE(ExecJs(web_contents(),
"chrome.gpuBenchmarking.tap(100, 150, ()=>{}, "
"50, chrome.gpuBenchmarking.TOUCH_INPUT);"));
ASSERT_TRUE(EvalJs(web_contents(), "runtest()").ExtractBool());
// Check event counts.
int expected_pointerdown =
EvalJs(web_contents(),
"window.performance.eventCounts.get('pointerdown')")
.ExtractInt();
int expected_pointerup =
EvalJs(web_contents(), "window.performance.eventCounts.get('pointerup')")
.ExtractInt();
int expected_mousedown =
EvalJs(web_contents(), "window.performance.eventCounts.get('mousedown')")
.ExtractInt();
int expected_mouseup =
EvalJs(web_contents(), "window.performance.eventCounts.get('mouseup')")
.ExtractInt();
int expected_click =
EvalJs(web_contents(), "window.performance.eventCounts.get('click')")
.ExtractInt();
int expected_touchstart =
EvalJs(web_contents(), "window.performance.eventCounts.get('touchstart')")
.ExtractInt();
int expected_touchend =
EvalJs(web_contents(), "window.performance.eventCounts.get('touchend')")
.ExtractInt();
EXPECT_EQ(expected_pointerdown, 1);
EXPECT_EQ(expected_pointerup, 1);
EXPECT_EQ(expected_mousedown, 1);
EXPECT_EQ(expected_mouseup, 1);
EXPECT_EQ(expected_click, 1);
EXPECT_EQ(expected_touchstart, 1);
EXPECT_EQ(expected_touchend, 1);
}
|