1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cstddef>
#include "ash/test/ash_test_util.h"
#include "ash/webui/shortcut_customization_ui/backend/accelerator_layout_table.h"
#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/ui/views/accelerator_table.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Internal builds add two extra accelerator for the Feedback app.
// The total number of Chrome accelerators (available on Chrome OS).
constexpr int kChromeAcceleratorsTotalNum = 103;
// The hash of Chrome accelerators (available on Chrome OS).
constexpr char kChromeAcceleratorsHash[] =
"c282a17ba234831076aa56d669e3f0b9029d000c63ecfb4b4536551e28f06974";
#else
// The total number of Chrome accelerators (available on Chrome OS).
constexpr int kChromeAcceleratorsTotalNum = 101;
// The hash of Chrome accelerators (available on Chrome OS).
constexpr char kChromeAcceleratorsHash[] =
"9402197253b0e51ab774a01cb4f8ed2ead743711c7a6a96f19b4901d3fd9dae3";
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
const char kCommonMessage[] =
"If you are modifying Chrome OS available shortcuts, please update "
"kAcceleratorLayouts & following the instruction in "
"ash/webui/shortcut_customization_ui/backend/"
"accelerator_layout_table.h and the following value(s) on the "
"top of this file:\n";
std::string ModifiersToString(int modifiers) {
return base::StringPrintf(
"shift=%s control=%s alt=%s search=%s",
base::ToString<bool>(modifiers & ui::EF_SHIFT_DOWN),
base::ToString<bool>(modifiers & ui::EF_CONTROL_DOWN),
base::ToString<bool>(modifiers & ui::EF_ALT_DOWN),
base::ToString<bool>(modifiers & ui::EF_COMMAND_DOWN));
}
struct ChromeAcceleratorMappingCmp {
bool operator()(const AcceleratorMapping& lhs,
const AcceleratorMapping& rhs) {
return std::tie(lhs.keycode, lhs.modifiers) <
std::tie(rhs.keycode, rhs.modifiers);
}
};
std::string ChromeAcceleratorMappingToString(
const AcceleratorMapping& accelerator) {
return base::StringPrintf("keycode=%d command_id=%d ", accelerator.keycode,
accelerator.command_id) +
ModifiersToString(accelerator.modifiers);
}
class ChromeAcceleratorMetadataTest : public testing::Test {
public:
ChromeAcceleratorMetadataTest() = default;
ChromeAcceleratorMetadataTest(const ChromeAcceleratorMetadataTest&) = delete;
ChromeAcceleratorMetadataTest& operator=(
const ChromeAcceleratorMetadataTest&) = delete;
~ChromeAcceleratorMetadataTest() override = default;
};
} // namespace
// Test that modifying chrome accelerator should update kAcceleratorLayouts.
// 1. If you are adding/deleting/modifying shortcuts, please also
// add/delete/modify the corresponding item in kAcceleratorLayouts.
// 2. Please update the number and hash value of chrome accelerators on the top
// of this file. The new number and hash value will be provided in the test
// output.
TEST_F(ChromeAcceleratorMetadataTest,
ModifyChromeAcceleratorShouldUpdateLayout) {
std::vector<AcceleratorMapping> chrome_accelerators;
for (const auto& accel_mapping : GetAcceleratorList()) {
chrome_accelerators.emplace_back(accel_mapping);
}
const int chrome_accelerators_number = chrome_accelerators.size();
EXPECT_EQ(chrome_accelerators_number, kChromeAcceleratorsTotalNum)
<< kCommonMessage
<< "kChromeAcceleratorsTotalNum=" << chrome_accelerators_number << "\n";
std::stable_sort(chrome_accelerators.begin(), chrome_accelerators.end(),
ChromeAcceleratorMappingCmp());
const std::string chrome_accelerators_hash = ash::StableHashOfCollection(
chrome_accelerators, ChromeAcceleratorMappingToString);
EXPECT_EQ(chrome_accelerators_hash, kChromeAcceleratorsHash)
<< kCommonMessage << "kChromeAcceleratorsHash=\""
<< chrome_accelerators_hash << "\"\n";
}
} // namespace ash
|