forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_instance_host_integration_test.cc
149 lines (127 loc) · 6.26 KB
/
web_instance_host_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2021 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 <fuchsia/io/cpp/fidl.h>
#include <fuchsia/web/cpp/fidl.h>
#include <lib/fdio/directory.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/sys/cpp/component_context.h>
#include "base/command_line.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"
#include "base/strings/string_piece_forward.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "fuchsia_web/common/test/fit_adapter.h"
#include "fuchsia_web/common/test/frame_test_util.h"
#include "fuchsia_web/common/test/test_devtools_list_fetcher.h"
#include "fuchsia_web/common/test/test_navigation_listener.h"
#include "fuchsia_web/webengine/test/frame_for_test.h"
#include "fuchsia_web/webinstance_host/web_instance_host.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
class WebInstanceHostIntegrationTest : public testing::Test {
public:
WebInstanceHostIntegrationTest()
: task_environment_(base::test::TaskEnvironment::MainThreadType::IO) {}
~WebInstanceHostIntegrationTest() override = default;
WebInstanceHostIntegrationTest(const WebInstanceHostIntegrationTest&) =
delete;
WebInstanceHostIntegrationTest& operator=(
const WebInstanceHostIntegrationTest&) = delete;
void SetUp() override {
// Override command line. This is necessary to get WebEngine logs.
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII("enable-logging", "stderr");
embedded_test_server_.ServeFilesFromSourceDirectory(
"fuchsia_web/webengine/test/data");
net::test_server::RegisterDefaultHandlers(&embedded_test_server_);
ASSERT_TRUE(embedded_test_server_.Start());
}
protected:
fuchsia::web::CreateContextParams TestContextParams() {
fuchsia::web::CreateContextParams create_params;
create_params.set_features(fuchsia::web::ContextFeatureFlags::NETWORK);
zx_status_t status =
base::ComponentContextForProcess()->svc()->CloneChannel(
create_params.mutable_service_directory()->NewRequest());
ZX_CHECK(status == ZX_OK, status) << "ServiceDirectory::CloneChannel";
return create_params;
}
void CreateContext(fuchsia::web::CreateContextParams context_params) {
EXPECT_FALSE(context_);
fuchsia::io::DirectoryHandle web_instance_services;
web_instance_host_.CreateInstanceForContextWithCopiedArgs(
std::move(context_params), web_instance_services.NewRequest(),
*base::CommandLine::ForCurrentProcess());
web_instance_services_ = std::make_unique<sys::ServiceDirectory>(
std::move(web_instance_services));
zx_status_t result = web_instance_services_->Connect(context_.NewRequest());
ZX_CHECK(result == ZX_OK, result) << "fdio_service_connect_at";
context_.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
}
void ConnectFrameHost() {
zx_status_t result =
web_instance_services_->Connect(frame_host_.NewRequest());
ZX_CHECK(result == ZX_OK, result) << "fdio_service_connect_at";
frame_host_.set_error_handler([](zx_status_t status) { ADD_FAILURE(); });
}
const base::test::TaskEnvironment task_environment_;
fidl::InterfaceHandle<fuchsia::sys::ComponentController>
web_engine_controller_;
WebInstanceHost web_instance_host_;
std::unique_ptr<sys::ServiceDirectory> web_instance_services_;
fuchsia::web::ContextPtr context_;
fuchsia::web::FrameHostPtr frame_host_;
net::EmbeddedTestServer embedded_test_server_;
};
// Check that connecting and disconnecting to the FrameHost service does not
// trigger shutdown of the devtools service.
TEST_F(WebInstanceHostIntegrationTest, FrameHostDebugging) {
fuchsia::web::CreateContextParams create_params = TestContextParams();
create_params.set_remote_debugging_port(0);
CreateContext(std::move(create_params));
fuchsia::web::CreateFrameParams create_frame_params;
create_frame_params.set_enable_remote_debugging(true);
auto frame = FrameForTest::Create(context_, std::move(create_frame_params));
// Expect to receive a notification of the selected DevTools port.
base::test::TestFuture<fuchsia::web::Context_GetRemoteDebuggingPort_Result>
port_receiver;
context_->GetRemoteDebuggingPort(
CallbackToFitFunction(port_receiver.GetCallback()));
ASSERT_TRUE(port_receiver.Wait());
ASSERT_TRUE(port_receiver.Get().is_response());
uint16_t remote_debugging_port = port_receiver.Get().response().port;
ASSERT_TRUE(remote_debugging_port != 0);
// Navigate to a URL, the devtools service should be active and report a
// single page.
GURL url = embedded_test_server_.GetURL("/defaultresponse");
ASSERT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url.spec()));
frame.navigation_listener().RunUntilUrlEquals(url);
base::Value devtools_list = GetDevToolsListFromPort(remote_debugging_port);
ASSERT_TRUE(devtools_list.is_list());
EXPECT_EQ(devtools_list.GetListDeprecated().size(), 1u);
base::Value* devtools_url =
devtools_list.GetListDeprecated()[0].FindPath("url");
ASSERT_TRUE(devtools_url->is_string());
EXPECT_EQ(devtools_url->GetString(), url);
// Connect to the FrameHost and immediately disconnect it.
ConnectFrameHost();
frame_host_.Unbind();
// Navigate to a different page. The devtools service should still be active
// and report the new page.
GURL url2 = embedded_test_server_.GetURL("/title1.html");
ASSERT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
url2.spec()));
frame.navigation_listener().RunUntilUrlEquals(url2);
devtools_list = GetDevToolsListFromPort(remote_debugging_port);
ASSERT_TRUE(devtools_list.is_list());
EXPECT_EQ(devtools_list.GetListDeprecated().size(), 1u);
devtools_url = devtools_list.GetListDeprecated()[0].FindPath("url");
ASSERT_TRUE(devtools_url->is_string());
EXPECT_EQ(devtools_url->GetString(), url2);
}