#include "ui/base/accelerators/accelerator_map.h"
#include <utility>
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/dom/dom_code.h"
namespace ui {
namespace {
bool IsValidMatch(AcceleratorMap<int>* map,
const Accelerator& pressed,
int expected) { … }
TEST(AcceleratorMapTest, MapIsEmpty) { … }
TEST(AcceleratorMapTest, EmptyFind) { … }
TEST(AcceleratorMapTest, FindExists) { … }
TEST(AcceleratorMapTest, FindDoesNotExist) { … }
TEST(AcceleratorMapTest, InsertDefaultCreatedNew) { … }
TEST(AcceleratorMapTest, ChangeValueViaReturnedRef) { … }
TEST(AcceleratorMapTest, SetValueDirect) { … }
TEST(AcceleratorMapTest, Iterate) { … }
#if BUILDFLAG(IS_CHROMEOS)
TEST(AcceleratorMapTest, PositionalLookupExistsVkeyOnly) {
AcceleratorMap<int> m;
m.set_use_positional_lookup(true);
Accelerator accelerator(VKEY_Z, EF_SHIFT_DOWN);
EXPECT_EQ(DomCode::NONE, accelerator.code());
const int expected = 77;
m.InsertNew(std::make_pair(accelerator, expected));
EXPECT_TRUE(IsValidMatch(&m, accelerator, expected));
}
TEST(AcceleratorMapTest, PositionalLookupExistsFullMatch) {
AcceleratorMap<int> m;
m.set_use_positional_lookup(true);
Accelerator registered(VKEY_OEM_6, EF_SHIFT_DOWN);
const int expected = 77;
m.InsertNew(std::make_pair(registered, expected));
Accelerator pressed(VKEY_OEM_6, DomCode::BRACKET_RIGHT, EF_SHIFT_DOWN);
EXPECT_TRUE(IsValidMatch(&m, pressed, expected));
}
TEST(AcceleratorMapTest, PositionalLookupDomCodeMatchOnly) {
AcceleratorMap<int> m;
m.set_use_positional_lookup(true);
Accelerator registered(VKEY_OEM_6, EF_SHIFT_DOWN);
const int expected = 77;
m.InsertNew(std::make_pair(registered, expected));
Accelerator pressed(VKEY_OEM_PLUS, DomCode::BRACKET_RIGHT, EF_SHIFT_DOWN);
EXPECT_TRUE(IsValidMatch(&m, pressed, expected));
}
TEST(AcceleratorMapTest, PositionalLookupDisabled) {
AcceleratorMap<int> m;
Accelerator registered(VKEY_OEM_6, EF_SHIFT_DOWN);
const int expected = 77;
m.InsertNew(std::make_pair(registered, expected));
m.set_use_positional_lookup(true);
Accelerator pressed(VKEY_OEM_PLUS, DomCode::BRACKET_RIGHT, EF_SHIFT_DOWN);
EXPECT_TRUE(IsValidMatch(&m, pressed, expected));
m.set_use_positional_lookup(false);
EXPECT_FALSE(IsValidMatch(&m, pressed, expected));
}
TEST(AcceleratorMapTest, PositionalLookupVkeyMatchOnlyBothDomCodesSpecified) {
AcceleratorMap<int> m;
m.set_use_positional_lookup(true);
Accelerator registered(VKEY_OEM_6, EF_SHIFT_DOWN);
const int expected = 77;
m.InsertNew(std::make_pair(registered, expected));
Accelerator pressed(VKEY_OEM_6, DomCode::EQUAL, EF_SHIFT_DOWN);
EXPECT_FALSE(IsValidMatch(&m, pressed, expected));
}
TEST(AcceleratorMapTest, PositionalLookupVkeyMatchOnlyRegisteredDomCodeIsNone) {
AcceleratorMap<int> m;
m.set_use_positional_lookup(true);
Accelerator registered(VKEY_Z, EF_SHIFT_DOWN);
const int expected = 77;
m.InsertNew(std::make_pair(registered, expected));
Accelerator pressed(VKEY_Z, DomCode::US_Y, EF_SHIFT_DOWN);
EXPECT_TRUE(IsValidMatch(&m, pressed, expected));
}
TEST(AcceleratorMapTest, DomCodesStrippedWhenInserted) {
AcceleratorMap<int> m;
m.set_use_positional_lookup(true);
Accelerator accelerator(ui::VKEY_F, DomCode::US_F,
ui::EF_ALT_DOWN | ui::EF_CONTROL_DOWN);
EXPECT_EQ(accelerator.code(), DomCode::US_F);
const int expected = 77;
m.InsertNew(std::make_pair(accelerator, expected));
accelerator.reset_code();
auto* value = m.Find(accelerator);
ASSERT_TRUE(value);
EXPECT_EQ(*value, expected);
}
#endif
}
}