chromium/ui/base/accelerators/accelerator_unittest.cc

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/accelerators/accelerator.h"

#include <string>

#include "base/strings/utf_string_conversions.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/ui_base_features.h"
#include "ui/events/event.h"
#include "ui/events/types/event_type.h"

namespace ui {

TEST(AcceleratorTest, Repeat) {}

TEST(AcceleratorTest, TimeStamp) {}

// Crash on Android builders. https://crbug.com/980267
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_GetShortcutText
#else
#define MAYBE_GetShortcutText
#endif
TEST(AcceleratorTest, MAYBE_GetShortcutText) {}

TEST(AcceleratorTest, ShortcutTextForUnknownKey) {}

TEST(AcceleratorTest, VerifyToKeyEventConstructor) {}

TEST(AcceleratorTest, ConversionFromKeyEvent) {}

#if BUILDFLAG(IS_MAC)
class AcceleratorTestMac : public testing::Test {
 public:
  AcceleratorTestMac() = default;
  ~AcceleratorTestMac() override = default;

  // Returns a "short" string representation of the modifier flags in
  // |modifier_mask|.
  std::u16string ShortFormStringForModifiers(int modifier_flags) {
    ui::KeyEvent key_event(ui::EventType::kKeyPressed, ui::VKEY_F,
                           modifier_flags);
    Accelerator accelerator(key_event);

    // Passing the empty string causes the method to return just the string
    // representation of the modifier flags.
    return accelerator.ApplyShortFormModifiers(std::u16string());
  }
};

// Checks that a string representation exists for all modifier masks that make
// sense on the Mac.
TEST_F(AcceleratorTestMac, ModifierFlagsShortFormRepresentation) {
  int modifier_flag = 1 << 0;
  while (modifier_flag) {
    // If |modifier_flag| is a valid modifier flag and it's not EF_ALTGR_DOWN
    // (the Linux Alt key on the right side of the keyboard), confirm that
    // a string representation for the modifier flag exists.
    if (Accelerator::MaskOutKeyEventFlags(modifier_flag) &&
        modifier_flag != EF_ALTGR_DOWN) {
      EXPECT_GT(this->ShortFormStringForModifiers(modifier_flag).size(), 0UL);
    }
    modifier_flag <<= 1;
  }
}
#endif

#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST(AcceleratorTest, ConversionFromKeyEvent_Ash) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeature(
      ::features::kImprovedKeyboardShortcuts);

  ui::KeyEvent key_event(ui::EventType::kKeyPressed, ui::VKEY_F,
                         ui::EF_ALT_DOWN | ui::EF_CONTROL_DOWN);
  Accelerator accelerator(key_event);

  EXPECT_EQ(accelerator.key_code(), ui::VKEY_F);
  EXPECT_EQ(accelerator.modifiers(), ui::EF_ALT_DOWN | ui::EF_CONTROL_DOWN);

  // Code is set when converting from a KeyEvent.
  EXPECT_EQ(accelerator.code(), DomCode::US_F);

  // Test resetting code.
  accelerator.reset_code();
  EXPECT_EQ(accelerator.code(), DomCode::NONE);
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

}  // namespace ui