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

Side by Side Diff: ash/devtools/ash_devtools_unittest.cc

Issue 2865713003: Remove ash dependency. (Closed)
Patch Set: rebase Created 3 years, 6 months 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/devtools/ash_devtools_css_agent.h" 5 #include "ash/devtools/ash_devtools_css_agent.h"
6 #include "ash/devtools/ash_devtools_dom_agent.h" 6 #include "ash/devtools/ash_devtools_dom_agent.h"
7 #include "ash/devtools/ui_element.h" 7 #include "ash/devtools/ui_element.h"
8 #include "ash/devtools/view_element.h" 8 #include "ash/devtools/view_element.h"
9 #include "ash/devtools/widget_element.h" 9 #include "ash/devtools/widget_element.h"
10 #include "ash/devtools/window_element.h" 10 #include "ash/devtools/window_element.h"
11 #include "ash/public/cpp/shell_window_ids.h"
12 #include "ash/root_window_controller.h"
13 #include "ash/shell.h" 11 #include "ash/shell.h"
14 #include "ash/test/ash_test_base.h" 12 #include "ash/test/ash_test_base.h"
15 #include "ash/wm/widget_finder.h" 13 #include "ash/wm/widget_finder.h"
16 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
17 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "ui/aura/client/window_parenting_client.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/display/display.h" 18 #include "ui/display/display.h"
19 #include "ui/views/background.h" 19 #include "ui/views/background.h"
20 #include "ui/views/widget/native_widget_private.h" 20 #include "ui/views/widget/native_widget_private.h"
21 #include "ui/views/widget/widget.h" 21 #include "ui/views/widget/widget.h"
22 #include "ui/wm/core/coordinate_conversion.h"
22 23
23 namespace ash { 24 namespace ash {
24 namespace { 25 namespace {
25 26
26 using namespace ui::devtools::protocol; 27 using namespace ui::devtools::protocol;
27 28
28 const int kDefaultChildNodeCount = -1; 29 const int kDefaultChildNodeCount = -1;
29 const SkColor kBackgroundColor = SK_ColorRED; 30 const SkColor kBackgroundColor = SK_ColorRED;
30 const SkColor kBorderColor = SK_ColorBLUE; 31 const SkColor kBorderColor = SK_ColorBLUE;
31 32
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 if (property->getName() == name) { 134 if (property->getName() == name) {
134 int value; 135 int value;
135 EXPECT_TRUE(base::StringToInt(property->getValue(), &value)); 136 EXPECT_TRUE(base::StringToInt(property->getValue(), &value));
136 return value; 137 return value;
137 } 138 }
138 } 139 }
139 NOTREACHED(); 140 NOTREACHED();
140 return -1; 141 return -1;
141 } 142 }
142 143
143 aura::Window* GetHighlightingWindow(int root_window_index) { 144 aura::Window* GetHighlightingWindow(aura::Window* root_window) {
144 const aura::Window::Windows& overlay_windows = 145 const aura::Window::Windows& overlay_windows = root_window->children();
145 Shell::GetAllRootWindows()[root_window_index]
146 ->GetChildById(kShellWindowId_OverlayContainer)
147 ->children();
148 for (aura::Window* window : overlay_windows) { 146 for (aura::Window* window : overlay_windows) {
149 if (window->GetName() == "HighlightingWidget") 147 if (window->GetName() == "HighlightingWidget")
150 return window; 148 return window;
151 } 149 }
152 NOTREACHED(); 150 NOTREACHED();
153 return nullptr; 151 return nullptr;
154 } 152 }
155 153
156 std::unique_ptr<DOM::RGBA> SkColorToRGBA(const SkColor& color) { 154 std::unique_ptr<DOM::RGBA> SkColorToRGBA(const SkColor& color) {
157 return DOM::RGBA::create() 155 return DOM::RGBA::create()
158 .setA(SkColorGetA(color) / 255) 156 .setA(SkColorGetA(color) / 255)
159 .setB(SkColorGetB(color)) 157 .setB(SkColorGetB(color))
160 .setG(SkColorGetG(color)) 158 .setG(SkColorGetG(color))
161 .setR(SkColorGetR(color)) 159 .setR(SkColorGetR(color))
162 .build(); 160 .build();
163 } 161 }
164 162
165 std::unique_ptr<DOM::HighlightConfig> CreateHighlightConfig( 163 std::unique_ptr<DOM::HighlightConfig> CreateHighlightConfig(
166 const SkColor& background_color, 164 const SkColor& background_color,
167 const SkColor& border_color) { 165 const SkColor& border_color) {
168 return DOM::HighlightConfig::create() 166 return DOM::HighlightConfig::create()
169 .setContentColor(SkColorToRGBA(background_color)) 167 .setContentColor(SkColorToRGBA(background_color))
170 .setBorderColor(SkColorToRGBA(border_color)) 168 .setBorderColor(SkColorToRGBA(border_color))
171 .build(); 169 .build();
172 } 170 }
173 171
174 void ExpectHighlighted(const gfx::Rect& bounds, int root_window_index) { 172 void ExpectHighlighted(const gfx::Rect& bounds, aura::Window* root_window) {
175 aura::Window* highlighting_window = GetHighlightingWindow(root_window_index); 173 aura::Window* highlighting_window = GetHighlightingWindow(root_window);
176 EXPECT_TRUE(highlighting_window->IsVisible()); 174 EXPECT_TRUE(highlighting_window->IsVisible());
177 EXPECT_EQ(bounds, highlighting_window->GetBoundsInScreen()); 175 EXPECT_EQ(bounds, highlighting_window->GetBoundsInScreen());
178 EXPECT_EQ(kBackgroundColor, GetInternalWidgetForWindow(highlighting_window) 176 EXPECT_EQ(kBackgroundColor, GetInternalWidgetForWindow(highlighting_window)
179 ->GetRootView() 177 ->GetRootView()
180 ->background() 178 ->background()
181 ->get_color()); 179 ->get_color());
182 } 180 }
183 181
184 aura::Window* GetPrimaryRootWindow() {
185 return Shell::Get()->GetPrimaryRootWindow();
186 }
187
188 } // namespace 182 } // namespace
189 183
190 class AshDevToolsTest : public test::AshTestBase { 184 class AshDevToolsTest : public test::AshTestBase {
191 public: 185 public:
192 AshDevToolsTest() {} 186 AshDevToolsTest() {}
193 ~AshDevToolsTest() override {} 187 ~AshDevToolsTest() override {}
194 188
195 views::internal::NativeWidgetPrivate* CreateTestNativeWidget() { 189 views::internal::NativeWidgetPrivate* CreateTestNativeWidget() {
196 views::Widget* widget = new views::Widget; 190 views::Widget* widget = new views::Widget;
197 views::Widget::InitParams params; 191 views::Widget::InitParams params;
198 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; 192 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET;
199 Shell::GetPrimaryRootWindowController()
200 ->ConfigureWidgetInitParamsForContainer(
201 widget, kShellWindowId_DefaultContainer, &params);
202 widget->Init(params); 193 widget->Init(params);
203 return widget->native_widget_private(); 194 return widget->native_widget_private();
204 } 195 }
205 196
206 std::unique_ptr<views::Widget> CreateTestWidget(const gfx::Rect& bounds) { 197 std::unique_ptr<views::Widget> CreateTestWidget(const gfx::Rect& bounds) {
207 return AshTestBase::CreateTestWidget(nullptr, kShellWindowId_Invalid, 198 return AshTestBase::CreateTestWidget(nullptr, kShellWindowId_Invalid,
208 bounds); 199 bounds);
209 } 200 }
210 201
211 void SetUp() override { 202 void SetUp() override {
212 AshTestBase::SetUp();
213 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); 203 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>();
214 uber_dispatcher_ = 204 uber_dispatcher_ =
215 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); 205 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get());
216 dom_agent_ = base::MakeUnique<devtools::AshDevToolsDOMAgent>(); 206 dom_agent_ = base::MakeUnique<devtools::AshDevToolsDOMAgent>();
217 dom_agent_->Init(uber_dispatcher_.get()); 207 dom_agent_->Init(uber_dispatcher_.get());
218 css_agent_ = 208 css_agent_ =
219 base::MakeUnique<devtools::AshDevToolsCSSAgent>(dom_agent_.get()); 209 base::MakeUnique<devtools::AshDevToolsCSSAgent>(dom_agent_.get());
220 css_agent_->Init(uber_dispatcher_.get()); 210 css_agent_->Init(uber_dispatcher_.get());
221 css_agent_->enable(); 211 css_agent_->enable();
212 // We need to create |dom_agent| first to observe creation of
213 // WindowTreeHosts in AshTestBase::SetUp().
214 AshTestBase::SetUp();
222 } 215 }
223 216
224 void TearDown() override { 217 void TearDown() override {
225 css_agent_.reset(); 218 css_agent_.reset();
226 dom_agent_.reset(); 219 dom_agent_.reset();
227 uber_dispatcher_.reset(); 220 uber_dispatcher_.reset();
228 fake_frontend_channel_.reset(); 221 fake_frontend_channel_.reset();
229 AshTestBase::TearDown(); 222 AshTestBase::TearDown();
230 } 223 }
231 224
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 ASSERT_FALSE(output); 275 ASSERT_FALSE(output);
283 } 276 }
284 277
285 void HighlightNode(int node_id) { 278 void HighlightNode(int node_id) {
286 dom_agent_->highlightNode( 279 dom_agent_->highlightNode(
287 CreateHighlightConfig(kBackgroundColor, kBorderColor), node_id); 280 CreateHighlightConfig(kBackgroundColor, kBorderColor), node_id);
288 } 281 }
289 282
290 void HideHighlight(int root_window_index) { 283 void HideHighlight(int root_window_index) {
291 dom_agent_->hideHighlight(); 284 dom_agent_->hideHighlight();
292 ASSERT_FALSE(GetHighlightingWindow(root_window_index)->IsVisible()); 285 DCHECK_GE(root_window_index, 0);
286 DCHECK_LE(root_window_index,
287 static_cast<int>(dom_agent()->root_windows().size()));
288 ASSERT_FALSE(
289 GetHighlightingWindow(dom_agent()->root_windows()[root_window_index])
290 ->IsVisible());
293 } 291 }
294 292
295 FakeFrontendChannel* frontend_channel() { 293 FakeFrontendChannel* frontend_channel() {
296 return fake_frontend_channel_.get(); 294 return fake_frontend_channel_.get();
297 } 295 }
298 296
297 aura::Window* GetPrimaryRootWindow() {
298 DCHECK(dom_agent()->root_windows().size());
299 return dom_agent()->root_windows()[0];
300 }
301
299 devtools::AshDevToolsCSSAgent* css_agent() { return css_agent_.get(); } 302 devtools::AshDevToolsCSSAgent* css_agent() { return css_agent_.get(); }
300 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } 303 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); }
301 304
302 private: 305 private:
303 std::unique_ptr<UberDispatcher> uber_dispatcher_; 306 std::unique_ptr<UberDispatcher> uber_dispatcher_;
304 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_; 307 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_;
305 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; 308 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_;
306 std::unique_ptr<devtools::AshDevToolsCSSAgent> css_agent_; 309 std::unique_ptr<devtools::AshDevToolsCSSAgent> css_agent_;
307 310
308 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); 311 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest);
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 632
630 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); 633 DOM::Node* parent_node = FindInRoot(parent_window, root.get());
631 ASSERT_TRUE(parent_node); 634 ASSERT_TRUE(parent_node);
632 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); 635 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr);
633 ASSERT_TRUE(parent_children); 636 ASSERT_TRUE(parent_children);
634 DOM::Node* window_node = parent_children->get(1); 637 DOM::Node* window_node = parent_children->get(1);
635 DOM::Node* widget_node = parent_children->get(0); 638 DOM::Node* widget_node = parent_children->get(0);
636 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); 639 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0);
637 640
638 HighlightNode(window_node->getNodeId()); 641 HighlightNode(window_node->getNodeId());
639 ExpectHighlighted(window->GetBoundsInScreen(), 0); 642 ExpectHighlighted(window->GetBoundsInScreen(), GetPrimaryRootWindow());
640 devtools::UIElement* element = 643 devtools::UIElement* element =
641 dom_agent()->GetElementFromNodeId(window_node->getNodeId()); 644 dom_agent()->GetElementFromNodeId(window_node->getNodeId());
642 ASSERT_EQ(devtools::UIElementType::WINDOW, element->type()); 645 ASSERT_EQ(devtools::UIElementType::WINDOW, element->type());
643 EXPECT_EQ(element->GetNodeWindowAndBounds().first, window.get()); 646 EXPECT_EQ(element->GetNodeWindowAndBounds().first, window.get());
644 EXPECT_EQ(element->GetNodeWindowAndBounds().second, 647 EXPECT_EQ(element->GetNodeWindowAndBounds().second,
645 window->GetBoundsInScreen()); 648 window->GetBoundsInScreen());
646 649
647 HideHighlight(0); 650 HideHighlight(0);
648 651
649 HighlightNode(widget_node->getNodeId()); 652 HighlightNode(widget_node->getNodeId());
650 ExpectHighlighted(widget->GetWindowBoundsInScreen(), 0); 653 ExpectHighlighted(widget->GetWindowBoundsInScreen(), GetPrimaryRootWindow());
651 654
652 element = dom_agent()->GetElementFromNodeId(widget_node->getNodeId()); 655 element = dom_agent()->GetElementFromNodeId(widget_node->getNodeId());
653 ASSERT_EQ(devtools::UIElementType::WIDGET, element->type()); 656 ASSERT_EQ(devtools::UIElementType::WIDGET, element->type());
654 EXPECT_EQ(element->GetNodeWindowAndBounds().first, widget->GetNativeWindow()); 657 EXPECT_EQ(element->GetNodeWindowAndBounds().first, widget->GetNativeWindow());
655 EXPECT_EQ(element->GetNodeWindowAndBounds().second, 658 EXPECT_EQ(element->GetNodeWindowAndBounds().second,
656 widget->GetWindowBoundsInScreen()); 659 widget->GetWindowBoundsInScreen());
657 660
658 HideHighlight(0); 661 HideHighlight(0);
659 662
660 HighlightNode(root_view_node->getNodeId()); 663 HighlightNode(root_view_node->getNodeId());
661 ExpectHighlighted(root_view->GetBoundsInScreen(), 0); 664 ExpectHighlighted(root_view->GetBoundsInScreen(), GetPrimaryRootWindow());
662 665
663 element = dom_agent()->GetElementFromNodeId(root_view_node->getNodeId()); 666 element = dom_agent()->GetElementFromNodeId(root_view_node->getNodeId());
664 ASSERT_EQ(devtools::UIElementType::VIEW, element->type()); 667 ASSERT_EQ(devtools::UIElementType::VIEW, element->type());
665 EXPECT_EQ(element->GetNodeWindowAndBounds().first, 668 EXPECT_EQ(element->GetNodeWindowAndBounds().first,
666 root_view->GetWidget()->GetNativeWindow()); 669 root_view->GetWidget()->GetNativeWindow());
667 EXPECT_EQ(element->GetNodeWindowAndBounds().second, 670 EXPECT_EQ(element->GetNodeWindowAndBounds().second,
668 root_view->GetBoundsInScreen()); 671 root_view->GetBoundsInScreen());
669 672
670 HideHighlight(0); 673 HideHighlight(0);
671 674
672 // Highlight non-existent node 675 // Highlight non-existent node
673 HighlightNode(10000); 676 HighlightNode(10000);
674 EXPECT_FALSE(GetHighlightingWindow(0)->IsVisible()); 677 EXPECT_FALSE(GetHighlightingWindow(GetPrimaryRootWindow())->IsVisible());
675 } 678 }
676 679
677 int GetNodeIdFromWindow(devtools::UIElement* ui_element, aura::Window* window) { 680 int GetNodeIdFromWindow(devtools::UIElement* ui_element, aura::Window* window) {
678 for (auto* child : ui_element->children()) { 681 for (auto* child : ui_element->children()) {
679 if (child->type() == devtools::UIElementType::WINDOW && 682 if (child->type() == devtools::UIElementType::WINDOW &&
680 static_cast<devtools::WindowElement*>(child)->window() == window) { 683 static_cast<devtools::WindowElement*>(child)->window() == window) {
681 return child->node_id(); 684 return child->node_id();
682 } 685 }
683 } 686 }
684 for (auto* child : ui_element->children()) { 687 for (auto* child : ui_element->children()) {
685 if (child->type() == devtools::UIElementType::WINDOW) { 688 if (child->type() == devtools::UIElementType::WINDOW) {
686 int node_id = GetNodeIdFromWindow(child, window); 689 int node_id = GetNodeIdFromWindow(child, window);
687 if (node_id > 0) 690 if (node_id > 0)
688 return node_id; 691 return node_id;
689 } 692 }
690 } 693 }
691 return 0; 694 return 0;
692 } 695 }
693 696
694 TEST_F(AshDevToolsTest, MultipleDisplayHighlight) { 697 // TODO(thanhph): Make test AshDevToolsTest.MultipleDisplayHighlight work with
695 UpdateDisplay("300x400,500x500"); 698 // multiple displays. https://crbug.com/726831.
sadrul 2017/05/29 16:48:45 Let's make sure we add the test back in the follow
696
697 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
698 std::unique_ptr<aura::Window> window(
699 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
700
701 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
702 dom_agent()->getDocument(&root);
703
704 EXPECT_EQ(root_windows[0], window->GetRootWindow());
705 HighlightNode(
706 GetNodeIdFromWindow(dom_agent()->window_element_root(), window.get()));
707 ExpectHighlighted(window->GetBoundsInScreen(), 0);
708
709 window->SetBoundsInScreen(gfx::Rect(500, 0, 50, 50), GetSecondaryDisplay());
710 EXPECT_EQ(root_windows[1], window->GetRootWindow());
711 HighlightNode(
712 GetNodeIdFromWindow(dom_agent()->window_element_root(), window.get()));
713 ExpectHighlighted(window->GetBoundsInScreen(), 1);
714 }
715 699
716 TEST_F(AshDevToolsTest, WindowWidgetViewGetMatchedStylesForNode) { 700 TEST_F(AshDevToolsTest, WindowWidgetViewGetMatchedStylesForNode) {
717 std::unique_ptr<views::Widget> widget( 701 std::unique_ptr<views::Widget> widget(
718 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); 702 CreateTestWidget(gfx::Rect(1, 1, 1, 1)));
719 aura::Window* parent_window = widget->GetNativeWindow(); 703 aura::Window* parent_window = widget->GetNativeWindow();
720 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window)); 704 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window));
721 gfx::Rect window_bounds(2, 2, 3, 3); 705 gfx::Rect window_bounds(2, 2, 3, 3);
722 gfx::Rect widget_bounds(50, 50, 100, 75); 706 gfx::Rect widget_bounds(50, 50, 100, 75);
723 gfx::Rect view_bounds(4, 4, 3, 3); 707 gfx::Rect view_bounds(4, 4, 3, 3);
724 window->SetBounds(window_bounds); 708 window->SetBounds(window_bounds);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 SetStyleTexts(root_view_node, "\nheight: 73;\n ", true); 820 SetStyleTexts(root_view_node, "\nheight: 73;\n ", true);
837 EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds()); 821 EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds());
838 822
839 SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\nvisibility: 1;\n", 823 SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\nvisibility: 1;\n",
840 true); 824 true);
841 EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds()); 825 EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds());
842 EXPECT_TRUE(root_view->visible()); 826 EXPECT_TRUE(root_view->visible());
843 } 827 }
844 828
845 } // namespace ash 829 } // namespace ash
OLDNEW
« ash/devtools/ash_devtools_dom_agent.cc ('K') | « ash/devtools/ash_devtools_dom_agent.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698