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 "mandoline/tab/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 mandoline::FrameTreeServer
{
35 TestFrameTreeServer() {}
36 ~TestFrameTreeServer() override
{}
38 // mandoline::FrameTreeServer:
39 void PostMessageEventToFrame(uint32_t source_frame_id
,
40 uint32_t target_frame_id
,
41 mandoline::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 SetClientProperty(uint32_t frame_id
,
46 const mojo::String
& name
,
47 mojo::Array
<uint8_t> value
) override
{}
48 void OnCreatedFrame(uint32_t parent_id
,
50 mojo::Map
<mojo::String
, mojo::Array
<uint8_t>>
51 client_properties
) override
{}
52 void RequestNavigate(mandoline::NavigationTargetType target_type
,
53 uint32_t target_frame_id
,
54 mojo::URLRequestPtr request
) override
{}
55 void DidNavigateLocally(uint32_t frame_id
, const mojo::String
& url
) override
{
59 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeServer
);
64 using AXProviderTest
= ViewManagerTestBase
;
66 TEST_F(AXProviderTest
, HelloWorld
) {
67 // Start a test server for net/data/test.html access.
68 net::SpawnedTestServer
server(
69 net::SpawnedTestServer::TYPE_HTTP
, net::SpawnedTestServer::kLocalhost
,
70 base::FilePath(FILE_PATH_LITERAL("net/data")));
71 ASSERT_TRUE(server
.Start());
73 // Connect to the URL through the mojo:html_viewer content handler.
74 const uint16_t assigned_port
= server
.host_port_pair().port();
75 mojo::URLRequestPtr
request(mojo::URLRequest::New());
76 request
->url
= mojo::String::From(
77 base::StringPrintf("http://127.0.0.1:%u/files/test.html", assigned_port
));
78 scoped_ptr
<ApplicationConnection
> connection
=
79 application_impl()->ConnectToApplication(request
.Pass());
81 // Embed the html_viewer in a View.
82 ViewTreeClientPtr tree_client
;
83 connection
->ConnectToService(&tree_client
);
84 View
* embed_view
= window_manager()->CreateView();
85 embed_view
->Embed(tree_client
.Pass());
87 TestFrameTreeServer frame_tree_server
;
88 mandoline::FrameTreeServerPtr frame_tree_server_ptr
;
89 mojo::Binding
<mandoline::FrameTreeServer
> frame_tree_server_binding(
91 frame_tree_server_binding
.Bind(GetProxy(&frame_tree_server_ptr
).Pass());
93 mojo::Array
<mandoline::FrameDataPtr
> array(1u);
94 array
[0] = mandoline::FrameData::New().Pass();
95 array
[0]->frame_id
= embed_view
->id();
96 array
[0]->parent_id
= 0u;
98 mandoline::FrameTreeClientPtr frame_tree_client
;
99 connection
->ConnectToService(&frame_tree_client
);
100 frame_tree_client
->OnConnect(frame_tree_server_ptr
.Pass(), 1u,
103 // Connect to the AxProvider of the HTML document and get the AxTree.
104 AxProviderPtr ax_provider
;
105 connection
->ConnectToService(&ax_provider
);
106 Array
<AxNodePtr
> ax_tree
;
107 ax_provider
->GetTree([&ax_tree
](Array
<AxNodePtr
> tree
) {
108 ax_tree
= tree
.Pass();
109 EXPECT_TRUE(QuitRunLoop());
111 ASSERT_TRUE(DoRunLoopWithTimeout());
113 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "Hello "));
114 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "World!"));
115 EXPECT_FALSE(AxTreeContainsText(ax_tree
, "foo"));