OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/html_viewer/ax_provider_impl.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "components/html_viewer/blink_platform_impl.h" | |
14 #include "components/scheduler/renderer/renderer_scheduler.h" | |
15 #include "gin/v8_initializer.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 #include "third_party/WebKit/public/platform/WebData.h" | |
18 #include "third_party/WebKit/public/platform/WebURL.h" | |
19 #include "third_party/WebKit/public/web/WebFrameClient.h" | |
20 #include "third_party/WebKit/public/web/WebKit.h" | |
21 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
22 #include "third_party/WebKit/public/web/WebView.h" | |
23 #include "third_party/WebKit/public/web/WebViewClient.h" | |
24 #include "url/gurl.h" | |
25 | |
26 using blink::WebData; | |
27 using blink::WebLocalFrame; | |
28 using blink::WebFrameClient; | |
29 using blink::WebURL; | |
30 using blink::WebView; | |
31 using blink::WebViewClient; | |
32 | |
33 using mojo::Array; | |
34 using mojo::AxNode; | |
35 using mojo::AxNodePtr; | |
36 | |
37 namespace { | |
38 | |
39 class TestWebFrameClient : public WebFrameClient { | |
40 public: | |
41 ~TestWebFrameClient() override {} | |
42 void didStopLoading() override { | |
43 base::MessageLoop::current()->QuitWhenIdle(); | |
44 } | |
45 }; | |
46 | |
47 class TestWebViewClient : public WebViewClient { | |
48 public: | |
49 bool allowsBrokenNullLayerTreeView() const override { return true; } | |
50 virtual ~TestWebViewClient() {} | |
51 }; | |
52 | |
53 class AxProviderImplTest : public testing::Test { | |
54 public: | |
55 AxProviderImplTest() | |
56 : message_loop_(new base::MessageLoopForUI()), | |
57 renderer_scheduler_(scheduler::RendererScheduler::Create()) { | |
58 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | |
59 gin::V8Initializer::LoadV8Snapshot(); | |
60 gin::V8Initializer::LoadV8Natives(); | |
61 #endif | |
62 blink::initialize( | |
63 new html_viewer::BlinkPlatformImpl(nullptr, nullptr, | |
64 renderer_scheduler_.get())); | |
65 } | |
66 | |
67 ~AxProviderImplTest() override { | |
68 renderer_scheduler_->Shutdown(); | |
69 message_loop_.reset(); | |
70 blink::shutdown(); | |
71 } | |
72 | |
73 private: | |
74 scoped_ptr<base::MessageLoopForUI> message_loop_; | |
75 scoped_ptr<scheduler::RendererScheduler> renderer_scheduler_; | |
76 }; | |
77 | |
78 struct NodeCatcher { | |
79 void OnNodes(Array<AxNodePtr> nodes) { this->nodes = std::move(nodes); } | |
80 Array<AxNodePtr> nodes; | |
81 }; | |
82 | |
83 AxNodePtr CreateNode(int id, | |
84 int parent_id, | |
85 int next_sibling_id, | |
86 const mojo::RectPtr& bounds, | |
87 const std::string& url, | |
88 const std::string& text) { | |
89 AxNodePtr node(AxNode::New()); | |
90 node->id = id; | |
91 node->parent_id = parent_id; | |
92 node->next_sibling_id = next_sibling_id; | |
93 node->bounds = bounds.Clone(); | |
94 | |
95 if (!url.empty()) { | |
96 node->link = mojo::AxLink::New(); | |
97 node->link->url = url; | |
98 } | |
99 if (!text.empty()) { | |
100 node->text = mojo::AxText::New(); | |
101 node->text->content = text; | |
102 } | |
103 return node; | |
104 } | |
105 | |
106 } // namespace | |
107 | |
108 TEST_F(AxProviderImplTest, Basic) { | |
109 TestWebViewClient web_view_client; | |
110 TestWebFrameClient web_frame_client; | |
111 WebView* view = WebView::create(&web_view_client); | |
112 view->setMainFrame(WebLocalFrame::create(blink::WebTreeScopeType::Document, | |
113 &web_frame_client)); | |
114 view->mainFrame()->loadHTMLString( | |
115 WebData( | |
116 "<html><body>foo<a " | |
117 "href='http://monkey.net'>bar</a>baz</body></html>"), | |
118 WebURL(GURL("http://someplace.net"))); | |
119 base::MessageLoop::current()->Run(); | |
120 | |
121 mojo::AxProviderPtr service_ptr; | |
122 html_viewer::AxProviderImpl ax_provider_impl(view, | |
123 mojo::GetProxy(&service_ptr)); | |
124 NodeCatcher catcher; | |
125 ax_provider_impl.GetTree( | |
126 base::Bind(&NodeCatcher::OnNodes, base::Unretained(&catcher))); | |
127 | |
128 std::map<uint32_t, AxNode*> lookup; | |
129 for (size_t i = 0; i < catcher.nodes.size(); ++i) { | |
130 auto& node = catcher.nodes[i]; | |
131 lookup[node->id] = node.get(); | |
132 } | |
133 | |
134 typedef decltype(lookup)::value_type MapEntry; | |
135 auto is_link = [](MapEntry pair) { return !pair.second->link.is_null(); }; | |
136 auto is_text = [](MapEntry pair, const char* content) { | |
137 return !pair.second->text.is_null() && | |
138 pair.second->text->content.To<std::string>() == content; | |
139 }; | |
140 auto is_foo = [&is_text](MapEntry pair) { return is_text(pair, "foo"); }; | |
141 auto is_bar = [&is_text](MapEntry pair) { return is_text(pair, "bar"); }; | |
142 auto is_baz = [&is_text](MapEntry pair) { return is_text(pair, "baz"); }; | |
143 | |
144 EXPECT_EQ(1, std::count_if(lookup.begin(), lookup.end(), is_link)); | |
145 EXPECT_EQ(1, std::count_if(lookup.begin(), lookup.end(), is_foo)); | |
146 EXPECT_EQ(1, std::count_if(lookup.begin(), lookup.end(), is_bar)); | |
147 EXPECT_EQ(1, std::count_if(lookup.begin(), lookup.end(), is_baz)); | |
148 | |
149 auto root = lookup[1u]; | |
150 auto link = std::find_if(lookup.begin(), lookup.end(), is_link)->second; | |
151 auto foo = std::find_if(lookup.begin(), lookup.end(), is_foo)->second; | |
152 auto bar = std::find_if(lookup.begin(), lookup.end(), is_bar)->second; | |
153 auto baz = std::find_if(lookup.begin(), lookup.end(), is_baz)->second; | |
154 | |
155 // Test basic content of each node. The properties we copy (like parent_id) | |
156 // here are tested differently below. | |
157 EXPECT_TRUE(CreateNode(root->id, 0, 0, root->bounds, "", "")->Equals(*root)); | |
158 EXPECT_TRUE(CreateNode(foo->id, foo->parent_id, 0, foo->bounds, "", "foo") | |
159 ->Equals(*foo)); | |
160 EXPECT_TRUE(CreateNode(bar->id, bar->parent_id, 0, bar->bounds, "", "bar") | |
161 ->Equals(*bar)); | |
162 EXPECT_TRUE(CreateNode(baz->id, baz->parent_id, 0, baz->bounds, "", "baz") | |
163 ->Equals(*baz)); | |
164 EXPECT_TRUE(CreateNode(link->id, | |
165 link->parent_id, | |
166 link->next_sibling_id, | |
167 link->bounds, | |
168 "http://monkey.net/", | |
169 "")->Equals(*link)); | |
170 | |
171 auto is_descendant_of = [&lookup](uint32_t id, uint32_t ancestor) { | |
172 for (; (id = lookup[id]->parent_id) != 0;) { | |
173 if (id == ancestor) | |
174 return true; | |
175 } | |
176 return false; | |
177 }; | |
178 | |
179 EXPECT_TRUE(is_descendant_of(bar->id, link->id)); | |
180 for (auto pair : lookup) { | |
181 AxNode* node = pair.second; | |
182 if (node != root) | |
183 EXPECT_TRUE(is_descendant_of(node->id, 1u)); | |
184 if (node != link && node != foo && node != bar && node != baz) { | |
185 EXPECT_TRUE(CreateNode(node->id, | |
186 node->parent_id, | |
187 node->next_sibling_id, | |
188 node->bounds, | |
189 "", | |
190 "")); | |
191 } | |
192 } | |
193 | |
194 // TODO(aa): Test bounds. | |
195 // TODO(aa): Test sibling ordering of foo/bar/baz. | |
196 | |
197 view->close(); | |
198 } | |
OLD | NEW |