From f7053c6bdff26aad75f2da498b69a40930b117a1 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Sat, 15 Aug 2015 12:45:08 -0700 Subject: [PATCH] Make it easier to run SkyShell from the command line on Mac Now you can use --snapshot to load snapshots from the command line and have them appear visually on Mac. Also, remove code duplication with Linux by factoring common testing code into //sky/shell/testing. --- sky/shell/BUILD.gn | 17 +++-- sky/shell/linux/main_linux.cc | 36 +++++++++++ sky/shell/mac/main_mac.mm | 64 ++++--------------- sky/shell/mac/sky_window.mm | 11 +++- .../{linux/main.cc => testing/testing.cc} | 42 +----------- sky/shell/testing/testing.h | 16 +++++ 6 files changed, 87 insertions(+), 99 deletions(-) create mode 100644 sky/shell/linux/main_linux.cc rename sky/shell/{linux/main.cc => testing/testing.cc} (54%) create mode 100644 sky/shell/testing/testing.h diff --git a/sky/shell/BUILD.gn b/sky/shell/BUILD.gn index 1da5045f953..d0e1e1410aa 100644 --- a/sky/shell/BUILD.gn +++ b/sky/shell/BUILD.gn @@ -27,6 +27,13 @@ common_deps = [ "//ui/gl", ] +testing_sources = [ + "testing/test_runner.cc", + "testing/test_runner.h", + "testing/testing.cc", + "testing/testing.h", +] + source_set("common") { sources = [ "gpu/ganesh_context.cc", @@ -225,12 +232,10 @@ if (is_android) { output_name = "sky_shell" sources = [ - "linux/main.cc", + "linux/main_linux.cc", "linux/platform_service_provider_linux.cc", "linux/platform_view_linux.cc", - "testing/test_runner.cc", - "testing/test_runner.h", - ] + ] + testing_sources deps = common_deps + [ ":common", @@ -254,9 +259,7 @@ if (is_android) { "mac/sky_application.mm", "mac/sky_window.h", "mac/sky_window.mm", - "testing/test_runner.cc", - "testing/test_runner.h", - ] + ] + testing_sources deps = common_deps + [ ":common", diff --git a/sky/shell/linux/main_linux.cc b/sky/shell/linux/main_linux.cc new file mode 100644 index 00000000000..0521cb61303 --- /dev/null +++ b/sky/shell/linux/main_linux.cc @@ -0,0 +1,36 @@ +// 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/at_exit.h" +#include "base/basictypes.h" +#include "base/bind.h" +#include "base/command_line.h" +#include "base/logging.h" +#include "base/message_loop/message_loop.h" +#include "sky/shell/service_provider.h" +#include "sky/shell/shell.h" +#include "sky/shell/switches.h" +#include "sky/shell/testing/testing.h" + +int main(int argc, const char* argv[]) { + base::AtExitManager exit_manager; + base::CommandLine::Init(argc, argv); + + base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); + + if (command_line.HasSwitch(sky::shell::switches::kHelp)) { + sky::shell::switches::PrintUsage("sky_shell"); + return 0; + } + + base::MessageLoop message_loop; + + sky::shell::Shell::Init(make_scoped_ptr( + new sky::shell::ServiceProviderContext(message_loop.task_runner()))); + + message_loop.PostTask(FROM_HERE, base::Bind(&sky::shell::InitForTesting)); + message_loop.Run(); + + return 0; +} diff --git a/sky/shell/mac/main_mac.mm b/sky/shell/mac/main_mac.mm index 3fadd6fb713..428c38ed246 100644 --- a/sky/shell/mac/main_mac.mm +++ b/sky/shell/mac/main_mac.mm @@ -5,58 +5,17 @@ #import #include #include "base/bind.h" -#include "base/message_loop/message_loop.h" -#include "sky/engine/public/web/WebRuntimeFeatures.h" -#include "sky/shell/mac/platform_mac.h" #include "base/command_line.h" -#include "sky/shell/switches.h" -#include "sky/shell/testing/test_runner.h" +#include "base/message_loop/message_loop.h" +#include "sky/shell/mac/platform_mac.h" #include "sky/shell/mac/sky_application.h" +#include "sky/shell/switches.h" +#include "sky/shell/testing/testing.h" namespace sky { namespace shell { namespace { -bool FlagsValid() { - base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - - if (command_line.HasSwitch(sky::shell::switches::kHelp) || - (!command_line.HasSwitch(sky::shell::switches::kPackageRoot) && - !command_line.HasSwitch(sky::shell::switches::kSnapshot))) { - return false; - } - - return true; -} - -void Init() { - base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - blink::WebRuntimeFeatures::enableObservatory( - !command_line.HasSwitch(switches::kNonInteractive)); - - // Explicitly boot the shared test runner. - TestRunner& runner = TestRunner::Shared(); - - std::string package_root = - command_line.GetSwitchValueASCII(switches::kPackageRoot); - runner.set_package_root(package_root); - - scoped_ptr single_test; - if (command_line.HasSwitch(switches::kSnapshot)) { - single_test.reset(new TestRunner::SingleTest); - single_test->path = command_line.GetSwitchValueASCII(switches::kSnapshot); - single_test->is_snapshot = true; - } else { - auto args = command_line.GetArgs(); - if (!args.empty()) { - single_test.reset(new TestRunner::SingleTest); - single_test->path = args[0]; - } - } - - runner.Start(single_test.Pass()); -} - void AttachMessageLoopToMainRunLoop(void) { // We want to call Run() on the MessageLoopForUI but after NSApplicationMain. // If called before this point, the call is blocking and will prevent the @@ -74,15 +33,20 @@ int main(int argc, const char* argv[]) { [SkyApplication sharedApplication]; return PlatformMacMain(argc, argv, ^() { - if (!sky::shell::FlagsValid()) { + base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); + if (command_line.HasSwitch(sky::shell::switches::kHelp)) { sky::shell::switches::PrintUsage("SkyShell"); - sky::shell::AttachMessageLoopToMainRunLoop(); - return NSApplicationMain(argc, argv); - } else { + return EXIT_SUCCESS; + } + + if (command_line.HasSwitch(sky::shell::switches::kNonInteractive)) { auto loop = base::MessageLoop::current(); - loop->PostTask(FROM_HERE, base::Bind(&sky::shell::Init)); + loop->PostTask(FROM_HERE, base::Bind(&sky::shell::InitForTesting)); loop->Run(); return EXIT_SUCCESS; } + + sky::shell::AttachMessageLoopToMainRunLoop(); + return NSApplicationMain(argc, argv); }); } diff --git a/sky/shell/mac/sky_window.mm b/sky/shell/mac/sky_window.mm index bfdd478f698..ed122984d38 100644 --- a/sky/shell/mac/sky_window.mm +++ b/sky/shell/mac/sky_window.mm @@ -8,11 +8,11 @@ #include "mojo/public/cpp/bindings/interface_request.h" #include "sky/services/engine/input_event.mojom.h" #include "sky/shell/mac/platform_view_mac.h" -#include "sky/shell/shell.h" #include "sky/shell/shell_view.h" +#include "sky/shell/shell.h" +#include "sky/shell/switches.h" #include "sky/shell/ui_delegate.h" - @interface SkyWindow () @property(assign) IBOutlet NSOpenGLView* renderSurface; @@ -87,12 +87,17 @@ static inline sky::EventType EventTypeFromNSEventPhase(NSEventPhase phase) { base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); base::CommandLine::StringVector args = command_line.GetArgs(); if (args.size() > 0) { - LOG(INFO) << "Loading " << args[0]; mojo::String string(args[0]); _sky_engine->RunFromNetwork(string); return; } + if (command_line.HasSwitch(sky::shell::switches::kSnapshot)) { + auto snapshot = command_line.GetSwitchValueASCII(sky::shell::switches::kSnapshot); + _sky_engine->RunFromSnapshot(snapshot); + return; + } + NSString *endpoint = self.skyInitialBundleURL; if (endpoint.length > 0) { // Load from bundle diff --git a/sky/shell/linux/main.cc b/sky/shell/testing/testing.cc similarity index 54% rename from sky/shell/linux/main.cc rename to sky/shell/testing/testing.cc index 674cf408db9..493eedb4921 100644 --- a/sky/shell/linux/main.cc +++ b/sky/shell/testing/testing.cc @@ -2,28 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include +#include "sky/shell/testing/testing.h" -#include "base/at_exit.h" -#include "base/basictypes.h" -#include "base/bind.h" -#include "base/command_line.h" -#include "base/i18n/icu_util.h" -#include "base/logging.h" -#include "base/message_loop/message_loop.h" #include "sky/engine/public/web/WebRuntimeFeatures.h" -#include "sky/shell/platform_view.h" -#include "sky/shell/service_provider.h" -#include "sky/shell/shell.h" -#include "sky/shell/shell_view.h" +#include "base/command_line.h" #include "sky/shell/switches.h" #include "sky/shell/testing/test_runner.h" namespace sky { namespace shell { -namespace { -void Init() { +void InitForTesting() { base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); blink::WebRuntimeFeatures::enableObservatory( !command_line.HasSwitch(switches::kNonInteractive)); @@ -51,30 +40,5 @@ void Init() { runner.Start(single_test.Pass()); } -} // namespace } // namespace shell } // namespace sky - -int main(int argc, const char* argv[]) { - base::AtExitManager exit_manager; - base::CommandLine::Init(argc, argv); - - base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - - if (command_line.HasSwitch(sky::shell::switches::kHelp) || - (!command_line.HasSwitch(sky::shell::switches::kPackageRoot) && - !command_line.HasSwitch(sky::shell::switches::kSnapshot))) { - sky::shell::switches::PrintUsage("sky_shell"); - return 0; - } - - base::MessageLoop message_loop; - - sky::shell::Shell::Init(make_scoped_ptr( - new sky::shell::ServiceProviderContext(message_loop.task_runner()))); - - message_loop.PostTask(FROM_HERE, base::Bind(&sky::shell::Init)); - message_loop.Run(); - - return 0; -} diff --git a/sky/shell/testing/testing.h b/sky/shell/testing/testing.h new file mode 100644 index 00000000000..ca5047c2e53 --- /dev/null +++ b/sky/shell/testing/testing.h @@ -0,0 +1,16 @@ +// 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. + +#ifndef SKY_SHELL_TESTING_TESTING_H_ +#define SKY_SHELL_TESTING_TESTING_H_ + +namespace sky { +namespace shell { + +void InitForTesting(); + +} // namespace shell +} // namespace sky + +#endif // SKY_SHELL_TESTING_TESTING_H_