Commit 14528e01 authored by Michael Sun's avatar Michael Sun Committed by Commit Bot

[Media Keys] Add handling for Media Pause

Unlike most keyboards, which have a single Play/Pause toggle key,
Bluetooth devices with AVRCP (Audio/Video Remote Control Profile)
generate separate Play and Pause key events based on the media status.
This patch adds handling for the Pause media key.

BUG=b/147364135

Change-Id: I0ee0eebf28411377dc7050e534abf1f4edb832d3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2063418
Commit-Queue: Michael Sun <michaelfsun@google.com>
Reviewed-by: default avatarBecca Hughes <beccahughes@chromium.org>
Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742826}
parent a474496a
......@@ -152,6 +152,31 @@ void MediaControllerImpl::HandleMediaPlay() {
client_->HandleMediaPlay();
}
void MediaControllerImpl::HandleMediaPause() {
if (Shell::Get()->session_controller()->IsScreenLocked() &&
!AreLockScreenMediaKeysEnabled()) {
return;
}
ui::RecordMediaHardwareKeyAction(ui::MediaHardwareKeyAction::kPause);
// If the |client_| is force handling the keys then we should forward them.
if (client_ && force_media_client_key_handling_) {
client_->HandleMediaPause();
return;
}
// If media session media key handling is enabled. Fire pause using the media
// session service.
if (ShouldUseMediaSession()) {
GetMediaSessionController()->Suspend();
return;
}
if (client_)
client_->HandleMediaPause();
}
void MediaControllerImpl::HandleMediaNextTrack() {
if (Shell::Get()->session_controller()->IsScreenLocked() &&
!AreLockScreenMediaKeysEnabled()) {
......
......@@ -61,6 +61,7 @@ class ASH_EXPORT MediaControllerImpl
// |client_|.
void HandleMediaPlayPause();
void HandleMediaPlay();
void HandleMediaPause();
void HandleMediaNextTrack();
void HandleMediaPrevTrack();
......
......@@ -74,6 +74,8 @@ class MediaControllerTest : public AshTestBase {
Flush();
Shell::Get()->media_controller()->HandleMediaPlay();
Flush();
Shell::Get()->media_controller()->HandleMediaPause();
Flush();
Shell::Get()->media_controller()->HandleMediaPrevTrack();
Flush();
Shell::Get()->media_controller()->HandleMediaNextTrack();
......@@ -94,7 +96,7 @@ TEST_F(MediaControllerTest, EnableMediaKeysWhenUnlocked) {
HandleMediaKeys();
EXPECT_EQ(1, controller()->suspend_count());
EXPECT_EQ(2, controller()->suspend_count());
EXPECT_EQ(1, controller()->resume_count());
EXPECT_EQ(1, controller()->previous_track_count());
EXPECT_EQ(1, controller()->next_track_count());
......@@ -158,7 +160,7 @@ TEST_F(MediaControllerTest, EnableMediaKeysWhenLockedAndControlsEnabled) {
HandleMediaKeys();
EXPECT_EQ(1, controller()->suspend_count());
EXPECT_EQ(2, controller()->suspend_count());
EXPECT_EQ(1, controller()->resume_count());
EXPECT_EQ(1, controller()->previous_track_count());
EXPECT_EQ(1, controller()->next_track_count());
......
......@@ -21,6 +21,9 @@ class ASH_PUBLIC_EXPORT MediaClient {
// Handles the Play Media shortcut key.
virtual void HandleMediaPlay() = 0;
// Handles the Pause Media shortcut key.
virtual void HandleMediaPause() = 0;
// Handles the Previous Track Media shortcut key.
virtual void HandleMediaPrevTrack() = 0;
......
......@@ -21,6 +21,10 @@ void TestMediaClient::HandleMediaPlay() {
++handle_media_play_count_;
}
void TestMediaClient::HandleMediaPause() {
++handle_media_pause_count_;
}
void TestMediaClient::HandleMediaPrevTrack() {
++handle_media_prev_track_count_;
}
......
......@@ -21,6 +21,7 @@ class TestMediaClient : public MediaClient {
void HandleMediaNextTrack() override;
void HandleMediaPlayPause() override;
void HandleMediaPlay() override;
void HandleMediaPause() override;
void HandleMediaPrevTrack() override;
void RequestCaptureState() override;
void SuspendMediaSessions() override;
......@@ -32,6 +33,7 @@ class TestMediaClient : public MediaClient {
return handle_media_play_pause_count_;
}
int handle_media_play_count() const { return handle_media_play_count_; }
int handle_media_pause_count() const { return handle_media_pause_count_; }
int handle_media_prev_track_count() const {
return handle_media_prev_track_count_;
}
......@@ -41,6 +43,7 @@ class TestMediaClient : public MediaClient {
int handle_media_next_track_count_ = 0;
int handle_media_play_pause_count_ = 0;
int handle_media_play_count_ = 0;
int handle_media_pause_count_ = 0;
int handle_media_prev_track_count_ = 0;
bool media_sessions_suspended_ = false;
......
......@@ -185,6 +185,10 @@ void MediaClientImpl::HandleMediaPlay() {
HandleMediaAction(ui::VKEY_MEDIA_PLAY);
}
void MediaClientImpl::HandleMediaPause() {
HandleMediaAction(ui::VKEY_MEDIA_PAUSE);
}
void MediaClientImpl::HandleMediaPrevTrack() {
HandleMediaAction(ui::VKEY_MEDIA_PREV_TRACK);
}
......@@ -299,8 +303,10 @@ void MediaClientImpl::HandleMediaAction(ui::KeyboardCode keycode) {
case ui::VKEY_MEDIA_PLAY_PAUSE:
router->NotifyTogglePlayState();
break;
// TODO(https://crbug.com/1053777): Handle media action for VKEY_MEDIA_PLAY.
// TODO(https://crbug.com/1053777): Handle media action for VKEY_MEDIA_PLAY
// and VKEY_MEDIA_PAUSE.
case ui::VKEY_MEDIA_PLAY:
case ui::VKEY_MEDIA_PAUSE:
break;
default:
break;
......
......@@ -38,6 +38,7 @@ class MediaClientImpl : public ash::MediaClient,
void HandleMediaNextTrack() override;
void HandleMediaPlayPause() override;
void HandleMediaPlay() override;
void HandleMediaPause() override;
void HandleMediaPrevTrack() override;
void RequestCaptureState() override;
void SuspendMediaSessions() override;
......
......@@ -205,6 +205,44 @@ TEST_F(MediaClientTest, HandleMediaPlay) {
EXPECT_EQ(base::nullopt, delegate()->ConsumeLastMediaKey());
}
TEST_F(MediaClientTest, HandleMediaPause) {
const ui::Accelerator test_accelerator(ui::VKEY_MEDIA_PAUSE, ui::EF_NONE);
// Enable custom media key handling for the current browser. Ensure that the
// client set the override on the controller.
client()->EnableCustomMediaKeyHandler(profile(), delegate());
EXPECT_TRUE(controller()->force_media_client_key_handling());
// Simulate the media key and check that the delegate received it.
client()->HandleMediaPause();
EXPECT_EQ(test_accelerator, delegate()->ConsumeLastMediaKey());
// Change the active browser and ensure the override was disabled.
BrowserList::SetLastActive(alt_browser());
EXPECT_FALSE(controller()->force_media_client_key_handling());
// Simulate the media key and check that the delegate did not receive it.
client()->HandleMediaPause();
EXPECT_EQ(base::nullopt, delegate()->ConsumeLastMediaKey());
// Change the active browser back and ensure the override was enabled.
BrowserList::SetLastActive(browser());
EXPECT_TRUE(controller()->force_media_client_key_handling());
// Simulate the media key and check the delegate received it.
client()->HandleMediaPause();
EXPECT_EQ(test_accelerator, delegate()->ConsumeLastMediaKey());
// Disable custom media key handling for the current browser and ensure the
// override was disabled.
client()->DisableCustomMediaKeyHandler(profile(), delegate());
EXPECT_FALSE(controller()->force_media_client_key_handling());
// Simulate the media key and check the delegate did not receive it.
client()->HandleMediaPause();
EXPECT_EQ(base::nullopt, delegate()->ConsumeLastMediaKey());
}
TEST_F(MediaClientTest, HandleMediaNextTrack) {
const ui::Accelerator test_accelerator(ui::VKEY_MEDIA_NEXT_TRACK,
ui::EF_NONE);
......
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