From 002abf279dc5781fc484b2e8b6dc9dc0b1b2833a Mon Sep 17 00:00:00 2001 From: John Abd-El-Malek Date: Wed, 18 Sep 2019 06:32:39 +0000 Subject: [PATCH] Create weblayer_browsertests target and one simple test. This will be added to FYI bots in a followup cl. Change-Id: I8ca2e3ce61e628cf6e342a8ab4fc604975c0d225 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1809897 Commit-Queue: John Abd-El-Malek Auto-Submit: John Abd-El-Malek Reviewed-by: Scott Violet Cr-Commit-Position: refs/heads/master@{#697497} --- BUILD.gn | 5 + weblayer/browser/browser_main_parts_impl.cc | 12 +- weblayer/browser/browser_main_parts_impl.h | 5 + weblayer/shell/BUILD.gn | 14 +- weblayer/shell/app/shell_main.cc | 109 +--------------- weblayer/shell/app/shell_main_params.cc | 127 +++++++++++++++++++ weblayer/shell/app/shell_main_params.h | 15 +++ weblayer/shell/browser/shell.h | 5 + weblayer/shell/common/shell_switches.cc | 12 ++ weblayer/shell/common/shell_switches.h | 14 ++ weblayer/test/BUILD.gn | 44 +++++++ weblayer/test/DEPS | 4 + weblayer/test/browsertests_main.cc | 15 +++ weblayer/test/data/simple_page.html | 6 + weblayer/test/test_launcher_delegate_impl.cc | 38 ++++++ weblayer/test/test_launcher_delegate_impl.h | 25 ++++ weblayer/test/weblayer_browser_test.cc | 32 +++++ weblayer/test/weblayer_browser_test.h | 36 ++++++ weblayer/test/weblayer_browser_test_test.cc | 19 +++ weblayer/test/weblayer_browser_test_utils.cc | 57 +++++++++ weblayer/test/weblayer_browser_test_utils.h | 18 +++ 21 files changed, 502 insertions(+), 110 deletions(-) create mode 100644 weblayer/shell/app/shell_main_params.cc create mode 100644 weblayer/shell/app/shell_main_params.h create mode 100644 weblayer/shell/common/shell_switches.cc create mode 100644 weblayer/shell/common/shell_switches.h create mode 100644 weblayer/test/BUILD.gn create mode 100644 weblayer/test/DEPS create mode 100644 weblayer/test/browsertests_main.cc create mode 100644 weblayer/test/data/simple_page.html create mode 100644 weblayer/test/test_launcher_delegate_impl.cc create mode 100644 weblayer/test/test_launcher_delegate_impl.h create mode 100644 weblayer/test/weblayer_browser_test.cc create mode 100644 weblayer/test/weblayer_browser_test.h create mode 100644 weblayer/test/weblayer_browser_test_test.cc create mode 100644 weblayer/test/weblayer_browser_test_utils.cc create mode 100644 weblayer/test/weblayer_browser_test_utils.h diff --git a/BUILD.gn b/BUILD.gn index 9a9ef73398222a..ad0028bd6d364a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -196,6 +196,11 @@ group("gn_all") { deps += [ "//weblayer/shell:weblayer_shell" ] } + # TODO: enable this on Android. + if (is_win || (is_linux && !is_chromeos)) { + deps += [ "//weblayer/test:weblayer_browsertests" ] + } + if (!is_ios && !is_android) { deps += [ "//components/cronet:cronet_tests", diff --git a/weblayer/browser/browser_main_parts_impl.cc b/weblayer/browser/browser_main_parts_impl.cc index 141e501b532589..c20c0095e97bc0 100644 --- a/weblayer/browser/browser_main_parts_impl.cc +++ b/weblayer/browser/browser_main_parts_impl.cc @@ -56,7 +56,7 @@ void StopMessageLoop(base::OnceClosure quit_closure) { BrowserMainPartsImpl::BrowserMainPartsImpl( MainParams* params, const content::MainFunctionParams& main_function_params) - : params_(params) {} + : params_(params), main_function_params_(main_function_params) {} BrowserMainPartsImpl::~BrowserMainPartsImpl() = default; @@ -83,6 +83,16 @@ int BrowserMainPartsImpl::PreEarlyInitialization() { void BrowserMainPartsImpl::PreMainMessageLoopRun() { ui::MaterialDesignController::Initialize(); params_->delegate->PreMainMessageLoopRun(); + + if (main_function_params_.ui_task) { + main_function_params_.ui_task->Run(); + delete main_function_params_.ui_task; + run_message_loop_ = false; + } +} + +bool BrowserMainPartsImpl::MainMessageLoopRun(int* result_code) { + return !run_message_loop_; } void BrowserMainPartsImpl::PreDefaultMainMessageLoopRun( diff --git a/weblayer/browser/browser_main_parts_impl.h b/weblayer/browser/browser_main_parts_impl.h index 00dd41a7653598..eb03851e8b4e53 100644 --- a/weblayer/browser/browser_main_parts_impl.h +++ b/weblayer/browser/browser_main_parts_impl.h @@ -26,11 +26,16 @@ class BrowserMainPartsImpl : public content::BrowserMainParts { int PreEarlyInitialization() override; void PreMainMessageLoopStart() override; void PreMainMessageLoopRun() override; + bool MainMessageLoopRun(int* result_code) override; void PreDefaultMainMessageLoopRun(base::OnceClosure quit_closure) override; private: MainParams* params_; + // For running weblayer_browsertests. + const content::MainFunctionParams main_function_params_; + bool run_message_loop_ = true; + DISALLOW_COPY_AND_ASSIGN(BrowserMainPartsImpl); }; diff --git a/weblayer/shell/BUILD.gn b/weblayer/shell/BUILD.gn index 44d80ae829bce3..14cf54c4379980 100644 --- a/weblayer/shell/BUILD.gn +++ b/weblayer/shell/BUILD.gn @@ -23,8 +23,12 @@ declare_args() { jumbo_static_library("weblayer_shell_lib") { testonly = true sources = [ + "app/shell_main_params.cc", + "app/shell_main_params.h", "browser/shell.cc", "browser/shell.h", + "common/shell_switches.cc", + "common/shell_switches.h", ] configs += [ @@ -34,6 +38,11 @@ jumbo_static_library("weblayer_shell_lib") { "//build/config/compiler:no_size_t_to_int_warning", ] + defines = [ + "WEBLAYER_SHELL_VERSION=\"$weblayer_shell_version\"", + "WEBLAYER_SHELL_MAJOR_VERSION=\"$weblayer_shell_major_version\"", + ] + public_deps = [ "//weblayer:weblayer_lib", ] @@ -239,10 +248,7 @@ if (is_android) { sources += [ "app/shell.rc" ] } - defines = [ - "WEBLAYER_SHELL_VERSION=\"$weblayer_shell_version\"", - "WEBLAYER_SHELL_MAJOR_VERSION=\"$weblayer_shell_major_version\"", - ] + defines = [] deps = [ ":pak", diff --git a/weblayer/shell/app/shell_main.cc b/weblayer/shell/app/shell_main.cc index 2fed096e3680b2..41a26d12b707d0 100644 --- a/weblayer/shell/app/shell_main.cc +++ b/weblayer/shell/app/shell_main.cc @@ -2,125 +2,24 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "base/callback.h" -#include "base/command_line.h" -#include "base/environment.h" -#include "base/files/file_path.h" -#include "base/files/file_util.h" -#include "base/no_destructor.h" -#include "base/path_service.h" -#include "build/build_config.h" -#include "net/base/filename_util.h" -#include "url/gurl.h" #include "weblayer/public/main.h" -#include "weblayer/public/profile.h" -#include "weblayer/shell/browser/shell.h" - -#if defined(OS_WIN) -#include "base/base_paths_win.h" -#elif defined(OS_LINUX) -#include "base/nix/xdg_util.h" -#endif - -namespace { - -GURL GetStartupURL() { - base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); - const base::CommandLine::StringVector& args = command_line->GetArgs(); - -#if defined(OS_ANDROID) - // Delay renderer creation on Android until surface is ready. - return GURL(); -#endif - - if (args.empty()) - return GURL("https://www.google.com/"); - - GURL url(args[0]); - if (url.is_valid() && url.has_scheme()) - return url; - - return net::FilePathToFileURL( - base::MakeAbsoluteFilePath(base::FilePath(args[0]))); -} - -class MainDelegateImpl : public weblayer::MainDelegate { - public: - void PreMainMessageLoopRun() override { - InitializeProfiles(); - - weblayer::Shell::Initialize(); - - weblayer::Shell::CreateNewWindow(profile_.get(), GetStartupURL(), - gfx::Size()); - } - - void SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) override { - weblayer::Shell::SetMainMessageLoopQuitClosure(std::move(quit_closure)); - } - - private: - void InitializeProfiles() { - base::FilePath path; -#if defined(OS_WIN) - CHECK(base::PathService::Get(base::DIR_LOCAL_APP_DATA, &path)); - path = path.AppendASCII("web_shell"); -#elif defined(OS_LINUX) - std::unique_ptr env(base::Environment::Create()); - base::FilePath config_dir(base::nix::GetXDGDirectory( - env.get(), base::nix::kXdgConfigHomeEnvVar, base::nix::kDotConfigDir)); - path = config_dir.AppendASCII("web_shell"); -#elif defined(OS_ANDROID) - CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, &path)); - path = path.AppendASCII("web_shell"); -#else - NOTIMPLEMENTED(); -#endif - - if (!base::PathExists(path)) - base::CreateDirectory(path); - - profile_ = weblayer::Profile::Create(path); - - // TODO: create an incognito profile as well. - } - - std::unique_ptr profile_; -}; - -weblayer::MainParams CreateMainParams() { - static const base::NoDestructor weblayer_delegate; - weblayer::MainParams params; - params.delegate = const_cast(&(*weblayer_delegate)); - - base::PathService::Get(base::DIR_EXE, ¶ms.log_filename); - params.log_filename = params.log_filename.AppendASCII("weblayer_shell.log"); - - params.pak_name = "weblayer.pak"; - - params.brand = "weblayer_shell"; - params.full_version = WEBLAYER_SHELL_VERSION; - params.major_version = WEBLAYER_SHELL_MAJOR_VERSION; - return params; -} - -} // namespace +#include "weblayer/shell/app/shell_main_params.h" #if defined(OS_WIN) #if defined(WIN_CONSOLE_APP) int main() { - return weblayer::Main(CreateMainParams()); + return weblayer::Main(weblayer::CreateMainParams()); #else int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) { - return weblayer::Main(CreateMainParams(), instance); + return weblayer::Main(weblayer::CreateMainParams(), instance); #endif } #else int main(int argc, const char** argv) { - return weblayer::Main(CreateMainParams(), argc, argv); + return weblayer::Main(weblayer::CreateMainParams(), argc, argv); } #endif // OS_POSIX diff --git a/weblayer/shell/app/shell_main_params.cc b/weblayer/shell/app/shell_main_params.cc new file mode 100644 index 00000000000000..3524d87ddaf8c1 --- /dev/null +++ b/weblayer/shell/app/shell_main_params.cc @@ -0,0 +1,127 @@ +// Copyright 2019 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 "weblayer/shell/app/shell_main_params.h" + +#include "base/callback.h" +#include "base/command_line.h" +#include "base/environment.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/no_destructor.h" +#include "base/path_service.h" +#include "build/build_config.h" +#include "net/base/filename_util.h" +#include "url/gurl.h" +#include "weblayer/public/main.h" +#include "weblayer/public/profile.h" +#include "weblayer/shell/browser/shell.h" +#include "weblayer/shell/common/shell_switches.h" + +#if defined(OS_WIN) +#include "base/base_paths_win.h" +#elif defined(OS_LINUX) +#include "base/nix/xdg_util.h" +#endif + +namespace weblayer { + +namespace { + +GURL GetStartupURL() { + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); + const base::CommandLine::StringVector& args = command_line->GetArgs(); + +#if defined(OS_ANDROID) + // Delay renderer creation on Android until surface is ready. + return GURL(); +#endif + + if (args.empty()) + return GURL("https://www.google.com/"); + + GURL url(args[0]); + if (url.is_valid() && url.has_scheme()) + return url; + + return net::FilePathToFileURL( + base::MakeAbsoluteFilePath(base::FilePath(args[0]))); +} + +class MainDelegateImpl : public MainDelegate { + public: + void PreMainMessageLoopRun() override { + InitializeProfiles(); + + Shell::Initialize(); + + Shell::CreateNewWindow(profile_.get(), GetStartupURL(), gfx::Size()); + } + + void SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure) override { + Shell::SetMainMessageLoopQuitClosure(std::move(quit_closure)); + } + + private: + void InitializeProfiles() { + base::FilePath path; + + base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); + if (cmd_line->HasSwitch(switches::kWebLayerShellDataPath)) { + path = cmd_line->GetSwitchValuePath(switches::kWebLayerShellDataPath); + if (base::DirectoryExists(path) || base::CreateDirectory(path)) { + // Profile needs an absolute path, which we would normally get via + // PathService. In this case, manually ensure the path is absolute. + if (!path.IsAbsolute()) + path = base::MakeAbsoluteFilePath(path); + } else { + LOG(ERROR) << "Unable to create data-path directory: " << path.value(); + } + } else { +#if defined(OS_WIN) + CHECK(base::PathService::Get(base::DIR_LOCAL_APP_DATA, &path)); + path = path.AppendASCII("web_shell"); +#elif defined(OS_LINUX) + std::unique_ptr env(base::Environment::Create()); + base::FilePath config_dir( + base::nix::GetXDGDirectory(env.get(), base::nix::kXdgConfigHomeEnvVar, + base::nix::kDotConfigDir)); + path = config_dir.AppendASCII("web_shell"); +#elif defined(OS_ANDROID) + CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, &path)); + path = path.AppendASCII("web_shell"); +#else + NOTIMPLEMENTED(); +#endif + if (!base::PathExists(path)) + base::CreateDirectory(path); + } + + profile_ = Profile::Create(path); + + // TODO: create an incognito profile as well. + } + + std::unique_ptr profile_; +}; + +} // namespace + +MainParams CreateMainParams() { + static const base::NoDestructor weblayer_delegate; + MainParams params; + params.delegate = const_cast(&(*weblayer_delegate)); + + base::PathService::Get(base::DIR_EXE, ¶ms.log_filename); + params.log_filename = params.log_filename.AppendASCII("weblayer_shell.log"); + + params.pak_name = "weblayer.pak"; + + params.brand = "weblayer_shell"; + params.full_version = WEBLAYER_SHELL_VERSION; + params.major_version = WEBLAYER_SHELL_MAJOR_VERSION; + return params; +} + +} // namespace weblayer diff --git a/weblayer/shell/app/shell_main_params.h b/weblayer/shell/app/shell_main_params.h new file mode 100644 index 00000000000000..15b99a281e9fde --- /dev/null +++ b/weblayer/shell/app/shell_main_params.h @@ -0,0 +1,15 @@ +// Copyright 2019 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. + +#ifndef WEBLAYER_SHELL_APP_SHELL_MAIN_PARAMS_H_ +#define WEBLAYER_SHELL_APP_SHELL_MAIN_PARAMS_H_ + +namespace weblayer { +struct MainParams; + +weblayer::MainParams CreateMainParams(); + +} // namespace weblayer + +#endif // WEBLAYER_SHELL_APP_SHELL_MAIN_PARAMS_H_ diff --git a/weblayer/shell/browser/shell.h b/weblayer/shell/browser/shell.h index c429bfb6cc9817..51a3de583da04a 100644 --- a/weblayer/shell/browser/shell.h +++ b/weblayer/shell/browser/shell.h @@ -56,6 +56,9 @@ class Shell : public BrowserObserver { const GURL& url, const gfx::Size& initial_size); + // Returns the currently open windows. + static std::vector& windows() { return windows_; } + // Closes all windows, pumps teardown tasks, then returns. The main message // loop will be signalled to quit, before the call returns. static void CloseAllWindows(); @@ -64,6 +67,8 @@ class Shell : public BrowserObserver { // instance is destroyed. static void SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure); + BrowserController* browser_controller() { return browser_controller_.get(); } + gfx::NativeWindow window() { return window_; } static gfx::Size GetShellDefaultSize(); diff --git a/weblayer/shell/common/shell_switches.cc b/weblayer/shell/common/shell_switches.cc new file mode 100644 index 00000000000000..feded8a9c9bab6 --- /dev/null +++ b/weblayer/shell/common/shell_switches.cc @@ -0,0 +1,12 @@ +// Copyright 2019 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 "weblayer/shell/common/shell_switches.h" + +namespace switches { + +// Makes WebLayer Shell use the given path for its data directory. +const char kWebLayerShellDataPath[] = "data-path"; + +} // namespace switches diff --git a/weblayer/shell/common/shell_switches.h b/weblayer/shell/common/shell_switches.h new file mode 100644 index 00000000000000..26548d940fa9d9 --- /dev/null +++ b/weblayer/shell/common/shell_switches.h @@ -0,0 +1,14 @@ +// Copyright 2019 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. + +#ifndef WEBLAYER_SHELL_COMMON_SHELL_SWITCHES_H_ +#define WEBLAYER_SHELL_COMMON_SHELL_SWITCHES_H_ + +namespace switches { + +extern const char kWebLayerShellDataPath[]; + +} // namespace switches + +#endif // WEBLAYER_SHELL_COMMON_SHELL_SWITCHES_H_ diff --git a/weblayer/test/BUILD.gn b/weblayer/test/BUILD.gn new file mode 100644 index 00000000000000..12a16fd8c9dde5 --- /dev/null +++ b/weblayer/test/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright 2019 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. + +import("//build/config/features.gni") +import("//build/config/ui.gni") +import("//testing/test.gni") +import("//tools/grit/grit_rule.gni") +import("//tools/v8_context_snapshot/v8_context_snapshot.gni") + +test("weblayer_browsertests") { + data = [ + "//net/tools/testserver/", + "//third_party/pywebsocket/src/mod_pywebsocket/", + "//third_party/tlslite/", + ] + + data_deps = [ + "//third_party/mesa_headers", + ] + + defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] + + deps = [ + "//base", + "//base/test:test_support", + "//content/test:test_support", + "//net:test_support", + "//testing/gtest", + "//ui/base", + "//weblayer/shell:weblayer_shell_lib", + ] + + sources = [ + "browsertests_main.cc", + "test_launcher_delegate_impl.cc", + "test_launcher_delegate_impl.h", + "weblayer_browser_test.cc", + "weblayer_browser_test.h", + "weblayer_browser_test_test.cc", + "weblayer_browser_test_utils.cc", + "weblayer_browser_test_utils.h", + ] +} diff --git a/weblayer/test/DEPS b/weblayer/test/DEPS new file mode 100644 index 00000000000000..f61957944df942 --- /dev/null +++ b/weblayer/test/DEPS @@ -0,0 +1,4 @@ +include_rules = [ + "+content/public/test", + "+net/test", +] diff --git a/weblayer/test/browsertests_main.cc b/weblayer/test/browsertests_main.cc new file mode 100644 index 00000000000000..c0dae98b0f3d90 --- /dev/null +++ b/weblayer/test/browsertests_main.cc @@ -0,0 +1,15 @@ +// Copyright 2019 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/command_line.h" +#include "base/test/launcher/test_launcher.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "weblayer/test/test_launcher_delegate_impl.h" + +int main(int argc, char** argv) { + base::CommandLine::Init(argc, argv); + size_t parallel_jobs = base::NumParallelJobs(); + weblayer::TestLauncherDelegateImpl launcher_delegate; + return content::LaunchTests(&launcher_delegate, parallel_jobs, argc, argv); +} diff --git a/weblayer/test/data/simple_page.html b/weblayer/test/data/simple_page.html new file mode 100644 index 00000000000000..6170792afd5a1b --- /dev/null +++ b/weblayer/test/data/simple_page.html @@ -0,0 +1,6 @@ + +OK + +Basic html test. + + diff --git a/weblayer/test/test_launcher_delegate_impl.cc b/weblayer/test/test_launcher_delegate_impl.cc new file mode 100644 index 00000000000000..a1a52189e5bf0d --- /dev/null +++ b/weblayer/test/test_launcher_delegate_impl.cc @@ -0,0 +1,38 @@ +// Copyright 2019 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 "weblayer/test/test_launcher_delegate_impl.h" + +#include "base/command_line.h" +#include "base/test/test_suite.h" +#include "weblayer/app/content_main_delegate_impl.h" +#include "weblayer/shell/app/shell_main_params.h" +#include "weblayer/shell/common/shell_switches.h" + +namespace weblayer { + +int TestLauncherDelegateImpl::RunTestSuite(int argc, char** argv) { + base::TestSuite test_suite(argc, argv); + // Browser tests are expected not to tear-down various globals and may + // complete with the thread priority being above NORMAL. + test_suite.DisableCheckForLeakedGlobals(); + return test_suite.Run(); +} + +bool TestLauncherDelegateImpl::AdjustChildProcessCommandLine( + base::CommandLine* command_line, + const base::FilePath& temp_data_dir) { + command_line->AppendSwitchPath(switches::kWebLayerShellDataPath, + temp_data_dir); + return true; +} + +#if !defined(OS_ANDROID) +content::ContentMainDelegate* +TestLauncherDelegateImpl::CreateContentMainDelegate() { + return new ContentMainDelegateImpl(CreateMainParams()); +} +#endif + +} // namespace weblayer diff --git a/weblayer/test/test_launcher_delegate_impl.h b/weblayer/test/test_launcher_delegate_impl.h new file mode 100644 index 00000000000000..6a5d43754a903c --- /dev/null +++ b/weblayer/test/test_launcher_delegate_impl.h @@ -0,0 +1,25 @@ +// Copyright 2019 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. + +#ifndef WEBLAYER_TEST_TEST_LAUNCHER_DELEGATE_IMPL_H_ +#define WEBLAYER_TEST_TEST_LAUNCHER_DELEGATE_IMPL_H_ + +#include "content/public/test/test_launcher.h" + +namespace weblayer { + +class TestLauncherDelegateImpl : public content::TestLauncherDelegate { + public: + int RunTestSuite(int argc, char** argv) override; + bool AdjustChildProcessCommandLine( + base::CommandLine* command_line, + const base::FilePath& temp_data_dir) override; +#if !defined(OS_ANDROID) + content::ContentMainDelegate* CreateContentMainDelegate() override; +#endif +}; + +} // namespace weblayer + +#endif // WEBLAYER_TEST_TEST_LAUNCHER_DELEGATE_IMPL_H_ diff --git a/weblayer/test/weblayer_browser_test.cc b/weblayer/test/weblayer_browser_test.cc new file mode 100644 index 00000000000000..fa80cf20b8d83e --- /dev/null +++ b/weblayer/test/weblayer_browser_test.cc @@ -0,0 +1,32 @@ +// Copyright 2019 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 "weblayer/test/weblayer_browser_test.h" + +#include "base/base_paths.h" +#include "base/command_line.h" +#include "weblayer/shell/browser/shell.h" + +namespace weblayer { + +WebLayerBrowserTest::WebLayerBrowserTest() { + CreateTestServer(base::FilePath(FILE_PATH_LITERAL("weblayer/test/data"))); +} + +WebLayerBrowserTest::~WebLayerBrowserTest() = default; + +void WebLayerBrowserTest::SetUp() { + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); + SetUpCommandLine(command_line); + content::BrowserTestBase::SetUp(); +} + +void WebLayerBrowserTest::PreRunTestOnMainThread() { + ASSERT_EQ(Shell::windows().size(), 1u); + shell_ = Shell::windows()[0]; +} + +void WebLayerBrowserTest::PostRunTestOnMainThread() {} + +} // namespace weblayer diff --git a/weblayer/test/weblayer_browser_test.h b/weblayer/test/weblayer_browser_test.h new file mode 100644 index 00000000000000..fd527657e7100b --- /dev/null +++ b/weblayer/test/weblayer_browser_test.h @@ -0,0 +1,36 @@ +// Copyright 2019 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. + +#ifndef WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_H_ +#define WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_H_ + +#include "base/macros.h" +#include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_base.h" + +namespace weblayer { +class Shell; + +class WebLayerBrowserTest : public content::BrowserTestBase { + public: + WebLayerBrowserTest(); + ~WebLayerBrowserTest() override; + + // content::BrowserTestBase implementation. + void SetUp() override; + void PreRunTestOnMainThread() override; + void PostRunTestOnMainThread() override; + + // Returns the window for the test. + Shell* shell() const { return shell_; } + + private: + Shell* shell_ = nullptr; + + DISALLOW_COPY_AND_ASSIGN(WebLayerBrowserTest); +}; + +} // namespace weblayer + +#endif // WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_H_ diff --git a/weblayer/test/weblayer_browser_test_test.cc b/weblayer/test/weblayer_browser_test_test.cc new file mode 100644 index 00000000000000..417dda7ebad832 --- /dev/null +++ b/weblayer/test/weblayer_browser_test_test.cc @@ -0,0 +1,19 @@ +// Copyright 2019 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 "weblayer/test/weblayer_browser_test.h" + +#include "net/test/embedded_test_server/embedded_test_server.h" +#include "weblayer/test/weblayer_browser_test_utils.h" + +namespace weblayer { + +IN_PROC_BROWSER_TEST_F(WebLayerBrowserTest, Basic) { + ASSERT_TRUE(embedded_test_server()->Start()); + GURL url = embedded_test_server()->GetURL("/simple_page.html"); + + NavigateAndWait(url, shell()); +} + +} // namespace weblayer diff --git a/weblayer/test/weblayer_browser_test_utils.cc b/weblayer/test/weblayer_browser_test_utils.cc new file mode 100644 index 00000000000000..a6754b379b64de --- /dev/null +++ b/weblayer/test/weblayer_browser_test_utils.cc @@ -0,0 +1,57 @@ +// Copyright 2019 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 "weblayer/test/weblayer_browser_test_utils.h" + +#include "base/run_loop.h" +#include "url/gurl.h" +#include "weblayer/public/browser_controller.h" +#include "weblayer/public/navigation.h" +#include "weblayer/public/navigation_controller.h" +#include "weblayer/public/navigation_observer.h" +#include "weblayer/shell/browser/shell.h" + +namespace weblayer { + +namespace { + +// Runs |closure| once |url| is successfully navigated to. +class TestNavigationObserver : public NavigationObserver { + public: + TestNavigationObserver(base::OnceClosure closure, + const GURL& url, + Shell* shell) + : closure_(std::move(closure)), + url_(url), + browser_(shell->browser_controller()) { + browser_->GetNavigationController()->AddObserver(this); + } + + ~TestNavigationObserver() override { + browser_->GetNavigationController()->RemoveObserver(this); + } + + private: + // NavigationObserver implementation: + void NavigationCompleted(Navigation* navigation) override { + if (navigation->GetURL() == url_) + std::move(closure_).Run(); + } + + base::OnceClosure closure_; + const GURL url_; + BrowserController* browser_; +}; + +} // namespace + +void NavigateAndWait(const GURL& url, Shell* shell) { + base::RunLoop run_loop; + TestNavigationObserver test_observer(run_loop.QuitClosure(), url, shell); + + shell->browser_controller()->GetNavigationController()->Navigate(url); + run_loop.Run(); +} + +} // namespace weblayer diff --git a/weblayer/test/weblayer_browser_test_utils.h b/weblayer/test/weblayer_browser_test_utils.h new file mode 100644 index 00000000000000..65daf4cae6c7e9 --- /dev/null +++ b/weblayer/test/weblayer_browser_test_utils.h @@ -0,0 +1,18 @@ +// Copyright 2019 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. + +#ifndef WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_ +#define WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_ + +class GURL; + +namespace weblayer { +class Shell; + +// Navigates |shell| to |url| and wait for successful navigation. +void NavigateAndWait(const GURL& url, Shell* shell); + +} // namespace weblayer + +#endif // WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_