exo: Simplifies WaylandKeyboardDelegate lifetime management.
It is just the delegate of Keyboard, so instead of using
the observer, this CL just defers the lifetime management
of WaylandKeyboardDelegate to Keyboard.
This allows us to remove the inheritance of KeyboardObserver
from WaylandKeyboardDelegate.
BUG=1123705
TEST=Ran exo_unittests.
Change-Id: I45b35a016edc3c7bd441223f1b25a28ad1debaa3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2416476
Commit-Queue: Hidehiko Abe <[email protected]>
Reviewed-by: Jun Mukai <[email protected]>
Cr-Commit-Position: refs/heads/master@{#808192}
diff --git a/components/exo/keyboard.cc b/components/exo/keyboard.cc
index 554e57e3..1b316fe6 100644
--- a/components/exo/keyboard.cc
+++ b/components/exo/keyboard.cc
@@ -165,8 +165,8 @@
////////////////////////////////////////////////////////////////////////////////
// Keyboard, public:
-Keyboard::Keyboard(KeyboardDelegate* delegate, Seat* seat)
- : delegate_(delegate),
+Keyboard::Keyboard(std::unique_ptr<KeyboardDelegate> delegate, Seat* seat)
+ : delegate_(std::move(delegate)),
seat_(seat),
expiration_delay_for_pending_key_acks_(base::TimeDelta::FromMilliseconds(
kExpirationDelayForPendingKeyAcksMs)) {
diff --git a/components/exo/keyboard.h b/components/exo/keyboard.h
index 843e9e6..2851d56 100644
--- a/components/exo/keyboard.h
+++ b/components/exo/keyboard.h
@@ -5,7 +5,7 @@
#ifndef COMPONENTS_EXO_KEYBOARD_H_
#define COMPONENTS_EXO_KEYBOARD_H_
-#include <vector>
+#include <memory>
#include "ash/public/cpp/keyboard/keyboard_controller_observer.h"
#include "base/containers/flat_map.h"
@@ -36,10 +36,10 @@
public SeatObserver,
public ash::KeyboardControllerObserver {
public:
- Keyboard(KeyboardDelegate* delegate, Seat* seat);
+ Keyboard(std::unique_ptr<KeyboardDelegate> delegate, Seat* seat);
~Keyboard() override;
- KeyboardDelegate* delegate() const { return delegate_; }
+ KeyboardDelegate* delegate() const { return delegate_.get(); }
bool HasDeviceConfigurationDelegate() const;
void SetDeviceConfigurationDelegate(
@@ -91,7 +91,7 @@
// The delegate instance that all events except for events about device
// configuration are dispatched to.
- KeyboardDelegate* const delegate_;
+ std::unique_ptr<KeyboardDelegate> delegate_;
// Seat that the Keyboard recieves focus events from.
Seat* const seat_;
diff --git a/components/exo/keyboard_delegate.h b/components/exo/keyboard_delegate.h
index 90484cb..6e9bb031 100644
--- a/components/exo/keyboard_delegate.h
+++ b/components/exo/keyboard_delegate.h
@@ -19,6 +19,8 @@
// Handles events on keyboards in context-specific ways.
class KeyboardDelegate {
public:
+ virtual ~KeyboardDelegate() = default;
+
// This should return true if |surface| is a valid target for this keyboard.
// E.g. the surface is owned by the same client as the keyboard.
virtual bool CanAcceptKeyboardEventsForSurface(Surface* surface) const = 0;
@@ -46,9 +48,6 @@
virtual void OnKeyRepeatSettingsChanged(bool enabled,
base::TimeDelta delay,
base::TimeDelta interval) = 0;
-
- protected:
- virtual ~KeyboardDelegate() {}
};
} // namespace exo
diff --git a/components/exo/keyboard_unittest.cc b/components/exo/keyboard_unittest.cc
index bda7e62..81a6f22 100644
--- a/components/exo/keyboard_unittest.cc
+++ b/components/exo/keyboard_unittest.cc
@@ -42,7 +42,6 @@
MockKeyboardDelegate() = default;
// Overridden from KeyboardDelegate:
- MOCK_METHOD(void, OnKeyboardDestroying, (Keyboard*));
MOCK_METHOD(bool, CanAcceptKeyboardEventsForSurface, (Surface*), (const));
MOCK_METHOD(void,
OnKeyboardEnter,
@@ -62,7 +61,6 @@
MockKeyboardDeviceConfigurationDelegate() = default;
// Overridden from KeyboardDeviceConfigurationDelegate:
- MOCK_METHOD(void, OnKeyboardDestroying, (Keyboard*));
MOCK_METHOD(void, OnKeyboardTypeChanged, (bool));
};
@@ -85,8 +83,7 @@
// key events. https://crbug.com/1008574.
TEST_F(KeyboardTest, CorrectSeatPressedKeysOnSwitchingDesks) {
Seat seat;
- NiceMockKeyboardDelegate delegate;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
// Create 2 desks.
auto* desks_controller = ash::DesksController::Get();
@@ -160,16 +157,17 @@
focus_client->FocusWindow(surface->window());
// Keyboard should try to set initial focus to surface.
- NiceMockKeyboardDelegate delegate;
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(false));
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ Keyboard keyboard(std::move(delegate), &seat);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(ui::EF_SHIFT_DOWN));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(ui::EF_SHIFT_DOWN));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>(
{{ui::DomCode::US_A, ui::DomCode::US_A}})));
@@ -181,17 +179,17 @@
// Release key after surface lost focus.
focus_client->FocusWindow(nullptr);
generator.ReleaseKey(ui::VKEY_A, ui::EF_SHIFT_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Key should no longer be pressed when focus returns.
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(ui::EF_SHIFT_DOWN));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(ui::EF_SHIFT_DOWN));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window()->GetToplevelWindow());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
}
TEST_F(KeyboardTest, OnKeyboardLeave) {
@@ -207,34 +205,37 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
- ON_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ auto keyboard = std::make_unique<Keyboard>(std::move(delegate), &seat);
+ ON_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillByDefault(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, OnKeyboardLeave(surface.get()));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
focus_client->FocusWindow(nullptr);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, OnKeyboardLeave(surface.get()));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
shell_surface.reset();
surface.reset();
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
}
TEST_F(KeyboardTest, OnKeyboardKey) {
@@ -250,75 +251,80 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
// This should only generate a press event for KEY_A.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_A);
generator.PressKey(ui::VKEY_A, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// This should not generate another press event for KEY_A.
generator.PressKey(ui::VKEY_A, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// This should only generate a single release event for KEY_A.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_A, false));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_A, false));
generator.ReleaseKey(ui::VKEY_A, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Test key event rewriting. In this case, ARROW_DOWN is rewritten to KEY_END
// as a result of ALT being pressed.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::END, true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(ui::EF_ALT_DOWN));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::END, true));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(ui::EF_ALT_DOWN));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::ARROW_DOWN);
generator.PressKey(ui::VKEY_END, ui::EF_ALT_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// This should generate a release event for KEY_END as that is the key
// associated with the key press.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::END, false));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::END, false));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
generator.ReleaseKey(ui::VKEY_DOWN, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Press accelerator after surface lost focus.
- EXPECT_CALL(delegate, OnKeyboardLeave(surface.get()));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
focus_client->FocusWindow(nullptr);
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_W);
generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Key should be pressed when focus returns.
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(ui::EF_CONTROL_DOWN));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(ui::EF_CONTROL_DOWN));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>(
{{ui::DomCode::US_W, ui::DomCode::US_W}})));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Releasing accelerator when surface has focus should generate event.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
generator.ReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Key events should be ignored when the focused window is not an
// exo::Surface.
@@ -326,24 +332,26 @@
gfx::Rect(buffer_size));
// Moving the focus away will trigger the fallback path in GetEffectiveFocus.
// TODO(oshima): Consider removing the fallback path.
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
focus_client->FocusWindow(window.get());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardKey(testing::_, ui::DomCode::ARROW_LEFT, true))
.Times(0);
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::ARROW_LEFT);
generator.PressKey(ui::VKEY_LEFT, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardKey(testing::_, ui::DomCode::ARROW_LEFT, false))
.Times(0);
generator.ReleaseKey(ui::VKEY_LEFT, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
}
TEST_F(KeyboardTest, OnKeyboardKey_NotSendKeyIfConsumedByIme) {
@@ -359,18 +367,19 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
views::Widget* widget =
@@ -381,12 +390,12 @@
// If a text field is focused, a pressed key event is not sent to a client
// because a key event should be consumed by the IME.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_A, true))
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_A, true))
.Times(0);
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_A);
generator.PressKey(ui::VKEY_A, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// TODO(yhanada): The below EXPECT_CALL fails because exo::Keyboard currently
// sends a key release event for the keys which exo::Keyboard sent a pressed
@@ -398,29 +407,33 @@
// ImeBlocking.
WMHelper::GetInstance()->SetImeBlocked(surface->window()->GetToplevelWindow(),
true);
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_B, true));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_B, true));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_B);
generator.PressKey(ui::VKEY_B, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_B, false));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_B, false));
generator.ReleaseKey(ui::VKEY_B, 0);
WMHelper::GetInstance()->SetImeBlocked(surface->window()->GetToplevelWindow(),
false);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Any key event should be sent to a client if a key event skips IME.
surface->window()->SetProperty(aura::client::kSkipImeProcessing, true);
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_C, true));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_C, true));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_C);
generator.PressKey(ui::VKEY_C, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_C, false));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_C, false));
generator.ReleaseKey(ui::VKEY_C, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
input_method->SetFocusedTextInputClient(nullptr);
}
@@ -438,42 +451,48 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
// This should generate a modifier event.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(ui::EF_SHIFT_DOWN));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_A, true));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(ui::EF_SHIFT_DOWN));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_A);
generator.PressKey(ui::VKEY_A, ui::EF_SHIFT_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// This should generate another modifier event.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_B, true));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_B, true));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardModifiers(ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_B);
generator.PressKey(ui::VKEY_B, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// This should generate a third modifier event.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_B, false));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_B, false));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
generator.ReleaseKey(ui::VKEY_B, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
}
TEST_F(KeyboardTest, OnKeyboardTypeChanged) {
@@ -506,9 +525,10 @@
ash::Shell::Get()->tablet_mode_controller();
tablet_mode_controller->SetEnabledForTest(true);
- NiceMockKeyboardDelegate delegate;
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ auto keyboard = std::make_unique<Keyboard>(
+ std::make_unique<NiceMockKeyboardDelegate>(), &seat);
+
MockKeyboardDeviceConfigurationDelegate configuration_delegate;
EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
@@ -554,14 +574,13 @@
ui::InputDevice(2, ui::InputDeviceType::INPUT_DEVICE_USB, "keyboard")};
device_data_manager->OnKeyboardDevicesUpdated(keyboards);
- NiceMockKeyboardDelegate delegate;
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
MockKeyboardDeviceConfigurationDelegate configuration_delegate;
EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
- keyboard->SetDeviceConfigurationDelegate(&configuration_delegate);
- EXPECT_TRUE(keyboard->HasDeviceConfigurationDelegate());
+ keyboard.SetDeviceConfigurationDelegate(&configuration_delegate);
+ EXPECT_TRUE(keyboard.HasDeviceConfigurationDelegate());
testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
ash::AccessibilityControllerImpl* accessibility_controller =
@@ -575,6 +594,8 @@
// Disable a11y keyboard calls OnKeyboardTypeChanged() with true.
EXPECT_CALL(configuration_delegate, OnKeyboardTypeChanged(true));
accessibility_controller->virtual_keyboard().SetEnabled(false);
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
testing::Mock::VerifyAndClearExpectations(&configuration_delegate);
}
@@ -584,13 +605,13 @@
base::TimeDelta::FromMilliseconds(1000);
TEST_F(KeyboardTest, KeyRepeatSettingsLoadDefaults) {
- MockKeyboardDelegate delegate;
- EXPECT_CALL(delegate, OnKeyRepeatSettingsChanged).Times(0);
- EXPECT_CALL(delegate,
+ auto delegate = std::make_unique<MockKeyboardDelegate>();
+ EXPECT_CALL(*delegate, OnKeyRepeatSettingsChanged).Times(0);
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(true, kDelta500Ms, kDelta50Ms));
Seat seat;
- Keyboard keyboard(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
}
TEST_F(KeyboardTest, KeyRepeatSettingsLoadInitially) {
@@ -599,34 +620,34 @@
SetUserPref(email, ash::prefs::kXkbAutoRepeatDelay, base::Value(1000));
SetUserPref(email, ash::prefs::kXkbAutoRepeatInterval, base::Value(1000));
- MockKeyboardDelegate delegate;
- EXPECT_CALL(delegate, OnKeyRepeatSettingsChanged).Times(0);
- EXPECT_CALL(delegate,
+ auto delegate = std::make_unique<MockKeyboardDelegate>();
+ EXPECT_CALL(*delegate, OnKeyRepeatSettingsChanged).Times(0);
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(true, kDelta1000Ms, kDelta1000Ms));
Seat seat;
- Keyboard keyboard(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
}
TEST_F(KeyboardTest, KeyRepeatSettingsUpdateAtRuntime) {
- MockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<MockKeyboardDelegate>();
{
testing::InSequence s;
// Initially load defaults.
- EXPECT_CALL(delegate, OnKeyRepeatSettingsChanged)
+ EXPECT_CALL(*delegate, OnKeyRepeatSettingsChanged)
.Times(testing::AtLeast(1));
// Respond to pref changes, in order
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(false, testing::_, testing::_));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(false, kDelta1000Ms, testing::_));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(false, kDelta1000Ms, kDelta1000Ms));
}
Seat seat;
- Keyboard keyboard(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
std::string email = "user0@tray";
SetUserPref(email, ash::prefs::kXkbAutoRepeatEnabled, base::Value(false));
@@ -639,12 +660,12 @@
CreateUserSessions(2);
// Key repeat settings should be sent exactly once, for the default values.
- MockKeyboardDelegate delegate;
- EXPECT_CALL(delegate, OnKeyRepeatSettingsChanged).Times(0);
- EXPECT_CALL(delegate,
+ auto delegate = std::make_unique<MockKeyboardDelegate>();
+ EXPECT_CALL(*delegate, OnKeyRepeatSettingsChanged).Times(0);
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(true, kDelta500Ms, kDelta50Ms));
Seat seat;
- Keyboard keyboard(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
// Set prefs for non-active user; no calls should result.
std::string email = "user1@tray";
@@ -663,46 +684,48 @@
SetUserPref(email, ash::prefs::kXkbAutoRepeatDelay, base::Value(1000));
SetUserPref(email, ash::prefs::kXkbAutoRepeatInterval, base::Value(1000));
- MockKeyboardDelegate delegate;
- EXPECT_CALL(delegate, OnKeyRepeatSettingsChanged).Times(0);
+ auto delegate = std::make_unique<MockKeyboardDelegate>();
+ EXPECT_CALL(*delegate, OnKeyRepeatSettingsChanged).Times(0);
{
testing::InSequence s;
// Initially, load default prefs for first user.
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(true, kDelta500Ms, kDelta50Ms));
// Switching user should load new prefs.
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate,
OnKeyRepeatSettingsChanged(true, kDelta1000Ms, kDelta1000Ms));
}
Seat seat;
- Keyboard keyboard(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
SimulateUserLogin(email, user_manager::UserType::USER_TYPE_REGULAR);
}
TEST_F(KeyboardTest, KeyboardObserver) {
- NiceMockKeyboardDelegate delegate;
- Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ // Declare before the keyboard so the mock verification happens
+ // after the keyboard destruction.
MockKeyboardObserver observer1;
MockKeyboardObserver observer2;
- keyboard->AddObserver(&observer1);
- keyboard->AddObserver(&observer2);
- EXPECT_TRUE(keyboard->HasObserver(&observer1));
- EXPECT_TRUE(keyboard->HasObserver(&observer2));
+ Seat seat;
+ Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
+
+ keyboard.AddObserver(&observer1);
+ keyboard.AddObserver(&observer2);
+ EXPECT_TRUE(keyboard.HasObserver(&observer1));
+ EXPECT_TRUE(keyboard.HasObserver(&observer2));
testing::Mock::VerifyAndClearExpectations(&observer1);
testing::Mock::VerifyAndClearExpectations(&observer2);
- keyboard->RemoveObserver(&observer1);
- EXPECT_FALSE(keyboard->HasObserver(&observer1));
- EXPECT_TRUE(keyboard->HasObserver(&observer2));
+ keyboard.RemoveObserver(&observer1);
+ EXPECT_FALSE(keyboard.HasObserver(&observer1));
+ EXPECT_TRUE(keyboard.HasObserver(&observer2));
testing::Mock::VerifyAndClearExpectations(&observer1);
testing::Mock::VerifyAndClearExpectations(&observer2);
- EXPECT_CALL(observer1, OnKeyboardDestroying(keyboard.get())).Times(0);
- EXPECT_CALL(observer2, OnKeyboardDestroying(keyboard.get()));
- keyboard.reset();
+ // Called from the destructor of Keyboard.
+ EXPECT_CALL(observer1, OnKeyboardDestroying(&keyboard)).Times(0);
+ EXPECT_CALL(observer2, OnKeyboardDestroying(&keyboard));
}
TEST_F(KeyboardTest, NeedKeyboardKeyAcks) {
@@ -718,15 +741,14 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::make_unique<NiceMockKeyboardDelegate>(), &seat);
- EXPECT_FALSE(keyboard->AreKeyboardKeyAcksNeeded());
- keyboard->SetNeedKeyboardKeyAcks(true);
- EXPECT_TRUE(keyboard->AreKeyboardKeyAcksNeeded());
- keyboard->SetNeedKeyboardKeyAcks(false);
- EXPECT_FALSE(keyboard->AreKeyboardKeyAcksNeeded());
+ EXPECT_FALSE(keyboard.AreKeyboardKeyAcksNeeded());
+ keyboard.SetNeedKeyboardKeyAcks(true);
+ EXPECT_TRUE(keyboard.AreKeyboardKeyAcksNeeded());
+ keyboard.SetNeedKeyboardKeyAcks(false);
+ EXPECT_FALSE(keyboard.AreKeyboardKeyAcksNeeded());
}
TEST_F(KeyboardTest, AckKeyboardKey) {
@@ -742,24 +764,25 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// If we don't set NeedKeyboardAckKeys to true, accelerators are always passed
// to ShellSurface.
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
// Press KEY_W with Ctrl.
- EXPECT_CALL(delegate, OnKeyboardModifiers(4));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(4));
EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
ui::VKEY_W, ui::EF_CONTROL_DOWN,
ui::Accelerator::KeyState::PRESSED)))
@@ -770,45 +793,46 @@
// Release KEY_W.
generator.ReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
testing::Mock::VerifyAndClearExpectations(shell_surface.get());
// If we set NeedKeyboardAckKeys to true, only unhandled accelerators are
// passed to ShellSurface.
- keyboard->SetNeedKeyboardKeyAcks(true);
+ keyboard.SetNeedKeyboardKeyAcks(true);
// Press KEY_W with Ctrl.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
.WillOnce(testing::Return(1));
generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Send ack for the key press.
EXPECT_CALL(*shell_surface.get(), AcceleratorPressed(ui::Accelerator(
ui::VKEY_W, ui::EF_CONTROL_DOWN,
ui::Accelerator::KeyState::PRESSED)))
.WillOnce(testing::Return(true));
- keyboard->AckKeyboardKey(1, false /* handled */);
+ keyboard.AckKeyboardKey(1, false /* handled */);
testing::Mock::VerifyAndClearExpectations(shell_surface.get());
// Release KEY_W.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, false))
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_W, false))
.WillOnce(testing::Return(2));
generator.ReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Send ack for the key release.
- keyboard->AckKeyboardKey(2, false /* handled */);
+ keyboard.AckKeyboardKey(2, false /* handled */);
// Press KEY_W with Ctrl again.
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
.WillOnce(testing::Return(3));
generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Send ack for the key press.
// AcceleratorPressed is not called when the accelerator is already handled.
- keyboard->AckKeyboardKey(3, true /* handled */);
+ keyboard.AckKeyboardKey(3, true /* handled */);
// A repeat key event should not be sent to the client and also should not
// invoke the accelerator.
@@ -829,10 +853,13 @@
testing::Mock::VerifyAndClearExpectations(shell_surface.get());
// Release the key and reset modifier_flags.
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
generator.ReleaseKey(ui::VKEY_W, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
}
TEST_F(KeyboardTest, AckKeyboardKeyMoveFocus) {
@@ -848,38 +875,39 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0)).Times(1);
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0)).Times(1);
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- keyboard->SetNeedKeyboardKeyAcks(true);
+ keyboard.SetNeedKeyboardKeyAcks(true);
// Press KEY_W with Ctrl.
- EXPECT_CALL(delegate, OnKeyboardModifiers(4)).Times(1);
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(4)).Times(1);
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
.WillOnce(testing::Return(1));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_W);
generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Move focus from the window
- EXPECT_CALL(delegate, OnKeyboardLeave(surface.get()));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
focus_client->FocusWindow(nullptr);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Send ack for the key press. |AcceleratorPressed()| should not be called.
- keyboard->AckKeyboardKey(1, false /* handled */);
+ keyboard.AckKeyboardKey(1, false /* handled */);
}
TEST_F(KeyboardTest, AckKeyboardKeyExpired) {
@@ -895,30 +923,31 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- keyboard->SetNeedKeyboardKeyAcks(true);
+ keyboard.SetNeedKeyboardKeyAcks(true);
// Press KEY_W with Ctrl.
- EXPECT_CALL(delegate, OnKeyboardModifiers(4));
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(4));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
.WillOnce(testing::Return(1));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_W);
generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
// Keyboard processes pending events as if it is handled when it expires,
// so |AcceleratorPressed()| should not be called.
@@ -940,13 +969,16 @@
// key should have been treated as handled already and removed from the
// pending_key_acks_ map. Since the event is no longer in the map,
// |AcceleratorPressed()| should not be called.
- keyboard->AckKeyboardKey(1, false /* handled */);
+ keyboard.AckKeyboardKey(1, false /* handled */);
// Release the key and reset modifier_flags.
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
+ OnKeyboardKey(testing::_, ui::DomCode::US_W, false));
generator.ReleaseKey(ui::VKEY_W, 0);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
}
// Test for crbug.com/753539. If action for an accelerator moves the focus to
@@ -981,36 +1013,37 @@
aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
focus_client->FocusWindow(nullptr);
- NiceMockKeyboardDelegate delegate;
+ auto delegate = std::make_unique<NiceMockKeyboardDelegate>();
+ auto* delegate_ptr = delegate.get();
Seat seat;
- auto keyboard = std::make_unique<Keyboard>(&delegate, &seat);
+ Keyboard keyboard(std::move(delegate), &seat);
- EXPECT_CALL(delegate, CanAcceptKeyboardEventsForSurface(surface.get()))
+ EXPECT_CALL(*delegate_ptr, CanAcceptKeyboardEventsForSurface(surface.get()))
.WillOnce(testing::Return(true));
- EXPECT_CALL(delegate, OnKeyboardModifiers(0));
- EXPECT_CALL(delegate,
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(0));
+ EXPECT_CALL(*delegate_ptr,
OnKeyboardEnter(surface.get(),
base::flat_map<ui::DomCode, ui::DomCode>()));
focus_client->FocusWindow(surface->window());
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
- keyboard->SetNeedKeyboardKeyAcks(true);
+ keyboard.SetNeedKeyboardKeyAcks(true);
// Press KEY_W with Ctrl.
- EXPECT_CALL(delegate, OnKeyboardModifiers(4));
- EXPECT_CALL(delegate, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
+ EXPECT_CALL(*delegate_ptr, OnKeyboardModifiers(4));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardKey(testing::_, ui::DomCode::US_W, true))
.WillOnce(testing::Return(1));
seat.set_physical_code_for_currently_processing_event_for_testing(
ui::DomCode::US_W);
generator.PressKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
- testing::Mock::VerifyAndClearExpectations(&delegate);
+ testing::Mock::VerifyAndClearExpectations(delegate_ptr);
- EXPECT_CALL(delegate, OnKeyboardLeave(surface.get()));
+ EXPECT_CALL(*delegate_ptr, OnKeyboardLeave(surface.get()));
// Send ack as unhandled. This will call |AcceleratorPressed| and move the
// focus.
- keyboard->AckKeyboardKey(1, false /* handled */);
+ keyboard.AckKeyboardKey(1, false /* handled */);
// Wait until |ProcessExpiredPendingKeyAcks| is fired.
base::RunLoop run_loop;
@@ -1020,6 +1053,8 @@
run_loop.Run();
base::RunLoop().RunUntilIdle();
+ // Verify before destroying keyboard to make sure the expected call
+ // is made on the methods above, rather than in the destructor.
testing::Mock::VerifyAndClearExpectations(&delegate);
}
} // namespace
diff --git a/components/exo/wayland/wayland_keyboard_delegate.cc b/components/exo/wayland/wayland_keyboard_delegate.cc
index de2b4dc..9b916a2 100644
--- a/components/exo/wayland/wayland_keyboard_delegate.cc
+++ b/components/exo/wayland/wayland_keyboard_delegate.cc
@@ -42,10 +42,6 @@
#endif
}
-void WaylandKeyboardDelegate::OnKeyboardDestroying(Keyboard* keyboard) {
- delete this;
-}
-
bool WaylandKeyboardDelegate::CanAcceptKeyboardEventsForSurface(
Surface* surface) const {
wl_resource* surface_resource = GetSurfaceResource(surface);
diff --git a/components/exo/wayland/wayland_keyboard_delegate.h b/components/exo/wayland/wayland_keyboard_delegate.h
index e376467d..14e869d 100644
--- a/components/exo/wayland/wayland_keyboard_delegate.h
+++ b/components/exo/wayland/wayland_keyboard_delegate.h
@@ -38,8 +38,7 @@
// Keyboard delegate class that accepts events for surfaces owned by the same
// client as a keyboard resource.
class WaylandKeyboardDelegate : public WaylandInputDelegate,
- public KeyboardDelegate,
- public KeyboardObserver
+ public KeyboardDelegate
#if defined(OS_CHROMEOS)
,
public ash::ImeControllerImpl::Observer
@@ -52,7 +51,6 @@
~WaylandKeyboardDelegate() override;
// Overridden from KeyboardDelegate:
- void OnKeyboardDestroying(Keyboard* keyboard) override;
bool CanAcceptKeyboardEventsForSurface(Surface* surface) const override;
void OnKeyboardEnter(
Surface* surface,
diff --git a/components/exo/wayland/wl_seat.cc b/components/exo/wayland/wl_seat.cc
index a22eb87..1b50eaa 100644
--- a/components/exo/wayland/wl_seat.cc
+++ b/components/exo/wayland/wl_seat.cc
@@ -86,27 +86,22 @@
}
void seat_get_keyboard(wl_client* client, wl_resource* resource, uint32_t id) {
-#if defined(OS_CHROMEOS)
-#if BUILDFLAG(USE_XKBCOMMON)
+#if defined(OS_CHROMEOS) && BUILDFLAG(USE_XKBCOMMON)
auto* data = GetUserDataAs<WaylandSeat>(resource);
uint32_t version = wl_resource_get_version(resource);
wl_resource* keyboard_resource =
wl_resource_create(client, &wl_keyboard_interface, version, id);
- WaylandKeyboardDelegate* delegate =
- new WaylandKeyboardDelegate(keyboard_resource, data->serial_tracker);
- std::unique_ptr<Keyboard> keyboard =
- std::make_unique<Keyboard>(delegate, data->seat);
- keyboard->AddObserver(delegate);
+ auto keyboard =
+ std::make_unique<Keyboard>(std::make_unique<WaylandKeyboardDelegate>(
+ keyboard_resource, data->serial_tracker),
+ data->seat);
SetImplementation(keyboard_resource, &keyboard_implementation,
std::move(keyboard));
#else
NOTIMPLEMENTED();
-#endif // BUILDFLAG(USE_XKBCOMMON)
-#else
- NOTIMPLEMENTED();
-#endif // defined(OS_CHROMEOS)
+#endif // defined(OS_CHROMEOS) && BUILDFLAG(USE_XKBCOMMON)
}
void seat_get_touch(wl_client* client, wl_resource* resource, uint32_t id) {