#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "ui/base/accelerators/accelerator_manager.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/test_accelerator_target.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace ui {
namespace test {
namespace {
Accelerator GetAccelerator(KeyboardCode code, int mask) { … }
const int kAcceleratorModifiers[] = …;
int BuildAcceleratorModifier(int id) { … }
class AcceleratorManagerTest : public testing::Test { … };
TEST_F(AcceleratorManagerTest, Register) { … }
TEST_F(AcceleratorManagerTest, RegisterMultipleTarget) { … }
TEST_F(AcceleratorManagerTest, Unregister) { … }
TEST_F(AcceleratorManagerTest, UnregisterAll) { … }
TEST_F(AcceleratorManagerTest, Process) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(AcceleratorManagerTest, PositionalShortcuts_AllEqual) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kImprovedKeyboardShortcuts);
AcceleratorManager manager;
manager.SetUsePositionalLookup(true);
TestAcceleratorTarget target;
const Accelerator accelerator(VKEY_OEM_6, EF_CONTROL_DOWN);
manager.Register({accelerator}, AcceleratorManager::kNormalPriority, &target);
KeyEvent event(ui::EventType::kKeyPressed, VKEY_OEM_6,
ui::DomCode::BRACKET_RIGHT, ui::EF_CONTROL_DOWN,
ui::DomKey::FromCharacter(']'), base::TimeTicks());
const Accelerator trigger(event);
EXPECT_TRUE(manager.IsRegistered(trigger));
EXPECT_TRUE(manager.Process(trigger));
}
TEST_F(AcceleratorManagerTest, PositionalShortcuts_MatchingDomCode) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kImprovedKeyboardShortcuts);
AcceleratorManager manager;
manager.SetUsePositionalLookup(true);
TestAcceleratorTarget target;
const Accelerator accelerator(VKEY_OEM_6, EF_CONTROL_DOWN);
manager.Register({accelerator}, AcceleratorManager::kNormalPriority, &target);
KeyEvent event(ui::EventType::kKeyPressed, VKEY_OEM_PLUS,
ui::DomCode::BRACKET_RIGHT, ui::EF_CONTROL_DOWN,
ui::DomKey::FromCharacter(']'), base::TimeTicks());
const Accelerator trigger(event);
EXPECT_TRUE(manager.IsRegistered(trigger));
EXPECT_TRUE(manager.Process(trigger));
}
TEST_F(AcceleratorManagerTest, PositionalShortcuts_NotMatchingDomCode) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kImprovedKeyboardShortcuts);
AcceleratorManager manager;
manager.SetUsePositionalLookup(true);
TestAcceleratorTarget target;
const Accelerator accelerator(VKEY_OEM_6, EF_CONTROL_DOWN);
manager.Register({accelerator}, AcceleratorManager::kNormalPriority, &target);
KeyEvent event(ui::EventType::kKeyPressed, VKEY_OEM_6,
ui::DomCode::BRACKET_LEFT, ui::EF_CONTROL_DOWN,
ui::DomKey::FromCharacter(']'), base::TimeTicks());
const Accelerator trigger(event);
EXPECT_FALSE(manager.IsRegistered(trigger));
EXPECT_FALSE(manager.Process(trigger));
}
TEST_F(AcceleratorManagerTest, PositionalShortcuts_NonPositionalMatch) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kImprovedKeyboardShortcuts);
AcceleratorManager manager;
manager.SetUsePositionalLookup(true);
TestAcceleratorTarget target;
const Accelerator accelerator(VKEY_Z, EF_CONTROL_DOWN);
manager.Register({accelerator}, AcceleratorManager::kNormalPriority, &target);
KeyEvent event(ui::EventType::kKeyPressed, VKEY_Z, ui::DomCode::US_Y,
ui::EF_CONTROL_DOWN, ui::DomKey::FromCharacter(']'),
base::TimeTicks());
const Accelerator trigger(event);
EXPECT_TRUE(manager.IsRegistered(trigger));
EXPECT_TRUE(manager.Process(trigger));
}
TEST_F(AcceleratorManagerTest, PositionalShortcuts_NonPositionalNonMatch) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(
features::kImprovedKeyboardShortcuts);
AcceleratorManager manager;
manager.SetUsePositionalLookup(true);
TestAcceleratorTarget target;
const Accelerator accelerator(VKEY_Z, EF_CONTROL_DOWN);
manager.Register({accelerator}, AcceleratorManager::kNormalPriority, &target);
KeyEvent event(ui::EventType::kKeyPressed, VKEY_Y, ui::DomCode::US_Z,
ui::EF_CONTROL_DOWN, ui::DomKey::FromCharacter(']'),
base::TimeTicks());
const Accelerator trigger(event);
EXPECT_FALSE(manager.IsRegistered(trigger));
EXPECT_FALSE(manager.Process(trigger));
}
#endif
}
}
}