1 // Copyright 2015 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.
6 #include "base/command_line.h"
7 #include "base/run_loop.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/test/test_timeouts.h"
10 #include "components/view_manager/public/cpp/tests/view_manager_test_base.h"
11 #include "components/view_manager/public/cpp/view.h"
12 #include "components/view_manager/public/cpp/view_tree_connection.h"
13 #include "components/web_view/public/interfaces/frame_tree.mojom.h"
14 #include "mojo/application/public/cpp/application_impl.h"
15 #include "mojo/application/public/cpp/application_test_base.h"
16 #include "net/test/spawned_test_server/spawned_test_server.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/mojo_services/src/accessibility/public/interfaces/accessibility.mojom.h"
24 // Returns true if the tree contains a text node with contents matching |text|.
25 bool AxTreeContainsText(const Array
<AxNodePtr
>& tree
, const String
& text
) {
26 for (size_t i
= 0; i
< tree
.size(); ++i
) {
27 if (!tree
[i
]->text
.is_null() && tree
[i
]->text
->content
== text
)
33 class TestFrameTreeServer
: public web_view::FrameTreeServer
{
35 TestFrameTreeServer() {}
36 ~TestFrameTreeServer() override
{}
38 // web_view::FrameTreeServer:
39 void PostMessageEventToFrame(uint32_t source_frame_id
,
40 uint32_t target_frame_id
,
41 web_view::HTMLMessageEventPtr event
) override
{}
42 void LoadingStarted(uint32_t frame_id
) override
{}
43 void LoadingStopped(uint32_t frame_id
) override
{}
44 void ProgressChanged(uint32_t frame_id
, double progress
) override
{}
45 void TitleChanged(uint32_t frame_id
, const mojo::String
& title
) override
{}
46 void SetClientProperty(uint32_t frame_id
,
47 const mojo::String
& name
,
48 mojo::Array
<uint8_t> value
) override
{}
49 void OnCreatedFrame(uint32_t parent_id
,
51 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>
52 client_properties
) override
{}
53 void RequestNavigate(web_view::NavigationTargetType target_type
,
54 uint32_t target_frame_id
,
55 mojo::URLRequestPtr request
) override
{}
56 void DidNavigateLocally(uint32_t frame_id
, const mojo::String
& url
) override
{
60 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeServer
);
65 using AXProviderTest
= ViewManagerTestBase
;
67 TEST_F(AXProviderTest
, HelloWorld
) {
68 // Start a test server for net/data/test.html access.
69 net::SpawnedTestServer
server(
70 net::SpawnedTestServer::TYPE_HTTP
, net::SpawnedTestServer::kLocalhost
,
71 base::FilePath(FILE_PATH_LITERAL("net/data")));
72 ASSERT_TRUE(server
.Start());
74 // Connect to the URL through the mojo:html_viewer content handler.
75 const uint16_t assigned_port
= server
.host_port_pair().port();
76 mojo::URLRequestPtr
request(mojo::URLRequest::New());
77 request
->url
= mojo::String::From(
78 base::StringPrintf("http://127.0.0.1:%u/files/test.html", assigned_port
));
79 scoped_ptr
<ApplicationConnection
> connection
=
80 application_impl()->ConnectToApplication(request
.Pass());
82 // Embed the html_viewer in a View.
83 ViewTreeClientPtr tree_client
;
84 connection
->ConnectToService(&tree_client
);
85 View
* embed_view
= window_manager()->CreateView();
86 embed_view
->Embed(tree_client
.Pass());
88 TestFrameTreeServer frame_tree_server
;
89 web_view::FrameTreeServerPtr frame_tree_server_ptr
;
90 mojo::Binding
<web_view::FrameTreeServer
> frame_tree_server_binding(
92 frame_tree_server_binding
.Bind(GetProxy(&frame_tree_server_ptr
).Pass());
94 mojo::Array
<web_view::FrameDataPtr
> array(1u);
95 array
[0] = web_view::FrameData::New().Pass();
96 array
[0]->frame_id
= embed_view
->id();
97 array
[0]->parent_id
= 0u;
99 web_view::FrameTreeClientPtr frame_tree_client
;
100 connection
->ConnectToService(&frame_tree_client
);
101 frame_tree_client
->OnConnect(
102 frame_tree_server_ptr
.Pass(), 1u, embed_view
->id(),
103 web_view::VIEW_CONNECT_TYPE_USE_NEW
, array
.Pass(), base::Closure());
105 // Connect to the AxProvider of the HTML document and get the AxTree.
106 AxProviderPtr ax_provider
;
107 connection
->ConnectToService(&ax_provider
);
108 Array
<AxNodePtr
> ax_tree
;
109 ax_provider
->GetTree([&ax_tree
](Array
<AxNodePtr
> tree
) {
110 ax_tree
= tree
.Pass();
111 EXPECT_TRUE(QuitRunLoop());
113 ASSERT_TRUE(DoRunLoopWithTimeout());
115 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "Hello "));
116 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "World!"));
117 EXPECT_FALSE(AxTreeContainsText(ax_tree
, "foo"));