Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1162)

Unified Diff: ash/common/devtools/ash_devtools_unittest.cc

Issue 2552913002: Add tests for highlighting, editing and updating CSS styles for UI DevTools (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ash/common/devtools/ash_devtools_unittest.cc
diff --git a/ash/common/devtools/ash_devtools_unittest.cc b/ash/common/devtools/ash_devtools_unittest.cc
index 2bdc8ccc1b1b370fdd64be7ad4f3ac96a6e8c332..030ada5e034aaf4078d623fdec96b5701194708d 100644
--- a/ash/common/devtools/ash_devtools_unittest.cc
+++ b/ash/common/devtools/ash_devtools_unittest.cc
@@ -2,14 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "ash/common/devtools/ash_devtools_css_agent.h"
#include "ash/common/devtools/ash_devtools_dom_agent.h"
-
#include "ash/common/test/ash_test.h"
#include "ash/common/wm_lookup.h"
#include "ash/common/wm_root_window_controller.h"
#include "ash/common/wm_shell.h"
#include "ash/common/wm_window.h"
#include "base/strings/stringprintf.h"
+#include "ui/display/display.h"
+#include "ui/views/background.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/widget.h"
@@ -17,6 +19,8 @@ namespace ash {
namespace {
using namespace ui::devtools::protocol;
const int kDefaultChildNodeCount = -1;
+const SkColor kBackgroundColor = SK_ColorRED;
+const SkColor kBorderColor = SK_ColorBLUE;
class TestView : public views::View {
public:
@@ -115,6 +119,62 @@ DOM::Node* FindInRoot(WmWindow* window, DOM::Node* root) {
return window_node;
}
+int GetPropertyByName(const std::string& name,
+ Array<CSS::CSSProperty>* properties) {
+ for (size_t i = 0; i < properties->length(); i++) {
+ CSS::CSSProperty* property = properties->get(i);
+ if (property->getName() == name) {
+ int value;
+ EXPECT_TRUE(base::StringToInt(property->getValue(), &value));
+ return value;
+ }
+ }
+ NOTREACHED();
+ return -1;
+}
+
+WmWindow* GetHighlightingWindow(int root_window_index) {
+ WmWindow::Windows overlay_windows =
+ WmShell::Get()
+ ->GetAllRootWindows()[root_window_index]
+ ->GetChildByShellWindowId(kShellWindowId_OverlayContainer)
+ ->GetChildren();
+ for (WmWindow* window : overlay_windows) {
+ if (window->GetName() == "HighlightingWidget")
+ return window;
+ }
+ NOTREACHED();
+ return nullptr;
+}
+
+std::unique_ptr<DOM::RGBA> SkColorToRGBA(const SkColor& color) {
+ return DOM::RGBA::create()
+ .setA(SkColorGetA(color) / 255)
+ .setB(SkColorGetB(color))
+ .setG(SkColorGetG(color))
+ .setR(SkColorGetR(color))
+ .build();
+}
+
+std::unique_ptr<DOM::HighlightConfig> CreateHighlightConfig(
+ const SkColor& background_color,
+ const SkColor& border_color) {
+ return DOM::HighlightConfig::create()
+ .setContentColor(SkColorToRGBA(background_color))
+ .setBorderColor(SkColorToRGBA(border_color))
+ .build();
+}
+
+void ExpectHighlighted(const gfx::Rect& bounds, int root_window_index) {
+ WmWindow* highlighting_window = GetHighlightingWindow(root_window_index);
+ EXPECT_TRUE(highlighting_window->IsVisible());
+ EXPECT_EQ(bounds, highlighting_window->GetBoundsInScreen());
+ EXPECT_EQ(kBackgroundColor, highlighting_window->GetInternalWidget()
+ ->GetRootView()
+ ->background()
+ ->get_color());
+}
+
} // namespace
class AshDevToolsTest : public AshTest {
@@ -127,8 +187,7 @@ class AshDevToolsTest : public AshTest {
views::Widget::InitParams params;
params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET;
WmShell::Get()
- ->GetPrimaryRootWindow()
- ->GetRootWindowController()
+ ->GetPrimaryRootWindowController()
->ConfigureWidgetInitParamsForContainer(
widget, kShellWindowId_DefaultContainer, &params);
widget->Init(params);
@@ -143,9 +202,14 @@ class AshDevToolsTest : public AshTest {
dom_agent_ =
base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get());
dom_agent_->Init(uber_dispatcher_.get());
+ css_agent_ =
+ base::MakeUnique<devtools::AshDevToolsCSSAgent>(dom_agent_.get());
+ css_agent_->Init(uber_dispatcher_.get());
+ css_agent_->enable();
}
void TearDown() override {
+ css_agent_.reset();
dom_agent_.reset();
uber_dispatcher_.reset();
fake_frontend_channel_.reset();
@@ -168,16 +232,66 @@ class AshDevToolsTest : public AshTest {
parent_id, node_id)));
}
+ void ExpectStyleSheetChanged(int node_id) {
+ EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessage(
+ base::StringPrintf(
+ "{\"method\":\"CSS.styleSheetChanged\",\"params\":{"
+ "\"styleSheetId\":\"%d\"}}",
+ node_id)));
+ }
+
+ void CompareNodeBounds(DOM::Node* node, const gfx::Rect& bounds) {
+ Maybe<CSS::CSSStyle> styles;
+ css_agent_->getMatchedStylesForNode(node->getNodeId(), &styles);
+ ASSERT_TRUE(styles.isJust());
+ Array<CSS::CSSProperty>* properties = styles.fromJust()->getCssProperties();
+ EXPECT_EQ(bounds.height(), GetPropertyByName("height", properties));
+ EXPECT_EQ(bounds.width(), GetPropertyByName("width", properties));
+ EXPECT_EQ(bounds.x(), GetPropertyByName("x", properties));
+ EXPECT_EQ(bounds.y(), GetPropertyByName("y", properties));
+ }
+
+ void SetStyleTexts(DOM::Node* node,
+ const std::string& style_text,
+ bool success) {
+ auto edits = Array<CSS::StyleDeclarationEdit>::create();
+ auto edit = CSS::StyleDeclarationEdit::create()
+ .setStyleSheetId(base::IntToString(node->getNodeId()))
+ .setText(style_text)
+ .build();
+ edits->addItem(std::move(edit));
+ std::unique_ptr<Array<CSS::CSSStyle>> output;
+ EXPECT_EQ(success,
+ css_agent_->setStyleTexts(std::move(edits), &output).isSuccess());
+
+ if (success)
+ ASSERT_TRUE(output);
+ else
+ ASSERT_FALSE(output);
+ }
+
+ void HighlightNode(int node_id) {
+ dom_agent_->highlightNode(
+ CreateHighlightConfig(kBackgroundColor, kBorderColor), node_id);
+ }
+
+ void HideHighlight(int root_window_index) {
+ dom_agent_->hideHighlight();
+ ASSERT_FALSE(GetHighlightingWindow(root_window_index)->IsVisible());
+ }
+
FakeFrontendChannel* frontend_channel() {
return fake_frontend_channel_.get();
}
+ devtools::AshDevToolsCSSAgent* css_agent() { return css_agent_.get(); }
devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); }
private:
std::unique_ptr<UberDispatcher> uber_dispatcher_;
std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_;
std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_;
+ std::unique_ptr<devtools::AshDevToolsCSSAgent> css_agent_;
DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest);
};
@@ -474,4 +588,184 @@ TEST_F(AshDevToolsTest, ViewRearrangedRemovedAndInserted) {
ExpectChildNodeInserted(target_view_node->getNodeId(), 0);
}
+TEST_F(AshDevToolsTest, WindowWidgetViewHighlight) {
+ std::unique_ptr<views::Widget> widget(
+ CreateTestWidget(gfx::Rect(0, 0, 400, 400)));
+ WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
+ std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
+ WmWindow* window = child_owner->window();
+ views::View* root_view = widget->GetRootView();
+
+ std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
+ dom_agent()->getDocument(&root);
+
+ DOM::Node* parent_node = FindInRoot(parent_window, root.get());
+ ASSERT_TRUE(parent_node);
+ Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr);
+ ASSERT_TRUE(parent_children);
+ DOM::Node* window_node = parent_children->get(1);
+ DOM::Node* widget_node = parent_children->get(0);
+ DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0);
+
+ HighlightNode(window_node->getNodeId());
+ ExpectHighlighted(window->GetBoundsInScreen(), 0);
+
+ HideHighlight(0);
+
+ HighlightNode(widget_node->getNodeId());
+ ExpectHighlighted(widget->GetWindowBoundsInScreen(), 0);
+
+ HideHighlight(0);
+
+ HighlightNode(root_view_node->getNodeId());
+ ExpectHighlighted(root_view->GetBoundsInScreen(), 0);
+
+ HideHighlight(0);
+
+ // Highlight non-existent node
+ HighlightNode(10000);
+ EXPECT_FALSE(GetHighlightingWindow(0)->IsVisible());
+}
+
+TEST_F(AshDevToolsTest, MultipleDisplayHighlight) {
+ if (!SupportsMultipleDisplays())
+ return;
+ UpdateDisplay("300x400,500x500");
+
+ WmWindow::Windows root_windows = WmShell::Get()->GetAllRootWindows();
+ std::unique_ptr<WindowOwner> window_owner(
+ CreateTestWindow(gfx::Rect(1, 2, 30, 40)));
+ WmWindow* window = window_owner->window();
+
+ std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
+ dom_agent()->getDocument(&root);
+
+ EXPECT_EQ(root_windows[0], window->GetRootWindow());
+ HighlightNode(dom_agent()->GetNodeIdFromWindow(window));
+ ExpectHighlighted(window->GetBoundsInScreen(), 0);
+
+ window->SetBoundsInScreen(gfx::Rect(500, 0, 50, 50), GetSecondaryDisplay());
+ EXPECT_EQ(root_windows[1], window->GetRootWindow());
+ HighlightNode(dom_agent()->GetNodeIdFromWindow(window));
+ ExpectHighlighted(window->GetBoundsInScreen(), 1);
+}
+
+TEST_F(AshDevToolsTest, WindowWidgetViewGetMatchedStylesForNode) {
+ std::unique_ptr<views::Widget> widget(
+ CreateTestWidget(gfx::Rect(1, 1, 1, 1)));
+ WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
+ std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
+ WmWindow* window = child_owner->window();
+ gfx::Rect window_bounds(2, 2, 3, 3);
+ gfx::Rect widget_bounds(50, 50, 100, 75);
+ gfx::Rect view_bounds(4, 4, 3, 3);
+ window->SetBounds(window_bounds);
+ widget->SetBounds(widget_bounds);
+ widget->GetRootView()->SetBoundsRect(view_bounds);
+
+ std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
+ dom_agent()->getDocument(&root);
+
+ DOM::Node* parent_node = FindInRoot(parent_window, root.get());
+ ASSERT_TRUE(parent_node);
+ Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr);
+ ASSERT_TRUE(parent_children);
+
+ CompareNodeBounds(parent_node, widget_bounds);
+ CompareNodeBounds(parent_children->get(1), window_bounds);
+ CompareNodeBounds(parent_children->get(0)->getChildren(nullptr)->get(0),
+ view_bounds);
+}
+
+TEST_F(AshDevToolsTest, WindowWidgetViewStyleSheetChanged) {
+ std::unique_ptr<views::Widget> widget(
+ CreateTestWidget(gfx::Rect(1, 1, 1, 1)));
+ WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
+ std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
+ WmWindow* window = child_owner->window();
+
+ std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
+ dom_agent()->getDocument(&root);
+
+ gfx::Rect window_bounds(2, 2, 3, 3);
+ gfx::Rect widget_bounds(10, 10, 5, 6);
+ gfx::Rect view_bounds(4, 4, 3, 3);
+ window->SetBounds(window_bounds);
+ widget->SetBounds(widget_bounds);
+ widget->GetRootView()->SetBoundsRect(view_bounds);
+
+ DOM::Node* parent_node = FindInRoot(parent_window, root.get());
+ ASSERT_TRUE(parent_node);
+ Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr);
+ ASSERT_TRUE(parent_children);
+
+ ExpectStyleSheetChanged(parent_node->getNodeId());
+ ExpectStyleSheetChanged(parent_children->get(1)->getNodeId());
+ ExpectStyleSheetChanged(
+ parent_children->get(0)->getChildren(nullptr)->get(0)->getNodeId());
+}
+
+TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) {
+ std::unique_ptr<views::Widget> widget(
+ CreateTestWidget(gfx::Rect(0, 0, 400, 400)));
+ WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
+ std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
+ WmWindow* window = child_owner->window();
+ views::View* root_view = widget->GetRootView();
+
+ std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
+ dom_agent()->getDocument(&root);
+
+ DOM::Node* parent_node = FindInRoot(parent_window, root.get());
+ ASSERT_TRUE(parent_node);
+ Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr);
+ ASSERT_TRUE(parent_children);
+
+ // Test different combinations on window node
+ DOM::Node* window_node = parent_children->get(1);
+
+ SetStyleTexts(window_node, "x: 25; y:35; width: 5; height: 20;", true);
+ EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->GetBounds());
+
+ SetStyleTexts(window_node, "test_nothing_happens:1;", false);
+ EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->GetBounds()); // Not changed
+
+ SetStyleTexts(window_node, "\nheight: 10;\n ", true);
+ EXPECT_EQ(gfx::Rect(25, 35, 5, 10), window->GetBounds());
+
+ SetStyleTexts(window_node, "\nx: 10; y: 23; width: 52;\n ", true);
+ EXPECT_EQ(gfx::Rect(10, 23, 52, 10), window->GetBounds());
+
+ // Test different combinations on widget node
+ DOM::Node* widget_node = parent_children->get(0);
+
+ SetStyleTexts(widget_node, "x: 25; y:35; width: 53; height: 64;", true);
+ EXPECT_EQ(gfx::Rect(25, 35, 53, 64), widget->GetRestoredBounds());
+
+ SetStyleTexts(widget_node, "test_nothing_happens:1;", false);
+ EXPECT_EQ(gfx::Rect(25, 35, 53, 64),
+ widget->GetRestoredBounds()); // Not changed
+
+ SetStyleTexts(widget_node, "\nheight: 123;\n ", true);
+ EXPECT_EQ(gfx::Rect(25, 35, 53, 123), widget->GetRestoredBounds());
+
+ SetStyleTexts(widget_node, "\nx: 10; y: 23; width: 98;\n ", true);
+ EXPECT_EQ(gfx::Rect(10, 23, 98, 123), widget->GetRestoredBounds());
+
+ // Test different combinations on view node
+ DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0);
+
+ SetStyleTexts(root_view_node, "x: 25; y:35; width: 45; height: 20;", true);
+ EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds());
+
+ SetStyleTexts(root_view_node, "test_nothing_happens:1;", false);
+ EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds()); // Not changed
+
+ SetStyleTexts(root_view_node, "\nheight: 73;\n ", true);
+ EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds());
+
+ SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\n ", true);
+ EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds());
+}
+
} // namespace ash
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698