summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/key_identifier_conversion_views_unittest.cc
blob: 3dc8d26bc88b746e7f91e804ee9ba7747a878d1d (plain)
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
// Copyright (c) 2011 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 "chrome/browser/extensions/key_identifier_conversion_views.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "content/browser/browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/keycodes/keyboard_codes.h"

namespace {

class KeyEventFromKeyIdentifierTest : public testing::Test {
 protected:
  KeyEventFromKeyIdentifierTest()
      : ui_thread_(BrowserThread::UI, &message_loop_) {}

  MessageLoopForUI message_loop_;
  BrowserThread ui_thread_;
};

TEST_F(KeyEventFromKeyIdentifierTest, MatchOnIdentifier) {
  EXPECT_EQ(ui::VKEY_APPS, KeyEventFromKeyIdentifier("Apps").key_code());
  EXPECT_EQ(ui::VKEY_UNKNOWN,
      KeyEventFromKeyIdentifier("Nonsense").key_code());
}

TEST_F(KeyEventFromKeyIdentifierTest, MatchOnCharacter) {
  EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("a").key_code());
  EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("A").key_code());
  EXPECT_EQ(ui::VKEY_OEM_PERIOD, KeyEventFromKeyIdentifier(">").key_code());

  std::string non_printing_char(" ");
  non_printing_char[0] = static_cast<char>(1);
  EXPECT_EQ(ui::VKEY_UNKNOWN,
      KeyEventFromKeyIdentifier(non_printing_char).key_code());
}

TEST_F(KeyEventFromKeyIdentifierTest, MatchOnUnicodeCodepoint) {
  EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("U+0041").key_code());
  EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("U+0061").key_code());
  EXPECT_EQ(ui::VKEY_DELETE, KeyEventFromKeyIdentifier("U+007F").key_code());

  // this one exists in the map, but has no valid ui::VKEY
  EXPECT_EQ(ui::VKEY_UNKNOWN, KeyEventFromKeyIdentifier("U+030A").key_code());

  // this one is not in the map
  EXPECT_EQ(ui::VKEY_UNKNOWN, KeyEventFromKeyIdentifier("U+0001").key_code());
}

TEST_F(KeyEventFromKeyIdentifierTest, DoesNotMatchEmptyString) {
  EXPECT_EQ(ui::VKEY_UNKNOWN, KeyEventFromKeyIdentifier("").key_code());
}

TEST_F(KeyEventFromKeyIdentifierTest, ShiftModifiersAreSet) {
  EXPECT_EQ(0, KeyEventFromKeyIdentifier("1").flags());

  const char* keys_with_shift[] = {
    "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+",
    "{", "}", "|", ":", "<", ">", "?", "\""
  };
  int kNumKeysWithShift = arraysize(keys_with_shift);

  for (int i = 0; i < kNumKeysWithShift; ++i) {
    EXPECT_EQ(ui::EF_SHIFT_DOWN,
        KeyEventFromKeyIdentifier(keys_with_shift[i]).flags());
  }
}

}  // namespace