| 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" | |
| 10 #include "ash/common/wm_shell.h" | 9 #include "ash/common/wm_shell.h" |
| 11 #include "ash/common/wm_window.h" | 10 #include "ash/common/wm_window.h" |
| 12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 13 #include "ui/views/widget/native_widget_private.h" | |
| 14 #include "ui/views/widget/widget.h" | 12 #include "ui/views/widget/widget.h" |
| 15 | 13 |
| 16 namespace ash { | 14 namespace ash { |
| 17 namespace { | 15 namespace { |
| 18 using namespace ui::devtools::protocol; | 16 using namespace ui::devtools::protocol; |
| 19 const int kDefaultChildNodeCount = -1; | 17 const int kDefaultChildNodeCount = -1; |
| 20 | 18 |
| 21 class TestView : public views::View { | 19 class TestView : public views::View { |
| 22 public: | 20 public: |
| 23 TestView(const char* name) : views::View(), name_(name) {} | 21 TestView(const char* name) : views::View(), name_(name) {} |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 return window_node; | 113 return window_node; |
| 116 } | 114 } |
| 117 | 115 |
| 118 } // namespace | 116 } // namespace |
| 119 | 117 |
| 120 class AshDevToolsTest : public AshTest { | 118 class AshDevToolsTest : public AshTest { |
| 121 public: | 119 public: |
| 122 AshDevToolsTest() {} | 120 AshDevToolsTest() {} |
| 123 ~AshDevToolsTest() override {} | 121 ~AshDevToolsTest() override {} |
| 124 | 122 |
| 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 | |
| 138 void SetUp() override { | 123 void SetUp() override { |
| 139 AshTest::SetUp(); | 124 AshTest::SetUp(); |
| 140 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); | 125 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); |
| 141 uber_dispatcher_ = | 126 uber_dispatcher_ = |
| 142 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); | 127 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); |
| 143 dom_agent_ = | 128 dom_agent_ = |
| 144 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); | 129 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); |
| 145 dom_agent_->Init(uber_dispatcher_.get()); | 130 dom_agent_->Init(uber_dispatcher_.get()); |
| 146 } | 131 } |
| 147 | 132 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 DOM::Node* widget_node = parent_children->get(0); | 189 DOM::Node* widget_node = parent_children->get(0); |
| 205 Compare(widget.get(), widget_node); | 190 Compare(widget.get(), widget_node); |
| 206 Compare(child_window, parent_children->get(1)); | 191 Compare(child_window, parent_children->get(1)); |
| 207 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); | 192 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); |
| 208 ASSERT_TRUE(widget_children); | 193 ASSERT_TRUE(widget_children); |
| 209 Compare(widget->GetRootView(), widget_children->get(0)); | 194 Compare(widget->GetRootView(), widget_children->get(0)); |
| 210 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); | 195 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); |
| 211 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); | 196 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); |
| 212 } | 197 } |
| 213 | 198 |
| 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 | |
| 231 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { | 199 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { |
| 232 // Initialize DOMAgent | 200 // Initialize DOMAgent |
| 233 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 201 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 234 dom_agent()->getDocument(&root); | 202 dom_agent()->getDocument(&root); |
| 235 | 203 |
| 236 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); | 204 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 237 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); | 205 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); |
| 238 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); | 206 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); |
| 239 DOM::Node* sibling_node = | 207 DOM::Node* sibling_node = |
| 240 parent_node_children->get(parent_node_children->length() - 1); | 208 parent_node_children->get(parent_node_children->length() - 1); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 DOM::Node* sibling_node = parent_node_children->get(1); | 304 DOM::Node* sibling_node = parent_node_children->get(1); |
| 337 int parent_id = parent_node->getNodeId(); | 305 int parent_id = parent_node->getNodeId(); |
| 338 | 306 |
| 339 Compare(parent_window, parent_node); | 307 Compare(parent_window, parent_node); |
| 340 Compare(child_window, child_node); | 308 Compare(child_window, child_node); |
| 341 parent_window->StackChildAbove(child_window, target_window); | 309 parent_window->StackChildAbove(child_window, target_window); |
| 342 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); | 310 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); |
| 343 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); | 311 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); |
| 344 } | 312 } |
| 345 | 313 |
| 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 | |
| 475 } // namespace ash | 314 } // namespace ash |
| OLD | NEW |