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/mus/public/cpp/tests/view_manager_test_base.h"
11 #include "components/mus/public/cpp/view.h"
12 #include "components/mus/public/cpp/view_tree_connection.h"
13 #include "components/web_view/public/interfaces/frame.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 TestFrame
: public web_view::mojom::Frame
{
36 ~TestFrame() override
{}
38 // web_view::mojom::Frame:
39 void PostMessageEventToFrame(
40 uint32_t target_frame_id
,
41 web_view::mojom::HTMLMessageEventPtr event
) override
{}
42 void LoadingStateChanged(bool loading
, double progress
) override
{}
43 void TitleChanged(const mojo::String
& title
) override
{}
44 void DidCommitProvisionalLoad() override
{}
45 void SetClientProperty(const mojo::String
& name
,
46 mojo::Array
<uint8_t> value
) override
{}
48 mojo::InterfaceRequest
<web_view::mojom::Frame
> frame_request
,
49 web_view::mojom::FrameClientPtr client
,
51 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>> client_properties
)
53 void RequestNavigate(web_view::mojom::NavigationTargetType target_type
,
54 uint32_t target_frame_id
,
55 mojo::URLRequestPtr request
) override
{}
56 void DidNavigateLocally(const mojo::String
& url
) override
{}
57 void DispatchLoadEventToParent() override
{}
60 DISALLOW_COPY_AND_ASSIGN(TestFrame
);
65 using AXProviderTest
= mus::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 mus::View
* embed_view
= window_manager()->CreateView();
86 embed_view
->Embed(tree_client
.Pass());
89 web_view::mojom::FramePtr frame_ptr
;
90 mojo::Binding
<web_view::mojom::Frame
> frame_binding(&frame
);
91 frame_binding
.Bind(GetProxy(&frame_ptr
).Pass());
93 mojo::Array
<web_view::mojom::FrameDataPtr
> array(1u);
94 array
[0] = web_view::mojom::FrameData::New().Pass();
95 array
[0]->frame_id
= embed_view
->id();
96 array
[0]->parent_id
= 0u;
98 web_view::mojom::FrameClientPtr frame_client
;
99 connection
->ConnectToService(&frame_client
);
100 frame_client
->OnConnect(frame_ptr
.Pass(), 1u, embed_view
->id(),
101 web_view::mojom::VIEW_CONNECT_TYPE_USE_NEW
,
102 array
.Pass(), base::Closure());
104 // Connect to the AxProvider of the HTML document and get the AxTree.
105 AxProviderPtr ax_provider
;
106 connection
->ConnectToService(&ax_provider
);
107 Array
<AxNodePtr
> ax_tree
;
108 ax_provider
->GetTree([&ax_tree
](Array
<AxNodePtr
> tree
) {
109 ax_tree
= tree
.Pass();
110 EXPECT_TRUE(QuitRunLoop());
112 ASSERT_TRUE(DoRunLoopWithTimeout());
114 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "Hello "));
115 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "World!"));
116 EXPECT_FALSE(AxTreeContainsText(ax_tree
, "foo"));