Chromium Code Reviews| 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 "mojo/services/html_viewer/ax_provider_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/metrics/stats_counters.h" | |
| 10 #include "mojo/services/html_viewer/blink_platform_impl.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/WebKit/public/platform/WebData.h" | |
| 13 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 14 #include "third_party/WebKit/public/web/WebFrameClient.h" | |
| 15 #include "third_party/WebKit/public/web/WebKit.h" | |
| 16 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 17 #include "third_party/WebKit/public/web/WebView.h" | |
| 18 #include "third_party/WebKit/public/web/WebViewClient.h" | |
| 19 #include "url/gurl.h" | |
| 20 | |
| 21 using blink::WebData; | |
| 22 using blink::WebLocalFrame; | |
| 23 using blink::WebFrameClient; | |
| 24 using blink::WebURL; | |
| 25 using blink::WebView; | |
| 26 using blink::WebViewClient; | |
| 27 | |
| 28 namespace mojo { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 class TestWebFrameClient : public WebFrameClient { | |
| 33 public: | |
| 34 virtual ~TestWebFrameClient() {} | |
| 35 virtual void didStopLoading() { base::MessageLoop::current()->Quit(); } | |
| 36 }; | |
| 37 | |
| 38 class TestWebViewClient : public WebViewClient { | |
| 39 public: | |
| 40 virtual bool allowsBrokenNullLayerTreeView() const { return true; } | |
| 41 virtual ~TestWebViewClient() {} | |
| 42 }; | |
| 43 | |
| 44 class AxProviderImplTest : public testing::Test { | |
| 45 public: | |
| 46 AxProviderImplTest() { blink::initialize(new BlinkPlatformImpl()); } | |
| 47 | |
| 48 virtual ~AxProviderImplTest() override { blink::shutdown(); } | |
| 49 | |
| 50 private: | |
| 51 base::MessageLoopForUI message_loop; | |
| 52 }; | |
| 53 | |
| 54 struct NodeCatcher { | |
|
abarth-chromium
2014/11/10 00:33:04
Cute
| |
| 55 void OnNodes(Array<AxNodePtr> nodes) { this->nodes = nodes.Pass(); } | |
| 56 Array<AxNodePtr> nodes; | |
| 57 }; | |
| 58 | |
| 59 AxNodePtr CreateNode(int id, | |
| 60 int parent_id, | |
| 61 int next_sibling_id, | |
| 62 const RectPtr& bounds, | |
| 63 const std::string& url, | |
| 64 const std::string& text) { | |
| 65 AxNodePtr node(AxNode::New()); | |
| 66 node->id = id; | |
| 67 node->parent_id = parent_id; | |
| 68 node->next_sibling_id = next_sibling_id; | |
| 69 node->bounds = bounds.Clone(); | |
| 70 | |
| 71 if (!url.empty()) { | |
| 72 node->link = AxLink::New(); | |
| 73 node->link->url = url; | |
| 74 } | |
| 75 if (!text.empty()) { | |
| 76 node->text = AxText::New(); | |
| 77 node->text->content = text; | |
| 78 } | |
| 79 return node.Pass(); | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 TEST_F(AxProviderImplTest, Basic) { | |
| 85 TestWebViewClient web_view_client; | |
| 86 TestWebFrameClient web_frame_client; | |
| 87 WebView* view = WebView::create(&web_view_client); | |
| 88 view->setMainFrame(WebLocalFrame::create(&web_frame_client)); | |
| 89 view->mainFrame()->loadHTMLString( | |
| 90 WebData( | |
| 91 "<html><body>foo<a " | |
| 92 "href='http://monkey.net'>bar</a>baz</body></html>"), | |
| 93 WebURL(GURL("http://someplace.net"))); | |
| 94 | |
| 95 AxProviderImpl ax_provider_impl(view); | |
| 96 NodeCatcher catcher; | |
| 97 base::MessageLoop::current()->Run(); | |
|
abarth-chromium
2014/11/10 00:33:04
I'd probably move this to just below loadHTMLStrin
Aaron Boodman
2014/11/10 07:05:53
Done.
| |
| 98 ax_provider_impl.GetTree( | |
| 99 base::Bind(&NodeCatcher::OnNodes, base::Unretained(&catcher))); | |
| 100 | |
| 101 std::map<uint32, AxNode*> lookup; | |
| 102 for (size_t i = 0; i < catcher.nodes.size(); ++i) { | |
| 103 auto& node = catcher.nodes[i]; | |
|
abarth-chromium
2014/11/10 00:33:04
We can't use for (auto& node : catcher.nodes) ? M
Aaron Boodman
2014/11/10 07:05:53
agree.
| |
| 104 lookup[node->id] = node.get(); | |
| 105 } | |
| 106 | |
| 107 typedef decltype(lookup)::value_type MapEntry; | |
| 108 auto is_link = [](MapEntry pair) { return pair.second->link.get(); }; | |
| 109 auto is_text = [](MapEntry pair, const char* content) { | |
| 110 return pair.second->text.get() && | |
| 111 pair.second->text->content.To<std::string>() == content; | |
| 112 }; | |
| 113 auto is_foo = [&is_text](MapEntry pair) { return is_text(pair, "foo"); }; | |
| 114 auto is_bar = [&is_text](MapEntry pair) { return is_text(pair, "bar"); }; | |
| 115 auto is_baz = [&is_text](MapEntry pair) { return is_text(pair, "baz"); }; | |
|
abarth-chromium
2014/11/10 00:33:04
Mind blown!
abarth-chromium
2014/11/10 00:33:04
Mind blown!
Aaron Boodman
2014/11/10 07:05:53
Too bad we can't use stl 11. Could do this with st
| |
| 116 | |
| 117 EXPECT_EQ(1u, std::count_if(lookup.begin(), lookup.end(), is_link)); | |
| 118 EXPECT_EQ(1u, std::count_if(lookup.begin(), lookup.end(), is_foo)); | |
| 119 EXPECT_EQ(1u, std::count_if(lookup.begin(), lookup.end(), is_bar)); | |
| 120 EXPECT_EQ(1u, std::count_if(lookup.begin(), lookup.end(), is_baz)); | |
| 121 | |
| 122 auto root = lookup[1u]; | |
| 123 auto link = std::find_if(lookup.begin(), lookup.end(), is_link)->second; | |
| 124 auto foo = std::find_if(lookup.begin(), lookup.end(), is_foo)->second; | |
| 125 auto bar = std::find_if(lookup.begin(), lookup.end(), is_bar)->second; | |
| 126 auto baz = std::find_if(lookup.begin(), lookup.end(), is_baz)->second; | |
| 127 | |
| 128 // Test basic content of each node. The properties we copy (like parent_id) | |
| 129 // here are tested differently below. | |
| 130 EXPECT_TRUE(CreateNode(root->id, 0, 0, root->bounds, "", "")->Equals(*root)); | |
| 131 EXPECT_TRUE(CreateNode(foo->id, foo->parent_id, 0, foo->bounds, "", "foo") | |
| 132 ->Equals(*foo)); | |
| 133 EXPECT_TRUE(CreateNode(bar->id, bar->parent_id, 0, bar->bounds, "", "bar") | |
| 134 ->Equals(*bar)); | |
| 135 EXPECT_TRUE(CreateNode(baz->id, baz->parent_id, 0, baz->bounds, "", "baz") | |
| 136 ->Equals(*baz)); | |
| 137 EXPECT_TRUE(CreateNode(link->id, | |
| 138 link->parent_id, | |
| 139 link->next_sibling_id, | |
| 140 link->bounds, | |
| 141 "http://monkey.net/", | |
| 142 "")->Equals(*link)); | |
| 143 | |
| 144 auto is_descendant_of = [&lookup](uint32 id, uint32 ancestor) { | |
| 145 for (; (id = lookup[id]->parent_id) != 0;) { | |
| 146 if (id == ancestor) | |
| 147 return true; | |
| 148 } | |
| 149 return false; | |
| 150 }; | |
| 151 | |
| 152 EXPECT_TRUE(is_descendant_of(bar->id, link->id)); | |
| 153 for (auto pair : lookup) { | |
| 154 AxNode* node = pair.second; | |
| 155 if (node != root) | |
| 156 EXPECT_TRUE(is_descendant_of(node->id, 1u)); | |
| 157 if (node != link && node != foo && node != bar && node != baz) { | |
| 158 EXPECT_TRUE(CreateNode(node->id, | |
| 159 node->parent_id, | |
| 160 node->next_sibling_id, | |
| 161 node->bounds, | |
| 162 "", | |
| 163 "")); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 // TODO(aa): Test bounds. | |
| 168 // TODO(aa): Test sibling ordering of foo/bar/baz. | |
| 169 | |
| 170 view->close(); | |
| 171 } | |
| 172 | |
| 173 } // namespace mojo | |
| OLD | NEW |