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_manager.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 // Switch to enable out of process iframes.
34 const char kOOPIF
[] = "oopifs";
37 return base::CommandLine::ForCurrentProcess()->HasSwitch(kOOPIF
);
40 class TestFrameTreeServer
: public mandoline::FrameTreeServer
{
42 TestFrameTreeServer() {}
43 ~TestFrameTreeServer() override
{}
45 // mandoline::FrameTreeServer:
46 void PostMessageEventToFrame(uint32_t frame_id
,
47 mandoline::MessageEventPtr event
) override
{}
48 void NavigateFrame(uint32_t frame_id
) override
{}
49 void ReloadFrame(uint32_t frame_id
) override
{}
52 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeServer
);
57 using AXProviderTest
= ViewManagerTestBase
;
59 TEST_F(AXProviderTest
, HelloWorld
) {
60 // Start a test server for net/data/test.html access.
61 net::SpawnedTestServer
server(
62 net::SpawnedTestServer::TYPE_HTTP
, net::SpawnedTestServer::kLocalhost
,
63 base::FilePath(FILE_PATH_LITERAL("net/data")));
64 ASSERT_TRUE(server
.Start());
66 // Connect to the URL through the mojo:html_viewer content handler.
67 const uint16_t assigned_port
= server
.host_port_pair().port();
68 mojo::URLRequestPtr
request(mojo::URLRequest::New());
69 request
->url
= mojo::String::From(
70 base::StringPrintf("http://127.0.0.1:%u/files/test.html", assigned_port
));
71 ApplicationConnection
* connection
= application_impl()->ConnectToApplication(
74 // Embed the html_viewer in a View.
75 ViewManagerClientPtr view_manager_client
;
76 connection
->ConnectToService(&view_manager_client
);
77 View
* embed_view
= window_manager()->CreateView();
78 embed_view
->Embed(view_manager_client
.Pass());
81 TestFrameTreeServer frame_tree_server
;
82 mandoline::FrameTreeServerPtr frame_tree_server_ptr
;
83 mojo::Binding
<mandoline::FrameTreeServer
> frame_tree_server_binding(
85 frame_tree_server_binding
.Bind(GetProxy(&frame_tree_server_ptr
).Pass());
87 mojo::Array
<mandoline::FrameDataPtr
> array(1u);
88 array
[0] = mandoline::FrameData::New().Pass();
89 array
[0]->frame_id
= embed_view
->id();
90 array
[0]->parent_id
= 0u;
92 mandoline::FrameTreeClientPtr frame_tree_client
;
93 connection
->ConnectToService(&frame_tree_client
);
94 frame_tree_client
->OnConnect(frame_tree_server_ptr
.Pass(), array
.Pass());
97 // Connect to the AxProvider of the HTML document and get the AxTree.
98 AxProviderPtr ax_provider
;
99 connection
->ConnectToService(&ax_provider
);
100 Array
<AxNodePtr
> ax_tree
;
101 ax_provider
->GetTree([&ax_tree
](Array
<AxNodePtr
> tree
) {
102 ax_tree
= tree
.Pass();
103 EXPECT_TRUE(QuitRunLoop());
105 ASSERT_TRUE(DoRunLoopWithTimeout());
107 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "Hello "));
108 EXPECT_TRUE(AxTreeContainsText(ax_tree
, "World!"));
109 EXPECT_FALSE(AxTreeContainsText(ax_tree
, "foo"));