| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/devtools/ash_devtools_dom_agent.h" | 5 #include "ash/common/devtools/ash_devtools_dom_agent.h" |
| 6 | 6 |
| 7 #include "ash/common/test/ash_test.h" | 7 #include "ash/common/test/ash_test.h" |
| 8 #include "ash/common/wm_lookup.h" | 8 #include "ash/common/wm_lookup.h" |
| 9 #include "ash/common/wm_shell.h" | 9 #include "ash/common/wm_shell.h" |
| 10 #include "ash/common/wm_window.h" | 10 #include "ash/common/wm_window.h" |
| 11 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 12 | 12 |
| 13 namespace ash { | 13 namespace ash { |
| 14 namespace { | 14 namespace { |
| 15 using namespace ui::devtools::protocol; | 15 using namespace ui::devtools::protocol; |
| 16 const int kDefaultChildNodeCount = -1; | 16 const int kDefaultChildNodeCount = -1; |
| 17 | 17 |
| 18 class TestView : public views::View { | 18 class TestView : public views::View { |
| 19 public: | 19 public: |
| 20 TestView(const char* name) : views::View(), name_(name) {} | 20 TestView(const char* name) : views::View(), name_(name) {} |
| 21 | 21 |
| 22 const char* GetClassName() const override { return name_; } | 22 const char* GetClassName() const override { return name_; } |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 const char* name_; | 25 const char* name_; |
| 26 | 26 |
| 27 DISALLOW_COPY_AND_ASSIGN(TestView); | 27 DISALLOW_COPY_AND_ASSIGN(TestView); |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 class FakeFrontendChannel : public FrontendChannel { |
| 31 public: |
| 32 FakeFrontendChannel() {} |
| 33 ~FakeFrontendChannel() override {} |
| 34 |
| 35 int CountProtocolNotificationMessageStartsWith(const std::string& message) { |
| 36 int count = 0; |
| 37 for (const std::string& s : protocol_notification_messages_) { |
| 38 if (base::StartsWith(s, message, base::CompareCase::SENSITIVE)) |
| 39 count++; |
| 40 } |
| 41 return count; |
| 42 } |
| 43 |
| 44 int CountProtocolNotificationMessage(const std::string& message) { |
| 45 return std::count(protocol_notification_messages_.begin(), |
| 46 protocol_notification_messages_.end(), message); |
| 47 } |
| 48 |
| 49 // FrontendChannel |
| 50 void sendProtocolResponse(int callId, const std::string& message) override {} |
| 51 void flushProtocolNotifications() override {} |
| 52 void sendProtocolNotification(const std::string& message) override { |
| 53 protocol_notification_messages_.push_back(message); |
| 54 } |
| 55 |
| 56 private: |
| 57 std::vector<std::string> protocol_notification_messages_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(FakeFrontendChannel); |
| 60 }; |
| 61 |
| 30 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { | 62 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { |
| 31 EXPECT_TRUE(node->hasAttributes()); | 63 EXPECT_TRUE(node->hasAttributes()); |
| 32 Array<std::string>* attributes = node->getAttributes(nullptr); | 64 Array<std::string>* attributes = node->getAttributes(nullptr); |
| 33 for (size_t i = 0; i < attributes->length() - 1; i++) { | 65 for (size_t i = 0; i < attributes->length() - 1; i++) { |
| 34 if (attributes->get(i) == attribute) | 66 if (attributes->get(i) == attribute) |
| 35 return attributes->get(i + 1); | 67 return attributes->get(i + 1); |
| 36 } | 68 } |
| 37 return nullptr; | 69 return nullptr; |
| 38 } | 70 } |
| 39 | 71 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 124 |
| 93 } // namespace | 125 } // namespace |
| 94 | 126 |
| 95 class AshDevToolsTest : public AshTest { | 127 class AshDevToolsTest : public AshTest { |
| 96 public: | 128 public: |
| 97 AshDevToolsTest() {} | 129 AshDevToolsTest() {} |
| 98 ~AshDevToolsTest() override {} | 130 ~AshDevToolsTest() override {} |
| 99 | 131 |
| 100 void SetUp() override { | 132 void SetUp() override { |
| 101 AshTest::SetUp(); | 133 AshTest::SetUp(); |
| 134 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); |
| 135 uber_dispatcher_ = |
| 136 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); |
| 102 dom_agent_ = | 137 dom_agent_ = |
| 103 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); | 138 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); |
| 139 dom_agent_->Init(uber_dispatcher_.get()); |
| 104 } | 140 } |
| 105 | 141 |
| 106 void TearDown() override { | 142 void TearDown() override { |
| 107 dom_agent_.reset(); | 143 dom_agent_.reset(); |
| 144 uber_dispatcher_.reset(); |
| 145 fake_frontend_channel_.reset(); |
| 108 AshTest::TearDown(); | 146 AshTest::TearDown(); |
| 109 } | 147 } |
| 110 | 148 |
| 149 void ExpectChildNodeInserted(int parent_id, int prev_sibling_id) { |
| 150 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessageStartsWith( |
| 151 base::StringPrintf("{\"method\":\"DOM.childNodeInserted\"," |
| 152 "\"params\":{\"parentNodeId\":%d," |
| 153 "\"previousNodeId\":%d", |
| 154 parent_id, prev_sibling_id))); |
| 155 } |
| 156 |
| 157 void ExpectChildNodeRemoved(int parent_id, int node_id) { |
| 158 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessage( |
| 159 base::StringPrintf( |
| 160 "{\"method\":\"DOM.childNodeRemoved\",\"params\":{" |
| 161 "\"parentNodeId\":%d,\"nodeId\":%d}}", |
| 162 parent_id, node_id))); |
| 163 } |
| 164 |
| 165 FakeFrontendChannel* frontend_channel() { |
| 166 return fake_frontend_channel_.get(); |
| 167 } |
| 168 |
| 111 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } | 169 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } |
| 112 | 170 |
| 113 private: | 171 private: |
| 172 std::unique_ptr<UberDispatcher> uber_dispatcher_; |
| 173 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_; |
| 114 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; | 174 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; |
| 115 | 175 |
| 116 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); | 176 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); |
| 117 }; | 177 }; |
| 118 | 178 |
| 119 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { | 179 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { |
| 120 std::unique_ptr<views::Widget> widget( | 180 std::unique_ptr<views::Widget> widget( |
| 121 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); | 181 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); |
| 122 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get()); | 182 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get()); |
| 123 parent_window->SetName("parent_window"); | 183 parent_window->SetName("parent_window"); |
| 124 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); | 184 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); |
| 125 WmWindow* child_window = child_owner->window(); | 185 WmWindow* child_window = child_owner->window(); |
| 126 child_window->SetName("child_window"); | 186 child_window->SetName("child_window"); |
| 127 widget->Show(); | 187 widget->Show(); |
| 128 views::View* child_view = new TestView("child_view"); | 188 views::View* child_view = new TestView("child_view"); |
| 129 widget->GetRootView()->AddChildView(child_view); | 189 widget->GetRootView()->AddChildView(child_view); |
| 130 | 190 |
| 131 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; | 191 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 192 dom_agent()->getDocument(&root); |
| 132 | 193 |
| 133 dom_agent()->getDocument(&root); | |
| 134 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); | 194 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); |
| 135 DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); | 195 DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); |
| 136 | |
| 137 ASSERT_TRUE(parent_node); | 196 ASSERT_TRUE(parent_node); |
| 138 ASSERT_TRUE(widget_node); | 197 ASSERT_TRUE(widget_node); |
| 139 Array<DOM::Node>* default_children = nullptr; | 198 ASSERT_TRUE(parent_node->getChildren(nullptr)); |
| 140 ASSERT_TRUE(parent_node->getChildren(default_children)); | 199 Compare(child_window, parent_node->getChildren(nullptr)->get(0)); |
| 141 Compare(child_window, parent_node->getChildren(default_children)->get(0)); | 200 Array<DOM::Node>* widget_children = widget_node->getChildren(nullptr); |
| 142 Array<DOM::Node>* widget_children = | |
| 143 widget_node->getChildren(default_children); | |
| 144 ASSERT_TRUE(widget_children); | 201 ASSERT_TRUE(widget_children); |
| 145 Compare(widget->GetRootView(), widget_children->get(0)); | 202 Compare(widget->GetRootView(), widget_children->get(0)); |
| 146 ASSERT_TRUE(widget_children->get(0)->getChildren(default_children)); | 203 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr)); |
| 147 Compare(child_view, | 204 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1)); |
| 148 widget_children->get(0)->getChildren(default_children)->get(1)); | 205 } |
| 149 // TODO(mhashmi): Remove this call and mock FrontendChannel | 206 |
| 150 dom_agent()->disable(); | 207 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { |
| 208 // Initialize DOMAgent |
| 209 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 210 dom_agent()->getDocument(&root); |
| 211 |
| 212 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 213 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); |
| 214 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); |
| 215 DOM::Node* sibling_node = |
| 216 parent_node_children->get(parent_node_children->length() - 1); |
| 217 |
| 218 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); |
| 219 ExpectChildNodeInserted(parent_node->getNodeId(), sibling_node->getNodeId()); |
| 220 } |
| 221 |
| 222 TEST_F(AshDevToolsTest, WindowDestroyedChildNodeRemoved) { |
| 223 // Initialize DOMAgent |
| 224 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 225 dom_agent()->getDocument(&root); |
| 226 |
| 227 WmWindow* parent_window = |
| 228 WmShell::Get()->GetPrimaryRootWindow()->GetChildren()[0]; |
| 229 WmWindow* child_window = parent_window->GetChildren()[0]; |
| 230 DOM::Node* root_node = root->getChildren(nullptr)->get(0); |
| 231 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); |
| 232 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); |
| 233 |
| 234 child_window->Destroy(); |
| 235 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); |
| 236 } |
| 237 |
| 238 TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRemovedAndInserted) { |
| 239 // Initialize DOMAgent |
| 240 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 241 dom_agent()->getDocument(&root); |
| 242 |
| 243 WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 244 WmWindow* target_window = root_window->GetChildren()[1]; |
| 245 WmWindow* child_window = root_window->GetChildren()[0]->GetChildren()[0]; |
| 246 |
| 247 DOM::Node* root_node = root->getChildren(nullptr)->get(0); |
| 248 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0); |
| 249 DOM::Node* target_node = root_node->getChildren(nullptr)->get(1); |
| 250 Array<DOM::Node>* target_node_children = target_node->getChildren(nullptr); |
| 251 DOM::Node* sibling_node = |
| 252 target_node_children->get(target_node_children->length() - 1); |
| 253 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0); |
| 254 |
| 255 target_window->AddChild(child_window); |
| 256 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); |
| 257 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId()); |
| 258 } |
| 259 |
| 260 TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) { |
| 261 // Initialize DOMAgent |
| 262 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| 263 dom_agent()->getDocument(&root); |
| 264 |
| 265 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| 266 WmWindow* child_window = parent_window->GetChildren()[0]; |
| 267 WmWindow* target_window = parent_window->GetChildren()[1]; |
| 268 |
| 269 DOM::Node* parent_node = root->getChildren(nullptr)->get(0); |
| 270 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr); |
| 271 DOM::Node* child_node = parent_node_children->get(0); |
| 272 DOM::Node* sibling_node = parent_node_children->get(1); |
| 273 int parent_id = parent_node->getNodeId(); |
| 274 |
| 275 parent_window->StackChildAbove(child_window, target_window); |
| 276 ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); |
| 277 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); |
| 151 } | 278 } |
| 152 | 279 |
| 153 } // namespace ash | 280 } // namespace ash |
| OLD | NEW |