1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include "session/internal/keymap_factory.h"
31 
32 #include <map>
33 
34 #include "composer/key_parser.h"
35 #include "config/config_handler.h"
36 #include "protocol/commands.pb.h"
37 #include "protocol/config.pb.h"
38 #include "session/internal/keymap.h"
39 #include "testing/base/public/googletest.h"
40 #include "testing/base/public/gunit.h"
41 
42 namespace mozc {
43 namespace keymap {
44 
45 class TestKeyMapFactoryProxy {
46  public:
Clear()47   static void Clear() {
48     KeyMapFactory::KeyMapManagerMap &keymaps = KeyMapFactory::keymaps_;
49 
50     for (KeyMapFactory::KeyMapManagerMap::iterator iter = keymaps.begin();
51          iter != keymaps.end(); ++iter) {
52       KeyMapFactory::pool_.Release(iter->second);
53     }
54 
55     keymaps.clear();
56   }
57 };
58 
59 namespace {
60 
61 class KeyMapFactoryTest : public testing::Test {
62  protected:
SetUp()63   virtual void SetUp() {
64     TestKeyMapFactoryProxy::Clear();
65   }
66 
TearDown()67   virtual void TearDown() {
68     TestKeyMapFactoryProxy::Clear();
69   }
70 };
71 
72 }  // namespace
73 
TEST_F(KeyMapFactoryTest,KeyMapFactoryTest)74 TEST_F(KeyMapFactoryTest, KeyMapFactoryTest) {
75   {  // KeyMapFactory returns correct instance.
76     KeyMapManager *atok =
77         KeyMapFactory::GetKeyMapManager(config::Config::ATOK);
78     KeyMapManager *msime =
79         KeyMapFactory::GetKeyMapManager(config::Config::MSIME);
80     KeyMapManager *kotoeri =
81         KeyMapFactory::GetKeyMapManager(config::Config::KOTOERI);
82     KeyMapManager *custom =
83         KeyMapFactory::GetKeyMapManager(config::Config::CUSTOM);
84     KeyMapManager *mobile =
85         KeyMapFactory::GetKeyMapManager(config::Config::MOBILE);
86 
87     // GetKeyMapFactory() returns the same instance when argument is the same
88     EXPECT_EQ(atok,
89               KeyMapFactory::GetKeyMapManager(config::Config::ATOK));
90     EXPECT_EQ(msime,
91               KeyMapFactory::GetKeyMapManager(config::Config::MSIME));
92     EXPECT_EQ(kotoeri,
93               KeyMapFactory::GetKeyMapManager(config::Config::KOTOERI));
94     EXPECT_EQ(custom,
95               KeyMapFactory::GetKeyMapManager(config::Config::CUSTOM));
96     EXPECT_EQ(mobile,
97               KeyMapFactory::GetKeyMapManager(config::Config::MOBILE));
98 
99     // GetKeyMapFactory() does not return the same instance
100     // when argument is not the same
101     KeyMapManager *keymap_array[] = {
102       atok, msime, kotoeri, custom, mobile,
103     };
104     const int array_size = arraysize(keymap_array);
105     for (int i = 0; i < array_size; ++i) {
106       for (int j = 0; j < array_size; ++j) {
107         if (i != j) {
108           EXPECT_NE(keymap_array[i], keymap_array[j]) <<
109               "current_index = (" << i << "," << j << ")";
110         }
111       }
112     }
113   }
114 }
115 
TEST_F(KeyMapFactoryTest,ReloadWhenGetInstance)116 TEST_F(KeyMapFactoryTest, ReloadWhenGetInstance) {
117   commands::KeyEvent key;
118   key.set_special_key(commands::KeyEvent::SPACE);
119   config::Config config;
120 
121   {  // ConvertNext
122     const char *kCustomTableConvertNext =
123         "status\tkey\tcommand\n"
124         "Conversion\tSpace\tConvertNext\n";
125     config.set_custom_keymap_table(kCustomTableConvertNext);
126 
127     KeyMapManager *keymap = KeyMapFactory::GetKeyMapManager(
128         config::Config::CUSTOM);
129     keymap->ReloadConfig(config);
130     ConversionState::Commands key_command;
131     keymap->GetCommandConversion(key, &key_command);
132     EXPECT_EQ(ConversionState::CONVERT_NEXT, key_command);
133   }
134 
135   // ConvertPrev
136   const char *kCustomTableConvertPrev =
137       "status\tkey\tcommand\n"
138       "Conversion\tSpace\tConvertPrev\n";
139   config.set_custom_keymap_table(kCustomTableConvertPrev);
140 
141   KeyMapManager *keymap = KeyMapFactory::GetKeyMapManager(
142       config::Config::CUSTOM);
143   keymap->ReloadConfig(config);
144   ConversionState::Commands key_command;
145   keymap->GetCommandConversion(key, &key_command);
146   EXPECT_EQ(ConversionState::CONVERT_PREV, key_command);
147 }
148 
149 }  // namespace keymap
150 }  // namespace mozc
151