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