Codebase list mozc / 25f3fcf
Move embedded typing models to data set file When data package is reloaded, typing models must be reloaded too. This CL moves the global instances of typing model to a member of Table class and implements cache clear method in TableManager. BUG= TEST= REF_BUG=26841123,30236194 REF_CL=127786726,128029339 REF_TIME=2016-07-19T11:08:50+09:00 REF_TIME_RAW=1468894130 +0900 Noriyuki Takahashi 7 years ago
33 changed file(s) with 592 addition(s) and 430 deletion(s). Raw diff Collapse all Expand all
4949 'table.cc',
5050 ],
5151 'dependencies': [
52 'embedded_typing_model#host',
5352 '../base/base.gyp:base',
5453 '../base/base.gyp:config_file_stream',
5554 '../composer/composer.gyp:key_event_util',
6160 '../protocol/protocol.gyp:commands_proto',
6261 '../transliteration/transliteration.gyp:transliteration',
6362 ],
64 },
65 {
66 'target_name': 'gen_typing_model',
67 'type': 'none',
68 'toolsets': ['host'],
69 'actions': [
70 {
71 'action_name': 'gen_qwerty_mobile-hiragana_typing_model',
72 'variables': {
73 'input_files': [
74 '<(mozc_dir)/data/typing/typing_model_qwerty_mobile-hiragana.tsv',
75 ],
76 },
77 'inputs': [
78 '<(mozc_dir)/composer/internal/gen_typing_model.py',
79 '<@(input_files)',
80 ],
81 'outputs': [
82 '<(gen_out_dir)/internal/typing_model_qwerty_mobile-hiragana.h',
83 ],
84 'action': [
85 'python',
86 '<(mozc_dir)/composer/internal/gen_typing_model.py',
87 '--input_path',
88 '<@(input_files)',
89 '--output_path',
90 '<@(_outputs)',
91 '--variable_name',
92 'QwertyMobileHiragana'
93 ],
94 },
95 {
96 'action_name': 'gen_12keys-hiragana_typing_model',
97 'variables': {
98 'input_files': [
99 '<(mozc_dir)/data/typing/typing_model_12keys-hiragana.tsv',
100 ],
101 },
102 'inputs': [
103 '<(mozc_dir)/composer/internal/gen_typing_model.py',
104 '<@(input_files)',
105 ],
106 'outputs': [
107 '<(gen_out_dir)/internal/typing_model_12keys-hiragana.h',
108 ],
109 'action': [
110 'python',
111 '<(mozc_dir)/composer/internal/gen_typing_model.py',
112 '--input_path',
113 '<@(input_files)',
114 '--output_path',
115 '<@(_outputs)',
116 '--variable_name',
117 '12keysHiragana'
118 ],
119 },
120 {
121 'action_name': 'gen_flick-hiragana_typing_model',
122 'variables': {
123 'input_files': [
124 '<(mozc_dir)/data/typing/typing_model_flick-hiragana.tsv',
125 ],
126 },
127 'inputs': [
128 '<(mozc_dir)/composer/internal/gen_typing_model.py',
129 '<@(input_files)',
130 ],
131 'outputs': [
132 '<(gen_out_dir)/internal/typing_model_flick-hiragana.h',
133 ],
134 'action': [
135 'python',
136 '<(mozc_dir)/composer/internal/gen_typing_model.py',
137 '--input_path',
138 '<@(input_files)',
139 '--output_path',
140 '<@(_outputs)',
141 '--variable_name',
142 'FlickHiragana'
143 ],
144 },
145 {
146 'action_name': 'gen_godan-hiragana_typing_model',
147 'variables': {
148 'input_files': [
149 '<(mozc_dir)/data/typing/typing_model_godan-hiragana.tsv',
150 ],
151 },
152 'inputs': [
153 '<(mozc_dir)/composer/internal/gen_typing_model.py',
154 '<@(input_files)',
155 ],
156 'outputs': [
157 '<(gen_out_dir)/internal/typing_model_godan-hiragana.h',
158 ],
159 'action': [
160 'python',
161 '<(mozc_dir)/composer/internal/gen_typing_model.py',
162 '--input_path',
163 '<@(input_files)',
164 '--output_path',
165 '<@(_outputs)',
166 '--variable_name',
167 'GodanHiragana'
168 ],
169 },
170 {
171 'action_name': 'gen_toggle_flick-hiragana_typing_model',
172 'variables': {
173 'input_files': [
174 '<(mozc_dir)/data/typing/typing_model_toggle_flick-hiragana.tsv',
175 ],
176 },
177 'inputs': [
178 '<(mozc_dir)/composer/internal/gen_typing_model.py',
179 '<@(input_files)',
180 ],
181 'outputs': [
182 '<(gen_out_dir)/internal/typing_model_toggle_flick-hiragana.h',
183 ],
184 'action': [
185 'python',
186 '<(mozc_dir)/composer/internal/gen_typing_model.py',
187 '--input_path',
188 '<@(input_files)',
189 '--output_path',
190 '<@(_outputs)',
191 '--variable_name',
192 'ToggleFlickHiragana'
193 ],
194 },
195 ],
196 },
197 {
198 'target_name': 'embedded_typing_model',
199 'type': 'none',
200 'toolsets': ['host'],
201 'hard_dependency': 1,
202 'dependencies': [
203 'gen_typing_model#host',
204 ],
205 'export_dependent_settings': [
206 'gen_typing_model#host',
207 ]
20863 },
20964 {
21065 'target_name': 'key_event_util',
3434 #include <vector>
3535
3636 #include "base/logging.h"
37 #include "base/singleton.h"
3837 #include "base/system_util.h"
3938 #include "base/util.h"
4039 #include "composer/internal/typing_model.h"
4241 #include "composer/table.h"
4342 #include "config/character_form_manager.h"
4443 #include "config/config_handler.h"
44 #include "data_manager/testing/mock_data_manager.h"
4545 #include "protocol/commands.pb.h"
4646 #include "protocol/config.pb.h"
4747 #include "testing/base/public/gunit.h"
121121
122122 } // namespace
123123
124 class ComposerTest : public testing::Test {
124 class ComposerTest : public ::testing::Test {
125125 protected:
126 ComposerTest() {}
127
128 virtual void SetUp() {
126 ComposerTest() = default;
127 ~ComposerTest() override = default;
128
129 void SetUp() override {
129130 table_.reset(new Table);
130131 config_.reset(new Config);
131132 request_.reset(new Request);
133134 CharacterFormManager::GetCharacterFormManager()->SetDefaultRule();
134135 }
135136
136 virtual void TearDown() {
137 void TearDown() override {
137138 CharacterFormManager::GetCharacterFormManager()->SetDefaultRule();
138139 composer_.reset();
139140 request_.reset();
141142 table_.reset();
142143 }
143144
145 const testing::MockDataManager mock_data_manager_;
144146 std::unique_ptr<Composer> composer_;
145147 std::unique_ptr<Table> table_;
146148 std::unique_ptr<Request> request_;
14361438
14371439 TEST_F(ComposerTest, ShiftKeyOperationForKatakana) {
14381440 config_->set_shift_key_mode_switch(Config::KATAKANA_INPUT_MODE);
1439 table_->InitializeWithRequestAndConfig(*request_, *config_);
1441 table_->InitializeWithRequestAndConfig(*request_, *config_,
1442 mock_data_manager_);
14401443 composer_->Reset();
14411444 composer_->SetInputMode(transliteration::HIRAGANA);
14421445 InsertKey("K", composer_.get());
14781481 config_->set_preedit_method(Config::ROMAN);
14791482 config_->set_use_auto_ime_turn_off(true);
14801483
1481 table_->InitializeWithRequestAndConfig(*request_, *config_);
1484 table_->InitializeWithRequestAndConfig(*request_, *config_,
1485 mock_data_manager_);
14821486
14831487 commands::KeyEvent key;
14841488
15971601 config_->set_preedit_method(Config::ROMAN);
15981602 config_->set_use_auto_ime_turn_off(false);
15991603
1600 table_->InitializeWithRequestAndConfig(*request_, *config_);
1604 table_->InitializeWithRequestAndConfig(*request_, *config_,
1605 mock_data_manager_);
16011606
16021607 commands::KeyEvent key;
16031608
16341639 config_->set_preedit_method(Config::KANA);
16351640 config_->set_use_auto_ime_turn_off(true);
16361641
1637 table_->InitializeWithRequestAndConfig(*request_, *config_);
1642 table_->InitializeWithRequestAndConfig(*request_, *config_,
1643 mock_data_manager_);
16381644
16391645 commands::KeyEvent key;
16401646
26102616 TEST_F(ComposerTest, CaseSensitiveByConfiguration) {
26112617 {
26122618 config_->set_shift_key_mode_switch(Config::OFF);
2613 table_->InitializeWithRequestAndConfig(*request_, *config_);
2619 table_->InitializeWithRequestAndConfig(*request_, *config_,
2620 mock_data_manager_);
26142621
26152622 // i -> "い"
26162623 table_->AddRule("i", "\xe3\x81\x84", "");
26292636 composer_->Reset();
26302637 {
26312638 config_->set_shift_key_mode_switch(Config::ASCII_INPUT_MODE);
2632 table_->InitializeWithRequestAndConfig(*request_, *config_);
2639 table_->InitializeWithRequestAndConfig(*request_, *config_,
2640 mock_data_manager_);
26332641
26342642 // i -> "い"
26352643 table_->AddRule("i", "\xe3\x81\x84", "");
26512659 InputUppercaseInAlphanumericModeWithShiftKeyModeSwitchIsKatakana) {
26522660 {
26532661 config_->set_shift_key_mode_switch(Config::KATAKANA_INPUT_MODE);
2654 table_->InitializeWithRequestAndConfig(*request_, *config_);
2662 table_->InitializeWithRequestAndConfig(*request_, *config_,
2663 mock_data_manager_);
26552664
26562665 // i -> "い"
26572666 table_->AddRule("i", "\xe3\x81\x84", "");
27162725 // 2. Type Back-space 6 times ("い")
27172726 // 3. Type "i" (should be "いい")
27182727
2719 table_->InitializeWithRequestAndConfig(*request_, *config_);
2728 table_->InitializeWithRequestAndConfig(*request_, *config_,
2729 mock_data_manager_);
27202730
27212731 // i -> "い"
27222732 table_->AddRule("i", "\xe3\x81\x84", "");
27632773 TEST_F(ComposerTest, InputModesChangeWhenCursorMoves) {
27642774 // The expectation of this test is the same as MS-IME's
27652775
2766 table_->InitializeWithRequestAndConfig(*request_, *config_);
2776 table_->InitializeWithRequestAndConfig(*request_, *config_,
2777 mock_data_manager_);
27672778
27682779 // i -> "い"
27692780 table_->AddRule("i", "\xe3\x81\x84", "");
31373148 commands::Request::TWELVE_KEYS_TO_HALFWIDTHASCII);
31383149 composer_->SetRequest(&request);
31393150 table_->InitializeWithRequestAndConfig(
3140 request, config::ConfigHandler::DefaultConfig());
3151 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
31413152 composer_->InsertCharacter("2");
31423153 EXPECT_EQ("a", GetPreedit(composer_.get()));
31433154 string result;
32083219
32093220 class MockTypingModel : public TypingModel {
32103221 public:
3211 MockTypingModel() : TypingModel(NULL, 0, NULL, 0, NULL) {}
3212 virtual ~MockTypingModel() {}
3213 virtual int GetCost(StringPiece key) const {
3222 MockTypingModel() : TypingModel(nullptr, 0, nullptr, 0, nullptr) {}
3223 ~MockTypingModel() override = default;
3224 int GetCost(StringPiece key) const override {
32143225 return 10;
32153226 }
32163227 };
32193230 // corrector inside composer.
32203231 class TypingCorrectionTest : public ::testing::Test {
32213232 protected:
3222 virtual void SetUp() {
3233 void SetUp() override {
32233234 config_.reset(new Config);
32243235 ConfigHandler::GetDefaultConfig(config_.get());
32253236 config_->set_use_typing_correction(true);
32323243
32333244 composer_.reset(new Composer(table_.get(), request_.get(), config_.get()));
32343245
3235 table_->typing_model_ = Singleton<MockTypingModel>::get();
3246 table_->typing_model_.reset(new MockTypingModel());
32363247 }
32373248
32383249 static bool IsTypingCorrectorClearedOrInvalidated(const Composer &composer) {
4444 ],
4545 'dependencies': [
4646 '../config/config.gyp:config_handler',
47 '../data_manager/testing/mock_data_manager.gyp:mock_data_manager',
4748 '../protocol/protocol.gyp:commands_proto',
4849 '../protocol/protocol.gyp:config_proto',
4950 '../session/session_base.gyp:request_test_util',
2727 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
30 """Converts a typing model file to C++ code.
30 """Converts a typing model file to binary image.
3131
3232 Usage:
3333 $ gen_typing_model.py model.tsv > output.h
34
35 Output file format:
36 +----------------------------------------------------+
37 | unique characters array size (4 bytes, uint32) |
38 +----------------------------------------------------+
39 | unique characters array (variable length, char[]) |
40 +----------------------------------------------------+
41 | padding (0 - 3 bytes) |
42 +----------------------------------------------------+
43 | cost array size (4 bytes, uint32) |
44 +----------------------------------------------------+
45 | cost array (variable length, uint8[]) |
46 +----------------------------------------------------+
47 | padding (0 - 3 bytes) |
48 +----------------------------------------------------+
49 | mapping table (variable length, int32[]) |
50 +----------------------------------------------------+
3451 """
3552
3653 __author__ = "noriyukit"
5269 parser.add_option('--input_path', dest='input_path',
5370 default='typing_model.tsv',
5471 help='Input file path')
55 parser.add_option('--variable_name', dest='variable_name',
56 default='typingmodel',
57 help='Suffix of created variable name.')
5872 parser.add_option('--output_path', dest='output_path',
5973 default='/tmp/typing_model.h',
6074 help='Output file path.')
144158 return result
145159
146160
147 def WriteResult(romaji_transition_cost, output_path, variable_name):
161 def WriteResult(romaji_transition_cost, output_path):
148162 unique_characters = GetUniqueCharacters(romaji_transition_cost.keys())
149163 mapping_table = GetMappingTable(romaji_transition_cost.values(),
150164 MAX_UINT8 + 1)
151165 value_list = GetValueTable(unique_characters, mapping_table,
152166 romaji_transition_cost)
153 quoted_unique_characters = ''.join(
154 [r'\x%X' % ord(c) for c in unique_characters])
155 with open(output_path, 'w') as out_file:
156 out_file.write('const size_t kKeyCharactersSize_%s = %d;\n' %
157 (variable_name, len(unique_characters)))
158 out_file.write('const char* kKeyCharacters_%s = "%s";\n' %
159 (variable_name, ''.join(quoted_unique_characters)))
160 out_file.write('const size_t kCostTableSize_%s = %d;\n' %
161 (variable_name, len(value_list)))
162 out_file.write('const uint8 kCostTable_%s[] = {\n' %
163 variable_name)
164 for value in value_list:
165 out_file.write('%d,\n' % value)
166 out_file.write('};\n')
167 out_file.write('const int32 kCostMappingTable_%s[] = {\n' %
168 variable_name)
169 for value in mapping_table:
170 out_file.write('%d,\n' % value)
171 out_file.write('};\n')
167 with open(output_path, 'wb') as f:
168 f.write(struct.pack('<I', len(unique_characters)))
169 f.write(''.join(unique_characters))
170 offset = 4 + len(unique_characters)
171
172 # Add padding to place value list size at 4-byte boundary.
173 if offset % 4:
174 padding_size = 4 - offset % 4
175 f.write('\x00' * padding_size)
176 offset += padding_size
177
178 f.write(struct.pack('<I', len(value_list)))
179 for v in value_list:
180 f.write(struct.pack('<B', v))
181 offset += 4 + len(value_list)
182
183 # Add padding to place mapping_table at 4-byte boundary.
184 if offset % 4:
185 padding_size = 4 - offset % 4
186 f.write('\x00' * padding_size)
187 offset += padding_size
188
189 for v in mapping_table:
190 f.write(struct.pack('<i', v))
172191
173192
174193 def main():
207226 # We use unsigned short to store cost value so range check is needed.
208227 romaji_transition_cost[ngram] = adjusted_cost
209228
210 WriteResult(romaji_transition_cost, options.output_path,
211 options.variable_name)
229 WriteResult(romaji_transition_cost, options.output_path)
212230
213231
214232 if __name__ == '__main__':
3636 #include "composer/table.h"
3737 #include "composer/type_corrected_query.h"
3838 #include "config/config_handler.h"
39 #include "data_manager/testing/mock_data_manager.h"
3940 #include "protocol/commands.pb.h"
4041 #include "testing/base/public/gunit.h"
4142
4243 namespace mozc {
4344 namespace composer {
44
45 namespace {
46 #include "composer/internal/typing_model_qwerty_mobile-hiragana.h"
47 }
4845
4946 using mozc::config::Config;
5047 using mozc::config::ConfigHandler;
287284
288285 class TypingCorrectorTest : public ::testing::Test {
289286 protected:
290 TypingCorrectorTest() :
291 qwerty_typing_model_(
292 kKeyCharacters_QwertyMobileHiragana,
293 kKeyCharactersSize_QwertyMobileHiragana,
294 kCostTable_QwertyMobileHiragana,
295 kCostTableSize_QwertyMobileHiragana,
296 kCostMappingTable_QwertyMobileHiragana) {
297 }
298
299 virtual void SetUp() {
287 TypingCorrectorTest() = default;
288
289 void SetUp() override {
300290 ConfigHandler::GetDefaultConfig(&config_);
301291 config_.set_use_typing_correction(true);
302292 commands::Request request;
303293 request.set_special_romanji_table(
304294 commands::Request::QWERTY_MOBILE_TO_HIRAGANA);
305 qwerty_table_.InitializeWithRequestAndConfig(request, config_);
306 qwerty_table_.typing_model_ = &qwerty_typing_model_;
295 qwerty_table_.InitializeWithRequestAndConfig(request, config_,
296 mock_data_manager_);
297 qwerty_table_.typing_model_ = TypingModel::CreateTypingModel(
298 commands::Request::QWERTY_MOBILE_TO_HIRAGANA,
299 mock_data_manager_);
307300 }
308301
309302 void InsertOneByOne(const char *keys, TypingCorrector *corrector) {
345338 }
346339 }
347340
341 const testing::MockDataManager mock_data_manager_;
348342 Config config_;
349343 Table qwerty_table_;
350 TypingModel qwerty_typing_model_;
351344 };
352345
353346 TEST_F(TypingCorrectorTest, TypingCorrection) {
3636
3737 namespace mozc {
3838 namespace composer {
39 namespace {
40
41 // These header files are automatically generated by gen_typing_model.py.
42 #include "composer/internal/typing_model_12keys-hiragana.h"
43 #include "composer/internal/typing_model_flick-hiragana.h"
44 #include "composer/internal/typing_model_godan-hiragana.h"
45 #include "composer/internal/typing_model_qwerty_mobile-hiragana.h"
46 #include "composer/internal/typing_model_toggle_flick-hiragana.h"
47
48 std::unique_ptr<TypingModel> g_typing_model_12keys_hiragana;
49 std::unique_ptr<TypingModel> g_typing_model_flick_hiragana;
50 std::unique_ptr<TypingModel> g_typing_model_godan_hiragana;
51 std::unique_ptr<TypingModel> g_typing_model_qwerty_mobile_hiragana;
52 std::unique_ptr<TypingModel> g_typing_model_toggle_flick_hiragana;
53
54 } // namespace
5539
5640 const uint8 TypingModel::kNoData = numeric_limits<uint8>::max();
5741 const int TypingModel::kInfinity = (2 << 20); // approximately equals 1e+6
7256 }
7357 }
7458
59 TypingModel::~TypingModel() = default;
60
7561 int TypingModel::GetCost(StringPiece key) const {
7662 size_t index = GetIndex(key);
7763 if (index >= cost_table_size_) {
9177 }
9278
9379 // static
94 const TypingModel *TypingModel::GetTypingModel(
95 const mozc::commands::Request::SpecialRomanjiTable &special_romanji_table) {
80 std::unique_ptr<const TypingModel> TypingModel::CreateTypingModel(
81 const mozc::commands::Request::SpecialRomanjiTable &special_romanji_table,
82 const DataManagerInterface &data_manager) {
83 const char *key = nullptr;
9684 switch (special_romanji_table) {
9785 case mozc::commands::Request::TWELVE_KEYS_TO_HIRAGANA:
98 if (!g_typing_model_12keys_hiragana.get()) {
99 g_typing_model_12keys_hiragana.reset(new TypingModel(
100 kKeyCharacters_12keysHiragana,
101 kKeyCharactersSize_12keysHiragana,
102 kCostTable_12keysHiragana,
103 kCostTableSize_12keysHiragana,
104 kCostMappingTable_12keysHiragana));
105 }
106 return g_typing_model_12keys_hiragana.get();
86 key = "typing_model_12keys-hiragana.tsv";
87 break;
10788 case mozc::commands::Request::FLICK_TO_HIRAGANA:
108 if (!g_typing_model_flick_hiragana.get()) {
109 g_typing_model_flick_hiragana.reset(new TypingModel(
110 kKeyCharacters_FlickHiragana,
111 kKeyCharactersSize_FlickHiragana,
112 kCostTable_FlickHiragana,
113 kCostTableSize_FlickHiragana,
114 kCostMappingTable_FlickHiragana));
115 }
116 return g_typing_model_flick_hiragana.get();
89 key = "typing_model_flick-hiragana.tsv";
90 break;
11791 case mozc::commands::Request::TOGGLE_FLICK_TO_HIRAGANA:
118 if (!g_typing_model_toggle_flick_hiragana.get()) {
119 g_typing_model_toggle_flick_hiragana.reset(new TypingModel(
120 kKeyCharacters_ToggleFlickHiragana,
121 kKeyCharactersSize_ToggleFlickHiragana,
122 kCostTable_ToggleFlickHiragana,
123 kCostTableSize_ToggleFlickHiragana,
124 kCostMappingTable_ToggleFlickHiragana));
125 }
126 return g_typing_model_toggle_flick_hiragana.get();
92 key = "typing_model_toggle_flick-hiragana.tsv";
93 break;
12794 case mozc::commands::Request::QWERTY_MOBILE_TO_HIRAGANA:
128 if (!g_typing_model_qwerty_mobile_hiragana.get()) {
129 g_typing_model_qwerty_mobile_hiragana.reset(new TypingModel(
130 kKeyCharacters_QwertyMobileHiragana,
131 kKeyCharactersSize_QwertyMobileHiragana,
132 kCostTable_QwertyMobileHiragana,
133 kCostTableSize_QwertyMobileHiragana,
134 kCostMappingTable_QwertyMobileHiragana));
135 }
136 return g_typing_model_qwerty_mobile_hiragana.get();
95 key = "typing_model_qwerty_mobile-hiragana.tsv";
96 break;
13797 case mozc::commands::Request::GODAN_TO_HIRAGANA:
138 if (!g_typing_model_godan_hiragana.get()) {
139 g_typing_model_godan_hiragana.reset(new TypingModel(
140 kKeyCharacters_GodanHiragana,
141 kKeyCharactersSize_GodanHiragana,
142 kCostTable_GodanHiragana,
143 kCostTableSize_GodanHiragana,
144 kCostMappingTable_GodanHiragana));
145 }
146 return g_typing_model_godan_hiragana.get();
98 key = "typing_model_godan-hiragana.tsv";
99 break;
147100 default:
148 return NULL;
101 return nullptr;
149102 }
103
104 const StringPiece data = data_manager.GetTypingModel(key);
105 if (data.empty()) {
106 return nullptr;
107 }
108 // Parse the binary image of typing model. See gen_typing_model.py for file
109 // format.
110 const uint32 characters_size =
111 *reinterpret_cast<const uint32*>(data.data());
112 const char *characters = data.data() + 4;
113
114 size_t offset = 4 + characters_size;
115 if (offset % 4 != 0) {
116 offset += 4 - offset % 4;
117 }
118 const uint32 cost_table_size =
119 *reinterpret_cast<const uint32*>(data.data() + offset);
120 const uint8 *cost_table =
121 reinterpret_cast<const uint8*>(data.data() + offset + 4);
122
123 offset += 4 + cost_table_size;
124 if (offset % 4 != 0) {
125 offset += 4 - offset % 4;
126 }
127 const int32 *mapping_table =
128 reinterpret_cast<const int32*>(data.data() + offset);
129
130 return std::unique_ptr<const TypingModel>(
131 new TypingModel(characters, characters_size, cost_table, cost_table_size,
132 mapping_table));
150133 }
151134
152135 } // namespace composer
3333
3434 #include "base/port.h"
3535 #include "base/string_piece.h"
36 #include "data_manager/data_manager_interface.h"
3637 #include "protocol/commands.pb.h"
3738 // for FRIEND_TEST()
3839 #include "testing/base/public/gunit_prod.h"
5253 const uint8 *cost_table, size_t cost_table_size,
5354 const int32 *mapping_table);
5455
55 virtual ~TypingModel() {}
56 virtual ~TypingModel();
5657
5758 // Gets cost value from key.
5859 // virtual for mocking.
5960 virtual int GetCost(StringPiece key) const;
6061
61 // Gets a TypingModel based on SpecialRomanjiTable.
62 // NULL if no corresponding model is available.
63 static const TypingModel *GetTypingModel(
64 const mozc::commands::Request::SpecialRomanjiTable
65 &special_romanji_table);
62 // Creates a TypingModel based on SpecialRomanjiTable.
63 // nullptr if no corresponding model is available.
64 static std::unique_ptr<const TypingModel> CreateTypingModel(
65 const mozc::commands::Request::SpecialRomanjiTable &special_romanji_table,
66 const DataManagerInterface& data_manager);
6667
6768 // No data means its const is infinity.
6869 static const int kInfinity;
3535 #include <memory>
3636 #include <sstream>
3737 #include <string>
38 #include <utility>
3839
3940 #include "base/config_file_stream.h"
4041 #include "base/file_stream.h"
101102 // ========================================
102103 Table::Table()
103104 : entries_(new EntryTrie),
104 case_sensitive_(false),
105 typing_model_(NULL) {}
105 case_sensitive_(false) {}
106106
107107 Table::~Table() {
108108 ResetEntrySet();
120120 static const char kSquareClose[] = "]";
121121 static const char kMiddleDot[] = "\xE3\x83\xBB"; // "・"
122122
123 bool Table::InitializeWithRequestAndConfig(const commands::Request &request,
124 const config::Config &config) {
123 bool Table::InitializeWithRequestAndConfig(
124 const commands::Request &request,
125 const config::Config &config,
126 const DataManagerInterface& data_manager) {
125127 case_sensitive_ = false;
126128 bool result = false;
127 typing_model_ = TypingModel::GetTypingModel(request.special_romanji_table());
129 typing_model_ = TypingModel::CreateTypingModel(
130 request.special_romanji_table(), data_manager);
128131 if (request.special_romanji_table()
129132 != mozc::commands::Request::DEFAULT_TABLE) {
130133 const char *table_file_name;
388391 }
389392
390393 const TypingModel* Table::typing_model() const {
391 return typing_model_;
394 return typing_model_.get();
392395 }
393396
394397 namespace {
606609 : custom_roman_table_fingerprint_(Hash::Fingerprint32("")) {
607610 }
608611
609 TableManager::~TableManager() {
610 for (map<uint32, const Table*>::iterator iterator = table_map_.begin();
611 iterator != table_map_.end();
612 ++iterator) {
613 delete iterator->second;
614 }
615 }
616
617 const Table *TableManager::GetTable(const mozc::commands::Request &request,
618 const mozc::config::Config &config) {
612 TableManager::~TableManager() = default;
613
614 const Table *TableManager::GetTable(
615 const mozc::commands::Request &request,
616 const mozc::config::Config &config,
617 const mozc::DataManagerInterface &data_manager) {
619618 // calculate the hash depending on the request and the config
620619 uint32 hash = request.special_romanji_table();
621620 hash = hash * (mozc::config::Config_PreeditMethod_PreeditMethod_MAX + 1)
639638 }
640639 }
641640
642 map<uint32, const Table*>::iterator iterator = table_map_.find(hash);
641 const auto iterator = table_map_.find(hash);
643642 if (iterator != table_map_.end()) {
644643 if (update_custom_roman_table) {
645644 // Delete the previous table to update the table.
646 delete iterator->second;
647645 table_map_.erase(iterator);
648646 } else {
649 return iterator->second;
647 return iterator->second.get();
650648 }
651649 }
652650
653651 std::unique_ptr<Table> table(new Table());
654 if (!table->InitializeWithRequestAndConfig(request, config)) {
655 return NULL;
656 }
657
658 Table* table_to_cache = table.release();
659 table_map_[hash] = table_to_cache;
660 return table_to_cache;
652 if (!table->InitializeWithRequestAndConfig(request, config, data_manager)) {
653 return nullptr;
654 }
655
656 const Table* ret = table.get();
657 table_map_[hash] = std::move(table);
658 return ret;
659 }
660
661 void TableManager::ClearCaches() {
662 table_map_.clear();
661663 }
662664
663665 } // namespace composer
3939
4040 #include "base/port.h"
4141 #include "base/trie.h"
42 #include "data_manager/data_manager_interface.h"
4243
4344 namespace mozc {
4445
99100 virtual ~Table();
100101
101102 bool InitializeWithRequestAndConfig(const commands::Request &request,
102 const config::Config &config);
103 const config::Config &config,
104 const DataManagerInterface &data_manager);
103105
104106
105107 // Return true if adding the input-pending pair makes a loop of
161163 // characters. The default value is false.
162164 bool case_sensitive_;
163165
164 // Typing model. NULL if no corresponding model is available.
165 const TypingModel* typing_model_;
166 // Typing model. nullptr if no corresponding model is available.
167 std::unique_ptr<const TypingModel> typing_model_;
166168
167169 DISALLOW_COPY_AND_ASSIGN(Table);
168170 };
174176 // Return Table for the request and the config
175177 // TableManager has ownership of the return value;
176178 const Table *GetTable(const commands::Request &request,
177 const config::Config &config);
179 const config::Config &config,
180 const DataManagerInterface &data_manager);
181
182 void ClearCaches();
178183
179184 private:
180185 // Table caches.
183188 // config::Config::PreeditMethod
184189 // config::Config::PunctuationMethod
185190 // config::Config::SymbolMethod
186 map<uint32, const Table*> table_map_;
191 map<uint32, std::unique_ptr<const Table>> table_map_;
187192 // Fingerprint for Config::custom_roman_table;
188193 uint32 custom_roman_table_fingerprint_;
189194 };
3333 #include "base/system_util.h"
3434 #include "composer/internal/composition_input.h"
3535 #include "config/config_handler.h"
36 #include "data_manager/testing/mock_data_manager.h"
3637 #include "protocol/commands.pb.h"
3738 #include "protocol/config.pb.h"
3839 #include "testing/base/public/gunit.h"
8889 return entry->input();
8990 }
9091
91 class TableTest : public testing::Test {
92 class TableTest : public ::testing::Test {
9293 protected:
93 TableTest() {}
94
95 virtual void SetUp() {
94 TableTest() = default;
95 ~TableTest() override = default;
96
97 void SetUp() override {
9698 config::ConfigHandler::GetDefaultConfig(&config_);
9799 }
98100
101 const testing::MockDataManager mock_data_manager_;
99102 config::Config config_;
100103
101104 private:
189192 config::Config config;
190193 config.set_punctuation_method(test_cases[i].method);
191194 Table table;
192 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config));
195 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config,
196 mock_data_manager_));
193197 const Entry *entry = table.LookUp(test_cases[i].input);
194198 EXPECT_TRUE(entry != NULL) << "Failed index = " << i;
195199 if (entry) {
232236 config::Config config;
233237 config.set_symbol_method(test_cases[i].method);
234238 Table table;
235 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config));
239 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config,
240 mock_data_manager_));
236241 const Entry *entry = table.LookUp(test_cases[i].input);
237242 EXPECT_TRUE(entry != NULL) << "Failed index = " << i;
238243 if (entry) {
247252 commands::Request request;
248253
249254 Table table;
250 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config_));
255 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config_,
256 mock_data_manager_));
251257
252258 const Entry *entry = table.LookUp("a");
253259 ASSERT_TRUE(entry != NULL);
259265 TEST_F(TableTest, KanaCombination) {
260266 Table table;
261267 commands::Request request;
262 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config_));
268 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config_,
269 mock_data_manager_));
263270 // "か゛"
264271 const Entry *entry = table.LookUp("\xE3\x81\x8B\xE3\x82\x9B");
265272 ASSERT_TRUE(entry != NULL);
398405
399406 Table table;
400407 commands::Request request;
401 table.InitializeWithRequestAndConfig(request, config_);
408 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
402409
403410 const Entry *entry = NULL;
404411 entry = table.LookUp("mozc");
502509 commands::Request request;
503510 {
504511 Table table;
505 table.InitializeWithRequestAndConfig(request, config_);
512 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
506513 EXPECT_FALSE(table.case_sensitive());
507514 }
508515 {
509516 Table table;
510 table.InitializeWithRequestAndConfig(request, config_);
517 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
511518 table.AddRule("", "", "");
512519 EXPECT_FALSE(table.case_sensitive());
513520 }
514521 {
515522 Table table;
516 table.InitializeWithRequestAndConfig(request, config_);
523 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
517524 table.AddRule("a", "", "");
518525 EXPECT_FALSE(table.case_sensitive());
519526 }
520527 {
521528 Table table;
522 table.InitializeWithRequestAndConfig(request, config_);
529 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
523530 table.AddRule("A", "", "");
524531 EXPECT_TRUE(table.case_sensitive());
525532 }
526533 {
527534 Table table;
528 table.InitializeWithRequestAndConfig(request, config_);
535 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
529536 table.AddRule("a{A}a", "", "");
530537 EXPECT_FALSE(table.case_sensitive());
531538 }
532539 {
533540 Table table;
534 table.InitializeWithRequestAndConfig(request, config_);
541 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
535542 table.AddRule("A{A}A", "", "");
536543 EXPECT_TRUE(table.case_sensitive());
537544 }
547554 // config::Config::OFF
548555 {
549556 config_.set_shift_key_mode_switch(config::Config::OFF);
550 table.InitializeWithRequestAndConfig(request, config_);
557 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
551558
552559 table.AddRule("a", "[a]", "");
553560 table.AddRule("A", "[A]", "");
587594 // config::Config::ASCII_INPUT_MODE
588595 {
589596 config_.set_shift_key_mode_switch(config::Config::ASCII_INPUT_MODE);
590 table.InitializeWithRequestAndConfig(request, config_);
597 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
591598
592599 table.AddRule("a", "[a]", "");
593600 table.AddRule("A", "[A]", "");
627634 // config::Config::KATAKANA_INPUT_MODE
628635 {
629636 config_.set_shift_key_mode_switch(config::Config::KATAKANA_INPUT_MODE);
630 table.InitializeWithRequestAndConfig(request, config_);
637 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
631638
632639 table.AddRule("a", "[a]", "");
633640 table.AddRule("A", "[A]", "");
692699 EXPECT_FALSE(table.case_sensitive())
693700 << "case-sensitive mode should be desabled by default.";
694701 // Load a custom config with case-sensitive custom roman table.
695 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config));
702 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config,
703 mock_data_manager_));
696704 EXPECT_TRUE(table.case_sensitive())
697705 << "Case sensitive roman table should enable case-sensitive mode.";
698706 // Explicitly disable case-sensitive mode.
705713 // Load a custom config with case-insensitive custom roman table.
706714 config::Config config(config::ConfigHandler::DefaultConfig());
707715 config.set_custom_roman_table(kCaseInsensitiveRomanTable);
708 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config));
716 ASSERT_TRUE(table.InitializeWithRequestAndConfig(request, config,
717 mock_data_manager_));
709718 EXPECT_FALSE(table.case_sensitive())
710719 << "Case insensitive roman table should disable case-sensitive mode.";
711720 // Explicitly enable case-sensitive mode.
724733 request.set_special_romanji_table(
725734 mozc::commands::Request::TWELVE_KEYS_TO_HIRAGANA);
726735 mozc::composer::Table table;
727 table.InitializeWithRequestAndConfig(request, config_);
736 table.InitializeWithRequestAndConfig(request, config_,
737 mock_data_manager_);
728738 {
729739 const mozc::composer::Entry *entry = NULL;
730740 size_t key_length = 0;
759769 request.set_special_romanji_table(
760770 mozc::commands::Request::TWELVE_KEYS_TO_HALFWIDTHASCII);
761771 Table table;
762 table.InitializeWithRequestAndConfig(request, config_);
772 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
763773 const mozc::composer::Entry *entry = NULL;
764774 size_t key_length = 0;
765775 bool fixed = false;
772782 request.set_special_romanji_table(
773783 mozc::commands::Request::GODAN_TO_HIRAGANA);
774784 Table table;
775 table.InitializeWithRequestAndConfig(request, config_);
785 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
776786 {
777787 const mozc::composer::Entry *entry = NULL;
778788 size_t key_length = 0;
790800 request.set_special_romanji_table(
791801 mozc::commands::Request::FLICK_TO_HIRAGANA);
792802 Table table;
793 table.InitializeWithRequestAndConfig(request, config_);
803 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
794804
795805 size_t key_length = 0;
796806 bool fixed = false;
805815 request.set_special_romanji_table(
806816 mozc::commands::Request::NOTOUCH_TO_HIRAGANA);
807817 Table table;
808 table.InitializeWithRequestAndConfig(request, config_);
818 table.InitializeWithRequestAndConfig(request, config_, mock_data_manager_);
809819
810820 size_t key_length = 0;
811821 bool fixed = false;
10601070 config.set_preedit_method(preedit_method[preedit]);
10611071 config.set_punctuation_method(punctuation_method[punctuation]);
10621072 config.set_symbol_method(symbol_method[symbol]);
1063 const Table *table = table_manager.GetTable(request, config);
1073 const Table *table = table_manager.GetTable(request, config,
1074 mock_data_manager_);
10641075 EXPECT_TRUE(table != NULL);
1065 EXPECT_TRUE(table_manager.GetTable(request, config) == table);
1076 EXPECT_TRUE(table_manager.GetTable(request, config,
1077 mock_data_manager_) == table);
10661078 EXPECT_TRUE(table_set.find(table) == table_set.end());
10671079 table_set.insert(table);
10681080 }
10821094 config.set_punctuation_method(Config::KUTEN_TOUTEN);
10831095 config.set_symbol_method(Config::CORNER_BRACKET_MIDDLE_DOT);
10841096 config.set_custom_roman_table(kRule);
1085 const Table *table = table_manager.GetTable(request, config);
1097 const Table *table = table_manager.GetTable(request, config,
1098 mock_data_manager_);
10861099 EXPECT_TRUE(table != NULL);
1087 EXPECT_TRUE(table_manager.GetTable(request, config) == table);
1100 EXPECT_TRUE(table_manager.GetTable(request, config,
1101 mock_data_manager_) == table);
10881102 EXPECT_TRUE(NULL != table->LookUp("a"));
10891103 EXPECT_TRUE(NULL == table->LookUp("kk"));
10901104
10921106 "a\t[A]\n" // 2 entry rule
10931107 "kk\t[X]\tk\n"; // 3 entry rule
10941108 config.set_custom_roman_table(kRule2);
1095 const Table *table2 = table_manager.GetTable(request, config);
1109 const Table *table2 = table_manager.GetTable(request, config,
1110 mock_data_manager_);
10961111 EXPECT_TRUE(table2 != NULL);
1097 EXPECT_TRUE(table_manager.GetTable(request, config) == table2);
1112 EXPECT_TRUE(table_manager.GetTable(request, config,
1113 mock_data_manager_) == table2);
10981114 EXPECT_TRUE(NULL != table2->LookUp("a"));
10991115 EXPECT_TRUE(NULL != table2->LookUp("kk"));
11001116 }
00 MAJOR=2
11 MINOR=18
2 BUILD=2577
2 BUILD=2578
33 REVISION=102
44 # CAUTION: NACL_DICTIONARY_VERSION is going to be migrated to ENGINE_VERSION.
55 # NACL_DICTIONARY_VERSION is the target version of the system dictionary to be
3434
3535 namespace mozc {
3636 namespace chromeos {
37 namespace {
3738
38 namespace {
3939 #include "data_manager/chromeos/segmenter_inl.h"
40
4041 } // namespace
4142
4243 class ChromeOsDataManagerTest : public DataManagerTestBase {
6465 "dictionary09.txt"}),
6566 mozc::testing::GetSourceFilesInDirOrDie(
6667 {"data", "dictionary_chromeos"},
67 {"suggestion_filter.txt"})) {}
68 {"suggestion_filter.txt"}),
69 // ChromeOS doesn't include typing correction models.
70 {}) {}
6871 };
6972
7073 TEST_F(ChromeOsDataManagerTest, AllTests) {
2828
2929 #include "data_manager/data_manager.h"
3030
31 #include <algorithm>
3132 #include <ostream>
3233
3334 #include "base/logging.h"
3435 #include "base/serialized_string_array.h"
36 #include "base/stl_util.h"
3537 #include "base/util.h"
3638 #include "base/version.h"
3739 #include "data_manager/dataset_reader.h"
340342 }
341343 }
342344
345 for (const auto &kv : reader.name_to_data_map()) {
346 if (!Util::StartsWith(kv.first, "typing_model")) {
347 continue;
348 }
349 typing_model_data_.push_back(kv);
350 }
351 std::sort(typing_model_data_.begin(), typing_model_data_.end(),
352 OrderBy<FirstKey, Less>());
353
343354 if (!reader.Get("version", &data_version_)) {
344355 LOG(ERROR) << "Cannot find data version";
345356 return Status::DATA_MISSING;
538549 }
539550 #endif // NO_USAGE_REWRITER
540551
552 StringPiece DataManager::GetTypingModel(const string &name) const {
553 const auto iter = std::lower_bound(
554 typing_model_data_.begin(), typing_model_data_.end(), name,
555 [](const pair<string, StringPiece> &elem, const string &key) {
556 return elem.first < key;
557 });
558 if (iter == typing_model_data_.end() || iter->first != name) {
559 return StringPiece();
560 }
561 return iter->second;
562 }
563
541564 StringPiece DataManager::GetDataVersion() const {
542565 return data_version_;
543566 }
121121 'gen_separate_single_kanji_rewriter_data_for_<(dataset_tag)#host',
122122 'gen_separate_zero_query_data_for_<(dataset_tag)#host',
123123 'gen_separate_version_data_for_<(dataset_tag)#host',
124 'gen_typing_model_for_<(dataset_tag)#host',
124125 ],
125126 'actions': [
126127 {
161162 'single_kanji_variant_string': '<(gen_out_dir)/single_kanji_variant_string.data',
162163 'single_kanji_noun_prefix_token': '<(gen_out_dir)/single_kanji_noun_prefix_token.data',
163164 'single_kanji_noun_prefix_string': '<(gen_out_dir)/single_kanji_noun_prefix_string.data',
165 'typing_model_qwerty_mobile-hiragana': '<(gen_out_dir)/typing_model_qwerty_mobile-hiragana.data',
166 'typing_model_12keys-hiragana': '<(gen_out_dir)/typing_model_12keys-hiragana.data',
167 'typing_model_flick-hiragana': '<(gen_out_dir)/typing_model_flick-hiragana.data',
168 'typing_model_godan-hiragana': '<(gen_out_dir)/typing_model_godan-hiragana.data',
169 'typing_model_toggle_flick-hiragana': '<(gen_out_dir)/typing_model_toggle_flick-hiragana.data',
164170 'zero_query_token_array': '<(gen_out_dir)/zero_query_token.data',
165171 'zero_query_string_array': '<(gen_out_dir)/zero_query_string.data',
166172 'zero_query_number_token_array': '<(gen_out_dir)/zero_query_number_token.data',
202208 '<(single_kanji_variant_string)',
203209 '<(single_kanji_noun_prefix_token)',
204210 '<(single_kanji_noun_prefix_string)',
211 '<(typing_model_qwerty_mobile-hiragana)',
212 '<(typing_model_12keys-hiragana)',
213 '<(typing_model_flick-hiragana)',
214 '<(typing_model_godan-hiragana)',
215 '<(typing_model_toggle_flick-hiragana)',
205216 '<(zero_query_token_array)',
206217 '<(zero_query_string_array)',
207218 '<(zero_query_number_token_array)',
253264 'zero_query_string_array:32:<(gen_out_dir)/zero_query_string.data',
254265 'zero_query_number_token_array:32:<(gen_out_dir)/zero_query_number_token.data',
255266 'zero_query_number_string_array:32:<(gen_out_dir)/zero_query_number_string.data',
267 # TODO(noriyukit): These typing models are not necessary for desktop.
268 'typing_model_qwerty_mobile-hiragana.tsv:32:<(gen_out_dir)/typing_model_qwerty_mobile-hiragana.data',
269 'typing_model_12keys-hiragana.tsv:32:<(gen_out_dir)/typing_model_12keys-hiragana.data',
270 'typing_model_flick-hiragana.tsv:32:<(gen_out_dir)/typing_model_flick-hiragana.data',
271 'typing_model_godan-hiragana.tsv:32:<(gen_out_dir)/typing_model_godan-hiragana.data',
272 'typing_model_toggle_flick-hiragana.tsv:32:<(gen_out_dir)/typing_model_toggle_flick-hiragana.data',
256273 'version:32:<(gen_out_dir)/version.data',
257274 ],
258275 'conditions': [
963980 ],
964981 },
965982 {
983 'target_name': 'gen_typing_model_for_<(dataset_tag)',
984 'type': 'none',
985 'toolsets': ['host'],
986 'actions': [
987 {
988 'action_name': 'gen_qwerty_mobile-hiragana_typing_model_<(dataset_tag)',
989 'variables': {
990 'input_files': [
991 '<(mozc_dir)/data/typing/typing_model_qwerty_mobile-hiragana.tsv',
992 ],
993 },
994 'inputs': [
995 '<(mozc_dir)/composer/internal/gen_typing_model.py',
996 '<@(input_files)',
997 ],
998 'outputs': [
999 '<(gen_out_dir)/typing_model_qwerty_mobile-hiragana.data',
1000 ],
1001 'action': [
1002 'python',
1003 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1004 '--input_path',
1005 '<@(input_files)',
1006 '--output_path',
1007 '<@(_outputs)',
1008 ],
1009 },
1010 {
1011 'action_name': 'gen_12keys-hiragana_typing_model_<(dataset_tag)',
1012 'variables': {
1013 'input_files': [
1014 '<(mozc_dir)/data/typing/typing_model_12keys-hiragana.tsv',
1015 ],
1016 },
1017 'inputs': [
1018 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1019 '<@(input_files)',
1020 ],
1021 'outputs': [
1022 '<(gen_out_dir)/typing_model_12keys-hiragana.data',
1023 ],
1024 'action': [
1025 'python',
1026 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1027 '--input_path',
1028 '<@(input_files)',
1029 '--output_path',
1030 '<@(_outputs)',
1031 ],
1032 },
1033 {
1034 'action_name': 'gen_flick-hiragana_typing_model_<(dataset_tag)',
1035 'variables': {
1036 'input_files': [
1037 '<(mozc_dir)/data/typing/typing_model_flick-hiragana.tsv',
1038 ],
1039 },
1040 'inputs': [
1041 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1042 '<@(input_files)',
1043 ],
1044 'outputs': [
1045 '<(gen_out_dir)/typing_model_flick-hiragana.data',
1046 ],
1047 'action': [
1048 'python',
1049 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1050 '--input_path',
1051 '<@(input_files)',
1052 '--output_path',
1053 '<@(_outputs)',
1054 ],
1055 },
1056 {
1057 'action_name': 'gen_godan-hiragana_typing_model_<(dataset_tag)',
1058 'variables': {
1059 'input_files': [
1060 '<(mozc_dir)/data/typing/typing_model_godan-hiragana.tsv',
1061 ],
1062 },
1063 'inputs': [
1064 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1065 '<@(input_files)',
1066 ],
1067 'outputs': [
1068 '<(gen_out_dir)/typing_model_godan-hiragana.data',
1069 ],
1070 'action': [
1071 'python',
1072 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1073 '--input_path',
1074 '<@(input_files)',
1075 '--output_path',
1076 '<@(_outputs)',
1077 ],
1078 },
1079 {
1080 'action_name': 'gen_toggle_flick-hiragana_typing_model_<(dataset_tag)',
1081 'variables': {
1082 'input_files': [
1083 '<(mozc_dir)/data/typing/typing_model_toggle_flick-hiragana.tsv',
1084 ],
1085 },
1086 'inputs': [
1087 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1088 '<@(input_files)',
1089 ],
1090 'outputs': [
1091 '<(gen_out_dir)/typing_model_toggle_flick-hiragana.data',
1092 ],
1093 'action': [
1094 'python',
1095 '<(mozc_dir)/composer/internal/gen_typing_model.py',
1096 '--input_path',
1097 '<@(input_files)',
1098 '--output_path',
1099 '<@(_outputs)',
1100 ],
1101 },
1102 ],
1103 },
1104 {
9661105 'target_name': 'gen_separate_version_data_for_<(dataset_tag)',
9671106 'type': 'none',
9681107 'toolsets': ['host'],
3131
3232 #include <iosfwd>
3333 #include <string>
34 #include <utility>
35 #include <vector>
3436
3537 #include "base/mmap.h"
3638 #include "base/port.h"
133135 StringPiece *string_array_data) const override;
134136 #endif // NO_USAGE_REWRITER
135137
138 StringPiece GetTypingModel(const string &name) const override;
136139 StringPiece GetDataVersion() const override;
137140
138141 private:
183186 StringPiece usage_conjugation_index_data_;
184187 StringPiece usage_items_data_;
185188 StringPiece usage_string_array_data_;
189 vector<pair<string, StringPiece>> typing_model_data_;
186190 StringPiece data_version_;
187191
188192 DISALLOW_COPY_AND_ASSIGN(DataManager);
2828
2929 #ifndef MOZC_DATA_MANAGER_DATA_MANAGER_INTERFACE_H_
3030 #define MOZC_DATA_MANAGER_DATA_MANAGER_INTERFACE_H_
31
32 #include <string>
3133
3234 #include "base/port.h"
3335 #include "base/string_piece.h"
128130 StringPiece *zero_query_number_token_array_data,
129131 StringPiece *zero_query_number_string_array_data) const = 0;
130132
133 // Gets the typing model binary data for the specified name.
134 virtual StringPiece GetTypingModel(const string &name) const = 0;
135
131136 // Gets the data version string.
132137 virtual StringPiece GetDataVersion() const = 0;
133138
6060 const string &connection_txt_file,
6161 const int expected_resolution,
6262 const vector<string> &dictionary_files,
63 const vector<string> &suggestion_filter_files)
63 const vector<string> &suggestion_filter_files,
64 const vector<pair<string, string>> &typing_model_files)
6465 : data_manager_(data_manager),
6566 lsize_(lsize),
6667 rsize_(rsize),
6869 connection_txt_file_(connection_txt_file),
6970 expected_resolution_(expected_resolution),
7071 dictionary_files_(dictionary_files),
71 suggestion_filter_files_(suggestion_filter_files) {}
72
73 DataManagerTestBase::~DataManagerTestBase() {}
72 suggestion_filter_files_(suggestion_filter_files),
73 typing_model_files_(typing_model_files) {}
74
75 DataManagerTestBase::~DataManagerTestBase() = default;
7476
7577 void DataManagerTestBase::SegmenterTest_SameAsInternal() {
7678 // This test verifies that a segmenter created by MockDataManager provides
273275 }
274276 }
275277
278 void DataManagerTestBase::TypingModelTest() {
279 // Check if typing models are included in the data set.
280 for (const auto &key_and_fname : typing_model_files_) {
281 InputFileStream ifs(key_and_fname.second.c_str(),
282 ios_base::in | ios_base::binary);
283 EXPECT_EQ(ifs.Read(), data_manager_->GetTypingModel(key_and_fname.first));
284 }
285 }
286
276287 void DataManagerTestBase::RunAllTests() {
277288 ConnectorTest_RandomValueCheck();
278289 SegmenterTest_LNodeTest();
282293 SegmenterTest_SameAsInternal();
283294 SuggestionFilterTest_IsBadSuggestion();
284295 CounterSuffixTest_ValidateTest();
296 TypingModelTest();
285297 }
286298
287299 } // namespace mozc
3131
3232 #include <memory>
3333 #include <string>
34 #include <utility>
3435 #include <vector>
3536
3637 #include "base/port.h"
5354 // The following two are used in connector test.
5455 const string &connection_txt_file,
5556 const int expected_resolution,
56 // The following two are used in suggestion filter test
57 // The following two are used in suggestion filter test.
5758 const vector<string> &dictionary_files,
58 const vector<string> &suggestion_filter_files);
59 virtual ~DataManagerTestBase();
59 const vector<string> &suggestion_filter_files,
60 // The following is used in typing model test.
61 const vector<pair<string, string>> &typing_model_files);
62 ~DataManagerTestBase() override;
6063
6164 void RunAllTests();
6265
6972 void SegmenterTest_SameAsInternal();
7073 void SuggestionFilterTest_IsBadSuggestion();
7174 void CounterSuffixTest_ValidateTest();
75 void TypingModelTest();
7276
7377 std::unique_ptr<DataManagerInterface> data_manager_;
7478 const uint16 lsize_;
7882 const int expected_resolution_;
7983 const vector<string> dictionary_files_;
8084 const vector<string> suggestion_filter_files_;
85 const vector<pair<string, string>> typing_model_files_;
8186
8287 DISALLOW_COPY_AND_ASSIGN(DataManagerTestBase);
8388 };
5555 // Verifies the checksum of binary image.
5656 static bool VerifyChecksum(StringPiece memblock);
5757
58 const map<string, StringPiece> &name_to_data_map() const {
59 return name_to_data_map_;
60 }
61
5862 private:
5963 // The value points to a block of the specified |memblock|.
6064 map<string, StringPiece> name_to_data_map_;
3434
3535 namespace mozc {
3636 namespace oss {
37 namespace {
3738
38 namespace {
3939 #include "data_manager/oss/segmenter_inl.h"
40
41 pair<string, string> GetTypingModelEntry(const string &fname) {
42 return pair<string, string>(
43 fname,
44 mozc::testing::GetSourceFileOrDie(
45 {"data_manager", "oss", fname + ".data"}));
46 }
47
4048 } // namespace
4149
4250 class OssDataManagerTest : public DataManagerTestBase {
6371 "dictionary08.txt",
6472 "dictionary09.txt"}),
6573 mozc::testing::GetSourceFilesInDirOrDie(
66 {"data", "dictionary_oss"}, {"suggestion_filter.txt"})) {}
74 {"data", "dictionary_oss"}, {"suggestion_filter.txt"}),
75 {
76 GetTypingModelEntry("typing_model_12keys-hiragana.tsv"),
77 GetTypingModelEntry("typing_model_flick-hiragana.tsv"),
78 GetTypingModelEntry("typing_model_godan-hiragana.tsv"),
79 GetTypingModelEntry("typing_model_qwerty_mobile-hiragana.tsv"),
80 GetTypingModelEntry("typing_model_toggle_flick-hiragana.tsv"),
81 }) {}
6782 };
6883
6984 TEST_F(OssDataManagerTest, AllTests) {
3434
3535 namespace mozc {
3636 namespace testing {
37 namespace {
3738
38 namespace {
3939 #include "data_manager/testing/segmenter_inl.h"
40
41 pair<string, string> GetTypingModelEntry(const string &fname) {
42 return pair<string, string>(
43 fname,
44 mozc::testing::GetSourceFileOrDie(
45 {"data_manager", "testing", fname + ".data"}));
46 }
47
4048 } // namespace
4149
4250 class MockDataManagerTest : public DataManagerTestBase {
5563 {"dictionary.txt"}),
5664 mozc::testing::GetSourceFilesInDirOrDie(
5765 {"data", "test", "dictionary"},
58 {"suggestion_filter.txt"})) {}
66 {"suggestion_filter.txt"}),
67 {
68 GetTypingModelEntry("typing_model_12keys-hiragana.tsv"),
69 GetTypingModelEntry("typing_model_flick-hiragana.tsv"),
70 GetTypingModelEntry("typing_model_godan-hiragana.tsv"),
71 GetTypingModelEntry("typing_model_qwerty_mobile-hiragana.tsv"),
72 GetTypingModelEntry("typing_model_toggle_flick-hiragana.tsv"),
73 }) {}
5974 };
6075
6176 TEST_F(MockDataManagerTest, AllTests) {
106106 return data_manager_->GetDataVersion();
107107 }
108108
109 const DataManagerInterface *GetDataManager() const override {
110 return data_manager_.get();
111 }
112
109113 private:
110114 // Initializes the object by the given data manager and predictor factory
111115 // function. Predictor factory is used to select DefaultPredictor and
3131
3232 #include "base/port.h"
3333 #include "base/string_piece.h"
34 #include "data_manager/data_manager_interface.h"
3435 #include "dictionary/suppression_dictionary.h"
3536
3637 namespace mozc {
4546 // well as Kana-Kanji converter/predictor, etc.
4647 class EngineInterface {
4748 public:
48 virtual ~EngineInterface() {}
49 virtual ~EngineInterface() = default;
4950
5051 // Returns a reference to a converter. The returned instance is managed by the
5152 // engine class and should not be deleted by callers.
6869 // Gets the version of underlying data set.
6970 virtual StringPiece GetDataVersion() const = 0;
7071
72 // Gets the data manager.
73 virtual const DataManagerInterface *GetDataManager() const = 0;
74
7175 protected:
7276 EngineInterface() {}
7377
4343 bool Reload() override { return true; }
4444 UserDataManagerInterface *GetUserDataManager() override { return nullptr; }
4545 StringPiece GetDataVersion() const override { return StringPiece(); }
46 const DataManagerInterface *GetDataManager() const { return nullptr; }
4647 };
4748
4849 } // namespace mozc
5858 return "mock converter engine";
5959 }
6060
61 const DataManagerInterface *GetDataManager() const override {
62 return nullptr;
63 }
64
6165 void SetUserDataManager(UserDataManagerMock *manager);
6266 ConverterMock* mutable_converter_mock();
6367
3939 #include "base/logging.h"
4040 #include "base/port.h"
4141 #include "base/serialized_string_array.h"
42 #include "base/singleton.h"
4342 #include "base/system_util.h"
4443 #include "base/util.h"
4544 #include "composer/composer.h"
370369
371370 class MockTypingModel : public mozc::composer::TypingModel {
372371 public:
373 MockTypingModel() : TypingModel(NULL, 0, NULL, 0, NULL) {}
374 ~MockTypingModel() {}
375 int GetCost(StringPiece key) const {
372 MockTypingModel() : TypingModel(nullptr, 0, nullptr, 0, nullptr) {}
373 ~MockTypingModel() override = default;
374 int GetCost(StringPiece key) const override {
376375 return 10;
377376 }
378377 };
821820 data_and_predictor->dictionary_predictor();
822821
823822 table_->LoadFromFile("system://qwerty_mobile-hiragana.tsv");
824 table_->typing_model_ = Singleton<MockTypingModel>::get();
823 table_->typing_model_.reset(new MockTypingModel());
825824 InsertInputSequenceForProbableKeyEvent(
826825 key, corrected_key_codes, composer_.get());
827826
3737 #include "composer/table.h"
3838 #include "config/config_handler.h"
3939 #include "converter/segments.h"
40 #include "request/conversion_request.h"
4140 #include "data_manager/testing/mock_data_manager.h"
4241 #include "dictionary/dictionary_mock.h"
4342 #include "dictionary/pos_matcher.h"
4443 #include "protocol/commands.pb.h"
4544 #include "protocol/config.pb.h"
45 #include "request/conversion_request.h"
4646 #include "testing/base/public/gunit.h"
4747 #include "testing/base/public/mozctest.h"
4848 #include "usage_stats/usage_stats.h"
8888 dictionary_mock_.get());
8989 }
9090
91 bool RewriteWithLanguageAwareInput(const LanguageAwareRewriter *rewriter,
92 const string &key, string *composition,
93 Segments *segments) {
94 commands::Request client_request;
95 client_request.set_language_aware_input(
96 commands::Request::LANGUAGE_AWARE_SUGGESTION);
97
98 composer::Table table;
99 config::Config default_config;
100 table.InitializeWithRequestAndConfig(client_request, default_config,
101 data_manager_);
102
103 composer::Composer composer(&table, &client_request, &default_config);
104 InsertASCIISequence(key, &composer);
105 composer.GetStringForPreedit(composition);
106
107 // Perform the rewrite command.
108 segments->set_request_type(Segments::SUGGESTION);
109 if (segments->conversion_segments_size() == 0) {
110 segments->add_segment();
111 }
112 Segment *segment = segments->mutable_conversion_segment(0);
113 segment->set_key(*composition);
114 ConversionRequest request(&composer, &client_request, &default_config);
115
116 return rewriter->Rewrite(request, segments);
117 }
118
91119 unique_ptr<DictionaryMock> dictionary_mock_;
92120 usage_stats::scoped_usage_stats_enabler usage_stats_enabler_;
93121
122 const testing::MockDataManager data_manager_;
123
94124 private:
95125 const testing::ScopedTmpUserProfileDirectory tmp_profile_dir_;
96 const testing::MockDataManager data_manager_;
97126 };
98127
99128 namespace {
100 bool RewriteWithLanguageAwareInput(const LanguageAwareRewriter *rewriter,
101 const string &key,
102 string *composition,
103 Segments *segments) {
104 commands::Request client_request;
105 client_request.set_language_aware_input(
106 commands::Request::LANGUAGE_AWARE_SUGGESTION);
107
108 composer::Table table;
109 config::Config default_config;
110 table.InitializeWithRequestAndConfig(client_request, default_config);
111
112 composer::Composer composer(&table, &client_request, &default_config);
113 InsertASCIISequence(key, &composer);
114 composer.GetStringForPreedit(composition);
115
116 // Perform the rewrite command.
117 segments->set_request_type(Segments::SUGGESTION);
118 if (segments->conversion_segments_size() == 0) {
119 segments->add_segment();
120 }
121 Segment *segment = segments->mutable_conversion_segment(0);
122 segment->set_key(*composition);
123 ConversionRequest request(&composer, &client_request, &default_config);
124
125 return rewriter->Rewrite(request, segments);
126 }
127129
128130 void PushFrontCandidate(const string &data, Segment *segment) {
129131 Segment::Candidate *candidate = segment->push_front_candidate();
304306
305307 composer::Table table;
306308 config::Config default_config;
307 table.InitializeWithRequestAndConfig(client_request, default_config);
309 table.InitializeWithRequestAndConfig(client_request, default_config,
310 data_manager_);
308311
309312 composer::Composer composer(&table, &client_request, &default_config);
310313 InsertASCIISequence("python", &composer);
3939 #include "composer/table.h"
4040 #include "config/config_handler.h"
4141 #include "converter/segments.h"
42 #include "request/conversion_request.h"
4342 #include "data_manager/testing/mock_data_manager.h"
4443 #include "dictionary/pos_matcher.h"
4544 #include "protocol/commands.pb.h"
4645 #include "protocol/config.pb.h"
46 #include "request/conversion_request.h"
4747 #include "testing/base/public/gunit.h"
4848 #include "testing/base/public/mozctest.h"
4949 #include "transliteration/transliteration.h"
100100
101101 usage_stats::scoped_usage_stats_enabler usage_stats_enabler_;
102102
103 const testing::MockDataManager mock_data_manager_;
104
103105 private:
104106 const testing::ScopedTmpUserProfileDirectory tmp_profile_dir_;
105 const testing::MockDataManager mock_data_manager_;
106107 const commands::Request default_request_;
107108 config::Config default_config_;
108109 };
163164 CreateTransliterationRewriter());
164165
165166 composer::Table table;
166 table.InitializeWithRequestAndConfig(default_request(), default_config());
167 table.InitializeWithRequestAndConfig(default_request(), default_config(),
168 mock_data_manager_);
167169 composer::Composer composer(&table, &default_request(), &default_config());
168170 SetAkann(&composer);
169171
224226 CreateTransliterationRewriter());
225227
226228 composer::Table table;
227 table.InitializeWithRequestAndConfig(default_request(), default_config());
229 table.InitializeWithRequestAndConfig(default_request(), default_config(),
230 mock_data_manager_);
228231 composer::Composer composer(&table, &default_request(), &default_config());
229232 InsertASCIISequence("ssh", &composer);
230233
267270 CreateTransliterationRewriter());
268271
269272 composer::Table table;
270 table.InitializeWithRequestAndConfig(default_request(), default_config());
273 table.InitializeWithRequestAndConfig(default_request(), default_config(),
274 mock_data_manager_);
271275 composer::Composer composer(&table, &default_request(), &default_config());
272276
273277 // Set kamabokoinbou to composer.
320324 CreateTransliterationRewriter());
321325
322326 composer::Table table;
323 table.InitializeWithRequestAndConfig(default_request(), default_config());
327 table.InitializeWithRequestAndConfig(default_request(), default_config(),
328 mock_data_manager_);
324329 composer::Composer composer(&table, &default_request(), &default_config());
325330
326331 // Set kan to composer.
383388 CreateTransliterationRewriter());
384389
385390 composer::Table table;
386 table.InitializeWithRequestAndConfig(default_request(), default_config());
391 table.InitializeWithRequestAndConfig(default_request(), default_config(),
392 mock_data_manager_);
387393 composer::Composer composer(&table, &default_request(), &default_config());
388394 SetAkann(&composer);
389395
542548 CreateTransliterationRewriter());
543549
544550 composer::Table table;
545 table.InitializeWithRequestAndConfig(default_request(), default_config());
551 table.InitializeWithRequestAndConfig(default_request(), default_config(),
552 mock_data_manager_);
546553 composer::Composer composer(&table, &default_request(), &default_config());
547554 InsertASCIISequence("a", &composer);
548555
569576 CreateTransliterationRewriter());
570577
571578 composer::Table table;
572 table.InitializeWithRequestAndConfig(default_request(), default_config());
579 table.InitializeWithRequestAndConfig(default_request(), default_config(),
580 mock_data_manager_);
573581
574582 Segments segments;
575583 Segment *segment = segments.add_segment();
609617 commands::Request::TWELVE_KEYS_TO_HIRAGANA);
610618
611619 composer::Table table;
612 table.InitializeWithRequestAndConfig(request, default_config());
620 table.InitializeWithRequestAndConfig(request, default_config(),
621 mock_data_manager_);
613622 composer::Composer composer(&table, &request, &default_config());
614623
615624 {
679688 commands::Request::TWELVE_KEYS_TO_HIRAGANA);
680689
681690 composer::Table table;
682 table.InitializeWithRequestAndConfig(request, default_config());
691 table.InitializeWithRequestAndConfig(request, default_config(),
692 mock_data_manager_);
683693 composer::Composer composer(&table, &request, &default_config());
684694
685695 {
750760 commands::Request::TOGGLE_FLICK_TO_HIRAGANA);
751761
752762 composer::Table table;
753 table.InitializeWithRequestAndConfig(request, default_config());
763 table.InitializeWithRequestAndConfig(request, default_config(),
764 mock_data_manager_);
754765 composer::Composer composer(&table, &request, &default_config());
755766
756767 {
822833 // "し";
823834 const string kShi = "\xE3\x81\x97";
824835 composer::Table table;
825 table.InitializeWithRequestAndConfig(client_request, default_config());
836 table.InitializeWithRequestAndConfig(client_request, default_config(),
837 mock_data_manager_);
826838
827839 {
828840 composer::Composer composer(&table, &client_request, &default_config());
871883 request.set_special_romanji_table(commands::Request::GODAN_TO_HIRAGANA);
872884
873885 composer::Table table;
874 table.InitializeWithRequestAndConfig(request, default_config());
886 table.InitializeWithRequestAndConfig(request, default_config(),
887 mock_data_manager_);
875888 composer::Composer composer(&table, &request, &default_config());
876889 {
877890 InsertASCIISequence("<'de", &composer);
939952 request.set_special_romanji_table(commands::Request::GODAN_TO_HIRAGANA);
940953
941954 composer::Table table;
942 table.InitializeWithRequestAndConfig(request, default_config());
955 table.InitializeWithRequestAndConfig(request, default_config(),
956 mock_data_manager_);
943957
944958 // Expected t13n of Godan keyboard.
945959 vector<const char *> keycode_to_t13n_map(
10161030 const string kXtsu = "\xE3\x81\xA3";
10171031
10181032 composer::Table table;
1019 table.InitializeWithRequestAndConfig(client_request, default_config());
1033 table.InitializeWithRequestAndConfig(client_request, default_config(),
1034 mock_data_manager_);
10201035 {
10211036 composer::Composer composer(&table, &client_request, &default_config());
10221037
10481063 const string kXtsu = "\xE3\x81\xA3";
10491064
10501065 composer::Table table;
1051 table.InitializeWithRequestAndConfig(client_request, default_config());
1066 table.InitializeWithRequestAndConfig(client_request, default_config(),
1067 mock_data_manager_);
10521068 {
10531069 composer::Composer composer(&table, &client_request, &default_config());
10541070
4848 #include "config/config_handler.h"
4949 #include "converter/converter_mock.h"
5050 #include "converter/segments.h"
51 #include "data_manager/testing/mock_data_manager.h"
5152 #include "protocol/candidates.pb.h"
5253 #include "protocol/commands.pb.h"
5354 #include "protocol/config.pb.h"
8788 protected:
8889 // Workaround for C2512 error (no default appropriate constructor) on MSVS.
8990 SessionConverterTest() {}
90 virtual ~SessionConverterTest() {}
91
92 virtual void SetUp() {
91 ~SessionConverterTest() override {}
92
93 void SetUp() override {
9394 convertermock_.reset(new ConverterMock());
9495 SystemUtil::SetUserProfileDirectory(FLAGS_test_tmpdir);
9596 mozc::usage_stats::UsageStats::ClearAllStatsForTest();
99100 request_.reset(new Request);
100101
101102 table_.reset(new composer::Table);
102 table_->InitializeWithRequestAndConfig(*request_, *config_);
103 table_->InitializeWithRequestAndConfig(*request_, *config_,
104 mock_data_manager_);
103105 composer_.reset(
104106 new composer::Composer(table_.get(), request_.get(), config_.get()));
105107 }
106108
107 virtual void TearDown() {
109 void TearDown() override {
108110 table_.reset();
109111 composer_.reset();
110112
399401 std::unique_ptr<Request> request_;
400402 std::unique_ptr<Config> config_;
401403 mozc::usage_stats::scoped_usage_stats_enabler usage_stats_enabler_;
404
405 private:
406 const testing::MockDataManager mock_data_manager_;
402407 };
403408
404409 #define EXPECT_SELECTED_CANDIDATE_INDICES_EQ(converter, indices) \
243243
244244 void SessionHandler::SetConfig(const config::Config &config) {
245245 *config_ = config;
246 const composer::Table *table = table_manager_->GetTable(*request_, *config_);
246 const composer::Table *table = table_manager_->GetTable(
247 *request_, *config_, *engine_->GetDataManager());
247248 for (SessionElement *element =
248249 const_cast<SessionElement *>(session_map_->Head());
249250 element != NULL; element = element->next) {
661662 engine_.reset();
662663 engine_ = engine_builder_->BuildFromPreparedData();
663664 LOG_IF(FATAL, !engine_) << "Critical failure in engine replace";
665 table_manager_->ClearCaches();
664666 response->set_status(EngineReloadResponse::RELOADED);
665667 }
666668 engine_builder_->Clear();
4141 #include "composer/table.h"
4242 #include "config/config_handler.h"
4343 #include "converter/segments.h"
44 #include "data_manager/testing/mock_data_manager.h"
4445 #include "engine/engine_factory.h"
4546 #include "protocol/candidates.pb.h"
4647 #include "protocol/commands.pb.h"
147148 session_.reset(static_cast<session::Session *>(handler_->NewSession()));
148149 commands::Request request;
149150 table_.reset(new composer::Table());
150 table_->InitializeWithRequestAndConfig(request, config_);
151 table_->InitializeWithRequestAndConfig(request, config_, data_manager_);
151152 session_->SetTable(table_.get());
152153 }
153154
155 const testing::MockDataManager data_manager_;
154156 bool orig_use_history_rewriter_;
155157 std::unique_ptr<SessionHandler> handler_;
156158 std::unique_ptr<session::Session> session_;
401401 // since History segments are almost hidden from
402402 class ConverterMockForReset : public ConverterMock {
403403 public:
404 virtual bool ResetConversion(Segments *segments) const {
404 ConverterMockForReset() : reset_conversion_called_(false) {}
405
406 bool ResetConversion(Segments *segments) const override {
405407 reset_conversion_called_ = true;
406408 return true;
407409 }
414416 reset_conversion_called_ = false;
415417 }
416418
417 ConverterMockForReset() : reset_conversion_called_(false) {}
418419 private:
419420 mutable bool reset_conversion_called_;
420421 };
444445 return nullptr;
445446 }
446447
448 const DataManagerInterface *GetDataManager() const override {
449 return nullptr;
450 }
451
447452 StringPiece GetDataVersion() const override { return StringPiece(); }
448453
449454 const ConverterMockForReset &converter_mock() const {
460465
461466 class ConverterMockForRevert : public ConverterMock {
462467 public:
463 virtual bool RevertConversion(Segments *segments) const {
468 ConverterMockForRevert() : revert_conversion_called_(false) {}
469
470 bool RevertConversion(Segments *segments) const override {
464471 revert_conversion_called_ = true;
465472 return true;
466473 }
473480 revert_conversion_called_ = false;
474481 }
475482
476 ConverterMockForRevert() : revert_conversion_called_(false) {}
477483 private:
478484 mutable bool revert_conversion_called_;
479485 };
501507 }
502508
503509 UserDataManagerInterface *GetUserDataManager() override {
510 return nullptr;
511 }
512
513 const DataManagerInterface *GetDataManager() const override {
504514 return nullptr;
505515 }
506516
664674 session->SetRequest(&request);
665675 table_.reset(new composer::Table());
666676 table_->InitializeWithRequestAndConfig(
667 request, config::ConfigHandler::DefaultConfig());
677 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
668678 session->SetTable(table_.get());
669679 }
670680
834844 std::unique_ptr<composer::Table> table_;
835845 std::unique_ptr<Request> mobile_request_;
836846 mozc::usage_stats::scoped_usage_stats_enabler usage_stats_enabler_;
847 const testing::MockDataManager mock_data_manager_;
837848
838849 private:
839850 const testing::ScopedTmpUserProfileDirectory scoped_profile_dir_;
840 const testing::MockDataManager mock_data_manager_;
841851 };
842852
843853 // This test is intentionally defined at this location so that this
82348244 session.SetRequest(&request);
82358245 std::unique_ptr<composer::Table> table(new composer::Table());
82368246 table->InitializeWithRequestAndConfig(
8237 request, config::ConfigHandler::DefaultConfig());
8247 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
82388248 session.SetTable(table.get());
82398249 // Type "2*" to produce "A".
82408250 SetSendKeyCommand("2", &command);
82548264 session.SetRequest(&request);
82558265 table.reset(new composer::Table());
82568266 table->InitializeWithRequestAndConfig(
8257 request, config::ConfigHandler::DefaultConfig());
8267 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
82588268 session.SetTable(table.get());
82598269 // Type "2" to produce "Aa".
82608270 SetSendKeyCommand("2", &command);
83108320 session.SetRequest(&request);
83118321 composer::Table table;
83128322 table.InitializeWithRequestAndConfig(
8313 request, config::ConfigHandler::DefaultConfig());
8323 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
83148324 session.SetTable(&table);
83158325
83168326 // Type "2" to produce "a".
83558365 session.SetRequest(&request);
83568366 composer::Table table;
83578367 table.InitializeWithRequestAndConfig(
8358 request, config::ConfigHandler::DefaultConfig());
8368 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
83598369 session.SetTable(&table);
83608370 // Type "33{<}{<}" to produce "さ"->"し"->"さ"->"そ".
83618371 SetSendKeyCommand("3", &command);
84018411 session.SetRequest(&request);
84028412 composer::Table table;
84038413 table.InitializeWithRequestAndConfig(
8404 request, config::ConfigHandler::DefaultConfig());
8414 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
84058415 session.SetTable(&table);
84068416 // Type "3*{<}*{<}", and composition should change
84078417 // "さ"->"ざ"->(No change)->"さ"->(No change).
84558465 session.SetRequest(&request);
84568466 composer::Table table;
84578467 table.InitializeWithRequestAndConfig(
8458 request, config::ConfigHandler::DefaultConfig());
8468 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
84598469 session.SetTable(&table);
84608470 // Type "{<}" and do nothing
84618471 SetSendCommandCommand(commands::SessionCommand::UNDO_OR_REWIND, &command);
85388548 session.SetRequest(&request);
85398549 composer::Table table;
85408550 table.InitializeWithRequestAndConfig(
8541 request, config::ConfigHandler::DefaultConfig());
8551 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
85428552 session.SetTable(&table);
85438553
85448554 // commit "あ" to push UNDO stack
85948604
85958605 composer::Table table;
85968606 table.InitializeWithRequestAndConfig(
8597 request, config::ConfigHandler::DefaultConfig());
8607 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
85988608 session.SetTable(&table);
85998609
86008610 // Type "!" to produce "!".
89588968
89598969 composer::Table table;
89608970 table.InitializeWithRequestAndConfig(
8961 request, config::ConfigHandler::DefaultConfig());
8971 request, config::ConfigHandler::DefaultConfig(), mock_data_manager_);
89628972 session->SetTable(&table);
89638973
89648974 SwitchInputFieldType(commands::Context::PASSWORD, session.get());
9696 'session_regression_test.cc',
9797 ],
9898 'dependencies': [
99 '../data_manager/testing/mock_data_manager.gyp:mock_data_manager',
99100 '../engine/engine.gyp:engine_factory',
100101 '../testing/testing.gyp:gtest_main',
101102 'session.gyp:session',
140141 ],
141142 'dependencies': [
142143 '../converter/converter_base.gyp:converter_mock',
144 '../data_manager/testing/mock_data_manager.gyp:mock_data_manager',
143145 '../testing/testing.gyp:gtest_main',
144146 '../testing/testing.gyp:testing_util',
145147 '../usage_stats/usage_stats_test.gyp:usage_stats_testing_util',