Commit a61e8610 authored by Liviu Tinta's avatar Liviu Tinta Committed by Chromium LUCI CQ

Use std::trunc instead of static_cast<int> in mouse_event.h

MouseEvent::screenX, MouseEvent::screenY, MouseEvent::clientX,
MouseEvent::clientY, MouseEvent::pageX, MouseEvent::pageY we expose as
double in mouse_event.idl [1].
Because of this is safe to use std::trunc instead of static_cast<int>
in order to avoid float-cast-overflow.

[1] https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/events/mouse_event.idl

Bug: 1164520, 1162288
Change-Id: I9aef2eea97090fb61aa28c3bbc03209e1401ee39
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2621675
Commit-Queue: Liviu Tinta <liviutinta@chromium.org>
Reviewed-by: default avatarRobert Flack <flackr@chromium.org>
Reviewed-by: default avatarMustaq Ahmed <mustaq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843832}
parent b38b8954
......@@ -1204,6 +1204,7 @@ source_set("unit_tests") {
"editing/keyboard_test.cc",
"editing/link_selection_test.cc",
"events/message_event_test.cc",
"events/mouse_event_test.cc",
"events/pointer_event_factory_test.cc",
"events/pointer_event_util_test.cc",
"events/touch_event_test.cc",
......
......@@ -24,6 +24,8 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_EVENTS_MOUSE_EVENT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_EVENTS_MOUSE_EVENT_H_
#include <cmath>
#include "third_party/blink/public/common/input/web_menu_source_type.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/events/simulated_click_options.h"
......@@ -135,21 +137,13 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState {
// Note that these values are adjusted to counter the effects of zoom, so that
// values exposed via DOM APIs are invariant under zooming.
virtual double screenX() const {
return static_cast<int>(screen_location_.X());
}
virtual double screenX() const { return std::trunc(screen_location_.X()); }
virtual double screenY() const {
return static_cast<int>(screen_location_.Y());
}
virtual double screenY() const { return std::trunc(screen_location_.Y()); }
virtual double clientX() const {
return static_cast<int>(client_location_.X());
}
virtual double clientX() const { return std::trunc(client_location_.X()); }
virtual double clientY() const {
return static_cast<int>(client_location_.Y());
}
virtual double clientY() const { return std::trunc(client_location_.Y()); }
int movementX() const { return movement_delta_.X(); }
int movementY() const { return movement_delta_.Y(); }
......@@ -160,9 +154,9 @@ class CORE_EXPORT MouseEvent : public UIEventWithKeyState {
virtual double offsetX() const;
virtual double offsetY() const;
virtual double pageX() const { return static_cast<int>(page_location_.X()); }
virtual double pageX() const { return std::trunc(page_location_.X()); }
virtual double pageY() const { return static_cast<int>(page_location_.Y()); }
virtual double pageY() const { return std::trunc(page_location_.Y()); }
double x() const { return clientX(); }
double y() const { return clientY(); }
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
#include <tuple>
#include "third_party/blink/renderer/core/events/mouse_event.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/geometry/double_point.h"
#include "third_party/blink/renderer/platform/geometry/int_point.h"
namespace blink {
class MouseEventScreenClientPagePositionTest
: public ::testing::TestWithParam<std::tuple<double, double>> {};
TEST_P(MouseEventScreenClientPagePositionTest, PositionAsExpected) {
MouseEvent& mouse_event = *MouseEvent::Create();
DoublePoint input_location(std::get<0>(GetParam()), std::get<0>(GetParam()));
DoublePoint expected_location(std::get<1>(GetParam()),
std::get<1>(GetParam()));
mouse_event.screen_location_ = input_location;
mouse_event.client_location_ = input_location;
mouse_event.page_location_ = input_location;
ASSERT_EQ(mouse_event.clientX(), expected_location.X());
ASSERT_EQ(mouse_event.clientY(), expected_location.Y());
ASSERT_EQ(mouse_event.screenX(), expected_location.X());
ASSERT_EQ(mouse_event.screenY(), expected_location.Y());
ASSERT_EQ(mouse_event.pageX(), expected_location.X());
ASSERT_EQ(mouse_event.pageY(), expected_location.Y());
}
INSTANTIATE_TEST_SUITE_P(
MouseEventScreenClientPagePositionNoOverflow,
MouseEventScreenClientPagePositionTest,
::testing::Values(
std::make_tuple(std::numeric_limits<int>::min() * 1.0,
std::numeric_limits<int>::min() * 1.0),
std::make_tuple(std::numeric_limits<int>::min() * 1.0 - 1.55,
std::numeric_limits<int>::min() * 1.0 - 1.0),
std::make_tuple(std::numeric_limits<int>::max() * 1.0,
std::numeric_limits<int>::max() * 1.0),
std::make_tuple(std::numeric_limits<int>::max() * 1.0 + 1.55,
std::numeric_limits<int>::max() * 1.0 + 1.00),
std::make_tuple(std::numeric_limits<double>::lowest(),
std::ceil(std::numeric_limits<double>::lowest())),
std::make_tuple(std::numeric_limits<double>::lowest() + 1.45,
std::ceil(std::numeric_limits<double>::lowest() +
1.45)),
std::make_tuple(std::numeric_limits<double>::max(),
std::floor(std::numeric_limits<double>::max())),
std::make_tuple(std::numeric_limits<double>::max() - 1.45,
std::floor(std::numeric_limits<double>::max() -
1.45))));
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment