| 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/devtools/ash_devtools_css_agent.h" | |
| 6 #include "ash/devtools/ash_devtools_dom_agent.h" | |
| 7 #include "ash/devtools/ui_element.h" | |
| 8 #include "ash/devtools/view_element.h" | |
| 9 #include "ash/devtools/widget_element.h" | |
| 10 #include "ash/devtools/window_element.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/test/ash_test_base.h" | |
| 13 #include "ash/wm/widget_finder.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "ui/aura/client/window_parenting_client.h" | |
| 17 #include "ui/aura/window_tree_host.h" | |
| 18 #include "ui/display/display.h" | |
| 19 #include "ui/views/background.h" | |
| 20 #include "ui/views/widget/native_widget_private.h" | |
| 21 #include "ui/views/widget/widget.h" | |
| 22 #include "ui/wm/core/coordinate_conversion.h" | |
| 23 | |
| 24 namespace ash { | |
| 25 namespace { | |
| 26 | |
| 27 using namespace ui::devtools::protocol; | |
| 28 | |
| 29 const int kDefaultChildNodeCount = -1; | |
| 30 const SkColor kBackgroundColor = SK_ColorRED; | |
| 31 const SkColor kBorderColor = SK_ColorBLUE; | |
| 32 | |
| 33 class TestView : public views::View { | |
| 34 public: | |
| 35 TestView(const char* name) : views::View(), name_(name) {} | |
| 36 | |
| 37 const char* GetClassName() const override { return name_; } | |
| 38 | |
| 39 private: | |
| 40 const char* name_; | |
| 41 | |
| 42 DISALLOW_COPY_AND_ASSIGN(TestView); | |
| 43 }; | |
| 44 | |
| 45 class FakeFrontendChannel : public FrontendChannel { | |
| 46 public: | |
| 47 FakeFrontendChannel() {} | |
| 48 ~FakeFrontendChannel() override {} | |
| 49 | |
| 50 int CountProtocolNotificationMessageStartsWith(const std::string& message) { | |
| 51 int count = 0; | |
| 52 for (const std::string& s : protocol_notification_messages_) { | |
| 53 if (base::StartsWith(s, message, base::CompareCase::SENSITIVE)) | |
| 54 count++; | |
| 55 } | |
| 56 return count; | |
| 57 } | |
| 58 | |
| 59 int CountProtocolNotificationMessage(const std::string& message) { | |
| 60 return std::count(protocol_notification_messages_.begin(), | |
| 61 protocol_notification_messages_.end(), message); | |
| 62 } | |
| 63 | |
| 64 // FrontendChannel | |
| 65 void sendProtocolResponse(int callId, | |
| 66 std::unique_ptr<Serializable> message) override {} | |
| 67 void flushProtocolNotifications() override {} | |
| 68 void sendProtocolNotification( | |
| 69 std::unique_ptr<Serializable> message) override { | |
| 70 protocol_notification_messages_.push_back(message->serialize()); | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 std::vector<std::string> protocol_notification_messages_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(FakeFrontendChannel); | |
| 77 }; | |
| 78 | |
| 79 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { | |
| 80 EXPECT_TRUE(node->hasAttributes()); | |
| 81 Array<std::string>* attributes = node->getAttributes(nullptr); | |
| 82 for (size_t i = 0; i < attributes->length() - 1; i++) { | |
| 83 if (attributes->get(i) == attribute) | |
| 84 return attributes->get(i + 1); | |
| 85 } | |
| 86 return nullptr; | |
| 87 } | |
| 88 | |
| 89 bool Equals(aura::Window* window, DOM::Node* node) { | |
| 90 int children_count = static_cast<int>(window->children().size()); | |
| 91 if (GetInternalWidgetForWindow(window)) | |
| 92 children_count++; | |
| 93 return "Window" == node->getNodeName() && | |
| 94 window->GetName() == GetAttributeValue("name", node) && | |
| 95 children_count == node->getChildNodeCount(kDefaultChildNodeCount); | |
| 96 } | |
| 97 | |
| 98 void Compare(views::Widget* widget, DOM::Node* node) { | |
| 99 EXPECT_EQ("Widget", node->getNodeName()); | |
| 100 EXPECT_EQ(widget->GetName(), GetAttributeValue("name", node)); | |
| 101 EXPECT_EQ(widget->GetRootView() ? 1 : 0, | |
| 102 node->getChildNodeCount(kDefaultChildNodeCount)); | |
| 103 } | |
| 104 | |
| 105 void Compare(views::View* view, DOM::Node* node) { | |
| 106 EXPECT_EQ("View", node->getNodeName()); | |
| 107 EXPECT_EQ(view->GetClassName(), GetAttributeValue("name", node)); | |
| 108 EXPECT_EQ(view->child_count(), | |
| 109 node->getChildNodeCount(kDefaultChildNodeCount)); | |
| 110 } | |
| 111 | |
| 112 void Compare(aura::Window* window, DOM::Node* node) { | |
| 113 EXPECT_TRUE(Equals(window, node)); | |
| 114 } | |
| 115 | |
| 116 DOM::Node* FindInRoot(aura::Window* window, DOM::Node* root) { | |
| 117 if (Equals(window, root)) | |
| 118 return root; | |
| 119 | |
| 120 Array<DOM::Node>* children = root->getChildren(nullptr); | |
| 121 DOM::Node* window_node = nullptr; | |
| 122 for (size_t i = 0; i < children->length(); i++) { | |
| 123 window_node = FindInRoot(window, children->get(i)); | |
| 124 if (window_node) | |
| 125 return window_node; | |
| 126 } | |
| 127 return window_node; | |
| 128 } | |
| 129 | |
| 130 int GetPropertyByName(const std::string& name, | |
| 131 Array<CSS::CSSProperty>* properties) { | |
| 132 for (size_t i = 0; i < properties->length(); i++) { | |
| 133 CSS::CSSProperty* property = properties->get(i); | |
| 134 if (property->getName() == name) { | |
| 135 int value; | |
| 136 EXPECT_TRUE(base::StringToInt(property->getValue(), &value)); | |
| 137 return value; | |
| 138 } | |
| 139 } | |
| 140 NOTREACHED(); | |
| 141 return -1; | |
| 142 } | |
| 143 | |
| 144 aura::Window* GetHighlightingWindow(aura::Window* root_window) { | |
| 145 const aura::Window::Windows& overlay_windows = root_window->children(); | |
| 146 for (aura::Window* window : overlay_windows) { | |
| 147 if (window->GetName() == "HighlightingWidget") | |
| 148 return window; | |
| 149 } | |
| 150 NOTREACHED(); | |
| 151 return nullptr; | |
| 152 } | |
| 153 | |
| 154 std::unique_ptr<DOM::RGBA> SkColorToRGBA(const SkColor& color) { | |
| 155 return DOM::RGBA::create() | |
| 156 .setA(SkColorGetA(color) / 255) | |
| 157 .setB(SkColorGetB(color)) | |
| 158 .setG(SkColorGetG(color)) | |
| 159 .setR(SkColorGetR(color)) | |
| 160 .build(); | |
| 161 } | |
| 162 | |
| 163 std::unique_ptr<DOM::HighlightConfig> CreateHighlightConfig( | |
| 164 const SkColor& background_color, | |
| 165 const SkColor& border_color) { | |
| 166 return DOM::HighlightConfig::create() | |
| 167 .setContentColor(SkColorToRGBA(background_color)) | |
| 168 .setBorderColor(SkColorToRGBA(border_color)) | |
| 169 .build(); | |
| 170 } | |
| 171 | |
| 172 void ExpectHighlighted(const gfx::Rect& bounds, aura::Window* root_window) { | |
| 173 aura::Window* highlighting_window = GetHighlightingWindow(root_window); | |
| 174 EXPECT_TRUE(highlighting_window->IsVisible()); | |
| 175 EXPECT_EQ(bounds, highlighting_window->GetBoundsInScreen()); | |
| 176 EXPECT_EQ(kBackgroundColor, GetInternalWidgetForWindow(highlighting_window) | |
| 177 ->GetRootView() | |
| 178 ->background() | |
| 179 ->get_color()); | |
| 180 } | |
| 181 | |
| 182 } // namespace | |
| 183 | |
| 184 class AshDevToolsTest : public test::AshTestBase { | |
| 185 public: | |
| 186 AshDevToolsTest() {} | |
| 187 ~AshDevToolsTest() override {} | |
| 188 | |
| 189 views::internal::NativeWidgetPrivate* CreateTestNativeWidget() { | |
| 190 views::Widget* widget = new views::Widget; | |
| 191 views::Widget::InitParams params; | |
| 192 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; | |
| 193 widget->Init(params); | |
| 194 return widget->native_widget_private(); | |
| 195 } | |
| 196 | |
| 197 std::unique_ptr<views::Widget> CreateTestWidget(const gfx::Rect& bounds) { | |
| 198 return AshTestBase::CreateTestWidget(nullptr, kShellWindowId_Invalid, | |
| 199 bounds); | |
| 200 } | |
| 201 | |
| 202 void SetUp() override { | |
| 203 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); | |
| 204 uber_dispatcher_ = | |
| 205 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); | |
| 206 dom_agent_ = base::MakeUnique<devtools::AshDevToolsDOMAgent>(); | |
| 207 dom_agent_->Init(uber_dispatcher_.get()); | |
| 208 css_agent_ = | |
| 209 base::MakeUnique<devtools::AshDevToolsCSSAgent>(dom_agent_.get()); | |
| 210 css_agent_->Init(uber_dispatcher_.get()); | |
| 211 css_agent_->enable(); | |
| 212 // We need to create |dom_agent| first to observe creation of | |
| 213 // WindowTreeHosts in AshTestBase::SetUp(). | |
| 214 AshTestBase::SetUp(); | |
| 215 } | |
| 216 | |
| 217 void TearDown() override { | |
| 218 css_agent_.reset(); | |
| 219 dom_agent_.reset(); | |
| 220 uber_dispatcher_.reset(); | |
| 221 fake_frontend_channel_.reset(); | |
| 222 AshTestBase::TearDown(); | |
| 223 } | |
| 224 | |
| 225 void ExpectChildNodeInserted(int parent_id, int prev_sibling_id) { | |
| 226 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessageStartsWith( | |
| 227 base::StringPrintf("{\"method\":\"DOM.childNodeInserted\"," | |
| 228 "\"params\":{\"parentNodeId\":%d," | |
| 229 "\"previousNodeId\":%d", | |
| 230 parent_id, prev_sibling_id))); | |
| 231 } | |
| 232 | |
| 233 void ExpectChildNodeRemoved(int parent_id, int node_id) { | |
| 234 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessage( | |
| 235 base::StringPrintf( | |
| 236 "{\"method\":\"DOM.childNodeRemoved\",\"params\":{" | |
| 237 "\"parentNodeId\":%d,\"nodeId\":%d}}", | |
| 238 parent_id, node_id))); | |
| 239 } | |
| 240 | |
| 241 int GetStyleSheetChangedCount(int node_id) { | |
| 242 return frontend_channel()->CountProtocolNotificationMessage( | |
| 243 base::StringPrintf("{\"method\":\"CSS.styleSheetChanged\",\"params\":{" | |
| 244 "\"styleSheetId\":\"%d\"}}", | |
| 245 node_id)); | |
| 246 } | |
| 247 | |
| 248 void CompareNodeBounds(DOM::Node* node, const gfx::Rect& bounds) { | |
| 249 Maybe<CSS::CSSStyle> styles; | |
| 250 css_agent_->getMatchedStylesForNode(node->getNodeId(), &styles); | |
| 251 ASSERT_TRUE(styles.isJust()); | |
| 252 Array<CSS::CSSProperty>* properties = styles.fromJust()->getCssProperties(); | |
| 253 EXPECT_EQ(bounds.height(), GetPropertyByName("height", properties)); | |
| 254 EXPECT_EQ(bounds.width(), GetPropertyByName("width", properties)); | |
| 255 EXPECT_EQ(bounds.x(), GetPropertyByName("x", properties)); | |
| 256 EXPECT_EQ(bounds.y(), GetPropertyByName("y", properties)); | |
| 257 } | |
| 258 | |
| 259 void SetStyleTexts(DOM::Node* node, | |
| 260 const std::string& style_text, | |
| 261 bool success) { | |
| 262 auto edits = Array<CSS::StyleDeclarationEdit>::create(); | |
| 263 auto edit = CSS::StyleDeclarationEdit::create() | |
| 264 .setStyleSheetId(base::IntToString(node->getNodeId())) | |
| 265 .setText(style_text) | |
| 266 .build(); | |
| 267 edits->addItem(std::move(edit)); | |
| 268 std::unique_ptr<Array<CSS::CSSStyle>> output; | |
| 269 EXPECT_EQ(success, | |
| 270 css_agent_->setStyleTexts(std::move(edits), &output).isSuccess()); | |
| 271 | |
| 272 if (success) | |
| 273 ASSERT_TRUE(output); | |
| 274 else | |
| 275 ASSERT_FALSE(output); | |
| 276 } | |
| 277 | |
| 278 void HighlightNode(int node_id) { | |
| 279 dom_agent_->highlightNode( | |
| 280 CreateHighlightConfig(kBackgroundColor, kBorderColor), node_id); | |
| 281 } | |
| 282 | |
| 283 void HideHighlight(int root_window_index) { | |
| 284 dom_agent_->hideHighlight(); | |
| 285 DCHECK_GE(root_window_index, 0); | |
| 286 DCHECK_LE(root_window_index, | |
| 287 static_cast<int>(dom_agent()->root_windows().size())); | |
| 288 ASSERT_FALSE( | |
| 289 GetHighlightingWindow(dom_agent()->root_windows()[root_window_index]) | |
| 290 ->IsVisible()); | |
| 291 } | |
| 292 | |
| 293 FakeFrontendChannel* frontend_channel() { | |
| 294 return fake_frontend_channel_.get(); | |
| 295 } | |
| 296 | |
| 297 aura::Window* GetPrimaryRootWindow() { | |
| 298 DCHECK(dom_agent()->root_windows().size()); | |
| 299 return dom_agent()->root_windows()[0]; | |
| 300 } | |
| 301 | |
| 302 devtools::AshDevToolsCSSAgent* css_agent() { return css_agent_.get(); } | |
| 303 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } | |
| 304 | |
| 305 private: | |
| 306 std::unique_ptr<UberDispatcher> uber_dispatcher_; | |
| 307 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_; | |
| 308 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; | |
| 309 std::unique_ptr<devtools::AshDevToolsCSSAgent> css_agent_; | |
| 310 | |
| 311 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); | |
| 312 }; | |
| 313 | |
| 314 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { | |
| 315 std::unique_ptr<views::Widget> widget( | |
| 316 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 317 aura::Window* parent_window = widget->GetNativeWindow(); | |
| 318 parent_window->SetName("parent_window"); | |
| 319 std::unique_ptr<aura::Window> child_window = CreateChildWindow(parent_window); | |
| 320 child_window->SetName("child_window"); | |
| 321 widget->Show(); | |
| 322 views::View* child_view = new TestView("child_view"); | |
| 323 widget->GetRootView()->AddChildView(child_view); | |
| 324 | |
| 325 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 326 dom_agent()->getDocument(&root); | |
| 327 | |
| 328 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | |
| 329 ASSERT_TRUE(parent_node); | |
| 330 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); | |
| 331 ASSERT_TRUE(parent_children); | |
| 332 DOM::Node* widget_node = parent_children->get(0); | |
| 333 Compare(widget.get(), widget_node); | |
| 334 Compare(child_window.get(), parent_children->get(1)); | |
| 335 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); | |
| 336 ASSERT_TRUE(widget_children); | |
| 337 Compare(widget->GetRootView(), widget_children->get(0)); | |
| 338 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); | |
| 339 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); | |
| 340 } | |
| 341 | |
| 342 TEST_F(AshDevToolsTest, GetDocumentNativeWidgetOwnsWidget) { | |
| 343 views::internal::NativeWidgetPrivate* native_widget_private = | |
| 344 CreateTestNativeWidget(); | |
| 345 views::Widget* widget = native_widget_private->GetWidget(); | |
| 346 aura::Window* parent_window = widget->GetNativeWindow(); | |
| 347 | |
| 348 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 349 dom_agent()->getDocument(&root); | |
| 350 | |
| 351 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | |
| 352 ASSERT_TRUE(parent_node); | |
| 353 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); | |
| 354 Compare(widget, widget_node); | |
| 355 // Destroy NativeWidget followed by |widget| | |
| 356 widget->CloseNow(); | |
| 357 } | |
| 358 | |
| 359 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { | |
| 360 // Initialize DOMAgent | |
| 361 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 362 dom_agent()->getDocument(&root); | |
| 363 | |
| 364 aura::Window* root_window = GetPrimaryRootWindow(); | |
| 365 aura::Window* parent_window = root_window->children()[0]; | |
| 366 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | |
| 367 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); | |
| 368 DOM::Node* sibling_node = | |
| 369 parent_node_children->get(parent_node_children->length() - 1); | |
| 370 | |
| 371 std::unique_ptr<aura::Window> child(CreateChildWindow(parent_window)); | |
| 372 ExpectChildNodeInserted(parent_node->getNodeId(), sibling_node->getNodeId()); | |
| 373 } | |
| 374 | |
| 375 TEST_F(AshDevToolsTest, WindowDestroyedChildNodeRemoved) { | |
| 376 // Initialize DOMAgent | |
| 377 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 378 dom_agent()->getDocument(&root); | |
| 379 | |
| 380 aura::Window* root_window = GetPrimaryRootWindow(); | |
| 381 aura::Window* rotation_window = root_window->children()[0]; | |
| 382 aura::Window* parent_window = rotation_window->children()[0]; | |
| 383 aura::Window* child_window = parent_window->children()[0]; | |
| 384 DOM::Node* root_node = | |
| 385 root->getChildren(nullptr)->get(0)->getChildren(nullptr)->get(0); | |
| 386 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); | |
| 387 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); | |
| 388 | |
| 389 Compare(parent_window, parent_node); | |
| 390 Compare(child_window, child_node); | |
| 391 delete child_window; | |
| 392 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); | |
| 393 } | |
| 394 | |
| 395 TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRearranged) { | |
| 396 // Initialize DOMAgent | |
| 397 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 398 dom_agent()->getDocument(&root); | |
| 399 | |
| 400 aura::Window* root_window = GetPrimaryRootWindow(); | |
| 401 aura::Window* rotation_window = root_window->children()[0]; | |
| 402 aura::Window* parent_window = rotation_window->children()[0]; | |
| 403 aura::Window* target_window = rotation_window->children()[1]; | |
| 404 aura::Window* child_window = parent_window->children()[0]; | |
| 405 | |
| 406 DOM::Node* root_node = | |
| 407 root->getChildren(nullptr)->get(0)->getChildren(nullptr)->get(0); | |
| 408 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); | |
| 409 DOM::Node* target_node = root_node->getChildren(nullptr)->get(1); | |
| 410 Array<DOM::Node>* target_node_children = target_node->getChildren(nullptr); | |
| 411 DOM::Node* sibling_node = | |
| 412 target_node_children->get(target_node_children->length() - 1); | |
| 413 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); | |
| 414 | |
| 415 Compare(parent_window, parent_node); | |
| 416 Compare(target_window, target_node); | |
| 417 Compare(child_window, child_node); | |
| 418 target_window->AddChild(child_window); | |
| 419 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); | |
| 420 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId()); | |
| 421 } | |
| 422 | |
| 423 TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRemovedAndInserted) { | |
| 424 aura::Window* root_window = GetPrimaryRootWindow(); | |
| 425 aura::Window* rotation_window = root_window->children()[0]; | |
| 426 aura::Window* parent_window = rotation_window->children()[0]; | |
| 427 aura::Window* target_window = rotation_window->children()[1]; | |
| 428 std::unique_ptr<aura::Window> child_window(CreateChildWindow(parent_window)); | |
| 429 | |
| 430 // Initialize DOMAgent | |
| 431 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 432 dom_agent()->getDocument(&root); | |
| 433 DOM::Node* root_node = | |
| 434 root->getChildren(nullptr)->get(0)->getChildren(nullptr)->get(0); | |
| 435 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); | |
| 436 DOM::Node* target_node = root_node->getChildren(nullptr)->get(1); | |
| 437 Array<DOM::Node>* target_node_children = target_node->getChildren(nullptr); | |
| 438 DOM::Node* sibling_node = | |
| 439 target_node_children->get(target_node_children->length() - 1); | |
| 440 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); | |
| 441 DOM::Node* child_node = | |
| 442 parent_node_children->get(parent_node_children->length() - 1); | |
| 443 | |
| 444 Compare(parent_window, parent_node); | |
| 445 Compare(target_window, target_node); | |
| 446 Compare(child_window.get(), child_node); | |
| 447 parent_window->RemoveChild(child_window.get()); | |
| 448 target_window->AddChild(child_window.get()); | |
| 449 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); | |
| 450 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId()); | |
| 451 } | |
| 452 | |
| 453 TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) { | |
| 454 // Initialize DOMAgent | |
| 455 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 456 dom_agent()->getDocument(&root); | |
| 457 | |
| 458 aura::Window* root_window = GetPrimaryRootWindow(); | |
| 459 aura::Window* parent_window = root_window->children()[0]; | |
| 460 aura::Window* child_window = parent_window->children()[0]; | |
| 461 aura::Window* target_window = parent_window->children()[1]; | |
| 462 | |
| 463 DOM::Node* parent_node = | |
| 464 root->getChildren(nullptr)->get(0)->getChildren(nullptr)->get(0); | |
| 465 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); | |
| 466 DOM::Node* child_node = parent_node_children->get(0); | |
| 467 DOM::Node* sibling_node = parent_node_children->get(1); | |
| 468 int parent_id = parent_node->getNodeId(); | |
| 469 | |
| 470 Compare(parent_window, parent_node); | |
| 471 Compare(child_window, child_node); | |
| 472 parent_window->StackChildAbove(child_window, target_window); | |
| 473 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); | |
| 474 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); | |
| 475 } | |
| 476 | |
| 477 TEST_F(AshDevToolsTest, ViewInserted) { | |
| 478 std::unique_ptr<views::Widget> widget( | |
| 479 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 480 aura::Window* window = widget->GetNativeWindow(); | |
| 481 widget->Show(); | |
| 482 | |
| 483 // Initialize DOMAgent | |
| 484 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 485 dom_agent()->getDocument(&root); | |
| 486 | |
| 487 DOM::Node* parent_node = FindInRoot(window, root.get()); | |
| 488 ASSERT_TRUE(parent_node); | |
| 489 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); | |
| 490 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | |
| 491 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); | |
| 492 ASSERT_TRUE(root_view_children); | |
| 493 DOM::Node* sibling_view_node = | |
| 494 root_view_children->get(root_view_children->length() - 1); | |
| 495 | |
| 496 widget->GetRootView()->AddChildView(new views::View); | |
| 497 ExpectChildNodeInserted(root_view_node->getNodeId(), | |
| 498 sibling_view_node->getNodeId()); | |
| 499 } | |
| 500 | |
| 501 TEST_F(AshDevToolsTest, ViewRemoved) { | |
| 502 std::unique_ptr<views::Widget> widget( | |
| 503 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 504 // Need to store |view| in unique_ptr because it is removed from the widget | |
| 505 // and needs to be destroyed independently | |
| 506 std::unique_ptr<views::View> child_view = base::MakeUnique<views::View>(); | |
| 507 aura::Window* window = widget->GetNativeWindow(); | |
| 508 widget->Show(); | |
| 509 views::View* root_view = widget->GetRootView(); | |
| 510 root_view->AddChildView(child_view.get()); | |
| 511 | |
| 512 // Initialize DOMAgent | |
| 513 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 514 dom_agent()->getDocument(&root); | |
| 515 | |
| 516 DOM::Node* parent_node = FindInRoot(window, root.get()); | |
| 517 ASSERT_TRUE(parent_node); | |
| 518 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); | |
| 519 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | |
| 520 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); | |
| 521 ASSERT_TRUE(root_view_children); | |
| 522 DOM::Node* child_view_node = | |
| 523 root_view_children->get(root_view_children->length() - 1); | |
| 524 | |
| 525 Compare(child_view.get(), child_view_node); | |
| 526 root_view->RemoveChildView(child_view.get()); | |
| 527 ExpectChildNodeRemoved(root_view_node->getNodeId(), | |
| 528 child_view_node->getNodeId()); | |
| 529 } | |
| 530 | |
| 531 TEST_F(AshDevToolsTest, ViewRearranged) { | |
| 532 std::unique_ptr<views::Widget> widget( | |
| 533 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 534 aura::Window* window = widget->GetNativeWindow(); | |
| 535 widget->Show(); | |
| 536 views::View* root_view = widget->GetRootView(); | |
| 537 views::View* parent_view = new views::View; | |
| 538 views::View* target_view = new views::View; | |
| 539 views::View* child_view = new views::View; | |
| 540 views::View* child_view_1 = new views::View; | |
| 541 | |
| 542 root_view->AddChildView(parent_view); | |
| 543 root_view->AddChildView(target_view); | |
| 544 parent_view->AddChildView(child_view); | |
| 545 parent_view->AddChildView(child_view_1); | |
| 546 | |
| 547 // Initialize DOMAgent | |
| 548 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 549 dom_agent()->getDocument(&root); | |
| 550 | |
| 551 DOM::Node* parent_node = FindInRoot(window, root.get()); | |
| 552 ASSERT_TRUE(parent_node); | |
| 553 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); | |
| 554 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | |
| 555 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); | |
| 556 ASSERT_TRUE(root_view_children); | |
| 557 size_t root_children_size = root_view_children->length(); | |
| 558 ASSERT_TRUE(root_children_size >= 2); | |
| 559 DOM::Node* parent_view_node = root_view_children->get(root_children_size - 2); | |
| 560 DOM::Node* target_view_node = root_view_children->get(root_children_size - 1); | |
| 561 DOM::Node* child_view_node = parent_view_node->getChildren(nullptr)->get(0); | |
| 562 DOM::Node* child_view_node_1 = parent_view_node->getChildren(nullptr)->get(1); | |
| 563 | |
| 564 Compare(parent_view, parent_view_node); | |
| 565 Compare(target_view, target_view_node); | |
| 566 Compare(child_view, child_view_node); | |
| 567 Compare(child_view_1, child_view_node_1); | |
| 568 | |
| 569 ASSERT_NE(child_view_node->getNodeId(), child_view_node_1->getNodeId()); | |
| 570 | |
| 571 // Reorder child_view_1 from index 1 to 0 in view::Views tree. This makes DOM | |
| 572 // tree remove view node at position 1 and insert it at position 0. | |
| 573 parent_view->ReorderChildView(child_view_1, 0); | |
| 574 ExpectChildNodeRemoved(parent_view_node->getNodeId(), | |
| 575 child_view_node_1->getNodeId()); | |
| 576 ExpectChildNodeInserted(parent_view_node->getNodeId(), 0); | |
| 577 | |
| 578 target_view->AddChildView(child_view); | |
| 579 ExpectChildNodeRemoved(parent_view_node->getNodeId(), | |
| 580 child_view_node->getNodeId()); | |
| 581 ExpectChildNodeInserted(target_view_node->getNodeId(), 0); | |
| 582 } | |
| 583 | |
| 584 TEST_F(AshDevToolsTest, ViewRearrangedRemovedAndInserted) { | |
| 585 std::unique_ptr<views::Widget> widget( | |
| 586 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 587 aura::Window* window = widget->GetNativeWindow(); | |
| 588 widget->Show(); | |
| 589 views::View* root_view = widget->GetRootView(); | |
| 590 views::View* parent_view = new views::View; | |
| 591 views::View* target_view = new views::View; | |
| 592 views::View* child_view = new views::View; | |
| 593 root_view->AddChildView(parent_view); | |
| 594 root_view->AddChildView(target_view); | |
| 595 parent_view->AddChildView(child_view); | |
| 596 | |
| 597 // Initialize DOMAgent | |
| 598 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 599 dom_agent()->getDocument(&root); | |
| 600 | |
| 601 DOM::Node* parent_node = FindInRoot(window, root.get()); | |
| 602 ASSERT_TRUE(parent_node); | |
| 603 DOM::Node* widget_node = parent_node->getChildren(nullptr)->get(0); | |
| 604 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | |
| 605 Array<DOM::Node>* root_view_children = root_view_node->getChildren(nullptr); | |
| 606 ASSERT_TRUE(root_view_children); | |
| 607 size_t root_children_size = root_view_children->length(); | |
| 608 ASSERT_TRUE(root_children_size >= 2); | |
| 609 DOM::Node* parent_view_node = root_view_children->get(root_children_size - 2); | |
| 610 DOM::Node* target_view_node = root_view_children->get(root_children_size - 1); | |
| 611 DOM::Node* child_view_node = parent_view_node->getChildren(nullptr)->get(0); | |
| 612 | |
| 613 Compare(parent_view, parent_view_node); | |
| 614 Compare(target_view, target_view_node); | |
| 615 Compare(child_view, child_view_node); | |
| 616 parent_view->RemoveChildView(child_view); | |
| 617 target_view->AddChildView(child_view); | |
| 618 ExpectChildNodeRemoved(parent_view_node->getNodeId(), | |
| 619 child_view_node->getNodeId()); | |
| 620 ExpectChildNodeInserted(target_view_node->getNodeId(), 0); | |
| 621 } | |
| 622 | |
| 623 TEST_F(AshDevToolsTest, WindowWidgetViewHighlight) { | |
| 624 std::unique_ptr<views::Widget> widget( | |
| 625 CreateTestWidget(gfx::Rect(0, 0, 400, 400))); | |
| 626 aura::Window* parent_window = widget->GetNativeWindow(); | |
| 627 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window)); | |
| 628 views::View* root_view = widget->GetRootView(); | |
| 629 | |
| 630 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 631 dom_agent()->getDocument(&root); | |
| 632 | |
| 633 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | |
| 634 ASSERT_TRUE(parent_node); | |
| 635 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); | |
| 636 ASSERT_TRUE(parent_children); | |
| 637 DOM::Node* window_node = parent_children->get(1); | |
| 638 DOM::Node* widget_node = parent_children->get(0); | |
| 639 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | |
| 640 | |
| 641 HighlightNode(window_node->getNodeId()); | |
| 642 ExpectHighlighted(window->GetBoundsInScreen(), GetPrimaryRootWindow()); | |
| 643 devtools::UIElement* element = | |
| 644 dom_agent()->GetElementFromNodeId(window_node->getNodeId()); | |
| 645 ASSERT_EQ(devtools::UIElementType::WINDOW, element->type()); | |
| 646 EXPECT_EQ(element->GetNodeWindowAndBounds().first, window.get()); | |
| 647 EXPECT_EQ(element->GetNodeWindowAndBounds().second, | |
| 648 window->GetBoundsInScreen()); | |
| 649 | |
| 650 HideHighlight(0); | |
| 651 | |
| 652 HighlightNode(widget_node->getNodeId()); | |
| 653 ExpectHighlighted(widget->GetWindowBoundsInScreen(), GetPrimaryRootWindow()); | |
| 654 | |
| 655 element = dom_agent()->GetElementFromNodeId(widget_node->getNodeId()); | |
| 656 ASSERT_EQ(devtools::UIElementType::WIDGET, element->type()); | |
| 657 EXPECT_EQ(element->GetNodeWindowAndBounds().first, widget->GetNativeWindow()); | |
| 658 EXPECT_EQ(element->GetNodeWindowAndBounds().second, | |
| 659 widget->GetWindowBoundsInScreen()); | |
| 660 | |
| 661 HideHighlight(0); | |
| 662 | |
| 663 HighlightNode(root_view_node->getNodeId()); | |
| 664 ExpectHighlighted(root_view->GetBoundsInScreen(), GetPrimaryRootWindow()); | |
| 665 | |
| 666 element = dom_agent()->GetElementFromNodeId(root_view_node->getNodeId()); | |
| 667 ASSERT_EQ(devtools::UIElementType::VIEW, element->type()); | |
| 668 EXPECT_EQ(element->GetNodeWindowAndBounds().first, | |
| 669 root_view->GetWidget()->GetNativeWindow()); | |
| 670 EXPECT_EQ(element->GetNodeWindowAndBounds().second, | |
| 671 root_view->GetBoundsInScreen()); | |
| 672 | |
| 673 HideHighlight(0); | |
| 674 | |
| 675 // Highlight non-existent node | |
| 676 HighlightNode(10000); | |
| 677 EXPECT_FALSE(GetHighlightingWindow(GetPrimaryRootWindow())->IsVisible()); | |
| 678 } | |
| 679 | |
| 680 int GetNodeIdFromWindow(devtools::UIElement* ui_element, aura::Window* window) { | |
| 681 for (auto* child : ui_element->children()) { | |
| 682 if (child->type() == devtools::UIElementType::WINDOW && | |
| 683 static_cast<devtools::WindowElement*>(child)->window() == window) { | |
| 684 return child->node_id(); | |
| 685 } | |
| 686 } | |
| 687 for (auto* child : ui_element->children()) { | |
| 688 if (child->type() == devtools::UIElementType::WINDOW) { | |
| 689 int node_id = GetNodeIdFromWindow(child, window); | |
| 690 if (node_id > 0) | |
| 691 return node_id; | |
| 692 } | |
| 693 } | |
| 694 return 0; | |
| 695 } | |
| 696 | |
| 697 // TODO(thanhph): Make test AshDevToolsTest.MultipleDisplayHighlight work with | |
| 698 // multiple displays. https://crbug.com/726831. | |
| 699 | |
| 700 TEST_F(AshDevToolsTest, WindowWidgetViewGetMatchedStylesForNode) { | |
| 701 std::unique_ptr<views::Widget> widget( | |
| 702 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 703 aura::Window* parent_window = widget->GetNativeWindow(); | |
| 704 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window)); | |
| 705 gfx::Rect window_bounds(2, 2, 3, 3); | |
| 706 gfx::Rect widget_bounds(50, 50, 100, 75); | |
| 707 gfx::Rect view_bounds(4, 4, 3, 3); | |
| 708 window->SetBounds(window_bounds); | |
| 709 widget->SetBounds(widget_bounds); | |
| 710 widget->GetRootView()->SetBoundsRect(view_bounds); | |
| 711 | |
| 712 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 713 dom_agent()->getDocument(&root); | |
| 714 | |
| 715 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | |
| 716 ASSERT_TRUE(parent_node); | |
| 717 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); | |
| 718 ASSERT_TRUE(parent_children); | |
| 719 | |
| 720 CompareNodeBounds(parent_node, widget_bounds); | |
| 721 CompareNodeBounds(parent_children->get(1), window_bounds); | |
| 722 CompareNodeBounds(parent_children->get(0)->getChildren(nullptr)->get(0), | |
| 723 view_bounds); | |
| 724 } | |
| 725 | |
| 726 TEST_F(AshDevToolsTest, WindowWidgetViewStyleSheetChanged) { | |
| 727 std::unique_ptr<views::Widget> widget( | |
| 728 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | |
| 729 aura::Window* widget_window = widget->GetNativeWindow(); | |
| 730 std::unique_ptr<aura::Window> child(CreateChildWindow(widget_window)); | |
| 731 | |
| 732 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 733 dom_agent()->getDocument(&root); | |
| 734 | |
| 735 gfx::Rect child_bounds(2, 2, 3, 3); | |
| 736 gfx::Rect widget_bounds(10, 10, 150, 160); | |
| 737 gfx::Rect view_bounds(4, 4, 3, 3); | |
| 738 child->SetBounds(child_bounds); | |
| 739 widget->SetBounds(widget_bounds); | |
| 740 widget->GetRootView()->SetBoundsRect(view_bounds); | |
| 741 | |
| 742 DOM::Node* widget_node = FindInRoot(widget_window, root.get()); | |
| 743 ASSERT_TRUE(widget_node); | |
| 744 Array<DOM::Node>* widget_node_children = widget_node->getChildren(nullptr); | |
| 745 ASSERT_TRUE(widget_node_children); | |
| 746 | |
| 747 EXPECT_EQ(1, GetStyleSheetChangedCount(widget_node->getNodeId())); | |
| 748 EXPECT_EQ( | |
| 749 1, GetStyleSheetChangedCount(widget_node_children->get(1)->getNodeId())); | |
| 750 EXPECT_EQ(2, | |
| 751 GetStyleSheetChangedCount(widget_node_children->get(0) | |
| 752 ->getChildren(nullptr) | |
| 753 ->get(0) | |
| 754 ->getNodeId())); | |
| 755 } | |
| 756 | |
| 757 TEST_F(AshDevToolsTest, WindowWidgetViewSetStyleText) { | |
| 758 std::unique_ptr<views::Widget> widget( | |
| 759 CreateTestWidget(gfx::Rect(0, 0, 400, 400))); | |
| 760 aura::Window* parent_window = widget->GetNativeWindow(); | |
| 761 std::unique_ptr<aura::Window> window(CreateChildWindow(parent_window)); | |
| 762 views::View* root_view = widget->GetRootView(); | |
| 763 | |
| 764 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | |
| 765 dom_agent()->getDocument(&root); | |
| 766 | |
| 767 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | |
| 768 ASSERT_TRUE(parent_node); | |
| 769 Array<DOM::Node>* parent_children = parent_node->getChildren(nullptr); | |
| 770 ASSERT_TRUE(parent_children); | |
| 771 | |
| 772 // Test different combinations on window node | |
| 773 DOM::Node* window_node = parent_children->get(1); | |
| 774 | |
| 775 SetStyleTexts(window_node, | |
| 776 "x: 25; y:35; width: 5; height: 20; visibility: 1;", true); | |
| 777 EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->bounds()); | |
| 778 EXPECT_TRUE(window->IsVisible()); | |
| 779 | |
| 780 SetStyleTexts(window_node, "test_nothing_happens:1;", false); | |
| 781 EXPECT_EQ(gfx::Rect(25, 35, 5, 20), window->bounds()); // Not changed | |
| 782 | |
| 783 SetStyleTexts(window_node, "\nheight: 10;\nvisibility: 0;\n", true); | |
| 784 EXPECT_EQ(gfx::Rect(25, 35, 5, 10), window->bounds()); | |
| 785 EXPECT_FALSE(window->IsVisible()); | |
| 786 | |
| 787 SetStyleTexts(window_node, "\nx: 10; y: 23; width: 52;\n ", true); | |
| 788 EXPECT_EQ(gfx::Rect(10, 23, 52, 10), window->bounds()); | |
| 789 | |
| 790 // Test different combinations on widget node | |
| 791 DOM::Node* widget_node = parent_children->get(0); | |
| 792 | |
| 793 SetStyleTexts(widget_node, | |
| 794 "x: 25; y:35; width: 53; height: 64; visibility: 0;", true); | |
| 795 EXPECT_EQ(gfx::Rect(25, 35, 53, 64), widget->GetRestoredBounds()); | |
| 796 EXPECT_FALSE(widget->IsVisible()); | |
| 797 | |
| 798 SetStyleTexts(widget_node, "test_nothing_happens:1;", false); | |
| 799 EXPECT_EQ(gfx::Rect(25, 35, 53, 64), | |
| 800 widget->GetRestoredBounds()); // Not changed | |
| 801 | |
| 802 SetStyleTexts(widget_node, "\nheight: 123;\nvisibility: 1;\n", true); | |
| 803 EXPECT_EQ(gfx::Rect(25, 35, 53, 123), widget->GetRestoredBounds()); | |
| 804 EXPECT_TRUE(widget->IsVisible()); | |
| 805 | |
| 806 SetStyleTexts(widget_node, "\nx: 10; y: 23; width: 98;\n ", true); | |
| 807 EXPECT_EQ(gfx::Rect(10, 23, 98, 123), widget->GetRestoredBounds()); | |
| 808 | |
| 809 // Test different combinations on view node | |
| 810 DOM::Node* root_view_node = widget_node->getChildren(nullptr)->get(0); | |
| 811 | |
| 812 SetStyleTexts(root_view_node, | |
| 813 "x: 25; y:35; width: 45; height: 20; visibility: 0;", true); | |
| 814 EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds()); | |
| 815 EXPECT_FALSE(root_view->visible()); | |
| 816 | |
| 817 SetStyleTexts(root_view_node, "test_nothing_happens:1;", false); | |
| 818 EXPECT_EQ(gfx::Rect(25, 35, 45, 20), root_view->bounds()); // Not changed | |
| 819 | |
| 820 SetStyleTexts(root_view_node, "\nheight: 73;\n ", true); | |
| 821 EXPECT_EQ(gfx::Rect(25, 35, 45, 73), root_view->bounds()); | |
| 822 | |
| 823 SetStyleTexts(root_view_node, "\nx: 10; y: 23; width: 52;\nvisibility: 1;\n", | |
| 824 true); | |
| 825 EXPECT_EQ(gfx::Rect(10, 23, 52, 73), root_view->bounds()); | |
| 826 EXPECT_TRUE(root_view->visible()); | |
| 827 } | |
| 828 | |
| 829 } // namespace ash | |
| OLD | NEW |