Commit b847f9c1 authored by Wei Li's avatar Wei Li Committed by Commit Bot

UIDevTools: Test pinned and hovered element calculation

The distance calculation was broken due to pinned element and
hovered element used different coordinates. r91bef92b also fixed
this issue. This CL refactored the function name to make the
coordinates used clearer, and also added unit test to prevent
future breakage.

BUG=899001

Change-Id: Ic1e2e577eb085a82f5ea32f608312335d6befd42
Reviewed-on: https://chromium-review.googlesource.com/c/1372551Reviewed-by: default avatarLeonard Grey <lgrey@chromium.org>
Commit-Queue: Wei Li <weili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#615684}
parent c6a3bafd
......@@ -41,8 +41,8 @@ std::unique_ptr<protocol::Array<std::string>> RootElement::GetAttributes()
return nullptr;
}
std::pair<gfx::NativeWindow, gfx::Rect> RootElement::GetNodeWindowAndBounds()
const {
std::pair<gfx::NativeWindow, gfx::Rect>
RootElement::GetNodeWindowAndScreenBounds() const {
NOTREACHED();
return {};
}
......
......@@ -23,7 +23,7 @@ class UI_DEVTOOLS_EXPORT RootElement : public UIElement {
void GetVisible(bool* visible) const override;
void SetVisible(bool visible) override;
std::unique_ptr<protocol::Array<std::string>> GetAttributes() const override;
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const override;
private:
......
......@@ -67,9 +67,9 @@ class UI_DEVTOOLS_EXPORT UIElement {
virtual void GetVisible(bool* visible) const = 0;
virtual void SetVisible(bool visible) = 0;
// If element exists, return its associated native window and its bounds.
// Otherwise, return null and empty bounds.
virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
// If element exists, return its associated native window and its screen
// bounds. Otherwise, return null and empty bounds.
virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const = 0;
// Get a list of interleaved keys and values of attributes to be displayed
// on the element in the dev tools hierarchy view.
......
......@@ -461,9 +461,13 @@ int OverlayAgentAura::FindElementIdTargetedByPoint(
void OverlayAgentAura::ShowDistancesInHighlightOverlay(int pinned_id,
int element_id) {
const std::pair<gfx::NativeWindow, gfx::Rect> pair_r2(
dom_agent()->GetElementFromNodeId(element_id)->GetNodeWindowAndBounds());
dom_agent()
->GetElementFromNodeId(element_id)
->GetNodeWindowAndScreenBounds());
const std::pair<gfx::NativeWindow, gfx::Rect> pair_r1(
dom_agent()->GetElementFromNodeId(pinned_id)->GetNodeWindowAndBounds());
dom_agent()
->GetElementFromNodeId(pinned_id)
->GetNodeWindowAndScreenBounds());
gfx::Rect r2(pair_r2.second);
gfx::Rect r1(pair_r1.second);
pinned_rect_ = r1;
......@@ -520,7 +524,7 @@ Response OverlayAgentAura::HighlightNode(int node_id, bool show_size) {
highlight_rect_config_ = HighlightRectsConfiguration::NO_DRAW;
show_size_on_canvas_ = show_size;
layer_for_highlighting_->SetVisible(
UpdateHighlight(element->GetNodeWindowAndBounds()));
UpdateHighlight(element->GetNodeWindowAndScreenBounds()));
return Response::OK();
}
......
......@@ -74,6 +74,7 @@ class OverlayAgentAura : public OverlayAgent,
private:
FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest,
MouseEventsGenerateFEEventsInInspectMode);
FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightRects);
FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightNonexistentNode);
FRIEND_TEST_ALL_PREFIXES(OverlayAgentTest, HighlightWidget);
#if defined(USE_AURA)
......@@ -83,7 +84,7 @@ class OverlayAgentAura : public OverlayAgent,
protocol::Response HighlightNode(int node_id, bool show_size = false);
// Returns true when there is any visible element to highlight.
bool UpdateHighlight(
const std::pair<gfx::NativeWindow, gfx::Rect>& window_and_bounds);
const std::pair<gfx::NativeWindow, gfx::Rect>& window_and_screen_bounds);
// Shows the distances between the nodes identified by |pinned_id| and
// |element_id| in the highlight overlay.
......
......@@ -105,18 +105,23 @@ class OverlayAgentTest : public views::ViewsTestBase {
return window;
}
std::unique_ptr<views::Widget> CreateWidget() {
std::unique_ptr<views::Widget> CreateWidget(const gfx::Rect& bounds) {
auto widget = std::make_unique<views::Widget>();
views::Widget::InitParams params;
params.delegate = nullptr;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 400, 400);
params.bounds = bounds;
params.parent = GetContext();
widget->Init(params);
widget->Show();
return widget;
}
std::unique_ptr<views::Widget> CreateWidget() {
// Create a widget with default bounds.
return CreateWidget(gfx::Rect(0, 0, 400, 400));
}
DOMAgent* dom_agent() { return dom_agent_.get(); }
OverlayAgentAura* overlay_agent() { return overlay_agent_.get(); }
FakeFrontendChannel* frontend_channel() {
......@@ -216,9 +221,12 @@ TEST_F(OverlayAgentTest, HighlightRects) {
{"R1_INTERSECTS_R2", gfx::Rect(100, 100, 50, 50),
gfx::Rect(120, 120, 50, 50), R1_INTERSECTS_R2},
};
// Use a non-zero origin to test screen coordinates.
const gfx::Rect kWidgetBounds(10, 10, 510, 510);
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(testing::Message() << "Case: " << test_case.name);
std::unique_ptr<views::Widget> widget = CreateWidget();
std::unique_ptr<views::Widget> widget = CreateWidget(kWidgetBounds);
std::unique_ptr<protocol::DOM::Node> root;
dom_agent()->getDocument(&root);
......@@ -251,8 +259,18 @@ TEST_F(OverlayAgentTest, HighlightRects) {
// Highlight child 2. Now, the distance overlay is showing.
generator.MoveMouseTo(GetOriginInScreen(child_2));
// Check calculated highlight config.
EXPECT_EQ(test_case.expected_configuration,
overlay_agent()->highlight_rect_config());
// Check results of pinned and hovered rectangles.
gfx::Rect expected_pinned_rect =
client_view->ConvertRectToParent(test_case.first_element_bounds);
expected_pinned_rect.Offset(kWidgetBounds.OffsetFromOrigin());
EXPECT_EQ(expected_pinned_rect, overlay_agent()->pinned_rect_);
gfx::Rect expected_hovered_rect =
client_view->ConvertRectToParent(test_case.second_element_bounds);
expected_hovered_rect.Offset(kWidgetBounds.OffsetFromOrigin());
EXPECT_EQ(expected_hovered_rect, overlay_agent()->hovered_rect_);
// If we don't explicitly stop inspecting, we'll leave ourselves as
// a pretarget handler for the root window and UAF in the next test.
// TODO(lgrey): Fix this when refactoring to support Mac.
......
......@@ -93,8 +93,8 @@ std::unique_ptr<protocol::Array<std::string>> ViewElement::GetAttributes()
return attributes;
}
std::pair<gfx::NativeWindow, gfx::Rect> ViewElement::GetNodeWindowAndBounds()
const {
std::pair<gfx::NativeWindow, gfx::Rect>
ViewElement::GetNodeWindowAndScreenBounds() const {
return std::make_pair(view_->GetWidget()->GetNativeWindow(),
view_->GetBoundsInScreen());
}
......
......@@ -38,7 +38,7 @@ class ViewElement : public views::ViewObserver, public UIElement {
void GetVisible(bool* visible) const override;
void SetVisible(bool visible) override;
std::unique_ptr<protocol::Array<std::string>> GetAttributes() const override;
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const override;
static views::View* From(const UIElement* element);
......
......@@ -128,7 +128,7 @@ TEST_F(ViewElementTest, GetCustomProperties) {
EXPECT_EQ(props[0].second, "This is the tooltip");
}
TEST_F(ViewElementTest, GetNodeWindowAndBounds) {
TEST_F(ViewElementTest, GetNodeWindowAndScreenBounds) {
// For this to be meaningful, the view must be in
// a widget.
auto widget = std::make_unique<views::Widget>();
......@@ -143,7 +143,7 @@ TEST_F(ViewElementTest, GetNodeWindowAndBounds) {
view()->SetBoundsRect(bounds);
std::pair<gfx::NativeWindow, gfx::Rect> window_and_bounds =
element()->GetNodeWindowAndBounds();
element()->GetNodeWindowAndScreenBounds();
EXPECT_EQ(window_and_bounds.first, widget->GetNativeWindow());
EXPECT_EQ(window_and_bounds.second, view()->GetBoundsInScreen());
......
......@@ -82,8 +82,8 @@ std::unique_ptr<protocol::Array<std::string>> WidgetElement::GetAttributes()
return attributes;
}
std::pair<gfx::NativeWindow, gfx::Rect> WidgetElement::GetNodeWindowAndBounds()
const {
std::pair<gfx::NativeWindow, gfx::Rect>
WidgetElement::GetNodeWindowAndScreenBounds() const {
return std::make_pair(widget_->GetNativeWindow(),
widget_->GetWindowBoundsInScreen());
}
......
......@@ -43,7 +43,7 @@ class WidgetElement : public views::WidgetRemovalsObserver,
void GetVisible(bool* visible) const override;
void SetVisible(bool visible) override;
std::unique_ptr<protocol::Array<std::string>> GetAttributes() const override;
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const override;
static views::Widget* From(const UIElement* element);
......
......@@ -130,9 +130,9 @@ TEST_F(WidgetElementTest, GetAttributes) {
EXPECT_EQ(attrs->get(3), "true");
}
TEST_F(WidgetElementTest, GetNodeWindowAndBounds) {
TEST_F(WidgetElementTest, GetNodeWindowAndScreenBounds) {
std::pair<gfx::NativeWindow, gfx::Rect> window_and_bounds =
element()->GetNodeWindowAndBounds();
element()->GetNodeWindowAndScreenBounds();
EXPECT_EQ(widget()->GetNativeWindow(), window_and_bounds.first);
EXPECT_EQ(widget()->GetWindowBoundsInScreen(), window_and_bounds.second);
}
......
......@@ -100,8 +100,8 @@ std::unique_ptr<protocol::Array<std::string>> WindowElement::GetAttributes()
return attributes;
}
std::pair<gfx::NativeWindow, gfx::Rect> WindowElement::GetNodeWindowAndBounds()
const {
std::pair<gfx::NativeWindow, gfx::Rect>
WindowElement::GetNodeWindowAndScreenBounds() const {
return std::make_pair(static_cast<aura::Window*>(window_),
window_->GetBoundsInScreen());
}
......
......@@ -41,7 +41,7 @@ class WindowElement : public aura::WindowObserver, public UIElement {
void GetVisible(bool* visible) const override;
void SetVisible(bool visible) override;
std::unique_ptr<protocol::Array<std::string>> GetAttributes() const override;
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const override;
static aura::Window* From(const UIElement* element);
......
......@@ -98,7 +98,7 @@ std::unique_ptr<protocol::Array<std::string>> FrameSinkElement::GetAttributes()
}
std::pair<gfx::NativeWindow, gfx::Rect>
FrameSinkElement::GetNodeWindowAndBounds() const {
FrameSinkElement::GetNodeWindowAndScreenBounds() const {
return {};
}
......
......@@ -43,7 +43,7 @@ class FrameSinkElement : public UIElement {
void GetVisible(bool* visible) const override;
void SetVisible(bool visible) override;
std::unique_ptr<protocol::Array<std::string>> GetAttributes() const override;
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const override;
static const viz::FrameSinkId& From(const UIElement* element);
......
......@@ -69,8 +69,8 @@ std::unique_ptr<protocol::Array<std::string>> SurfaceElement::GetAttributes()
return attributes;
}
std::pair<gfx::NativeWindow, gfx::Rect> SurfaceElement::GetNodeWindowAndBounds()
const {
std::pair<gfx::NativeWindow, gfx::Rect>
SurfaceElement::GetNodeWindowAndScreenBounds() const {
return {};
}
......
......@@ -32,7 +32,7 @@ class SurfaceElement : public UIElement {
void GetVisible(bool* visible) const override;
void SetVisible(bool visible) override;
std::unique_ptr<protocol::Array<std::string>> GetAttributes() const override;
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndBounds()
std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
const override;
static const viz::SurfaceId& From(const UIElement* element);
......
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