OLD | NEW |
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/window_element.h" | 8 #include "ash/devtools/window_element.h" |
9 #include "ash/public/cpp/shell_window_ids.h" | |
10 #include "ash/root_window_controller.h" | |
11 #include "ash/shell.h" | 9 #include "ash/shell.h" |
12 #include "ash/test/ash_test_base.h" | 10 #include "ash/test/ash_test_base.h" |
13 #include "ash/wm/widget_finder.h" | 11 #include "ash/wm/widget_finder.h" |
14 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
15 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "ui/aura/client/window_parenting_client.h" |
| 15 #include "ui/aura/window_tree_host.h" |
16 #include "ui/display/display.h" | 16 #include "ui/display/display.h" |
17 #include "ui/views/background.h" | 17 #include "ui/views/background.h" |
18 #include "ui/views/widget/native_widget_private.h" | 18 #include "ui/views/widget/native_widget_private.h" |
19 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 20 #include "ui/wm/core/coordinate_conversion.h" |
20 | 21 |
21 namespace ash { | 22 namespace ash { |
22 namespace { | 23 namespace { |
23 | 24 |
24 using namespace ui::devtools::protocol; | 25 using namespace ui::devtools::protocol; |
25 | 26 |
26 const int kDefaultChildNodeCount = -1; | 27 const int kDefaultChildNodeCount = -1; |
27 const SkColor kBackgroundColor = SK_ColorRED; | 28 const SkColor kBackgroundColor = SK_ColorRED; |
28 const SkColor kBorderColor = SK_ColorBLUE; | 29 const SkColor kBorderColor = SK_ColorBLUE; |
29 | 30 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if (property->getName() == name) { | 132 if (property->getName() == name) { |
132 int value; | 133 int value; |
133 EXPECT_TRUE(base::StringToInt(property->getValue(), &value)); | 134 EXPECT_TRUE(base::StringToInt(property->getValue(), &value)); |
134 return value; | 135 return value; |
135 } | 136 } |
136 } | 137 } |
137 NOTREACHED(); | 138 NOTREACHED(); |
138 return -1; | 139 return -1; |
139 } | 140 } |
140 | 141 |
141 aura::Window* GetHighlightingWindow(int root_window_index) { | 142 aura::Window* GetHighlightingWindow(aura::Window* root_window) { |
142 const aura::Window::Windows& overlay_windows = | 143 // const aura::Window::Windows& overlay_windows = |
143 Shell::GetAllRootWindows()[root_window_index] | 144 // root_window->GetChildById(kShellWindowId_OverlayContainer)->children(); |
144 ->GetChildById(kShellWindowId_OverlayContainer) | 145 const aura::Window::Windows& overlay_windows = root_window->children(); |
145 ->children(); | |
146 for (aura::Window* window : overlay_windows) { | 146 for (aura::Window* window : overlay_windows) { |
147 if (window->GetName() == "HighlightingWidget") | 147 if (window->GetName() == "HighlightingWidget") |
148 return window; | 148 return window; |
149 } | 149 } |
150 NOTREACHED(); | 150 NOTREACHED(); |
151 return nullptr; | 151 return nullptr; |
152 } | 152 } |
153 | 153 |
154 std::unique_ptr<DOM::RGBA> SkColorToRGBA(const SkColor& color) { | 154 std::unique_ptr<DOM::RGBA> SkColorToRGBA(const SkColor& color) { |
155 return DOM::RGBA::create() | 155 return DOM::RGBA::create() |
156 .setA(SkColorGetA(color) / 255) | 156 .setA(SkColorGetA(color) / 255) |
157 .setB(SkColorGetB(color)) | 157 .setB(SkColorGetB(color)) |
158 .setG(SkColorGetG(color)) | 158 .setG(SkColorGetG(color)) |
159 .setR(SkColorGetR(color)) | 159 .setR(SkColorGetR(color)) |
160 .build(); | 160 .build(); |
161 } | 161 } |
162 | 162 |
163 std::unique_ptr<DOM::HighlightConfig> CreateHighlightConfig( | 163 std::unique_ptr<DOM::HighlightConfig> CreateHighlightConfig( |
164 const SkColor& background_color, | 164 const SkColor& background_color, |
165 const SkColor& border_color) { | 165 const SkColor& border_color) { |
166 return DOM::HighlightConfig::create() | 166 return DOM::HighlightConfig::create() |
167 .setContentColor(SkColorToRGBA(background_color)) | 167 .setContentColor(SkColorToRGBA(background_color)) |
168 .setBorderColor(SkColorToRGBA(border_color)) | 168 .setBorderColor(SkColorToRGBA(border_color)) |
169 .build(); | 169 .build(); |
170 } | 170 } |
171 | 171 |
172 void ExpectHighlighted(const gfx::Rect& bounds, int root_window_index) { | 172 void ExpectHighlighted(const gfx::Rect& bounds, aura::Window* root_window) { |
173 aura::Window* highlighting_window = GetHighlightingWindow(root_window_index); | 173 aura::Window* highlighting_window = GetHighlightingWindow(root_window); |
174 EXPECT_TRUE(highlighting_window->IsVisible()); | 174 EXPECT_TRUE(highlighting_window->IsVisible()); |
175 EXPECT_EQ(bounds, highlighting_window->GetBoundsInScreen()); | 175 EXPECT_EQ(bounds, highlighting_window->GetBoundsInScreen()); |
176 EXPECT_EQ(kBackgroundColor, GetInternalWidgetForWindow(highlighting_window) | 176 EXPECT_EQ(kBackgroundColor, GetInternalWidgetForWindow(highlighting_window) |
177 ->GetRootView() | 177 ->GetRootView() |
178 ->background() | 178 ->background() |
179 ->get_color()); | 179 ->get_color()); |
180 } | 180 } |
181 | 181 |
182 aura::Window* GetPrimaryRootWindow() { | |
183 return Shell::Get()->GetPrimaryRootWindow(); | |
184 } | |
185 | |
186 } // namespace | 182 } // namespace |
187 | 183 |
188 class AshDevToolsTest : public test::AshTestBase { | 184 class AshDevToolsTest : public test::AshTestBase { |
189 public: | 185 public: |
190 AshDevToolsTest() {} | 186 AshDevToolsTest() {} |
191 ~AshDevToolsTest() override {} | 187 ~AshDevToolsTest() override {} |
192 | 188 |
193 views::internal::NativeWidgetPrivate* CreateTestNativeWidget() { | 189 views::internal::NativeWidgetPrivate* CreateTestNativeWidget() { |
194 views::Widget* widget = new views::Widget; | 190 views::Widget* widget = new views::Widget; |
195 views::Widget::InitParams params; | 191 views::Widget::InitParams params; |
196 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; | 192 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; |
197 Shell::GetPrimaryRootWindowController() | 193 DCHECK(dom_agent()->root_windows().size()); |
198 ->ConfigureWidgetInitParamsForContainer( | 194 params.parent = GetPrimaryRootWindow(); |
199 widget, kShellWindowId_DefaultContainer, ¶ms); | |
200 widget->Init(params); | 195 widget->Init(params); |
201 return widget->native_widget_private(); | 196 return widget->native_widget_private(); |
202 } | 197 } |
203 | 198 |
204 std::unique_ptr<views::Widget> CreateTestWidget(const gfx::Rect& bounds) { | 199 std::unique_ptr<views::Widget> CreateTestWidget(const gfx::Rect& bounds) { |
205 return AshTestBase::CreateTestWidget(nullptr, kShellWindowId_Invalid, | 200 std::unique_ptr<views::Widget> widget(new views::Widget); |
206 bounds); | 201 views::Widget::InitParams params; |
| 202 params.delegate = nullptr; |
| 203 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 204 params.bounds = bounds; |
| 205 params.parent = GetPrimaryRootWindow(); |
| 206 widget->Init(params); |
| 207 widget->Show(); |
| 208 return widget; |
| 209 } |
| 210 |
| 211 aura::Window* CreateTestWindow(const gfx::Rect& bounds) { |
| 212 aura::Window* window = new aura::Window(nullptr); |
| 213 window->set_id(0); |
| 214 window->Init(ui::LAYER_TEXTURED); |
| 215 window->Show(); |
| 216 aura::Window* root = dom_agent()->root_windows()[0]; |
| 217 gfx::Point origin = bounds.origin(); |
| 218 ::wm::ConvertPointFromScreen(root, &origin); |
| 219 window->SetBounds(gfx::Rect(origin, bounds.size())); |
| 220 aura::client::ParentWindowWithContext(window, root, bounds); |
| 221 return window; |
207 } | 222 } |
208 | 223 |
209 void SetUp() override { | 224 void SetUp() override { |
210 AshTestBase::SetUp(); | |
211 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); | 225 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); |
212 uber_dispatcher_ = | 226 uber_dispatcher_ = |
213 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); | 227 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); |
214 dom_agent_ = base::MakeUnique<devtools::AshDevToolsDOMAgent>(); | 228 dom_agent_ = base::MakeUnique<devtools::AshDevToolsDOMAgent>(); |
215 dom_agent_->Init(uber_dispatcher_.get()); | 229 dom_agent_->Init(uber_dispatcher_.get()); |
216 css_agent_ = | 230 css_agent_ = |
217 base::MakeUnique<devtools::AshDevToolsCSSAgent>(dom_agent_.get()); | 231 base::MakeUnique<devtools::AshDevToolsCSSAgent>(dom_agent_.get()); |
218 css_agent_->Init(uber_dispatcher_.get()); | 232 css_agent_->Init(uber_dispatcher_.get()); |
219 css_agent_->enable(); | 233 css_agent_->enable(); |
| 234 AshTestBase::SetUp(); |
220 } | 235 } |
221 | 236 |
222 void TearDown() override { | 237 void TearDown() override { |
223 css_agent_.reset(); | 238 css_agent_.reset(); |
224 dom_agent_.reset(); | 239 dom_agent_.reset(); |
225 uber_dispatcher_.reset(); | 240 uber_dispatcher_.reset(); |
226 fake_frontend_channel_.reset(); | 241 fake_frontend_channel_.reset(); |
227 AshTestBase::TearDown(); | 242 AshTestBase::TearDown(); |
228 } | 243 } |
229 | 244 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 ASSERT_FALSE(output); | 295 ASSERT_FALSE(output); |
281 } | 296 } |
282 | 297 |
283 void HighlightNode(int node_id) { | 298 void HighlightNode(int node_id) { |
284 dom_agent_->highlightNode( | 299 dom_agent_->highlightNode( |
285 CreateHighlightConfig(kBackgroundColor, kBorderColor), node_id); | 300 CreateHighlightConfig(kBackgroundColor, kBorderColor), node_id); |
286 } | 301 } |
287 | 302 |
288 void HideHighlight(int root_window_index) { | 303 void HideHighlight(int root_window_index) { |
289 dom_agent_->hideHighlight(); | 304 dom_agent_->hideHighlight(); |
290 ASSERT_FALSE(GetHighlightingWindow(root_window_index)->IsVisible()); | 305 DCHECK_GE(root_window_index, 0); |
| 306 DCHECK_LE(root_window_index, |
| 307 static_cast<int>(dom_agent()->root_windows().size())); |
| 308 ASSERT_FALSE( |
| 309 GetHighlightingWindow(dom_agent()->root_windows()[root_window_index]) |
| 310 ->IsVisible()); |
291 } | 311 } |
292 | 312 |
293 FakeFrontendChannel* frontend_channel() { | 313 FakeFrontendChannel* frontend_channel() { |
294 return fake_frontend_channel_.get(); | 314 return fake_frontend_channel_.get(); |
295 } | 315 } |
296 | 316 |
| 317 aura::Window* GetPrimaryRootWindow() { |
| 318 DCHECK(dom_agent()->root_windows().size()); |
| 319 return dom_agent()->root_windows()[0]; |
| 320 } |
| 321 |
297 devtools::AshDevToolsCSSAgent* css_agent() { return css_agent_.get(); } | 322 devtools::AshDevToolsCSSAgent* css_agent() { return css_agent_.get(); } |
298 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } | 323 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } |
299 | 324 |
300 private: | 325 private: |
301 std::unique_ptr<UberDispatcher> uber_dispatcher_; | 326 std::unique_ptr<UberDispatcher> uber_dispatcher_; |
302 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_; | 327 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_; |
303 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; | 328 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; |
304 std::unique_ptr<devtools::AshDevToolsCSSAgent> css_agent_; | 329 std::unique_ptr<devtools::AshDevToolsCSSAgent> css_agent_; |
305 | 330 |
306 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); | 331 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 | 652 |
628 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | 653 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); |
629 ASSERT_TRUE(parent_node); | 654 ASSERT_TRUE(parent_node); |
630 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); | 655 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); |
631 ASSERT_TRUE(parent_children); | 656 ASSERT_TRUE(parent_children); |
632 DOM::Node* window_node = parent_children->get(1); | 657 DOM::Node* window_node = parent_children->get(1); |
633 DOM::Node* widget_node = parent_children->get(0); | 658 DOM::Node* widget_node = parent_children->get(0); |
634 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | 659 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); |
635 | 660 |
636 HighlightNode(window_node->getNodeId()); | 661 HighlightNode(window_node->getNodeId()); |
637 ExpectHighlighted(window->GetBoundsInScreen(), 0); | 662 ExpectHighlighted(window->GetBoundsInScreen(), GetPrimaryRootWindow()); |
638 | 663 |
639 HideHighlight(0); | 664 HideHighlight(0); |
640 | 665 |
641 HighlightNode(widget_node->getNodeId()); | 666 HighlightNode(widget_node->getNodeId()); |
642 ExpectHighlighted(widget->GetWindowBoundsInScreen(), 0); | 667 ExpectHighlighted(widget->GetWindowBoundsInScreen(), GetPrimaryRootWindow()); |
643 | 668 |
644 HideHighlight(0); | 669 HideHighlight(0); |
645 | 670 |
646 HighlightNode(root_view_node->getNodeId()); | 671 HighlightNode(root_view_node->getNodeId()); |
647 ExpectHighlighted(root_view->GetBoundsInScreen(), 0); | 672 ExpectHighlighted(root_view->GetBoundsInScreen(), GetPrimaryRootWindow()); |
648 | 673 |
649 HideHighlight(0); | 674 HideHighlight(0); |
650 | 675 |
651 // Highlight non-existent node | 676 // Highlight non-existent node |
652 HighlightNode(10000); | 677 HighlightNode(10000); |
653 EXPECT_FALSE(GetHighlightingWindow(0)->IsVisible()); | 678 EXPECT_FALSE(GetHighlightingWindow(GetPrimaryRootWindow())->IsVisible()); |
654 } | 679 } |
655 | 680 |
656 int GetNodeIdFromWindow(devtools::UIElement* ui_element, aura::Window* window) { | 681 int GetNodeIdFromWindow(devtools::UIElement* ui_element, aura::Window* window) { |
657 for (auto* child : ui_element->children()) { | 682 for (auto* child : ui_element->children()) { |
658 if (child->type() == devtools::UIElementType::WINDOW && | 683 if (child->type() == devtools::UIElementType::WINDOW && |
659 static_cast<devtools::WindowElement*>(child)->window() == window) { | 684 static_cast<devtools::WindowElement*>(child)->window() == window) { |
660 return child->node_id(); | 685 return child->node_id(); |
661 } | 686 } |
662 } | 687 } |
663 for (auto* child : ui_element->children()) { | 688 for (auto* child : ui_element->children()) { |
664 if (child->type() == devtools::UIElementType::WINDOW) { | 689 if (child->type() == devtools::UIElementType::WINDOW) { |
665 int node_id = GetNodeIdFromWindow(child, window); | 690 int node_id = GetNodeIdFromWindow(child, window); |
666 if (node_id > 0) | 691 if (node_id > 0) |
667 return node_id; | 692 return node_id; |
668 } | 693 } |
669 } | 694 } |
670 return 0; | 695 return 0; |
671 } | 696 } |
672 | 697 |
| 698 // TODO(thanhph): Make test AshDevToolsTest.MultipleDisplayHighlight work with |
| 699 // multiple displays by updating params.parent in |
| 700 // InitializeHighlightingWidget(). |
| 701 |
673 TEST_F(AshDevToolsTest, MultipleDisplayHighlight) { | 702 TEST_F(AshDevToolsTest, MultipleDisplayHighlight) { |
| 703 LOG(ERROR) << "1."; |
674 UpdateDisplay("300x400,500x500"); | 704 UpdateDisplay("300x400,500x500"); |
| 705 LOG(ERROR) << "2."; |
675 | 706 |
676 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | 707 aura::Window::Windows root_windows = dom_agent()->root_windows(); |
| 708 LOG(ERROR) << "root_windows[0]->GetBoundsInScreen().ToString(): " |
| 709 << root_windows[0]->GetBoundsInScreen().ToString(); |
| 710 LOG(ERROR) << "root_windows[1]->GetBoundsInScreen().ToString(): " |
| 711 << root_windows[1]->GetBoundsInScreen().ToString(); |
| 712 |
677 std::unique_ptr<aura::Window> window( | 713 std::unique_ptr<aura::Window> window( |
678 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40))); | 714 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40))); |
679 | 715 |
680 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 716 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
681 dom_agent()->getDocument(&root); | 717 dom_agent()->getDocument(&root); |
682 | 718 |
683 EXPECT_EQ(root_windows[0], window->GetRootWindow()); | 719 EXPECT_EQ(root_windows[0], window->GetRootWindow()); |
| 720 LOG(ERROR) << "4."; |
684 HighlightNode( | 721 HighlightNode( |
685 GetNodeIdFromWindow(dom_agent()->window_element_root(), window.get())); | 722 GetNodeIdFromWindow(dom_agent()->window_element_root(), window.get())); |
686 ExpectHighlighted(window->GetBoundsInScreen(), 0); | 723 LOG(ERROR) << "5."; |
| 724 ExpectHighlighted(window->GetBoundsInScreen(), |
| 725 dom_agent()->root_windows()[0]); |
| 726 LOG(ERROR) << "6."; |
687 | 727 |
688 window->SetBoundsInScreen(gfx::Rect(500, 0, 50, 50), GetSecondaryDisplay()); | 728 window->SetBoundsInScreen(gfx::Rect(500, 0, 50, 50), GetSecondaryDisplay()); |
| 729 LOG(ERROR) << "7."; |
689 EXPECT_EQ(root_windows[1], window->GetRootWindow()); | 730 EXPECT_EQ(root_windows[1], window->GetRootWindow()); |
| 731 LOG(ERROR) << "8."; |
690 HighlightNode( | 732 HighlightNode( |
691 GetNodeIdFromWindow(dom_agent()->window_element_root(), window.get())); | 733 GetNodeIdFromWindow(dom_agent()->window_element_root(), window.get())); |
692 ExpectHighlighted(window->GetBoundsInScreen(), 1); | 734 LOG(ERROR) << "9."; |
| 735 ExpectHighlighted(window->GetBoundsInScreen(), |
| 736 dom_agent()->root_windows()[2]); |
| 737 LOG(ERROR) << "10."; |
693 } | 738 } |
694 | 739 |
695 TEST_F(AshDevToolsTest, WindowWidgetViewGetMatchedStylesForNode) { | 740 TEST_F(AshDevToolsTest, WindowWidgetViewGetMatchedStylesForNode) { |
696 std::unique_ptr<views::Widget> widget( | 741 std::unique_ptr<views::Widget> widget( |
697 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | 742 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
698 aura::Window* parent_window = widget->GetNativeWindow(); | 743 aura::Window* parent_window = widget->GetNativeWindow(); |
699 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window)); | 744 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window)); |
700 gfx::Rect window_bounds(2, 2, 3, 3); | 745 gfx::Rect window_bounds(2, 2, 3, 3); |
701 gfx::Rect widget_bounds(50, 50, 100, 75); | 746 gfx::Rect widget_bounds(50, 50, 100, 75); |
702 gfx::Rect view_bounds(4, 4, 3, 3); | 747 gfx::Rect view_bounds(4, 4, 3, 3); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 SetStyleTexts(root_view_node, "\nheight: 73;\n ", true); | 860 SetStyleTexts(root_view_node, "\nheight: 73;\n ", true); |
816 EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds()); | 861 EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds()); |
817 | 862 |
818 SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\nvisibility: 1;\n", | 863 SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\nvisibility: 1;\n", |
819 true); | 864 true); |
820 EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds()); | 865 EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds()); |
821 EXPECT_TRUE(root_view->visible()); | 866 EXPECT_TRUE(root_view->visible()); |
822 } | 867 } |
823 | 868 |
824 } // namespace ash | 869 } // namespace ash |
OLD | NEW |