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

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

Issue 2469883002: Add test for ash devtools DOM agent getDocument method (Closed)
Patch Set: Remove unnecessary code 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 | « ash/common/devtools/ash_devtools_dom_agent.cc ('k') | 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/common/devtools/ash_devtools_dom_agent.h"
6
7 #include "ash/common/test/ash_test.h"
8 #include "ash/common/wm_lookup.h"
9 #include "ash/common/wm_shell.h"
10 #include "ash/common/wm_window.h"
11 #include "ui/views/widget/widget.h"
12
13 namespace ash {
14 namespace {
15 using namespace ui::devtools::protocol;
16 const int kDefaultChildNodeCount = -1;
17
18 class TestView : public views::View {
19 public:
20 TestView(const char* name) : views::View(), name_(name) {}
21
22 const char* GetClassName() const override { return name_; }
23
24 private:
25 const char* name_;
26
27 DISALLOW_COPY_AND_ASSIGN(TestView);
28 };
29
30 std::string GetAttributeValue(const std::string& attribute, DOM::Node* node) {
31 EXPECT_TRUE(node->hasAttributes());
32 Array<std::string>* attributes = node->getAttributes(nullptr);
33 for (size_t i = 0; i < attributes->length() - 1; i++) {
34 if (attributes->get(i) == attribute) {
sadrul 2016/11/02 15:23:05 Don't need {}
Sarmad Hashmi 2016/11/02 16:32:34 Done.
35 return attributes->get(i + 1);
36 }
37 }
38 return nullptr;
39 }
40
41 bool Equals(views::Widget* widget, DOM::Node* node) {
42 return "Widget" == node->getNodeName() &&
43 widget->GetName() == GetAttributeValue("name", node) &&
44 (widget->GetRootView() ? 1 : 0) ==
45 node->getChildNodeCount(kDefaultChildNodeCount);
46 }
47
48 bool Equals(WmWindow* window, DOM::Node* node) {
49 return "Window" == node->getNodeName() &&
50 window->GetName() == GetAttributeValue("name", node) &&
51 static_cast<int>(window->GetChildren().size()) ==
52 node->getChildNodeCount(kDefaultChildNodeCount);
53 }
54
55 void Compare(views::View* view, DOM::Node* node) {
56 EXPECT_EQ("View", node->getNodeName());
57 EXPECT_EQ(view->GetClassName(), GetAttributeValue("name", node));
58 EXPECT_EQ(view->child_count(),
59 node->getChildNodeCount(kDefaultChildNodeCount));
60 }
61
62 void Compare(WmWindow* window, DOM::Node* node) {
63 EXPECT_TRUE(Equals(window, node));
64 }
65
66 DOM::Node* FindInRoot(WmWindow* window, DOM::Node* root) {
67 if (Equals(window, root))
68 return root;
69
70 Array<DOM::Node>* children = root->getChildren(nullptr);
71 DOM::Node* window_node = nullptr;
72 for (size_t i = 0; i < children->length(); i++) {
73 window_node = FindInRoot(window, children->get(i));
74 if (window_node)
75 return window_node;
76 }
77 return window_node;
78 }
79
80 DOM::Node* FindInRoot(views::Widget* widget, DOM::Node* root) {
81 if (Equals(widget, root))
82 return root;
83
84 Array<DOM::Node>* children = root->getChildren(nullptr);
85 DOM::Node* widget_node = nullptr;
86 for (size_t i = 0; i < children->length(); i++) {
87 widget_node = FindInRoot(widget, children->get(i));
88 if (widget_node)
89 return widget_node;
90 }
91 return widget_node;
92 }
93
94 } // namespace
95
96 class AshDevToolsTest : public AshTest {
97 public:
98 AshDevToolsTest() {}
99 ~AshDevToolsTest() override {}
100
101 void SetUp() override {
102 AshTest::SetUp();
103 dom_agent_ =
104 base::MakeUnique<devtools::AshDevToolsDOMAgent>(WmShell::Get());
105 }
106
107 void TearDown() override {
108 dom_agent_.reset();
109 AshTest::TearDown();
110 }
111
112 devtools::AshDevToolsDOMAgent* dom_agent() { return dom_agent_.get(); }
113
114 private:
115 std::unique_ptr<devtools::AshDevToolsDOMAgent> dom_agent_;
116
117 DISALLOW_COPY_AND_ASSIGN(AshDevToolsTest);
118 };
119
120 TEST_F(AshDevToolsTest, GetDocumentWithWindowWidgetView) {
121 std::unique_ptr<views::Widget> widget(
122 CreateTestWidget(gfx::Rect(1, 1, 1, 1)));
123 WmWindow* parent_window = WmLookup::Get()->GetWindowForWidget(widget.get());
124 parent_window->SetName("parent_window");
125 std::unique_ptr<WindowOwner> child_owner(CreateChildWindow(parent_window));
126 WmWindow* child_window = child_owner->window();
127 child_window->SetName("child_window");
128 widget->Show();
129 views::View* child_view = new TestView("child_view");
130 widget->GetRootView()->AddChildView(child_view);
131
132 std::unique_ptr<ui::devtools::protocol::DOM::Node> root;
133 dom_agent()->getDocument(nullptr, &root);
sadrul 2016/11/02 15:23:05 use some variable for the nullptr error string, e.
Sarmad Hashmi 2016/11/02 16:32:34 Done.
134 DOM::Node* parent_node = FindInRoot(parent_window, root.get());
135 DOM::Node* widget_node = FindInRoot(widget.get(), root.get());
136
137 EXPECT_TRUE(parent_node);
138 EXPECT_TRUE(widget_node);
139 Compare(child_window, parent_node->getChildren(nullptr)->get(0));
sadrul 2016/11/02 15:23:05 First ASSERT() that parent_node->getChildren(nullp
Sarmad Hashmi 2016/11/02 16:32:34 Done.
140 Compare(widget->GetRootView(), widget_node->getChildren(nullptr)->get(0));
141 Compare(
142 child_view,
143 widget_node->getChildren(nullptr)->get(0)->getChildren(nullptr)->get(1));
144 }
145
146 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/devtools/ash_devtools_dom_agent.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698