Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / components / html_viewer / ax_provider_apptest.cc
blob130a56c18057df01207ab3df567ac70afb3359f5
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/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"
20 namespace mojo {
22 namespace {
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)
28 return true;
30 return false;
33 // Switch to enable out of process iframes.
34 const char kOOPIF[] = "oopifs";
36 bool EnableOOPIFs() {
37 return base::CommandLine::ForCurrentProcess()->HasSwitch(kOOPIF);
40 class TestFrameTreeServer : public mandoline::FrameTreeServer {
41 public:
42 TestFrameTreeServer() {}
43 ~TestFrameTreeServer() override {}
45 // mandoline::FrameTreeServer:
46 void PostMessageEventToFrame(uint32_t frame_id,
47 mandoline::MessageEventPtr event) override {}
48 void LoadingStarted(uint32_t frame_id) override {}
49 void LoadingStopped(uint32_t frame_id) override {}
50 void ProgressChanged(uint32_t frame_id, double progress) override {}
51 void SetFrameName(uint32_t frame_id, const mojo::String& name) override {}
52 void OnCreatedFrame(uint32_t parent_id, uint32_t frame_id) override {}
53 void RequestNavigate(uint32_t frame_id,
54 mandoline::NavigationTarget target,
55 mojo::URLRequestPtr request) override {}
56 void DidNavigateLocally(uint32_t frame_id, const mojo::String& url) override {
59 private:
60 DISALLOW_COPY_AND_ASSIGN(TestFrameTreeServer);
63 } // namespace
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 ApplicationConnection* connection = application_impl()->ConnectToApplication(
80 request.Pass());
82 // Embed the html_viewer in a View.
83 ViewManagerClientPtr view_manager_client;
84 connection->ConnectToService(&view_manager_client);
85 View* embed_view = window_manager()->CreateView();
86 embed_view->Embed(view_manager_client.Pass());
88 if (EnableOOPIFs()) {
89 TestFrameTreeServer frame_tree_server;
90 mandoline::FrameTreeServerPtr frame_tree_server_ptr;
91 mojo::Binding<mandoline::FrameTreeServer> frame_tree_server_binding(
92 &frame_tree_server);
93 frame_tree_server_binding.Bind(GetProxy(&frame_tree_server_ptr).Pass());
95 mojo::Array<mandoline::FrameDataPtr> array(1u);
96 array[0] = mandoline::FrameData::New().Pass();
97 array[0]->frame_id = embed_view->id();
98 array[0]->parent_id = 0u;
99 array[0]->origin = "origin";
101 mandoline::FrameTreeClientPtr frame_tree_client;
102 connection->ConnectToService(&frame_tree_client);
103 frame_tree_client->OnConnect(frame_tree_server_ptr.Pass(), array.Pass());
106 // Connect to the AxProvider of the HTML document and get the AxTree.
107 AxProviderPtr ax_provider;
108 connection->ConnectToService(&ax_provider);
109 Array<AxNodePtr> ax_tree;
110 ax_provider->GetTree([&ax_tree](Array<AxNodePtr> tree) {
111 ax_tree = tree.Pass();
112 EXPECT_TRUE(QuitRunLoop());
114 ASSERT_TRUE(DoRunLoopWithTimeout());
116 EXPECT_TRUE(AxTreeContainsText(ax_tree, "Hello "));
117 EXPECT_TRUE(AxTreeContainsText(ax_tree, "World!"));
118 EXPECT_FALSE(AxTreeContainsText(ax_tree, "foo"));
121 } // namespace mojo