Chromium Code Reviews| Index: ash/common/devtools/ash_devtools_unittest.cc |
| diff --git a/ash/common/devtools/ash_devtools_unittest.cc b/ash/common/devtools/ash_devtools_unittest.cc |
| index 4bdcb034d756fc6d0cd081d6054616e722f78b93..c586d46d006fda53167c8b1a2c3d4db04c8abf71 100644 |
| --- a/ash/common/devtools/ash_devtools_unittest.cc |
| +++ b/ash/common/devtools/ash_devtools_unittest.cc |
| @@ -14,6 +14,7 @@ namespace ash { |
| namespace { |
| using namespace ui::devtools::protocol; |
| const int kDefaultChildNodeCount = -1; |
| +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.
|
| class TestView : public views::View { |
| public: |
| @@ -27,6 +28,33 @@ class TestView : public views::View { |
| DISALLOW_COPY_AND_ASSIGN(TestView); |
| }; |
| +class FakeFrontendChannel : public FrontendChannel { |
| + public: |
| + // FrontendChannel |
| + 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.
|
| + void flushProtocolNotifications() override{}; |
|
sadrul
2016/11/08 01:58:33
ditto
Sarmad Hashmi
2016/11/08 16:51:47
Done.
|
| + void sendProtocolNotification(const std::string& message) override { |
| + protocol_notification_messages_.push_back(message); |
| + } |
| + |
| + int CountProtocolNotificationMessageStartsWith(const std::string& message) { |
| + int count = 0; |
| + for (const std::string& s : protocol_notification_messages_) { |
| + if (base::StartsWith(s, message, base::CompareCase::SENSITIVE)) |
| + count++; |
| + } |
| + return count; |
| + } |
| + |
| + 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.
|
| + return std::count(protocol_notification_messages_.begin(), |
| + protocol_notification_messages_.end(), message); |
| + } |
| + |
| + private: |
| + std::vector<std::string> protocol_notification_messages_; |
| +}; |
|
sadrul
2016/11/08 01:58:33
DISALLOW_COPY_AND_ASSIGN
Sarmad Hashmi
2016/11/08 16:51:47
Done.
|
| + |
| std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { |
| EXPECT_TRUE(node->hasAttributes()); |
| Array<std::string>* attributes = node->getAttributes(nullptr); |
| @@ -99,18 +127,46 @@ class AshDevToolsTest : public AshTest { |
| void SetUp() override { |
| AshTest::SetUp(); |
| + fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>(); |
| + uber_dispatcher_ = |
| + base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get()); |
| dom_agent_ = |
| base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); |
| + dom_agent_->Init(uber_dispatcher_.get()); |
| } |
| void TearDown() override { |
| dom_agent_.reset(); |
| + uber_dispatcher_.reset(); |
| + fake_frontend_channel_.reset(); |
| AshTest::TearDown(); |
| } |
| + void ExpectChildNodeInserted(int parent_id, int prev_sibling_id) { |
| + EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessageStartsWith( |
| + base::StringPrintf("{\"method\":\"DOM.childNodeInserted\"," |
| + "\"params\":{\"parentNodeId\":%d," |
| + "\"previousNodeId\":%d", |
| + parent_id, prev_sibling_id))); |
| + } |
| + |
| + void ExpectChildNodeRemoved(int parent_id, int node_id) { |
| + EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessage( |
| + base::StringPrintf( |
| + "{\"method\":\"DOM.childNodeRemoved\",\"params\":{" |
| + "\"parentNodeId\":%d,\"nodeId\":%d}}", |
| + parent_id, node_id))); |
| + } |
| + |
| + FakeFrontendChannel* frontend_channel() { |
| + return fake_frontend_channel_.get(); |
| + } |
| + |
| devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } |
| private: |
| + std::unique_ptr<UberDispatcher> uber_dispatcher_; |
| + std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_; |
| std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; |
| DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); |
| @@ -129,14 +185,12 @@ TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { |
| widget->GetRootView()->AddChildView(child_view); |
| std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| - |
| dom_agent()->getDocument(&root); |
| + |
| DOM::Node* parent_node = FindInRoot(parent_window, root.get()); |
| DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); |
| - |
| ASSERT_TRUE(parent_node); |
| ASSERT_TRUE(widget_node); |
| - Array<DOM::Node>* default_children = nullptr; |
| ASSERT_TRUE(parent_node->getChildren(default_children)); |
| Compare(child_window, parent_node->getChildren(default_children)->get(0)); |
| Array<DOM::Node>* widget_children = |
| @@ -146,8 +200,82 @@ TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { |
| ASSERT_TRUE(widget_children->get(0)->getChildren(default_children)); |
| Compare(child_view, |
| widget_children->get(0)->getChildren(default_children)->get(1)); |
| - // TODO(mhashmi): Remove this call and mock FrontendChannel |
| - dom_agent()->disable(); |
| +} |
| + |
| +TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) { |
| + // Initialize DOMAgent |
| + std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| + dom_agent()->getDocument(&root); |
| + |
| + WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| + DOM::Node* parent_node = root->getChildren(default_children)->get(0); |
| + Array<DOM::Node>* parent_node_children = |
| + parent_node->getChildren(default_children); |
| + DOM::Node* sibling_node = |
| + parent_node_children->get(parent_node_children->length() - 1); |
| + |
| + std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); |
| + ExpectChildNodeInserted(parent_node->getNodeId(), sibling_node->getNodeId()); |
| +} |
| + |
| +TEST_F(AshDevToolsTest, WindowDestroyedChildNodeRemoved) { |
| + // Initialize DOMAgent |
| + std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| + dom_agent()->getDocument(&root); |
| + |
| + WmWindow* parent_window = |
| + WmShell::Get()->GetPrimaryRootWindow()->GetChildren()[0]; |
| + WmWindow* child_window = parent_window->GetChildren()[0]; |
| + DOM::Node* root_node = root->getChildren(default_children)->get(0); |
| + DOM::Node* parent_node = root_node->getChildren(default_children)->get(0); |
| + DOM::Node* child_node = parent_node->getChildren(default_children)->get(0); |
| + |
| + child_window->Destroy(); |
| + ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); |
| +} |
| + |
| +TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRemovedAndInserted) { |
| + // Initialize DOMAgent |
| + std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| + dom_agent()->getDocument(&root); |
| + |
| + WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow(); |
| + WmWindow* target_window = root_window->GetChildren()[1]; |
| + WmWindow* child_window = root_window->GetChildren()[0]->GetChildren()[0]; |
| + |
| + DOM::Node* root_node = root->getChildren(default_children)->get(0); |
| + DOM::Node* parent_node = root_node->getChildren(default_children)->get(0); |
| + DOM::Node* target_node = root_node->getChildren(default_children)->get(1); |
| + Array<DOM::Node>* target_node_children = |
| + target_node->getChildren(default_children); |
| + DOM::Node* sibling_node = |
| + target_node_children->get(target_node_children->length() - 1); |
| + DOM::Node* child_node = parent_node->getChildren(default_children)->get(0); |
| + |
| + target_window->AddChild(child_window); |
| + ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId()); |
| + ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId()); |
| +} |
| + |
| +TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) { |
| + // Initialize DOMAgent |
| + std::unique_ptr<ui::devtools::protocol::DOM::Node> root; |
| + dom_agent()->getDocument(&root); |
| + |
| + WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow(); |
| + WmWindow* child_window = parent_window->GetChildren()[0]; |
| + WmWindow* target_window = parent_window->GetChildren()[1]; |
| + |
| + DOM::Node* parent_node = root->getChildren(default_children)->get(0); |
| + Array<DOM::Node>* parent_node_children = |
| + parent_node->getChildren(default_children); |
| + DOM::Node* child_node = parent_node_children->get(0); |
| + DOM::Node* sibling_node = parent_node_children->get(1); |
| + int parent_id = parent_node->getNodeId(); |
| + |
| + parent_window->StackChildAbove(child_window, target_window); |
| + ExpectChildNodeRemoved(parent_id, child_node->getNodeId()); |
| + ExpectChildNodeInserted(parent_id, sibling_node->getNodeId()); |
| } |
| } // namespace ash |