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