Commit e028f090 authored by wutao's avatar wutao Committed by Chromium LUCI CQ

ambient: Consume key press event

Currently ambient mode will exit on key press. However, the key event is
not consumed and will enter into the login pod.
To be consistent with other platforms, the key press is consumed in
ambient mode by making the ambient controller a pretarget event handler
and stopping propagation of the key event.

Bug: b/173642446
Test: new unittests
Change-Id: Iad04b07a0e6124773326c1d52d4a48a919ab87fd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2626911
Commit-Queue: Tao Wu <wutao@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843970}
parent f3fbc922
...@@ -208,6 +208,8 @@ void AmbientController::OnAmbientUiVisibilityChanged( ...@@ -208,6 +208,8 @@ void AmbientController::OnAmbientUiVisibilityChanged(
if (!user_activity_observer_.IsObserving()) if (!user_activity_observer_.IsObserving())
user_activity_observer_.Observe(ui::UserActivityDetector::Get()); user_activity_observer_.Observe(ui::UserActivityDetector::Get());
Shell::Get()->AddPreTargetHandler(this);
StartRefreshingImages(); StartRefreshingImages();
break; break;
case AmbientUiVisibility::kHidden: case AmbientUiVisibility::kHidden:
...@@ -228,6 +230,8 @@ void AmbientController::OnAmbientUiVisibilityChanged( ...@@ -228,6 +230,8 @@ void AmbientController::OnAmbientUiVisibilityChanged(
// Should do nothing if the wake lock has already been released. // Should do nothing if the wake lock has already been released.
ReleaseWakeLock(); ReleaseWakeLock();
Shell::Get()->RemovePreTargetHandler(this);
// |start_time_| may be empty in case of |AmbientUiVisibility::kHidden| if // |start_time_| may be empty in case of |AmbientUiVisibility::kHidden| if
// ambient mode has just started. // ambient mode has just started.
if (start_time_) { if (start_time_) {
...@@ -411,6 +415,12 @@ void AmbientController::OnUserActivity(const ui::Event* event) { ...@@ -411,6 +415,12 @@ void AmbientController::OnUserActivity(const ui::Event* event) {
DismissUI(); DismissUI();
} }
void AmbientController::OnKeyEvent(ui::KeyEvent* event) {
// Prevent dispatching key press event to the login UI.
event->StopPropagation();
DismissUI();
}
void AmbientController::AddAmbientViewDelegateObserver( void AmbientController::AddAmbientViewDelegateObserver(
AmbientViewDelegateObserver* observer) { AmbientViewDelegateObserver* observer) {
delegate_.AddObserver(observer); delegate_.AddObserver(observer);
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "services/device/public/mojom/wake_lock.mojom.h" #include "services/device/public/mojom/wake_lock.mojom.h"
#include "ui/base/user_activity/user_activity_detector.h" #include "ui/base/user_activity/user_activity_detector.h"
#include "ui/base/user_activity/user_activity_observer.h" #include "ui/base/user_activity/user_activity_observer.h"
#include "ui/events/event_handler.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h" #include "ui/views/widget/widget_observer.h"
...@@ -52,7 +53,8 @@ class ASH_EXPORT AmbientController ...@@ -52,7 +53,8 @@ class ASH_EXPORT AmbientController
public PowerStatus::Observer, public PowerStatus::Observer,
public chromeos::PowerManagerClient::Observer, public chromeos::PowerManagerClient::Observer,
public device::mojom::FingerprintObserver, public device::mojom::FingerprintObserver,
public ui::UserActivityObserver { public ui::UserActivityObserver,
public ui::EventHandler {
public: public:
static void RegisterProfilePrefs(PrefRegistrySimple* registry); static void RegisterProfilePrefs(PrefRegistrySimple* registry);
...@@ -90,6 +92,9 @@ class ASH_EXPORT AmbientController ...@@ -90,6 +92,9 @@ class ASH_EXPORT AmbientController
// ui::UserActivityObserver: // ui::UserActivityObserver:
void OnUserActivity(const ui::Event* event) override; void OnUserActivity(const ui::Event* event) override;
// ui::EventHandler:
void OnKeyEvent(ui::KeyEvent* event) override;
void AddAmbientViewDelegateObserver(AmbientViewDelegateObserver* observer); void AddAmbientViewDelegateObserver(AmbientViewDelegateObserver* observer);
void RemoveAmbientViewDelegateObserver(AmbientViewDelegateObserver* observer); void RemoveAmbientViewDelegateObserver(AmbientViewDelegateObserver* observer);
......
...@@ -474,9 +474,6 @@ TEST_F(AmbientControllerTest, ShouldDismissContainerViewOnEvents) { ...@@ -474,9 +474,6 @@ TEST_F(AmbientControllerTest, ShouldDismissContainerViewOnEvents) {
gfx::Vector2d(), gfx::PointF(), gfx::PointF(), base::TimeTicks(), gfx::Vector2d(), gfx::PointF(), gfx::PointF(), base::TimeTicks(),
ui::EF_NONE, ui::EF_NONE)); ui::EF_NONE, ui::EF_NONE));
events.emplace_back(std::make_unique<ui::KeyEvent>(
ui::ET_KEY_PRESSED, ui::VKEY_SPACE, ui::EF_NONE));
events.emplace_back(std::make_unique<ui::ScrollEvent>( events.emplace_back(std::make_unique<ui::ScrollEvent>(
ui::ET_SCROLL, gfx::PointF(), gfx::PointF(), base::TimeTicks(), ui::ET_SCROLL, gfx::PointF(), gfx::PointF(), base::TimeTicks(),
ui::EF_NONE, /*x_offset=*/0.0f, ui::EF_NONE, /*x_offset=*/0.0f,
...@@ -509,9 +506,9 @@ TEST_F(AmbientControllerTest, ShouldDismissAndThenComesBack) { ...@@ -509,9 +506,9 @@ TEST_F(AmbientControllerTest, ShouldDismissAndThenComesBack) {
FastForwardTiny(); FastForwardTiny();
EXPECT_TRUE(WidgetsVisible()); EXPECT_TRUE(WidgetsVisible());
ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::KeyboardCode::VKEY_1, ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
ui::EF_NONE); base::TimeTicks(), ui::EF_NONE, ui::EF_NONE);
ambient_controller()->OnUserActivity(&key_event); ambient_controller()->OnUserActivity(&mouse_event);
FastForwardTiny(); FastForwardTiny();
EXPECT_TRUE(GetContainerViews().empty()); EXPECT_TRUE(GetContainerViews().empty());
...@@ -520,6 +517,49 @@ TEST_F(AmbientControllerTest, ShouldDismissAndThenComesBack) { ...@@ -520,6 +517,49 @@ TEST_F(AmbientControllerTest, ShouldDismissAndThenComesBack) {
EXPECT_TRUE(WidgetsVisible()); EXPECT_TRUE(WidgetsVisible());
} }
TEST_F(AmbientControllerTest, ShouldDismissContainerViewOnKeyEvent) {
// Without user interaction, should show ambient mode.
ambient_controller()->ShowUi();
EXPECT_FALSE(WidgetsVisible());
FastForwardTiny();
EXPECT_TRUE(WidgetsVisible());
CloseAmbientScreen();
// When ambient is shown, OnUserActivity() should ignore key event.
ambient_controller()->ShowUi();
EXPECT_TRUE(ambient_controller()->IsShown());
// General key press will exit ambient mode.
// Simulate key press to close the widget.
ui::test::EventGenerator* event_generator = GetEventGenerator();
event_generator->PressKey(ui::VKEY_A, /*flags=*/0);
EXPECT_FALSE(ambient_controller()->IsShown());
}
TEST_F(AmbientControllerTest,
ShouldDismissContainerViewOnKeyEventWhenLockScreenInBackground) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
SetPowerStateCharging();
EXPECT_FALSE(ambient_controller()->IsShown());
// Should not lock the device and enter ambient mode when the screen is
// dimmed.
SetScreenIdleStateAndWait(/*dimmed=*/true, /*off=*/false);
EXPECT_FALSE(IsLocked());
EXPECT_TRUE(ambient_controller()->IsShown());
FastForwardToBackgroundLockScreenTimeout();
EXPECT_TRUE(IsLocked());
// Should not disrupt ongoing ambient mode.
EXPECT_TRUE(ambient_controller()->IsShown());
// General key press will exit ambient mode.
// Simulate key press to close the widget.
ui::test::EventGenerator* event_generator = GetEventGenerator();
event_generator->PressKey(ui::VKEY_A, /*flags=*/0);
EXPECT_FALSE(ambient_controller()->IsShown());
}
TEST_F(AmbientControllerTest, TEST_F(AmbientControllerTest,
ShouldShowAmbientScreenWithLockscreenWhenScreenIsDimmed) { ShouldShowAmbientScreenWithLockscreenWhenScreenIsDimmed) {
GetSessionControllerClient()->SetShouldLockScreenAutomatically(true); GetSessionControllerClient()->SetShouldLockScreenAutomatically(true);
......
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