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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
Index: mojo/services/html_viewer/html_viewer_apptest.cc
diff --git a/mojo/services/html_viewer/html_viewer_apptest.cc b/mojo/services/html_viewer/html_viewer_apptest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..99285f22c3ed530ab16db3efbf1a0057da17af0c
--- /dev/null
+++ b/mojo/services/html_viewer/html_viewer_apptest.cc
@@ -0,0 +1,158 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/bind.h"
+#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "mojo/public/cpp/application/application_impl.h"
+#include "mojo/public/cpp/application/application_test_base.h"
+#include "mojo/services/html_viewer/public/interfaces/html_viewer.mojom.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/mojo_services/src/accessibility/public/interfaces/accessibility.mojom.h"
+#include "third_party/mojo_services/src/http_server/public/cpp/http_server_util.h"
+#include "third_party/mojo_services/src/http_server/public/interfaces/http_server.mojom.h"
+#include "third_party/mojo_services/src/http_server/public/interfaces/http_server_factory.mojom.h"
+
+namespace mojo {
+
+namespace {
+
+// A simple test server and handler that uses the mojo:http_server service.
+class TestHttpServer : public http_server::HttpHandler {
sky 2015/04/02 16:56:17 Why do you need your own server instead of using t
+ public:
+ TestHttpServer(ApplicationImpl* application) : handler_binding_(this) {
+ // Construct a server from the factory with a local address.
+ http_server::HttpServerFactoryPtr http_server_factory;
+ application->ConnectToService("mojo:http_server", &http_server_factory);
+ NetAddressPtr local_address(NetAddress::New());
+ local_address->family = NET_ADDRESS_FAMILY_IPV4;
+ local_address->ipv4 = NetAddressIPv4::New();
+ local_address->ipv4->addr.resize(4);
+ local_address->ipv4->addr[0] = 127;
+ local_address->ipv4->addr[1] = 0;
+ local_address->ipv4->addr[2] = 0;
+ local_address->ipv4->addr[3] = 1;
+ local_address->ipv4->port = 0;
+ http_server_factory->CreateHttpServer(GetProxy(&http_server_).Pass(),
+ local_address.Pass());
+
+ // Get the assigned port.
+ http_server_->GetPort([=](uint16_t p) { this->assigned_port_ = p; });
+ http_server_.WaitForIncomingMethodCall();
+
+ // Set this object as the handler and wait for confirmation.
+ http_server::HttpHandlerPtr http_handler_ptr;
+ handler_binding_.Bind(GetProxy(&http_handler_ptr).Pass());
+ http_server_->SetHandler("/test", http_handler_ptr.Pass(),
+ [](bool result) { EXPECT_TRUE(result); });
+ http_server_.WaitForIncomingMethodCall();
+ }
+
+ uint16_t assigned_port() const { return assigned_port_; }
+
+ private:
+ // http_server::HttpHandler:
+ void HandleRequest(http_server::HttpRequestPtr request,
+ const Callback<void(http_server::HttpResponsePtr)>&
+ callback) override {
+ const char* kExampleHTML = "<!DOCTYPE html><html>Hello world!</html>";
+ callback.Run(http_server::CreateHttpResponse(200, kExampleHTML));
+ }
+
+ http_server::HttpServerPtr http_server_;
+ Binding<http_server::HttpHandler> handler_binding_;
+ uint16_t assigned_port_ = 0u;
+
+ MOJO_DISALLOW_COPY_AND_ASSIGN(TestHttpServer);
+};
+
+void PrintAxTree(Array<AxNodePtr> ax_tree) {
+ printf("MSW GetTree size %zu\n", ax_tree.size());
+ for (size_t i = 0; i < ax_tree.size(); ++i) {
+ const AxNodePtr& ax_node = ax_tree[i];
+ printf("MSW tree[%zu] id=%d parent=%d sibling=%d ",
+ i, ax_node->id, ax_node->parent_id, ax_node->next_sibling_id);
+ if (!ax_node->text.is_null())
+ printf("text=[%s] ", ax_node->text->content.data());
+ if (!ax_node->link.is_null())
+ printf("link=[%s] ", ax_node->link->url.data());
+ printf("\n");
+ }
+}
+
+} // namespace
+
+typedef test::ApplicationTestBase HTMLViewerTest;
+
+TEST_F(HTMLViewerTest, SimpleTest) {
+ TestHttpServer server(application_impl());
+
+ // TODO(msw): Test a url without a handler? http://127.0.0.1:%u/bad_url
+ // [ERROR:network_fetcher.cc(229)] Error (404: HTTP/1.1 404 Not Found):
+ // while fetching http://127.0.0.1:44419/bad_url
+
+ ApplicationConnection* connection = application_impl()->ConnectToApplication(
+ base::StringPrintf("http://127.0.0.1:%u/test", server.assigned_port()));
+ printf("MSW good url: %s\n", connection->GetConnectionURL().c_str());
+
+ // Connect to the HTMLViewerTestAPI of the HTMLViewerApplication.
+ {
+ base::RunLoop run_loop;
+ HTMLViewerTestAPIPtr html_viewer_test_api;
+ connection->ConnectToService(&html_viewer_test_api);
+ html_viewer_test_api->WaitForLoad(run_loop.QuitClosure());
+ run_loop.Run();
+ }
+
+ // Connect to the AxProvider of the HTMLDocument.
+ {
+ base::RunLoop run_loop;
+ AxProviderPtr ax_provider;
+ connection->ConnectToService(&ax_provider);
+ ax_provider->GetTree(
+ [&run_loop](Array<AxNodePtr> tree) {
+ PrintAxTree(tree.Pass());
+ run_loop.Quit();
+ });
+ run_loop.Run();
+ }
+
+ // TODO(msw): Connect to the ViewManagerClient service via HTMLDocument... ?
+ // TODO(msw): Embed the html_viewer for some visible UI? (in another test?)
+}
+
+// TEST_F(HTMLViewerTest, SimpleTest1) {
+// TestHttpServer server(application_impl());
+// ApplicationConnection* connection = application_impl()->ConnectToApplication(
+// base::StringPrintf("http://127.0.0.1:%u/test", server.assigned_port()));
+// printf("MSW good url: %s\n", connection->GetConnectionURL().c_str());
+
+// // Connect to the HTMLViewerTestAPI service...
+// HTMLViewerTestAPIPtr html_viewer_test_api;
+// connection->ConnectToService(&html_viewer_test_api);
+// // TODO(msw): Exercise the HTMLViewerTestAPI interface (load, wait, etc.)...
+
+// // TODO(msw): Let HTMLViewer connect to the network service itself...
+// NetworkServicePtr network_service;
+// ApplicationConnection* network_service_connection =
+// application_impl()->ConnectToApplication("mojo:network_service");
+// network_service_connection->ConnectToService(&network_service);
+
+// base::RunLoop run_loop;
+// html_viewer_test_api->LoadURL("foo", network_service.Pass(),
+// [&run_loop](bool result) {
+// EXPECT_TRUE(result);
+// run_loop.Quit();
+// });
+// run_loop.Run();
+
+// // TODO(msw): Exercise the AxProvider interface on the loaded document...
+// //AxProviderPtr ax_provider;
+// //connection->ConnectToService(&ax_provider);
+// //mojo::ConnectToService(service_provider.get(), &ax_provider);
+// //service_provider->ConnectToService("mojo:accessibility", &ax_provider);
+// //ax_provider->GetTree([](Array<AxNodePtr> tree) { PrintAxTree(tree.Pass()); });
+// }
+
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698