Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: mojo/services/html_viewer/html_viewer_apptest.cc

Issue 1049013004: Add some simple HTMLViewer apptests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Continue cleanup. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "base/bind.h"
6 #include "base/run_loop.h"
7 #include "base/strings/stringprintf.h"
8 #include "mojo/public/cpp/application/application_impl.h"
9 #include "mojo/public/cpp/application/application_test_base.h"
10 #include "mojo/services/html_viewer/public/interfaces/html_viewer.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/mojo_services/src/accessibility/public/interfaces/accessib ility.mojom.h"
13 #include "third_party/mojo_services/src/http_server/public/cpp/http_server_util. h"
14 #include "third_party/mojo_services/src/http_server/public/interfaces/http_serve r.mojom.h"
15 #include "third_party/mojo_services/src/http_server/public/interfaces/http_serve r_factory.mojom.h"
16
17 namespace mojo {
18
19 namespace {
20
21 // A simple test server and handler that uses the mojo:http_server service.
22 class TestHttpServer : public http_server::HttpHandler {
sky 2015/04/02 16:56:17 Why do you need your own server instead of using t
23 public:
24 TestHttpServer(ApplicationImpl* application) : handler_binding_(this) {
25 // Construct a server from the factory with a local address.
26 http_server::HttpServerFactoryPtr http_server_factory;
27 application->ConnectToService("mojo:http_server", &http_server_factory);
28 NetAddressPtr local_address(NetAddress::New());
29 local_address->family = NET_ADDRESS_FAMILY_IPV4;
30 local_address->ipv4 = NetAddressIPv4::New();
31 local_address->ipv4->addr.resize(4);
32 local_address->ipv4->addr[0] = 127;
33 local_address->ipv4->addr[1] = 0;
34 local_address->ipv4->addr[2] = 0;
35 local_address->ipv4->addr[3] = 1;
36 local_address->ipv4->port = 0;
37 http_server_factory->CreateHttpServer(GetProxy(&http_server_).Pass(),
38 local_address.Pass());
39
40 // Get the assigned port.
41 http_server_->GetPort([=](uint16_t p) { this->assigned_port_ = p; });
42 http_server_.WaitForIncomingMethodCall();
43
44 // Set this object as the handler and wait for confirmation.
45 http_server::HttpHandlerPtr http_handler_ptr;
46 handler_binding_.Bind(GetProxy(&http_handler_ptr).Pass());
47 http_server_->SetHandler("/test", http_handler_ptr.Pass(),
48 [](bool result) { EXPECT_TRUE(result); });
49 http_server_.WaitForIncomingMethodCall();
50 }
51
52 uint16_t assigned_port() const { return assigned_port_; }
53
54 private:
55 // http_server::HttpHandler:
56 void HandleRequest(http_server::HttpRequestPtr request,
57 const Callback<void(http_server::HttpResponsePtr)>&
58 callback) override {
59 const char* kExampleHTML = "<!DOCTYPE html><html>Hello world!</html>";
60 callback.Run(http_server::CreateHttpResponse(200, kExampleHTML));
61 }
62
63 http_server::HttpServerPtr http_server_;
64 Binding<http_server::HttpHandler> handler_binding_;
65 uint16_t assigned_port_ = 0u;
66
67 MOJO_DISALLOW_COPY_AND_ASSIGN(TestHttpServer);
68 };
69
70 void PrintAxTree(Array<AxNodePtr> ax_tree) {
71 printf("MSW GetTree size %zu\n", ax_tree.size());
72 for (size_t i = 0; i < ax_tree.size(); ++i) {
73 const AxNodePtr& ax_node = ax_tree[i];
74 printf("MSW tree[%zu] id=%d parent=%d sibling=%d ",
75 i, ax_node->id, ax_node->parent_id, ax_node->next_sibling_id);
76 if (!ax_node->text.is_null())
77 printf("text=[%s] ", ax_node->text->content.data());
78 if (!ax_node->link.is_null())
79 printf("link=[%s] ", ax_node->link->url.data());
80 printf("\n");
81 }
82 }
83
84 } // namespace
85
86 typedef test::ApplicationTestBase HTMLViewerTest;
87
88 TEST_F(HTMLViewerTest, SimpleTest) {
89 TestHttpServer server(application_impl());
90
91 // TODO(msw): Test a url without a handler? http://127.0.0.1:%u/bad_url
92 // [ERROR:network_fetcher.cc(229)] Error (404: HTTP/1.1 404 Not Found):
93 // while fetching http://127.0.0.1:44419/bad_url
94
95 ApplicationConnection* connection = application_impl()->ConnectToApplication(
96 base::StringPrintf("http://127.0.0.1:%u/test", server.assigned_port()));
97 printf("MSW good url: %s\n", connection->GetConnectionURL().c_str());
98
99 // Connect to the HTMLViewerTestAPI of the HTMLViewerApplication.
100 {
101 base::RunLoop run_loop;
102 HTMLViewerTestAPIPtr html_viewer_test_api;
103 connection->ConnectToService(&html_viewer_test_api);
104 html_viewer_test_api->WaitForLoad(run_loop.QuitClosure());
105 run_loop.Run();
106 }
107
108 // Connect to the AxProvider of the HTMLDocument.
109 {
110 base::RunLoop run_loop;
111 AxProviderPtr ax_provider;
112 connection->ConnectToService(&ax_provider);
113 ax_provider->GetTree(
114 [&run_loop](Array<AxNodePtr> tree) {
115 PrintAxTree(tree.Pass());
116 run_loop.Quit();
117 });
118 run_loop.Run();
119 }
120
121 // TODO(msw): Connect to the ViewManagerClient service via HTMLDocument... ?
122 // TODO(msw): Embed the html_viewer for some visible UI? (in another test?)
123 }
124
125 // TEST_F(HTMLViewerTest, SimpleTest1) {
126 // TestHttpServer server(application_impl());
127 // ApplicationConnection* connection = application_impl()->ConnectToApplicatio n(
128 // base::StringPrintf("http://127.0.0.1:%u/test", server.assigned_port())) ;
129 // printf("MSW good url: %s\n", connection->GetConnectionURL().c_str());
130
131 // // Connect to the HTMLViewerTestAPI service...
132 // HTMLViewerTestAPIPtr html_viewer_test_api;
133 // connection->ConnectToService(&html_viewer_test_api);
134 // // TODO(msw): Exercise the HTMLViewerTestAPI interface (load, wait, etc.).. .
135
136 // // TODO(msw): Let HTMLViewer connect to the network service itself...
137 // NetworkServicePtr network_service;
138 // ApplicationConnection* network_service_connection =
139 // application_impl()->ConnectToApplication("mojo:network_service");
140 // network_service_connection->ConnectToService(&network_service);
141
142 // base::RunLoop run_loop;
143 // html_viewer_test_api->LoadURL("foo", network_service.Pass(),
144 // [&run_loop](bool result) {
145 // EXPECT_TRUE(result);
146 // run_loop.Quit();
147 // });
148 // run_loop.Run();
149
150 // // TODO(msw): Exercise the AxProvider interface on the loaded document...
151 // //AxProviderPtr ax_provider;
152 // //connection->ConnectToService(&ax_provider);
153 // //mojo::ConnectToService(service_provider.get(), &ax_provider);
154 // //service_provider->ConnectToService("mojo:accessibility", &ax_provider);
155 // //ax_provider->GetTree([](Array<AxNodePtr> tree) { PrintAxTree(tree.Pass()) ; });
156 // }
157
158 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698