Commit 4bf6c8d6 authored by Yuke Liao's avatar Yuke Liao Committed by Commit Bot

[lacros] Mojo connection in browser_tests

This CL makes the browser_tests test launcher initiate and establish a
mojo connection with ash-chrome upon launching lacros-chrome.

Bug: 1120582
Change-Id: I2bc42a837804d988575378799044ea1d582972b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2402580Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Commit-Queue: Yuke Liao <liaoyuke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806835}
parent 907f3e0a
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "content/public/test/browser_test_base.h" #include "content/public/test/browser_test_base.h"
#include <fcntl.h>
#include <stddef.h> #include <stddef.h>
#include <iostream> #include <iostream>
...@@ -16,6 +17,8 @@ ...@@ -16,6 +17,8 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/debug/stack_trace.h" #include "base/debug/stack_trace.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/i18n/icu_util.h" #include "base/i18n/icu_util.h"
#include "base/location.h" #include "base/location.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -33,6 +36,7 @@ ...@@ -33,6 +36,7 @@
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/browser/browser_main_loop.h" #include "content/browser/browser_main_loop.h"
#include "content/browser/browser_thread_impl.h" #include "content/browser/browser_thread_impl.h"
#include "content/browser/network_service_instance_impl.h" #include "content/browser/network_service_instance_impl.h"
...@@ -62,6 +66,8 @@ ...@@ -62,6 +66,8 @@
#include "media/base/media_switches.h" #include "media/base/media_switches.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h" #include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "net/dns/mock_host_resolver.h" #include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/cpp/features.h" #include "services/network/public/cpp/features.h"
...@@ -109,6 +115,14 @@ ...@@ -109,6 +115,14 @@
#include "ui/aura/test/event_generator_delegate_aura.h" // nogncheck #include "ui/aura/test/event_generator_delegate_aura.h" // nogncheck
#endif #endif
#if BUILDFLAG(IS_LACROS)
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/platform/socket_utils_posix.h"
#endif
namespace content { namespace content {
namespace { namespace {
...@@ -338,6 +352,45 @@ void BrowserTestBase::SetUp() { ...@@ -338,6 +352,45 @@ void BrowserTestBase::SetUp() {
use_software_gl = false; use_software_gl = false;
#endif #endif
#if BUILDFLAG(IS_LACROS)
// If the test is running on the lacros environment, a file descriptor needs
// to be obtained and used to launch lacros-chrome so that a mojo connection
// between lacros-chrome and ash-chrome can be established.
// For more details, please see:
// //chrome/browser/chromeos/crosapi/test_mojo_connection_manager.h.
{
// TODO(crbug.com/1127581): Switch to use |kLacrosMojoSocketForTesting| in
// //chromeos/constants/chromeos_switches.h.
// Please refer to the CL comments for why it can't be done now:
// http://crrev.com/c/2402580/2/content/public/test/browser_test_base.cc
std::string socket_path =
command_line->GetSwitchValueASCII("lacros-mojo-socket-for-testing");
if (!socket_path.empty()) {
auto channel = mojo::NamedPlatformChannel::ConnectToServer(socket_path);
base::ScopedFD socket_fd = channel.TakePlatformHandle().TakeFD();
// Mark the channel as blocking.
int flags = fcntl(socket_fd.get(), F_GETFL);
PCHECK(flags != -1);
fcntl(socket_fd.get(), F_SETFL, flags & ~O_NONBLOCK);
uint8_t buf[32];
std::vector<base::ScopedFD> descriptors;
auto size = mojo::SocketRecvmsg(socket_fd.get(), buf, sizeof(buf),
&descriptors, true /*block*/);
if (size < 0)
PLOG(ERROR) << "Error receiving message from the socket";
ASSERT_EQ(1, size);
EXPECT_EQ(0u, buf[0]);
ASSERT_EQ(1u, descriptors.size());
// It's OK to release the FD because lacros-chrome's code will consume it.
command_line->AppendSwitchASCII(
mojo::PlatformChannel::kHandleSwitch,
base::NumberToString(descriptors[0].release()));
}
}
#endif
if (use_software_gl && !use_software_compositing_) if (use_software_gl && !use_software_compositing_)
command_line->AppendSwitch(switches::kOverrideUseSoftwareGLForTests); command_line->AppendSwitch(switches::kOverrideUseSoftwareGLForTests);
......
...@@ -387,6 +387,7 @@ static_library("test_support") { ...@@ -387,6 +387,7 @@ static_library("test_support") {
deps = [ deps = [
":content_test_mojo_bindings", ":content_test_mojo_bindings",
"//base/third_party/dynamic_annotations", "//base/third_party/dynamic_annotations",
"//build:chromeos_buildflags",
"//cc:test_support", "//cc:test_support",
"//components/viz/client", "//components/viz/client",
"//components/viz/host", "//components/viz/host",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment