Roll src/third_party/skia 0a24297:ceadfef
[chromium-blink-merge.git] / components / html_viewer / ax_provider_apptest.cc
blobcc3638d950f100f102610068d3482f83ff474372
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.
5 #include "base/bind.h"
6 #include "base/run_loop.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/test/test_timeouts.h"
9 #include "mojo/application/public/cpp/application_impl.h"
10 #include "mojo/application/public/cpp/application_test_base.h"
11 #include "net/test/spawned_test_server/spawned_test_server.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/mojo_services/src/accessibility/public/interfaces/accessibility.mojom.h"
15 namespace mojo {
17 namespace {
19 base::RunLoop* current_run_loop = nullptr;
21 void TimeoutRunLoop(const base::Closure& timeout_task, bool* timeout) {
22 CHECK(current_run_loop);
23 *timeout = true;
24 timeout_task.Run();
27 bool DoRunLoopWithTimeout() {
28 if (current_run_loop != nullptr)
29 return false;
31 bool timeout = false;
32 base::RunLoop run_loop;
33 base::MessageLoop::current()->PostDelayedTask(
34 FROM_HERE, base::Bind(&TimeoutRunLoop, run_loop.QuitClosure(), &timeout),
35 TestTimeouts::action_timeout());
37 current_run_loop = &run_loop;
38 current_run_loop->Run();
39 current_run_loop = nullptr;
40 return !timeout;
43 void QuitRunLoop() {
44 current_run_loop->Quit();
45 current_run_loop = nullptr;
48 // Returns true if the tree contains a text node with contents matching |text|.
49 bool AxTreeContainsText(const Array<AxNodePtr>& tree, const String& text) {
50 for (size_t i = 0; i < tree.size(); ++i) {
51 if (!tree[i]->text.is_null() && tree[i]->text->content == text)
52 return true;
54 return false;
57 } // namespace
59 typedef test::ApplicationTestBase AXProviderTest;
61 TEST_F(AXProviderTest, HelloWorld) {
62 // Start a test server for net/data/test.html access.
63 net::SpawnedTestServer server(
64 net::SpawnedTestServer::TYPE_HTTP, net::SpawnedTestServer::kLocalhost,
65 base::FilePath(FILE_PATH_LITERAL("net/data")));
66 ASSERT_TRUE(server.Start());
68 // Connect to the URL through the mojo:html_viewer content handler.
69 const uint16_t assigned_port = server.host_port_pair().port();
70 mojo::URLRequestPtr request(mojo::URLRequest::New());
71 request->url = mojo::String::From(
72 base::StringPrintf("http://127.0.0.1:%u/files/test.html", assigned_port));
73 ApplicationConnection* connection = application_impl()->ConnectToApplication(
74 request.Pass());
76 // Connect to the AxProvider of the HTML document and get the AxTree.
77 AxProviderPtr ax_provider;
78 connection->ConnectToService(&ax_provider);
79 Array<AxNodePtr> ax_tree;
80 ax_provider->GetTree([&ax_tree](Array<AxNodePtr> tree) {
81 ax_tree = tree.Pass();
82 QuitRunLoop();
83 });
84 ASSERT_TRUE(DoRunLoopWithTimeout());
86 EXPECT_TRUE(AxTreeContainsText(ax_tree, "Hello "));
87 EXPECT_TRUE(AxTreeContainsText(ax_tree, "World!"));
88 EXPECT_FALSE(AxTreeContainsText(ax_tree, "foo"));
91 } // namespace mojo