| 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_root_window_controller.h" |
| 9 #include "ash/common/wm_shell.h" | 10 #include "ash/common/wm_shell.h" |
| 10 #include "ash/common/wm_window.h" | 11 #include "ash/common/wm_window.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "ui/views/widget/native_widget_private.h" |
| 12 #include "ui/views/widget/widget.h" | 14 #include "ui/views/widget/widget.h" |
| 13 | 15 |
| 14 namespace ash { | 16 namespace ash { |
| 15 namespace { | 17 namespace { |
| 16 using namespace ui::devtools::protocol; | 18 using namespace ui::devtools::protocol; |
| 17 const int kDefaultChildNodeCount = -1; | 19 const int kDefaultChildNodeCount = -1; |
| 18 | 20 |
| 19 class TestView : public views::View { | 21 class TestView : public views::View { |
| 20 public: | 22 public: |
| 21 TestView(const char* name) : views::View(), name_(name) {} | 23 TestView(const char* name) : views::View(), name_(name) {} |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 return window_node; | 115 return window_node; |
| 114 } | 116 } |
| 115 | 117 |
| 116 } // namespace | 118 } // namespace |
| 117 | 119 |
| 118 class AshDevToolsTest : public AshTest { | 120 class AshDevToolsTest : public AshTest { |
| 119 public: | 121 public: |
| 120 AshDevToolsTest() {} | 122 AshDevToolsTest() {} |
| 121 ~AshDevToolsTest() override {} | 123 ~AshDevToolsTest() override {} |
| 122 | 124 |
| 125 views::internal::NativeWidgetPrivate* CreateTestNativeWidget() { |
| 126 views::Widget* widget = new views::Widget; |
| 127 views::Widget::InitParams params; |
| 128 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; |
| 129 WmShell::Get() |
| 130 ->GetPrimaryRootWindow() |
| 131 ->GetRootWindowController() |
| 132 ->ConfigureWidgetInitParamsForContainer( |
| 133 widget, kShellWindowId_DefaultContainer, ¶ms); |
| 134 widget->Init(params); |
| 135 return widget->native_widget_private(); |
| 136 } |
| 137 |
| 123 void SetUp() override { | 138 void SetUp() override { |
| 124 AshTest::SetUp(); | 139 AshTest::SetUp(); |
| 125 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); | 140 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); |
| 126 uber_dispatcher_ = | 141 uber_dispatcher_ = |
| 127 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); | 142 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); |
| 128 dom_agent_ = | 143 dom_agent_ = |
| 129 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); | 144 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); |
| 130 dom_agent_->Init(uber_dispatcher_.get()); | 145 dom_agent_->Init(uber_dispatcher_.get()); |
| 131 } | 146 } |
| 132 | 147 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 DOM::Node* widget_node = parent_children->get(0); | 204 DOM::Node* widget_node = parent_children->get(0); |
| 190 Compare(widget.get(), widget_node); | 205 Compare(widget.get(), widget_node); |
| 191 Compare(child_window, parent_children->get(1)); | 206 Compare(child_window, parent_children->get(1)); |
| 192 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); | 207 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); |
| 193 ASSERT_TRUE(widget_children); | 208 ASSERT_TRUE(widget_children); |
| 194 Compare(widget->GetRootView(), widget_children->get(0)); | 209 Compare(widget->GetRootView(), widget_children->get(0)); |
| 195 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); | 210 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); |
| 196 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); | 211 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); |
| 197 } | 212 } |
| 198 | 213 |
| 214 TEST_F(AshDevToolsTest, GetDocumentNativeWidgetOwnsWidget) { |
| 215 views::internal::NativeWidgetPrivate* native_widget_private = |
| 216 CreateTestNativeWidget(); |
| 217 views::Widget* widget = native_widget_private->GetWidget(); |
| 218 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget); |
| 219 |
| 220 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 221 dom_agent()->getDocument(&root); |
| 222 |
| 223 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); |
| 224 ASSERT_TRUE(parent_node); |
| 225 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); |
| 226 Compare(widget, widget_node); |
| 227 // Destroy NativeWidget followed by |widget| |
| 228 widget->CloseNow(); |
| 229 } |
| 230 |
| 199 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { | 231 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { |
| 200 // Initialize DOMAgent | 232 // Initialize DOMAgent |
| 201 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 233 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 202 dom_agent()->getDocument(&root); | 234 dom_agent()->getDocument(&root); |
| 203 | 235 |
| 204 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); | 236 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 205 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); | 237 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); |
| 206 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); | 238 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); |
| 207 DOM::Node* sibling_node = | 239 DOM::Node* sibling_node = |
| 208 parent_node_children->get(parent_node_children->length() - 1); | 240 parent_node_children->get(parent_node_children->length() - 1); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 DOM::Node* sibling_node = parent_node_children->get(1); | 336 DOM::Node* sibling_node = parent_node_children->get(1); |
| 305 int parent_id = parent_node->getNodeId(); | 337 int parent_id = parent_node->getNodeId(); |
| 306 | 338 |
| 307 Compare(parent_window, parent_node); | 339 Compare(parent_window, parent_node); |
| 308 Compare(child_window, child_node); | 340 Compare(child_window, child_node); |
| 309 parent_window->StackChildAbove(child_window, target_window); | 341 parent_window->StackChildAbove(child_window, target_window); |
| 310 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); | 342 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); |
| 311 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); | 343 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); |
| 312 } | 344 } |
| 313 | 345 |
| 346 TEST_F(AshDevToolsTest, ViewInserted) { |
| 347 std::unique_ptr<views::Widget> widget( |
| 348 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
| 349 WmWindow* window = WmLookup::Get()->GetWindowForWidget(widget.get()); |
| 350 widget->Show(); |
| 351 |
| 352 // Initialize DOMAgent |
| 353 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 354 dom_agent()->getDocument(&root); |
| 355 |
| 356 DOM::Node* parent_node = FindInRoot(window, root.get()); |
| 357 ASSERT_TRUE(parent_node); |
| 358 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); |
| 359 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); |
| 360 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); |
| 361 ASSERT_TRUE(root_view_children); |
| 362 DOM::Node* sibling_view_node = |
| 363 root_view_children->get(root_view_children->length() - 1); |
| 364 |
| 365 widget->GetRootView()->AddChildView(new views::View); |
| 366 ExpectChildNodeInserted(root_view_node->getNodeId(), |
| 367 sibling_view_node->getNodeId()); |
| 368 } |
| 369 |
| 370 TEST_F(AshDevToolsTest, ViewRemoved) { |
| 371 std::unique_ptr<views::Widget> widget( |
| 372 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
| 373 WmWindow* window = WmLookup::Get()->GetWindowForWidget(widget.get()); |
| 374 widget->Show(); |
| 375 views::View* root_view = widget->GetRootView(); |
| 376 views::View* child_view = new views::View; |
| 377 root_view->AddChildView(child_view); |
| 378 |
| 379 // Initialize DOMAgent |
| 380 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 381 dom_agent()->getDocument(&root); |
| 382 |
| 383 DOM::Node* parent_node = FindInRoot(window, root.get()); |
| 384 ASSERT_TRUE(parent_node); |
| 385 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); |
| 386 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); |
| 387 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); |
| 388 ASSERT_TRUE(root_view_children); |
| 389 DOM::Node* child_view_node = |
| 390 root_view_children->get(root_view_children->length() - 1); |
| 391 |
| 392 Compare(child_view, child_view_node); |
| 393 root_view->RemoveChildView(child_view); |
| 394 ExpectChildNodeRemoved(root_view_node->getNodeId(), |
| 395 child_view_node->getNodeId()); |
| 396 } |
| 397 |
| 398 TEST_F(AshDevToolsTest, ViewRearranged) { |
| 399 std::unique_ptr<views::Widget> widget( |
| 400 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
| 401 WmWindow* window = WmLookup::Get()->GetWindowForWidget(widget.get()); |
| 402 widget->Show(); |
| 403 views::View* root_view = widget->GetRootView(); |
| 404 views::View* parent_view = new views::View; |
| 405 views::View* target_view = new views::View; |
| 406 views::View* child_view = new views::View; |
| 407 root_view->AddChildView(parent_view); |
| 408 root_view->AddChildView(target_view); |
| 409 parent_view->AddChildView(child_view); |
| 410 |
| 411 // Initialize DOMAgent |
| 412 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 413 dom_agent()->getDocument(&root); |
| 414 |
| 415 DOM::Node* parent_node = FindInRoot(window, root.get()); |
| 416 ASSERT_TRUE(parent_node); |
| 417 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); |
| 418 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); |
| 419 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); |
| 420 ASSERT_TRUE(root_view_children); |
| 421 size_t root_children_size = root_view_children->length(); |
| 422 ASSERT_TRUE(root_children_size >= 2); |
| 423 DOM::Node* parent_view_node = root_view_children->get(root_children_size - 2); |
| 424 DOM::Node* target_view_node = root_view_children->get(root_children_size - 1); |
| 425 DOM::Node* child_view_node = parent_view_node->getChildren(nullptr)->get(0); |
| 426 |
| 427 Compare(parent_view, parent_view_node); |
| 428 Compare(target_view, target_view_node); |
| 429 Compare(child_view, child_view_node); |
| 430 target_view->AddChildView(child_view); |
| 431 ExpectChildNodeRemoved(parent_view_node->getNodeId(), |
| 432 child_view_node->getNodeId()); |
| 433 ExpectChildNodeInserted(target_view_node->getNodeId(), 0); |
| 434 } |
| 435 |
| 436 TEST_F(AshDevToolsTest, ViewRearrangedRemovedAndInserted) { |
| 437 std::unique_ptr<views::Widget> widget( |
| 438 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
| 439 WmWindow* window = WmLookup::Get()->GetWindowForWidget(widget.get()); |
| 440 widget->Show(); |
| 441 views::View* root_view = widget->GetRootView(); |
| 442 views::View* parent_view = new views::View; |
| 443 views::View* target_view = new views::View; |
| 444 views::View* child_view = new views::View; |
| 445 root_view->AddChildView(parent_view); |
| 446 root_view->AddChildView(target_view); |
| 447 parent_view->AddChildView(child_view); |
| 448 |
| 449 // Initialize DOMAgent |
| 450 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 451 dom_agent()->getDocument(&root); |
| 452 |
| 453 DOM::Node* parent_node = FindInRoot(window, root.get()); |
| 454 ASSERT_TRUE(parent_node); |
| 455 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); |
| 456 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); |
| 457 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); |
| 458 ASSERT_TRUE(root_view_children); |
| 459 size_t root_children_size = root_view_children->length(); |
| 460 ASSERT_TRUE(root_children_size >= 2); |
| 461 DOM::Node* parent_view_node = root_view_children->get(root_children_size - 2); |
| 462 DOM::Node* target_view_node = root_view_children->get(root_children_size - 1); |
| 463 DOM::Node* child_view_node = parent_view_node->getChildren(nullptr)->get(0); |
| 464 |
| 465 Compare(parent_view, parent_view_node); |
| 466 Compare(target_view, target_view_node); |
| 467 Compare(child_view, child_view_node); |
| 468 parent_view->RemoveChildView(child_view); |
| 469 target_view->AddChildView(child_view); |
| 470 ExpectChildNodeRemoved(parent_view_node->getNodeId(), |
| 471 child_view_node->getNodeId()); |
| 472 ExpectChildNodeInserted(target_view_node->getNodeId(), 0); |
| 473 } |
| 474 |
| 314 } // namespace ash | 475 } // namespace ash |
| OLD | NEW |