| 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/common/devtools/ash_devtools_dom_agent.h" | 5 #include "ash/common/devtools/ash_devtools_dom_agent.h" |
| 6 | 6 |
| 7 #include "ash/common/test/ash_test.h" | 7 #include "ash/common/test/ash_test.h" |
| 8 #include "ash/common/wm_lookup.h" | 8 #include "ash/common/wm_lookup.h" |
| 9 #include "ash/common/wm_shell.h" | 9 #include "ash/common/wm_shell.h" |
| 10 #include "ash/common/wm_window.h" | 10 #include "ash/common/wm_window.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { | 61 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { |
| 62 EXPECT_TRUE(node->hasAttributes()); | 62 EXPECT_TRUE(node->hasAttributes()); |
| 63 Array<std::string>* attributes = node->getAttributes(nullptr); | 63 Array<std::string>* attributes = node->getAttributes(nullptr); |
| 64 for (size_t i = 0; i < attributes->length() - 1; i++) { | 64 for (size_t i = 0; i < attributes->length() - 1; i++) { |
| 65 if (attributes->get(i) == attribute) | 65 if (attributes->get(i) == attribute) |
| 66 return attributes->get(i + 1); | 66 return attributes->get(i + 1); |
| 67 } | 67 } |
| 68 return nullptr; | 68 return nullptr; |
| 69 } | 69 } |
| 70 | 70 |
| 71 bool Equals(views::Widget* widget, DOM::Node* node) { | 71 bool Equals(WmWindow* window, DOM::Node* node) { |
| 72 return "Widget" == node->getNodeName() && | 72 int children_count = static_cast<int>(window->GetChildren().size()); |
| 73 widget->GetName() == GetAttributeValue("name", node) && | 73 if (window->GetInternalWidget()) |
| 74 (widget->GetRootView() ? 1 : 0) == | 74 children_count++; |
| 75 node->getChildNodeCount(kDefaultChildNodeCount); | 75 return "Window" == node->getNodeName() && |
| 76 window->GetName() == GetAttributeValue("name", node) && |
| 77 children_count == node->getChildNodeCount(kDefaultChildNodeCount); |
| 76 } | 78 } |
| 77 | 79 |
| 78 bool Equals(WmWindow* window, DOM::Node* node) { | 80 void Compare(views::Widget* widget, DOM::Node* node) { |
| 79 return "Window" == node->getNodeName() && | 81 EXPECT_EQ("Widget", node->getNodeName()); |
| 80 window->GetName() == GetAttributeValue("name", node) && | 82 EXPECT_EQ(widget->GetName(), GetAttributeValue("name", node)); |
| 81 static_cast<int>(window->GetChildren().size()) == | 83 EXPECT_EQ(widget->GetRootView() ? 1 : 0, |
| 82 node->getChildNodeCount(kDefaultChildNodeCount); | 84 node->getChildNodeCount(kDefaultChildNodeCount)); |
| 83 } | 85 } |
| 84 | 86 |
| 85 void Compare(views::View* view, DOM::Node* node) { | 87 void Compare(views::View* view, DOM::Node* node) { |
| 86 EXPECT_EQ("View", node->getNodeName()); | 88 EXPECT_EQ("View", node->getNodeName()); |
| 87 EXPECT_EQ(view->GetClassName(), GetAttributeValue("name", node)); | 89 EXPECT_EQ(view->GetClassName(), GetAttributeValue("name", node)); |
| 88 EXPECT_EQ(view->child_count(), | 90 EXPECT_EQ(view->child_count(), |
| 89 node->getChildNodeCount(kDefaultChildNodeCount)); | 91 node->getChildNodeCount(kDefaultChildNodeCount)); |
| 90 } | 92 } |
| 91 | 93 |
| 92 void Compare(WmWindow* window, DOM::Node* node) { | 94 void Compare(WmWindow* window, DOM::Node* node) { |
| 93 EXPECT_TRUE(Equals(window, node)); | 95 EXPECT_TRUE(Equals(window, node)); |
| 94 } | 96 } |
| 95 | 97 |
| 96 DOM::Node* FindInRoot(WmWindow* window, DOM::Node* root) { | 98 DOM::Node* FindInRoot(WmWindow* window, DOM::Node* root) { |
| 97 if (Equals(window, root)) | 99 if (Equals(window, root)) |
| 98 return root; | 100 return root; |
| 99 | 101 |
| 100 Array<DOM::Node>* children = root->getChildren(nullptr); | 102 Array<DOM::Node>* children = root->getChildren(nullptr); |
| 101 DOM::Node* window_node = nullptr; | 103 DOM::Node* window_node = nullptr; |
| 102 for (size_t i = 0; i < children->length(); i++) { | 104 for (size_t i = 0; i < children->length(); i++) { |
| 103 window_node = FindInRoot(window, children->get(i)); | 105 window_node = FindInRoot(window, children->get(i)); |
| 104 if (window_node) | 106 if (window_node) |
| 105 return window_node; | 107 return window_node; |
| 106 } | 108 } |
| 107 return window_node; | 109 return window_node; |
| 108 } | 110 } |
| 109 | 111 |
| 110 DOM::Node* FindInRoot(views::Widget* widget, DOM::Node* root) { | |
| 111 if (Equals(widget, root)) | |
| 112 return root; | |
| 113 | |
| 114 Array<DOM::Node>* children = root->getChildren(nullptr); | |
| 115 DOM::Node* widget_node = nullptr; | |
| 116 for (size_t i = 0; i < children->length(); i++) { | |
| 117 widget_node = FindInRoot(widget, children->get(i)); | |
| 118 if (widget_node) | |
| 119 return widget_node; | |
| 120 } | |
| 121 return widget_node; | |
| 122 } | |
| 123 | |
| 124 } // namespace | 112 } // namespace |
| 125 | 113 |
| 126 class AshDevToolsTest : public AshTest { | 114 class AshDevToolsTest : public AshTest { |
| 127 public: | 115 public: |
| 128 AshDevToolsTest() {} | 116 AshDevToolsTest() {} |
| 129 ~AshDevToolsTest() override {} | 117 ~AshDevToolsTest() override {} |
| 130 | 118 |
| 131 void SetUp() override { | 119 void SetUp() override { |
| 132 AshTest::SetUp(); | 120 AshTest::SetUp(); |
| 133 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); | 121 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 WmWindow* child_window = child_owner->window(); | 172 WmWindow* child_window = child_owner->window(); |
| 185 child_window->SetName("child_window"); | 173 child_window->SetName("child_window"); |
| 186 widget->Show(); | 174 widget->Show(); |
| 187 views::View* child_view = new TestView("child_view"); | 175 views::View* child_view = new TestView("child_view"); |
| 188 widget->GetRootView()->AddChildView(child_view); | 176 widget->GetRootView()->AddChildView(child_view); |
| 189 | 177 |
| 190 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 178 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 191 dom_agent()->getDocument(&root); | 179 dom_agent()->getDocument(&root); |
| 192 | 180 |
| 193 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | 181 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); |
| 194 DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); | |
| 195 ASSERT_TRUE(parent_node); | 182 ASSERT_TRUE(parent_node); |
| 196 ASSERT_TRUE(widget_node); | 183 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); |
| 197 ASSERT_TRUE(parent_node->getChildren(nullptr)); | 184 ASSERT_TRUE(parent_children); |
| 198 Compare(child_window, parent_node->getChildren(nullptr)->get(0)); | 185 DOM::Node* widget_node = parent_children->get(0); |
| 186 Compare(widget.get(), widget_node); |
| 187 Compare(child_window, parent_children->get(1)); |
| 199 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); | 188 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); |
| 200 ASSERT_TRUE(widget_children); | 189 ASSERT_TRUE(widget_children); |
| 201 Compare(widget->GetRootView(), widget_children->get(0)); | 190 Compare(widget->GetRootView(), widget_children->get(0)); |
| 202 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); | 191 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); |
| 203 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); | 192 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); |
| 204 } | 193 } |
| 205 | 194 |
| 206 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { | 195 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { |
| 207 // Initialize DOMAgent | 196 // Initialize DOMAgent |
| 208 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 197 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 223 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 212 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 224 dom_agent()->getDocument(&root); | 213 dom_agent()->getDocument(&root); |
| 225 | 214 |
| 226 WmWindow* parent_window = | 215 WmWindow* parent_window = |
| 227 WmShell::Get()->GetPrimaryRootWindow()->GetChildren()[0]; | 216 WmShell::Get()->GetPrimaryRootWindow()->GetChildren()[0]; |
| 228 WmWindow* child_window = parent_window->GetChildren()[0]; | 217 WmWindow* child_window = parent_window->GetChildren()[0]; |
| 229 DOM::Node* root_node = root->getChildren(nullptr)->get(0); | 218 DOM::Node* root_node = root->getChildren(nullptr)->get(0); |
| 230 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); | 219 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); |
| 231 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); | 220 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); |
| 232 | 221 |
| 222 Compare(parent_window, parent_node); |
| 223 Compare(child_window, child_node); |
| 233 child_window->Destroy(); | 224 child_window->Destroy(); |
| 234 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); | 225 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); |
| 235 } | 226 } |
| 236 | 227 |
| 237 TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRemovedAndInserted) { | 228 TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRemovedAndInserted) { |
| 238 // Initialize DOMAgent | 229 // Initialize DOMAgent |
| 239 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 230 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 240 dom_agent()->getDocument(&root); | 231 dom_agent()->getDocument(&root); |
| 241 | 232 |
| 242 WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow(); | 233 WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 243 WmWindow* target_window = root_window->GetChildren()[1]; | 234 WmWindow* target_window = root_window->GetChildren()[1]; |
| 244 WmWindow* child_window = root_window->GetChildren()[0]->GetChildren()[0]; | 235 WmWindow* child_window = root_window->GetChildren()[0]->GetChildren()[0]; |
| 245 | 236 |
| 246 DOM::Node* root_node = root->getChildren(nullptr)->get(0); | 237 DOM::Node* root_node = root->getChildren(nullptr)->get(0); |
| 247 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); | 238 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); |
| 248 DOM::Node* target_node = root_node->getChildren(nullptr)->get(1); | 239 DOM::Node* target_node = root_node->getChildren(nullptr)->get(1); |
| 249 Array<DOM::Node>* target_node_children = target_node->getChildren(nullptr); | 240 Array<DOM::Node>* target_node_children = target_node->getChildren(nullptr); |
| 250 DOM::Node* sibling_node = | 241 DOM::Node* sibling_node = |
| 251 target_node_children->get(target_node_children->length() - 1); | 242 target_node_children->get(target_node_children->length() - 1); |
| 252 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); | 243 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); |
| 253 | 244 |
| 245 Compare(target_window, target_node); |
| 246 Compare(child_window, child_node); |
| 254 target_window->AddChild(child_window); | 247 target_window->AddChild(child_window); |
| 255 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); | 248 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); |
| 256 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId()); | 249 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId()); |
| 257 } | 250 } |
| 258 | 251 |
| 259 TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) { | 252 TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) { |
| 260 // Initialize DOMAgent | 253 // Initialize DOMAgent |
| 261 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 254 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 262 dom_agent()->getDocument(&root); | 255 dom_agent()->getDocument(&root); |
| 263 | 256 |
| 264 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); | 257 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 265 WmWindow* child_window = parent_window->GetChildren()[0]; | 258 WmWindow* child_window = parent_window->GetChildren()[0]; |
| 266 WmWindow* target_window = parent_window->GetChildren()[1]; | 259 WmWindow* target_window = parent_window->GetChildren()[1]; |
| 267 | 260 |
| 268 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); | 261 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); |
| 269 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); | 262 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); |
| 270 DOM::Node* child_node = parent_node_children->get(0); | 263 DOM::Node* child_node = parent_node_children->get(0); |
| 271 DOM::Node* sibling_node = parent_node_children->get(1); | 264 DOM::Node* sibling_node = parent_node_children->get(1); |
| 272 int parent_id = parent_node->getNodeId(); | 265 int parent_id = parent_node->getNodeId(); |
| 273 | 266 |
| 267 Compare(parent_window, parent_node); |
| 268 Compare(child_window, child_node); |
| 274 parent_window->StackChildAbove(child_window, target_window); | 269 parent_window->StackChildAbove(child_window, target_window); |
| 275 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); | 270 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); |
| 276 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); | 271 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); |
| 277 } | 272 } |
| 278 | 273 |
| 279 } // namespace ash | 274 } // namespace ash |
| OLD | NEW |