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

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

Issue 2480683003: Add tests for ash devtools window updates (Closed)
Patch Set: Replace default_children with nullptr 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(){};
sadrul 2016/11/10 20:11:41 space after (). no ; at the end You probably need
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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 123
93 } // namespace 124 } // namespace
94 125
95 class AshDevToolsTest : public AshTest { 126 class AshDevToolsTest : public AshTest {
96 public: 127 public:
97 AshDevToolsTest() {} 128 AshDevToolsTest() {}
98 ~AshDevToolsTest() override {} 129 ~AshDevToolsTest() override {}
99 130
100 void SetUp() override { 131 void SetUp() override {
101 AshTest::SetUp(); 132 AshTest::SetUp();
133 fake_frontend_channel_ = base::MakeUnique<FakeFrontendChannel>();
134 uber_dispatcher_ =
135 base::MakeUnique<UberDispatcher>(fake_frontend_channel_.get());
102 dom_agent_ = 136 dom_agent_ =
103 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get()); 137 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get());
138 dom_agent_->Init(uber_dispatcher_.get());
104 } 139 }
105 140
106 void TearDown() override { 141 void TearDown() override {
107 dom_agent_.reset(); 142 dom_agent_.reset();
143 uber_dispatcher_.reset();
144 fake_frontend_channel_.reset();
108 AshTest::TearDown(); 145 AshTest::TearDown();
109 } 146 }
110 147
148 void ExpectChildNodeInserted(int parent_id, int prev_sibling_id) {
149 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessageStartsWith(
150 base::StringPrintf("{\"method\":\"DOM.childNodeInserted\","
151 "\"params\":{\"parentNodeId\":%d,"
152 "\"previousNodeId\":%d",
153 parent_id, prev_sibling_id)));
154 }
155
156 void ExpectChildNodeRemoved(int parent_id, int node_id) {
157 EXPECT_EQ(1, frontend_channel()->CountProtocolNotificationMessage(
158 base::StringPrintf(
159 "{\"method\":\"DOM.childNodeRemoved\",\"params\":{"
160 "\"parentNodeId\":%d,\"nodeId\":%d}}",
161 parent_id, node_id)));
162 }
163
164 FakeFrontendChannel* frontend_channel() {
165 return fake_frontend_channel_.get();
166 }
167
111 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); } 168 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); }
112 169
113 private: 170 private:
171 std::unique_ptr<UberDispatcher> uber_dispatcher_;
172 std::unique_ptr<FakeFrontendChannel> fake_frontend_channel_;
114 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_; 173 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_;
115 174
116 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest); 175 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest);
117 }; 176 };
118 177
119 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) { 178 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) {
120 std::unique_ptr<views::Widget> widget( 179 std::unique_ptr<views::Widget> widget(
121 CreateTestWidget(gfx::Rect(1, 1, 1, 1))); 180 CreateTestWidget(gfx::Rect(1, 1, 1, 1)));
122 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get()); 181 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
123 parent_window->SetName("parent_window"); 182 parent_window->SetName("parent_window");
124 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window)); 183 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
125 WmWindow* child_window = child_owner->window(); 184 WmWindow* child_window = child_owner->window();
126 child_window->SetName("child_window"); 185 child_window->SetName("child_window");
127 widget->Show(); 186 widget->Show();
128 views::View* child_view = new TestView("child_view"); 187 views::View* child_view = new TestView("child_view");
129 widget->GetRootView()->AddChildView(child_view); 188 widget->GetRootView()->AddChildView(child_view);
130 189
131 std::unique_ptr<ui::devtools::protocol::DOM::Node> root; 190 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
191 dom_agent()->getDocument(&root);
132 192
133 dom_agent()->getDocument(&root);
134 DOM::Node* parent_node = FindInRoot(parent_window, root.get()); 193 DOM::Node* parent_node = FindInRoot(parent_window, root.get());
135 DOM::Node* widget_node = FindInRoot(widget.get(), root.get()); 194 DOM::Node* widget_node = FindInRoot(widget.get(), root.get());
136
137 ASSERT_TRUE(parent_node); 195 ASSERT_TRUE(parent_node);
138 ASSERT_TRUE(widget_node); 196 ASSERT_TRUE(widget_node);
139 Array<DOM::Node>* default_children = nullptr; 197 ASSERT_TRUE(parent_node->getChildren(nullptr));
140 ASSERT_TRUE(parent_node->getChildren(default_children)); 198 Compare(child_window, parent_node->getChildren(nullptr)->get(0));
141 Compare(child_window, parent_node->getChildren(default_children)->get(0)); 199 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); 200 ASSERT_TRUE(widget_children);
145 Compare(widget->GetRootView(), widget_children->get(0)); 201 Compare(widget->GetRootView(), widget_children->get(0));
146 ASSERT_TRUE(widget_children->get(0)->getChildren(default_children)); 202 ASSERT_TRUE(widget_children->get(0)->getChildren(nullptr));
147 Compare(child_view, 203 Compare(child_view, widget_children->get(0)->getChildren(nullptr)->get(1));
148 widget_children->get(0)->getChildren(default_children)->get(1)); 204 }
149 // TODO(mhashmi): Remove this call and mock FrontendChannel 205
150 dom_agent()->disable(); 206 TEST_F(AshDevToolsTest, WindowAddedChildNodeInserted) {
207 // Initialize DOMAgent
208 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
209 dom_agent()->getDocument(&root);
210
211 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow();
212 DOM::Node* parent_node = root->getChildren(nullptr)->get(0);
213 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr);
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(nullptr)->get(0);
230 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0);
231 DOM::Node* child_node = parent_node->getChildren(nullptr)->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(nullptr)->get(0);
247 DOM::Node* parent_node = root_node->getChildren(nullptr)->get(0);
248 DOM::Node* target_node = root_node->getChildren(nullptr)->get(1);
249 Array<DOM::Node>* target_node_children = target_node->getChildren(nullptr);
250 DOM::Node* sibling_node =
251 target_node_children->get(target_node_children->length() - 1);
252 DOM::Node* child_node = parent_node->getChildren(nullptr)->get(0);
253
254 target_window->AddChild(child_window);
255 ExpectChildNodeRemoved(parent_node->getNodeId(), child_node->getNodeId());
256 ExpectChildNodeInserted(target_node->getNodeId(), sibling_node->getNodeId());
257 }
258
259 TEST_F(AshDevToolsTest, WindowStackingChangedChildNodeRemovedAndInserted) {
260 // Initialize DOMAgent
261 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
262 dom_agent()->getDocument(&root);
263
264 WmWindow* parent_window = WmShell::Get()->GetPrimaryRootWindow();
265 WmWindow* child_window = parent_window->GetChildren()[0];
266 WmWindow* target_window = parent_window->GetChildren()[1];
267
268 DOM::Node* parent_node = root->getChildren(nullptr)->get(0);
269 Array<DOM::Node>* parent_node_children = parent_node->getChildren(nullptr);
270 DOM::Node* child_node = parent_node_children->get(0);
271 DOM::Node* sibling_node = parent_node_children->get(1);
272 int parent_id = parent_node->getNodeId();
273
274 parent_window->StackChildAbove(child_window, target_window);
275 ExpectChildNodeRemoved(parent_id, child_node->getNodeId());
276 ExpectChildNodeInserted(parent_id, sibling_node->getNodeId());
151 } 277 }
152 278
153 } // namespace ash 279 } // 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