| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/common/devtools/ash_devtools_dom_agent.h" |
| 6 |
| 7 #include "ash/common/test/ash_test.h" |
| 8 #include "ash/common/wm_lookup.h" |
| 9 #include "ash/common/wm_shell.h" |
| 10 #include "ash/common/wm_window.h" |
| 11 #include "ui/views/widget/widget.h" |
| 12 |
| 13 namespace ash { |
| 14 namespace { |
| 15 using namespace ui::devtools::protocol; |
| 16 const int kDefaultChildNodeCount = -1; |
| 17 |
| 18 class TestView : public views::View { |
| 19 public: |
| 20 TestView(const char* name) : views::View(), name_(name) {} |
| 21 |
| 22 const char* GetClassName() const override { return name_; } |
| 23 |
| 24 private: |
| 25 const char* name_; |
| 26 |
| 27 DISALLOW_COPY_AND_ASSIGN(TestView); |
| 28 }; |
| 29 |
| 30 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { |
| 31 EXPECT_TRUE(node->hasAttributes()); |
| 32 Array<std::string>* attributes = node->getAttributes(nullptr); |
| 33 for (size_t i = 0; i < attributes->length() - 1; i++) { |
| 34 if (attributes->get(i) == attribute) |
| 35 return attributes->get(i + 1); |
| 36 } |
| 37 return nullptr; |
| 38 } |
| 39 |
| 40 bool Equals(views::Widget* widget, DOM::Node* node) { |
| 41 return "Widget" == node->getNodeName() && |
| 42 widget->GetName() == GetAttributeValue("name", node) && |
| 43 (widget->GetRootView() ? 1 : 0) == |
| 44 node->getChildNodeCount(kDefaultChildNodeCount); |
| 45 } |
| 46 |
| 47 bool Equals(WmWindow* window, DOM::Node* node) { |
| 48 return "Window" == node->getNodeName() && |
| 49 window->GetName() == GetAttributeValue("name", node) && |
| 50 static_cast<int>(window->GetChildren().size()) == |
| 51 node->getChildNodeCount(kDefaultChildNodeCount); |
| 52 } |
| 53 |
| 54 void Compare(views::View* view, DOM::Node* node) { |
| 55 EXPECT_EQ("View", node->getNodeName()); |
| 56 EXPECT_EQ(view->GetClassName(), GetAttributeValue("name", node)); |
| 57 EXPECT_EQ(view->child_count(), |
| 58 node->getChildNodeCount(kDefaultChildNodeCount)); |
| 59 } |
| 60 |
| 61 void Compare(WmWindow* window, DOM::Node* node) { |
| 62 EXPECT_TRUE(Equals(window, node)); |
| 63 } |
| 64 |
| 65 DOM::Node* FindInRoot(WmWindow* window, DOM::Node* root) { |
| 66 if (Equals(window, root)) |
| 67 return root; |
| 68 |
| 69 Array<DOM::Node>* children = root->getChildren(nullptr); |
| 70 DOM::Node* window_node = nullptr; |
| 71 for (size_t i = 0; i < children->length(); i++) { |
| 72 window_node = FindInRoot(window, children->get(i)); |
| 73 if (window_node) |
| 74 return window_node; |
| 75 } |
| 76 return window_node; |
| 77 } |
| 78 |
| 79 DOM::Node* FindInRoot(views::Widget* widget, DOM::Node* root) { |
| 80 if (Equals(widget, root)) |
| 81 return root; |
| 82 |
| 83 Array<DOM::Node>* children = root->getChildren(nullptr); |
| 84 DOM::Node* widget_node = nullptr; |
| 85 for (size_t i = 0; i < children->length(); i++) { |
| 86 widget_node = FindInRoot(widget, children->get(i)); |
| 87 if (widget_node) |
| 88 return widget_node; |
| 89 } |
| 90 return widget_node; |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 class AshDevToolsTest : public AshTest { |
| 96 public: |
| 97 AshDevToolsTest() {} |
| 98 ~AshDevToolsTest() override {} |
| 99 |
| 100 void SetUp() override { |
| 101 AshTest::SetUp(); |
| 102 dom_agent_ = |
| 103 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); |
| 104 } |
| 105 |
| 106 void TearDown() override { |
| 107 dom_agent_.reset(); |
| 108 AshTest::TearDown(); |
| 109 } |
| 110 |
| 111 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } |
| 112 |
| 113 private: |
| 114 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); |
| 117 }; |
| 118 |
| 119 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { |
| 120 std::unique_ptr<views::Widget> widget( |
| 121 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
| 122 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get()); |
| 123 parent_window->SetName("parent_window"); |
| 124 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); |
| 125 WmWindow* child_window = child_owner->window(); |
| 126 child_window->SetName("child_window"); |
| 127 widget->Show(); |
| 128 views::View* child_view = new TestView("child_view"); |
| 129 widget->GetRootView()->AddChildView(child_view); |
| 130 |
| 131 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 132 |
| 133 dom_agent()->getDocument(&root); |
| 134 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); |
| 135 DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); |
| 136 |
| 137 ASSERT_TRUE(parent_node); |
| 138 ASSERT_TRUE(widget_node); |
| 139 Array<DOM::Node>* default_children = nullptr; |
| 140 ASSERT_TRUE(parent_node->getChildren(default_children)); |
| 141 Compare(child_window, parent_node->getChildren(default_children)->get(0)); |
| 142 Array<DOM::Node>* widget_children = |
| 143 widget_node->getChildren(default_children); |
| 144 ASSERT_TRUE(widget_children); |
| 145 Compare(widget->GetRootView(), widget_children->get(0)); |
| 146 ASSERT_TRUE(widget_children->get(0)->getChildren(default_children)); |
| 147 Compare(child_view, |
| 148 widget_children->get(0)->getChildren(default_children)->get(1)); |
| 149 } |
| 150 |
| 151 } // namespace ash |
| OLD | NEW |