Chromium Code Reviews| Index: chrome/browser/ui/views/accelerator_table_unittest_mac.mm |
| diff --git a/chrome/browser/ui/views/accelerator_table_unittest_mac.mm b/chrome/browser/ui/views/accelerator_table_unittest_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9bf94eeb1373b2ff9ac643cb228e619b6740a926 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/accelerator_table_unittest_mac.mm |
| @@ -0,0 +1,178 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <stddef.h> |
| + |
| +#include <set> |
| + |
| +#include "base/stl_util.h" |
| +#include "build/build_config.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#import "chrome/browser/global_keyboard_shortcuts_mac.h" |
| +#include "chrome/browser/ui/views/accelerator_table.h" |
|
sky
2016/11/08 18:34:10
This should be the first include.
|
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/events/event_constants.h" |
| +#import "ui/events/keycodes/keyboard_code_conversion_mac.h" |
| + |
| +using namespace chrome; |
| + |
| +namespace { |
| + |
| +void VerifyTableDoesntHaveDuplicates( |
| + const std::vector<KeyboardShortcutData>& table, |
| + const std::string& table_name) { |
| + const std::vector<AcceleratorMapping> accelerators(GetAcceleratorList()); |
| + |
| + for (const auto& e : table) { |
| + int modifiers = 0; |
| + if (e.command_key) |
| + modifiers |= ui::EF_COMMAND_DOWN; |
| + if (e.shift_key) |
| + modifiers |= ui::EF_SHIFT_DOWN; |
| + if (e.cntrl_key) |
| + modifiers |= ui::EF_CONTROL_DOWN; |
| + if (e.opt_key) |
| + modifiers |= ui::EF_ALT_DOWN; |
| + |
| + for (const auto& accelerator_entry : accelerators) { |
| + unichar character; |
| + unichar shifted_character; |
| + const int vkey_code = ui::MacKeyCodeForWindowsKeyCode( |
| + accelerator_entry.keycode, accelerator_entry.modifiers, |
| + &shifted_character, &character); |
| + |
| + EXPECT_FALSE(modifiers == accelerator_entry.modifiers && |
| + e.chrome_command == accelerator_entry.command_id && |
| + (e.vkey_code ? (e.vkey_code == vkey_code) |
| + : (e.key_char == character || |
| + e.key_char == shifted_character))) |
| + << "Duplicate command: " << accelerator_entry.command_id |
| + << " in table " << table_name; |
| + } |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +// Vefifies that only the whitelisted accelerators could have Control key |
| +// modifier, while running on macOS. |
| +TEST(AcceleratorTableTest, CheckMacOSControlAccelerators) { |
| + // Only the accelerators that also work in Cocoa browser are allowed to appear |
| + // on this whitelist. |
| + const std::set<int> whitelisted_control_shortcuts = { |
| + IDC_SELECT_NEXT_TAB, |
| + IDC_SELECT_PREVIOUS_TAB, |
| + IDC_FULLSCREEN, |
| + }; |
| + |
| + const std::vector<AcceleratorMapping> accelerators(GetAcceleratorList()); |
| + |
| + // Control modifier is rarely used on Mac, and all valid uses must be |
| + // whitelisted. |
| + for (const auto& entry : accelerators) { |
| + if (base::ContainsKey(whitelisted_control_shortcuts, entry.command_id)) |
| + continue; |
| + EXPECT_FALSE(entry.modifiers & ui::EF_CONTROL_DOWN) |
| + << "Found non-whitelisted accelerator that contains Control " |
| + "modifier: " << entry.command_id; |
| + } |
| + |
| + // Test that whitelist is not outdated. |
| + for (const auto& whitelist_entry : whitelisted_control_shortcuts) { |
| + const auto entry = |
| + std::find_if(accelerators.begin(), accelerators.end(), |
| + [whitelist_entry](const AcceleratorMapping& a) { |
| + return a.command_id == whitelist_entry && |
| + (a.modifiers & ui::EF_CONTROL_DOWN) != 0; |
| + }); |
| + EXPECT_NE(entry, accelerators.end()) |
| + << "Whitelisted accelerator not found in the actual list: " |
| + << whitelist_entry; |
| + } |
| +} |
| + |
| +// Verifies that Alt-only (or with just Shift) accelerators are not present in |
| +// the list. |
| +TEST(AcceleratorTableTest, CheckMacOSAltAccelerators) { |
| + const int kNonShiftMask = |
| + ui::EF_COMMAND_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN; |
| + for (const auto& entry : GetAcceleratorList()) { |
| + EXPECT_FALSE((entry.modifiers & kNonShiftMask) == ui::EF_ALT_DOWN) |
| + << "Found accelerator that uses solely Alt modifier: " |
| + << entry.command_id; |
| + } |
| +} |
| + |
| +// Vefifies that accelerator filtering works correctly and we have an |
| +// alternative shortcut for each filtered-out accelerator. |
| +TEST(AcceleratorTableTest, CheckMacOSFilteredOutAcceleratorsHaveAlternatives) { |
| + // It's okay not to have an alternative accelerator for these commands as |
| + // there's no alternative for them in the Cocoa browser. |
| + const std::set<int> whitelisted_unique_shortcuts = { |
| + IDC_FOCUS_TOOLBAR, |
| + IDC_FOCUS_BOOKMARKS, |
| + IDC_FOCUS_INFOBARS, |
| + IDC_SHOW_APP_MENU, |
| + }; |
| + |
| + // Convert the accelerator lists to sets for easy searching/filtering. |
| + auto ToSet = [](const std::vector<AcceleratorMapping>& v) { |
| + return std::set<AcceleratorMapping, bool (*)(const AcceleratorMapping&, |
| + const AcceleratorMapping&)>( |
| + v.begin(), v.end(), |
| + [](const AcceleratorMapping& lhs, const AcceleratorMapping& rhs) { |
| + return std::tie(lhs.command_id, lhs.keycode, lhs.modifiers) < |
| + std::tie(rhs.command_id, rhs.keycode, rhs.modifiers); |
| + }); |
| + }; |
| + |
| + const auto accelerators = ToSet(GetAcceleratorList()); |
| + const auto full_accelerators = |
| + ToSet(GetUnfilteredAcceleratorListForTesting()); |
| + |
| + // Ensure that we still have alternative mappings for all filtered-out |
| + // accelerators. |
| + auto filtered_accelerators(full_accelerators); |
| + // Remove all accelerators that are present in |accelerators|. |
| + for (const auto& m : accelerators) |
| + filtered_accelerators.erase(m); |
| + EXPECT_GT(filtered_accelerators.size(), 1u); |
| + |
| + for (const auto& removed_entry : filtered_accelerators) { |
| + if (base::ContainsKey(whitelisted_unique_shortcuts, |
| + removed_entry.command_id)) |
| + continue; |
| + |
| + const auto entry = |
| + std::find_if(accelerators.begin(), accelerators.end(), |
| + [&removed_entry](const AcceleratorMapping& m) { |
| + return removed_entry.command_id == m.command_id; |
| + }); |
| + EXPECT_NE(entry, accelerators.end()) |
| + << "Filtered command doesn't have an alternative mapping: " |
| + << removed_entry.command_id; |
| + } |
| + |
| + for (const auto& whitelist_entry : whitelisted_unique_shortcuts) { |
| + const auto entry = |
| + std::find_if(filtered_accelerators.begin(), filtered_accelerators.end(), |
| + [&whitelist_entry](const AcceleratorMapping& m) { |
| + return whitelist_entry == m.command_id; |
| + }); |
| + EXPECT_NE(entry, filtered_accelerators.end()) |
| + << "Whitelisted accelerator not found in the filtered-out list: " |
| + << whitelist_entry; |
| + } |
| +} |
| + |
| +// Verifies that we're not processing any duplicate accelerators in |
| +// global_keyboard_shortcuts_mac.mm functions. |
| +TEST(AcceleratorTableTest, CheckNoDuplicatesGlobalKeyboardShortcutsMac) { |
| + VerifyTableDoesntHaveDuplicates(GetWindowKeyboardShortcutTable(), |
| + "WindowKeyboardShortcutTable"); |
| + VerifyTableDoesntHaveDuplicates(GetDelayedWindowKeyboardShortcutTable(), |
| + "DelayedWindowKeyboardShortcutTable"); |
| + VerifyTableDoesntHaveDuplicates(GetBrowserKeyboardShortcutTable(), |
| + "BrowserKeyboardShortcutTable"); |
| +} |