Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: ash/common/devtools/ash_devtools_unittest.cc

Issue 2480683003: Add tests for ash devtools window updates (Closed)
Patch Set: sadruls comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
34 int CountProtocolNotificationMessageStartsWith(const std::string& message) {
35 int count = 0;
36 for (const std::string& s : protocol_notification_messages_) {
37 if (base::StartsWith(s, message, base::CompareCase::SENSITIVE))
38 count++;
39 }
40 return count;
41 }
42
43 int CountProtocolNotificationMessage(const std::string& message) {
44 return std::count(protocol_notification_messages_.begin(),
45 protocol_notification_messages_.end(), message);
46 }
47
48 // FrontendChannel
49 void sendProtocolResponse(int callId, const std::string& message) override {}
50 void flushProtocolNotifications() override {}
51 void sendProtocolNotification(const std::string& message) override {
52 protocol_notification_messages_.push_back(message);
53 }
54
55 private:
56 std::vector<std::string> protocol_notification_messages_;
57
58 DISALLOW_COPY_AND_ASSIGN(FakeFrontendChannel);
59 };
60
30 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) { 61 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) {
31 EXPECT_TRUE(node->hasAttributes()); 62 EXPECT_TRUE(node->hasAttributes());
32 Array<std::string>* attributes = node->getAttributes(nullptr); 63 Array<std::string>* attributes = node->getAttributes(nullptr);
33 for (size_t i = 0; i < attributes->length() - 1; i++) { 64 for (size_t i = 0; i < attributes->length() - 1; i++) {
34 if (attributes->get(i) == attribute) 65 if (attributes->get(i) == attribute)
35 return attributes->get(i + 1); 66 return attributes->get(i + 1);
36 } 67 }
37 return nullptr; 68 return nullptr;
38 } 69 }
39 70
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 if (widget_node) 118 if (widget_node)
88 return widget_node; 119 return widget_node;
89 } 120 }
90 return widget_node; 121 return widget_node;
91 } 122 }
92 123
93 } // namespace 124 } // namespace
94 125
95 class AshDevToolsTest : public AshTest { 126 class AshDevToolsTest : public AshTest {
96 public: 127 public:
128 Array<DOM::Node>* default_children = nullptr;
129
97 AshDevToolsTest() {} 130 AshDevToolsTest() {}
98 ~AshDevToolsTest() override {} 131 ~AshDevToolsTest() override {}
99 132
100 void SetUp() override { 133 void SetUp() override {
101 AshTest::SetUp(); 134 AshTest::SetUp();
135 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>();
136 uber_dispatcher_ =
137 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get());
102 dom_agent_ = 138 dom_agent_ =
103 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); 139 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get());
140 dom_agent_->Init(uber_dispatcher_.get());
104 } 141 }
105 142
106 void TearDown() override { 143 void TearDown() override {
107 dom_agent_.reset(); 144 dom_agent_.reset();
145 uber_dispatcher_.reset();
146 fake_frontend_channel_.reset();
108 AshTest::TearDown(); 147 AshTest::TearDown();
109 } 148 }
110 149
150 void ExpectChildNodeInserted(int parent_id, int prev_sibling_id) {
151 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessageStartsWith(
152 base::StringPrintf("{\"method\":\"DOM.childNodeInserted\","
153 "\"params\":{\"parentNodeId\":%d,"
154 "\"previousNodeId\":%d",
155 parent_id, prev_sibling_id)));
156 }
157
158 void ExpectChildNodeRemoved(int parent_id, int node_id) {
159 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessage(
160 base::StringPrintf(
161 "{\"method\":\"DOM.childNodeRemoved\",\"params\":{"
162 "\"parentNodeId\":%d,\"nodeId\":%d}}",
163 parent_id, node_id)));
164 }
165
166 FakeFrontendChannel* frontend_channel() {
167 return fake_frontend_channel_.get();
168 }
169
111 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } 170 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); }
112 171
113 private: 172 private:
173 std::unique_ptr<UberDispatcher> uber_dispatcher_;
174 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_;
114 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; 175 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_;
115 176
116 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); 177 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest);
117 }; 178 };
118 179
119 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { 180 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) {
120 std::unique_ptr<views::Widget> widget( 181 std::unique_ptr<views::Widget> widget(
121 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); 182 CreateTestWidget(gfx::Rect(1, 1, 1, 1)));
122 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get()); 183 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
123 parent_window->SetName("parent_window"); 184 parent_window->SetName("parent_window");
124 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); 185 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
125 WmWindow* child_window = child_owner->window(); 186 WmWindow* child_window = child_owner->window();
126 child_window->SetName("child_window"); 187 child_window->SetName("child_window");
127 widget->Show(); 188 widget->Show();
128 views::View* child_view = new TestView("child_view"); 189 views::View* child_view = new TestView("child_view");
129 widget->GetRootView()->AddChildView(child_view); 190 widget->GetRootView()->AddChildView(child_view);
130 191
131 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; 192 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
193 dom_agent()->getDocument(&root);
132 194
133 dom_agent()->getDocument(&root);
134 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); 195 DOM::Node* parent_node = FindInRoot(parent_window, root.get());
135 DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); 196 DOM::Node* widget_node = FindInRoot(widget.get(), root.get());
136
137 ASSERT_TRUE(parent_node); 197 ASSERT_TRUE(parent_node);
138 ASSERT_TRUE(widget_node); 198 ASSERT_TRUE(widget_node);
139 Array<DOM::Node>* default_children = nullptr;
140 ASSERT_TRUE(parent_node->getChildren(default_children)); 199 ASSERT_TRUE(parent_node->getChildren(default_children));
sadrul 2016/11/09 15:50:24 Looking more at the usage of |default_children|, c
Sarmad Hashmi 2016/11/09 17:10:40 Done.
141 Compare(child_window, parent_node->getChildren(default_children)->get(0)); 200 Compare(child_window, parent_node->getChildren(default_children)->get(0));
142 Array<DOM::Node>* widget_children = 201 Array<DOM::Node>* widget_children =
143 widget_node->getChildren(default_children); 202 widget_node->getChildren(default_children);
144 ASSERT_TRUE(widget_children); 203 ASSERT_TRUE(widget_children);
145 Compare(widget->GetRootView(), widget_children->get(0)); 204 Compare(widget->GetRootView(), widget_children->get(0));
146 ASSERT_TRUE(widget_children->get(0)->getChildren(default_children)); 205 ASSERT_TRUE(widget_children->get(0)->getChildren(default_children));
147 Compare(child_view, 206 Compare(child_view,
148 widget_children->get(0)->getChildren(default_children)->get(1)); 207 widget_children->get(0)->getChildren(default_children)->get(1));
149 // TODO(mhashmi): Remove this call and mock FrontendChannel 208 }
150 dom_agent()->disable(); 209
210 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) {
211 // Initialize DOMAgent
212 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
213 dom_agent()->getDocument(&root);
214
215 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow();
216 DOM::Node* parent_node = root->getChildren(default_children)->get(0);
217 Array<DOM::Node>* parent_node_children =
218 parent_node->getChildren(default_children);
219 DOM::Node* sibling_node =
220 parent_node_children->get(parent_node_children->length() - 1);
221
222 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
223 ExpectChildNodeInserted(parent_node->getNodeId(), sibling_node->getNodeId());
224 }
225
226 TEST_F(AshDevToolsTest, WindowDestroyedChildNodeRemoved) {
227 // Initialize DOMAgent
228 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
229 dom_agent()->getDocument(&root);
230
231 WmWindow* parent_window =
232 WmShell::Get()->GetPrimaryRootWindow()->GetChildren()[0];
233 WmWindow* child_window = parent_window->GetChildren()[0];
234 DOM::Node* root_node = root->getChildren(default_children)->get(0);
235 DOM::Node* parent_node = root_node->getChildren(default_children)->get(0);
236 DOM::Node* child_node = parent_node->getChildren(default_children)->get(0);
237
238 child_window->Destroy();
239 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId());
240 }
241
242 TEST_F(AshDevToolsTest, WindowReorganizedChildNodeRemovedAndInserted) {
243 // Initialize DOMAgent
244 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
245 dom_agent()->getDocument(&root);
246
247 WmWindow* root_window = WmShell::Get()->GetPrimaryRootWindow();
248 WmWindow* target_window = root_window->GetChildren()[1];
249 WmWindow* child_window = root_window->GetChildren()[0]->GetChildren()[0];
250
251 DOM::Node* root_node = root->getChildren(default_children)->get(0);
252 DOM::Node* parent_node = root_node->getChildren(default_children)->get(0);
253 DOM::Node* target_node = root_node->getChildren(default_children)->get(1);
254 Array<DOM::Node>* target_node_children =
255 target_node->getChildren(default_children);
256 DOM::Node* sibling_node =
257 target_node_children->get(target_node_children->length() - 1);
258 DOM::Node* child_node = parent_node->getChildren(default_children)->get(0);
259
260 target_window->AddChild(child_window);
261 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId());
262 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId());
263 }
264
265 TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) {
266 // Initialize DOMAgent
267 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
268 dom_agent()->getDocument(&root);
269
270 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow();
271 WmWindow* child_window = parent_window->GetChildren()[0];
272 WmWindow* target_window = parent_window->GetChildren()[1];
273
274 DOM::Node* parent_node = root->getChildren(default_children)->get(0);
275 Array<DOM::Node>* parent_node_children =
276 parent_node->getChildren(default_children);
277 DOM::Node* child_node = parent_node_children->get(0);
278 DOM::Node* sibling_node = parent_node_children->get(1);
279 int parent_id = parent_node->getNodeId();
280
281 parent_window->StackChildAbove(child_window, target_window);
282 ExpectChildNodeRemoved(parent_id, child_node->getNodeId());
283 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId());
151 } 284 }
152 285
153 } // namespace ash 286 } // namespace ash
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698