Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/key_identifier_conversion_views.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop.h"
     10 #include "content/browser/browser_thread.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 #include "ui/base/keycodes/keyboard_codes.h"
     13 
     14 namespace {
     15 
     16 class KeyEventFromKeyIdentifierTest : public testing::Test {
     17  protected:
     18   KeyEventFromKeyIdentifierTest()
     19       : ui_thread_(BrowserThread::UI, &message_loop_) {}
     20 
     21   MessageLoopForUI message_loop_;
     22   BrowserThread ui_thread_;
     23 };
     24 
     25 TEST_F(KeyEventFromKeyIdentifierTest, MatchOnIdentifier) {
     26   EXPECT_EQ(ui::VKEY_APPS, KeyEventFromKeyIdentifier("Apps").key_code());
     27   EXPECT_EQ(ui::VKEY_UNKNOWN,
     28       KeyEventFromKeyIdentifier("Nonsense").key_code());
     29 }
     30 
     31 TEST_F(KeyEventFromKeyIdentifierTest, MatchOnCharacter) {
     32   EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("a").key_code());
     33   EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("A").key_code());
     34   EXPECT_EQ(ui::VKEY_OEM_PERIOD, KeyEventFromKeyIdentifier(">").key_code());
     35 
     36   std::string non_printing_char(" ");
     37   non_printing_char[0] = static_cast<char>(1);
     38   EXPECT_EQ(ui::VKEY_UNKNOWN,
     39       KeyEventFromKeyIdentifier(non_printing_char).key_code());
     40 }
     41 
     42 TEST_F(KeyEventFromKeyIdentifierTest, MatchOnUnicodeCodepoint) {
     43   EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("U+0041").key_code());
     44   EXPECT_EQ(ui::VKEY_A, KeyEventFromKeyIdentifier("U+0061").key_code());
     45   EXPECT_EQ(ui::VKEY_DELETE, KeyEventFromKeyIdentifier("U+007F").key_code());
     46 
     47   // this one exists in the map, but has no valid ui::VKEY
     48   EXPECT_EQ(ui::VKEY_UNKNOWN, KeyEventFromKeyIdentifier("U+030A").key_code());
     49 
     50   // this one is not in the map
     51   EXPECT_EQ(ui::VKEY_UNKNOWN, KeyEventFromKeyIdentifier("U+0001").key_code());
     52 }
     53 
     54 TEST_F(KeyEventFromKeyIdentifierTest, DoesNotMatchEmptyString) {
     55   EXPECT_EQ(ui::VKEY_UNKNOWN, KeyEventFromKeyIdentifier("").key_code());
     56 }
     57 
     58 TEST_F(KeyEventFromKeyIdentifierTest, ShiftModifiersAreSet) {
     59   EXPECT_EQ(0, KeyEventFromKeyIdentifier("1").flags());
     60 
     61   const char* keys_with_shift[] = {
     62     "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+",
     63     "{", "}", "|", ":", "<", ">", "?", "\""
     64   };
     65   int kNumKeysWithShift = arraysize(keys_with_shift);
     66 
     67   for (int i = 0; i < kNumKeysWithShift; ++i) {
     68     EXPECT_EQ(ui::EF_SHIFT_DOWN,
     69         KeyEventFromKeyIdentifier(keys_with_shift[i]).flags());
     70   }
     71 }
     72 
     73 }  // namespace
     74