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