diff options
3 files changed, 72 insertions, 1 deletions
diff --git a/chrome/browser/chromeos/input_method/hotkey_manager.cc b/chrome/browser/chromeos/input_method/hotkey_manager.cc index e45e2df..a1d2999 100644 --- a/chrome/browser/chromeos/input_method/hotkey_manager.cc +++ b/chrome/browser/chromeos/input_method/hotkey_manager.cc @@ -95,6 +95,20 @@ bool HotkeyManager::AddHotkey(int event_id, std::make_pair(std::make_pair(keysym, modifiers), event_id)).second; } +bool HotkeyManager::RemoveHotkey(int event_id) { + bool result = false; + std::map<std::pair<uint32, uint32>, int>::iterator iter; + for (iter = hotkeys_.begin(); iter != hotkeys_.end();) { + if (iter->second == event_id) { + hotkeys_.erase(iter++); + result = true; + } else { + ++iter; + } + } + return result; +} + bool HotkeyManager::FilterKeyEvent(const XEvent& key_event) { bool is_key_press = true; switch (key_event.type) { diff --git a/chrome/browser/chromeos/input_method/hotkey_manager.h b/chrome/browser/chromeos/input_method/hotkey_manager.h index 96de64f..bcee045 100644 --- a/chrome/browser/chromeos/input_method/hotkey_manager.h +++ b/chrome/browser/chromeos/input_method/hotkey_manager.h @@ -40,7 +40,10 @@ class HotkeyManager { uint32 keysym, // one of XK_* in X11/keysymdef.h. uint32 modifiers, // set of *Mask flags in X11/X.h, or zero. bool trigger_on_key_press); - // TODO(yusukes): Add RemoveHotkey function if we support crosbug.com/6267. + + // Removes all hotkeys whose ID is |event_id|. Returns false if no such hotkey + // is registered. + bool RemoveHotkey(int event_id); // Checks if |key_event| is a hotkey. If it is, Observer::HotkeyPressed() is // called for each observers. This function returns true if the key_event diff --git a/chrome/browser/chromeos/input_method/hotkey_manager_unittest.cc b/chrome/browser/chromeos/input_method/hotkey_manager_unittest.cc index 590562f..604da4b 100644 --- a/chrome/browser/chromeos/input_method/hotkey_manager_unittest.cc +++ b/chrome/browser/chromeos/input_method/hotkey_manager_unittest.cc @@ -191,6 +191,60 @@ TEST(HotkeyManagerTest, TestAddTwoHotkeys) { XK_a, 0x0, false)); } +TEST(HotkeyManagerTest, TestRemoveHotkey1) { + TestableHotkeyManager manager; + EXPECT_TRUE(manager.AddHotkey(0, XK_a, 0x0, true)); + EXPECT_TRUE(manager.AddHotkey(1, XK_b, 0x0, false)); + EXPECT_EQ(0, manager.FilterKeyEventInternal(XK_a, 0x0, true)); + EXPECT_EQ(TestableHotkeyManager::kFiltered, manager.FilterKeyEventInternal( + XK_a, 0x0, false)); + + // Remove the second hotkey and confirm that it is actually removed. + EXPECT_TRUE(manager.RemoveHotkey(1)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_b, 0x0, true)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_b, 0x0, false)); + // Remove the first one as well. + EXPECT_TRUE(manager.RemoveHotkey(0)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_a, 0x0, true)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_a, 0x0, false)); + + // Can't remove a hotkey twice. + EXPECT_FALSE(manager.RemoveHotkey(0)); + EXPECT_FALSE(manager.RemoveHotkey(1)); + // "2" is not registered yet. + EXPECT_FALSE(manager.RemoveHotkey(2)); +} + +TEST(HotkeyManagerTest, TestRemoveHotkey2) { + TestableHotkeyManager manager; + // Add two hotkeys that have the same id "0". + EXPECT_TRUE(manager.AddHotkey(0, XK_a, 0x0, true)); + EXPECT_TRUE(manager.AddHotkey(0, XK_b, 0x0, false)); + EXPECT_EQ(0, manager.FilterKeyEventInternal(XK_a, 0x0, true)); + EXPECT_EQ(TestableHotkeyManager::kFiltered, manager.FilterKeyEventInternal( + XK_a, 0x0, false)); + + // Remove the hotkey and confirm that it is actually removed. + EXPECT_TRUE(manager.RemoveHotkey(0)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_b, 0x0, true)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_b, 0x0, false)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_a, 0x0, true)); + EXPECT_EQ(TestableHotkeyManager::kNoEvent, manager.FilterKeyEventInternal( + XK_a, 0x0, false)); + + // Can't remove a hotkey twice. + EXPECT_FALSE(manager.RemoveHotkey(0)); + // "1" is not registered yet. + EXPECT_FALSE(manager.RemoveHotkey(1)); +} + // Press Shift, then press Alt. Release Alt first. TEST(HotkeyManagerTest, TestAltShift1) { scoped_ptr<TestableHotkeyManager> manager(CreateManager()); |