diff options
author | derat <derat@chromium.org> | 2014-08-23 15:18:09 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-08-23 22:19:26 +0000 |
commit | 5a4560307a4250de157ac7af8954f4b059ed9fc2 (patch) | |
tree | 08f1b76abddb334a741703b95e6e78ef6e12d39a | |
parent | 4d6acafdc75e94256ab7c75f2f3e8a46e42bb9ec (diff) | |
download | chromium_src-5a4560307a4250de157ac7af8954f4b059ed9fc2.zip chromium_src-5a4560307a4250de157ac7af8954f4b059ed9fc2.tar.gz chromium_src-5a4560307a4250de157ac7af8954f4b059ed9fc2.tar.bz2 |
app_shell: Shut down in response to power button.
Also fix a dependency issue in
chromeos_test_support_without_gmock, make
ExtensionsTestSuite call
GLSurface::InitializeOneOffForTests(), and avoid
double-initializating Aura in app_shell.
BUG=405967
Review URL: https://codereview.chromium.org/468393005
Cr-Commit-Position: refs/heads/master@{#291607}
-rw-r--r-- | chromeos/chromeos.gyp | 4 | ||||
-rw-r--r-- | chromeos/dbus/fake_power_manager_client.cc | 49 | ||||
-rw-r--r-- | chromeos/dbus/fake_power_manager_client.h | 23 | ||||
-rw-r--r-- | extensions/shell/app_shell.gyp | 7 | ||||
-rw-r--r-- | extensions/shell/browser/shell_browser_main_parts.cc | 5 | ||||
-rw-r--r-- | extensions/shell/browser/shell_desktop_controller.cc | 93 | ||||
-rw-r--r-- | extensions/shell/browser/shell_desktop_controller.h | 21 | ||||
-rw-r--r-- | extensions/shell/browser/shell_desktop_controller_unittest.cc | 78 | ||||
-rw-r--r-- | extensions/test/extensions_unittests_main.cc | 2 | ||||
-rw-r--r-- | ui/compositor/test/in_process_context_factory.cc | 5 |
10 files changed, 204 insertions, 83 deletions
diff --git a/chromeos/chromeos.gyp b/chromeos/chromeos.gyp index 51cb17a..6a28795 100644 --- a/chromeos/chromeos.gyp +++ b/chromeos/chromeos.gyp @@ -491,6 +491,10 @@ { 'target_name': 'chromeos_test_support_without_gmock', 'type': 'static_library', + 'export_dependent_settings': [ + # fake_power_manager_client.h includes pb.h files. + 'power_manager_proto', + ], 'dependencies': [ '../build/linux/system.gyp:dbus', '../crypto/crypto.gyp:crypto', diff --git a/chromeos/dbus/fake_power_manager_client.cc b/chromeos/dbus/fake_power_manager_client.cc index 9fbc7d1..5231cf8 100644 --- a/chromeos/dbus/fake_power_manager_client.cc +++ b/chromeos/dbus/fake_power_manager_client.cc @@ -11,6 +11,7 @@ namespace chromeos { FakePowerManagerClient::FakePowerManagerClient() : num_request_restart_calls_(0), + num_request_shutdown_calls_(0), num_set_policy_calls_(0), num_set_is_projecting_calls_(0), is_projecting_(false) { @@ -30,19 +31,14 @@ void FakePowerManagerClient::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } -void FakePowerManagerClient::RequestStatusUpdate() { -} - -void FakePowerManagerClient::SetPolicy( - const power_manager::PowerManagementPolicy& policy) { - policy_ = policy; - ++num_set_policy_calls_; +bool FakePowerManagerClient::HasObserver(Observer* observer) { + return false; } -void FakePowerManagerClient::RequestShutdown() { +void FakePowerManagerClient::DecreaseScreenBrightness(bool allow_off) { } -void FakePowerManagerClient::DecreaseScreenBrightness(bool allow_off) { +void FakePowerManagerClient::IncreaseScreenBrightness() { } void FakePowerManagerClient::SetScreenBrightnessPercent(double percent, @@ -53,32 +49,34 @@ void FakePowerManagerClient::GetScreenBrightnessPercent( const GetScreenBrightnessPercentCallback& callback) { } -base::Closure FakePowerManagerClient::GetSuspendReadinessCallback() { - return base::Closure(); +void FakePowerManagerClient::DecreaseKeyboardBrightness() { } -int FakePowerManagerClient::GetNumPendingSuspendReadinessCallbacks() { - return 0; +void FakePowerManagerClient::IncreaseKeyboardBrightness() { } -bool FakePowerManagerClient::HasObserver(Observer* observer) { - return false; +void FakePowerManagerClient::RequestStatusUpdate() { } void FakePowerManagerClient::RequestRestart() { ++num_request_restart_calls_; } -void FakePowerManagerClient::IncreaseKeyboardBrightness() { +void FakePowerManagerClient::RequestShutdown() { + ++num_request_shutdown_calls_; } -void FakePowerManagerClient::IncreaseScreenBrightness() { +void FakePowerManagerClient::NotifyUserActivity( + power_manager::UserActivityType type) { } void FakePowerManagerClient::NotifyVideoActivity(bool is_fullscreen) { } -void FakePowerManagerClient::DecreaseKeyboardBrightness() { +void FakePowerManagerClient::SetPolicy( + const power_manager::PowerManagementPolicy& policy) { + policy_ = policy; + ++num_set_policy_calls_; } void FakePowerManagerClient::SetIsProjecting(bool is_projecting) { @@ -86,8 +84,12 @@ void FakePowerManagerClient::SetIsProjecting(bool is_projecting) { is_projecting_ = is_projecting; } -void FakePowerManagerClient::NotifyUserActivity( - power_manager::UserActivityType type) { +base::Closure FakePowerManagerClient::GetSuspendReadinessCallback() { + return base::Closure(); +} + +int FakePowerManagerClient::GetNumPendingSuspendReadinessCallbacks() { + return 0; } void FakePowerManagerClient::SendSuspendImminent() { @@ -102,4 +104,11 @@ void FakePowerManagerClient::SendDarkSuspendImminent() { FOR_EACH_OBSERVER(Observer, observers_, DarkSuspendImminent()); } +void FakePowerManagerClient::SendPowerButtonEvent( + bool down, + const base::TimeTicks& timestamp) { + FOR_EACH_OBSERVER(Observer, observers_, + PowerButtonEventReceived(down, timestamp)); +} + } // namespace chromeos diff --git a/chromeos/dbus/fake_power_manager_client.h b/chromeos/dbus/fake_power_manager_client.h index 893448c..f4b9af5 100644 --- a/chromeos/dbus/fake_power_manager_client.h +++ b/chromeos/dbus/fake_power_manager_client.h @@ -24,18 +24,13 @@ class FakePowerManagerClient : public PowerManagerClient { virtual ~FakePowerManagerClient(); power_manager::PowerManagementPolicy& policy() { return policy_; } - int num_request_restart_calls() const { - return num_request_restart_calls_; - } - int num_set_policy_calls() const { - return num_set_policy_calls_; - } + int num_request_restart_calls() const { return num_request_restart_calls_; } + int num_request_shutdown_calls() const { return num_request_shutdown_calls_; } + int num_set_policy_calls() const { return num_set_policy_calls_; } int num_set_is_projecting_calls() const { return num_set_is_projecting_calls_; } - bool is_projecting() const { - return is_projecting_; - } + bool is_projecting() const { return is_projecting_; } // PowerManagerClient overrides virtual void Init(dbus::Bus* bus) OVERRIDE; @@ -68,19 +63,19 @@ class FakePowerManagerClient : public PowerManagerClient { void SendSuspendDone(); void SendDarkSuspendImminent(); + // Notifies observers that the power button has been pressed or released. + void SendPowerButtonEvent(bool down, const base::TimeTicks& timestamp); + private: ObserverList<Observer> observers_; // Last policy passed to SetPolicy(). power_manager::PowerManagementPolicy policy_; - // Number of times that RequestRestart() has been called. + // Number of times that various methods have been called. int num_request_restart_calls_; - - // Number of times that SetPolicy() has been called. + int num_request_shutdown_calls_; int num_set_policy_calls_; - - // Count the number of times SetIsProjecting() has been called. int num_set_is_projecting_calls_; // Last projecting state set in SetIsProjecting(). diff --git a/extensions/shell/app_shell.gyp b/extensions/shell/app_shell.gyp index 1abdf13..8a24db5 100644 --- a/extensions/shell/app_shell.gyp +++ b/extensions/shell/app_shell.gyp @@ -198,9 +198,11 @@ '<(DEPTH)/extensions/extensions.gyp:extensions_shell_and_test_pak', '<(DEPTH)/extensions/extensions.gyp:extensions_test_support', '<(DEPTH)/testing/gtest.gyp:gtest', + '<(DEPTH)/ui/aura/aura.gyp:aura_test_support', ], 'sources': [ '../test/extensions_unittests_main.cc', + 'browser/shell_desktop_controller_unittest.cc', 'browser/shell_nacl_browser_delegate_unittest.cc', ], 'conditions': [ @@ -209,6 +211,11 @@ 'browser/shell_nacl_browser_delegate_unittest.cc', ], }], + ['chromeos==1', { + 'dependencies': [ + '<(DEPTH)/chromeos/chromeos.gyp:chromeos_test_support_without_gmock', + ], + }], ], }, { diff --git a/extensions/shell/browser/shell_browser_main_parts.cc b/extensions/shell/browser/shell_browser_main_parts.cc index 820f1b4..24f7e028 100644 --- a/extensions/shell/browser/shell_browser_main_parts.cc +++ b/extensions/shell/browser/shell_browser_main_parts.cc @@ -8,6 +8,7 @@ #include "base/run_loop.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/omaha_query_params/omaha_query_params.h" +#include "content/public/browser/context_factory.h" #include "content/public/common/result_codes.h" #include "content/shell/browser/shell_devtools_delegate.h" #include "content/shell/browser/shell_net_log.h" @@ -22,6 +23,7 @@ #include "extensions/shell/browser/shell_omaha_query_params_delegate.h" #include "extensions/shell/common/shell_extensions_client.h" #include "extensions/shell/common/switches.h" +#include "ui/aura/env.h" #include "ui/aura/window_tree_host.h" #include "ui/base/ime/input_method_initializer.h" #include "ui/base/resource/resource_bundle.h" @@ -95,8 +97,9 @@ void ShellBrowserMainParts::PreMainMessageLoopRun() { // Initialize our "profile" equivalent. browser_context_.reset(new ShellBrowserContext); + aura::Env::GetInstance()->set_context_factory(content::GetContextFactory()); + desktop_controller_.reset(browser_main_delegate_->CreateDesktopController()); - desktop_controller_->CreateRootWindow(); // NOTE: Much of this is culled from chrome/test/base/chrome_test_suite.cc // TODO(jamescook): Initialize user_manager::UserManager. diff --git a/extensions/shell/browser/shell_desktop_controller.cc b/extensions/shell/browser/shell_desktop_controller.cc index 8fff328..6ab9bac 100644 --- a/extensions/shell/browser/shell_desktop_controller.cc +++ b/extensions/shell/browser/shell_desktop_controller.cc @@ -5,12 +5,10 @@ #include "extensions/shell/browser/shell_desktop_controller.h" #include "base/command_line.h" -#include "content/public/browser/context_factory.h" #include "extensions/shell/browser/shell_app_window_controller.h" #include "extensions/shell/common/switches.h" #include "ui/aura/client/cursor_client.h" #include "ui/aura/client/default_capture_client.h" -#include "ui/aura/env.h" #include "ui/aura/layout_manager.h" #include "ui/aura/test/test_screen.h" #include "ui/aura/window.h" @@ -31,6 +29,7 @@ #include "ui/wm/core/user_activity_detector.h" #if defined(OS_CHROMEOS) +#include "chromeos/dbus/dbus_thread_manager.h" #include "ui/chromeos/user_activity_power_manager_notifier.h" #include "ui/display/types/chromeos/display_mode.h" #include "ui/display/types/chromeos/display_snapshot.h" @@ -157,14 +156,18 @@ ShellDesktopController* g_instance = NULL; } // namespace ShellDesktopController::ShellDesktopController() { + CHECK(!g_instance) << "ShellDesktopController already exists"; + #if defined(OS_CHROMEOS) + chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> + AddObserver(this); display_configurator_.reset(new ui::DisplayConfigurator); display_configurator_->Init(false); display_configurator_->ForceInitialConfigure(0); display_configurator_->AddObserver(this); #endif - aura::Env::CreateInstance(true); - aura::Env::GetInstance()->set_context_factory(content::GetContextFactory()); + + CreateRootWindow(); g_instance = this; } @@ -173,7 +176,10 @@ ShellDesktopController::~ShellDesktopController() { app_window_controller_.reset(); g_instance = NULL; DestroyRootWindow(); - aura::Env::DeleteInstance(); +#if defined(OS_CHROMEOS) + chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> + RemoveObserver(this); +#endif } // static @@ -210,6 +216,15 @@ aura::Window* ShellDesktopController::GetDefaultParent( } #if defined(OS_CHROMEOS) +void ShellDesktopController::PowerButtonEventReceived( + bool down, + const base::TimeTicks& timestamp) { + if (down) { + chromeos::DBusThreadManager::Get()->GetPowerManagerClient()-> + RequestShutdown(); + } +} + void ShellDesktopController::OnDisplayModeChanged( const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { gfx::Size size = GetPrimaryDisplaySize(); @@ -226,40 +241,6 @@ void ShellDesktopController::OnHostCloseRequested( base::MessageLoop::QuitClosure()); } -void ShellDesktopController::CreateRootWindow() { - // Set up basic pieces of ui::wm. - gfx::Size size; - base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); - if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) { - const std::string size_str = - command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds); - int width, height; - CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height)); - size = gfx::Size(width, height); - } else { - size = GetPrimaryDisplaySize(); - } - if (size.IsEmpty()) - size = gfx::Size(1280, 720); - - test_screen_.reset(aura::TestScreen::Create(size)); - // TODO(jamescook): Replace this with a real Screen implementation. - gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); - // TODO(mukai): Set up input method. - - host_.reset(test_screen_->CreateHostForPrimaryDisplay()); - host_->InitHost(); - aura::client::SetWindowTreeClient(host_->window(), this); - root_window_event_filter_.reset(new wm::CompoundEventFilter); - host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); - InitWindowManager(); - - host_->AddObserver(this); - - // Ensure the X window gets mapped. - host_->Show(); -} - void ShellDesktopController::InitWindowManager() { wm::FocusController* focus_controller = new wm::FocusController(CreateFocusRules()); @@ -300,6 +281,40 @@ wm::FocusRules* ShellDesktopController::CreateFocusRules() { return new AppsFocusRules(); } +void ShellDesktopController::CreateRootWindow() { + // Set up basic pieces of ui::wm. + gfx::Size size; + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); + if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) { + const std::string size_str = + command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds); + int width, height; + CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height)); + size = gfx::Size(width, height); + } else { + size = GetPrimaryDisplaySize(); + } + if (size.IsEmpty()) + size = gfx::Size(1280, 720); + + test_screen_.reset(aura::TestScreen::Create(size)); + // TODO(jamescook): Replace this with a real Screen implementation. + gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); + // TODO(mukai): Set up input method. + + host_.reset(test_screen_->CreateHostForPrimaryDisplay()); + host_->InitHost(); + aura::client::SetWindowTreeClient(host_->window(), this); + root_window_event_filter_.reset(new wm::CompoundEventFilter); + host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); + InitWindowManager(); + + host_->AddObserver(this); + + // Ensure the X window gets mapped. + host_->Show(); +} + void ShellDesktopController::DestroyRootWindow() { host_->RemoveObserver(this); if (input_method_filter_) diff --git a/extensions/shell/browser/shell_desktop_controller.h b/extensions/shell/browser/shell_desktop_controller.h index 90ba2eb..20a3e8c 100644 --- a/extensions/shell/browser/shell_desktop_controller.h +++ b/extensions/shell/browser/shell_desktop_controller.h @@ -12,6 +12,7 @@ #include "ui/aura/window_tree_host_observer.h" #if defined(OS_CHROMEOS) +#include "chromeos/dbus/power_manager_client.h" #include "ui/display/chromeos/display_configurator.h" #endif @@ -56,12 +57,11 @@ class ShellAppWindowController; // Handles desktop-related tasks for app_shell. class ShellDesktopController : public aura::client::WindowTreeClient, - public aura::WindowTreeHostObserver #if defined(OS_CHROMEOS) - , - public ui::DisplayConfigurator::Observer + public chromeos::PowerManagerClient::Observer, + public ui::DisplayConfigurator::Observer, #endif - { + public aura::WindowTreeHostObserver { public: ShellDesktopController(); virtual ~ShellDesktopController(); @@ -73,9 +73,6 @@ class ShellDesktopController : public aura::client::WindowTreeClient, aura::WindowTreeHost* host() { return host_.get(); } - // Creates the window that hosts the app. - void CreateRootWindow(); - // Sets the controller to create/close the app windows. Takes the ownership of // |app_window_controller|. void SetAppWindowController(ShellAppWindowController* app_window_controller); @@ -92,12 +89,17 @@ class ShellDesktopController : public aura::client::WindowTreeClient, // Sets the screen's work area insets. void SetDisplayWorkAreaInsets(const gfx::Insets& insets); - // Overridden from aura::client::WindowTreeClient: + // aura::client::WindowTreeClient overrides: virtual aura::Window* GetDefaultParent(aura::Window* context, aura::Window* window, const gfx::Rect& bounds) OVERRIDE; #if defined(OS_CHROMEOS) + // chromeos::PowerManagerClient::Observer overrides: + virtual void PowerButtonEventReceived(bool down, + const base::TimeTicks& timestamp) + OVERRIDE; + // ui::DisplayConfigurator::Observer overrides. virtual void OnDisplayModeChanged(const std::vector< ui::DisplayConfigurator::DisplayState>& displays) OVERRIDE; @@ -115,6 +117,9 @@ class ShellDesktopController : public aura::client::WindowTreeClient, virtual wm::FocusRules* CreateFocusRules(); private: + // Creates the window that hosts the app. + void CreateRootWindow(); + // Closes and destroys the root window hosting the app. void DestroyRootWindow(); diff --git a/extensions/shell/browser/shell_desktop_controller_unittest.cc b/extensions/shell/browser/shell_desktop_controller_unittest.cc new file mode 100644 index 0000000..e72bd73 --- /dev/null +++ b/extensions/shell/browser/shell_desktop_controller_unittest.cc @@ -0,0 +1,78 @@ +// Copyright 2014 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 "extensions/shell/browser/shell_desktop_controller.h" + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" +#include "base/time/time.h" +#include "ui/aura/test/aura_test_base.h" + +#if defined(OS_CHROMEOS) +#include "chromeos/dbus/dbus_thread_manager.h" +#include "chromeos/dbus/fake_dbus_thread_manager.h" +#include "chromeos/dbus/fake_power_manager_client.h" +#endif + +namespace extensions { + +class ShellDesktopControllerTest : public aura::test::AuraTestBase { + public: + ShellDesktopControllerTest() +#if defined(OS_CHROMEOS) + : power_manager_client_(NULL) +#endif + { + } + virtual ~ShellDesktopControllerTest() {} + + virtual void SetUp() OVERRIDE { +#if defined(OS_CHROMEOS) + chromeos::FakeDBusThreadManager* manager = + new chromeos::FakeDBusThreadManager(); + power_manager_client_ = new chromeos::FakePowerManagerClient(); + manager->SetPowerManagerClient(make_scoped_ptr(power_manager_client_). + PassAs<chromeos::PowerManagerClient>()); + chromeos::DBusThreadManager::InitializeForTesting(manager); +#endif + aura::test::AuraTestBase::SetUp(); + controller_.reset(new ShellDesktopController()); + } + + virtual void TearDown() OVERRIDE { + controller_.reset(); + aura::test::AuraTestBase::TearDown(); +#if defined(OS_CHROMEOS) + chromeos::DBusThreadManager::Shutdown(); +#endif + } + + protected: + scoped_ptr<ShellDesktopController> controller_; + +#if defined(OS_CHROMEOS) + chromeos::FakePowerManagerClient* power_manager_client_; // Not owned. +#endif + + private: + DISALLOW_COPY_AND_ASSIGN(ShellDesktopControllerTest); +}; + +#if defined(OS_CHROMEOS) +// Tests that a shutdown request is sent to the power manager when the power +// button is pressed. +TEST_F(ShellDesktopControllerTest, PowerButton) { + // Ignore button releases. + power_manager_client_->SendPowerButtonEvent(false /* down */, + base::TimeTicks()); + EXPECT_EQ(0, power_manager_client_->num_request_shutdown_calls()); + + // A button press should trigger a shutdown request. + power_manager_client_->SendPowerButtonEvent(true /* down */, + base::TimeTicks()); + EXPECT_EQ(1, power_manager_client_->num_request_shutdown_calls()); +} +#endif + +} // namespace extensions diff --git a/extensions/test/extensions_unittests_main.cc b/extensions/test/extensions_unittests_main.cc index dd7e402..9ec7e24 100644 --- a/extensions/test/extensions_unittests_main.cc +++ b/extensions/test/extensions_unittests_main.cc @@ -15,6 +15,7 @@ #include "extensions/test/test_extensions_client.h" #include "mojo/embedder/test_embedder.h" #include "ui/base/resource/resource_bundle.h" +#include "ui/gl/gl_surface.h" namespace { @@ -64,6 +65,7 @@ ExtensionsTestSuite::~ExtensionsTestSuite() {} void ExtensionsTestSuite::Initialize() { content::ContentTestSuiteBase::Initialize(); + gfx::GLSurface::InitializeOneOffForTests(); // Register the chrome-extension:// scheme via this circuitous path. Note // that this does not persistently set up a ContentClient; individual tests diff --git a/ui/compositor/test/in_process_context_factory.cc b/ui/compositor/test/in_process_context_factory.cc index fbc4c81..f680e99 100644 --- a/ui/compositor/test/in_process_context_factory.cc +++ b/ui/compositor/test/in_process_context_factory.cc @@ -20,7 +20,10 @@ namespace ui { InProcessContextFactory::InProcessContextFactory() : shared_bitmap_manager_(new cc::TestSharedBitmapManager()) { - DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone); + DCHECK_NE(gfx::GetGLImplementation(), gfx::kGLImplementationNone) + << "If running tests, ensure that main() is calling " + << "gfx::GLSurface::InitializeOneOffForTests()"; + #if defined(OS_CHROMEOS) bool use_thread = !CommandLine::ForCurrentProcess()->HasSwitch( switches::kUIDisableThreadedCompositing); |