| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/glue/webkit_glue.h" | 5 #include "webkit/glue/webkit_glue.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
| 11 #include "chrome/browser/chrome_thread.h" |
| 11 #include "chrome/browser/spellchecker.h" | 12 #include "chrome/browser/spellchecker.h" |
| 12 #include "chrome/browser/spellchecker_platform_engine.h" | 13 #include "chrome/browser/spellchecker_platform_engine.h" |
| 13 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 const FilePath::CharType kTempCustomDictionaryFile[] = | 18 const FilePath::CharType kTempCustomDictionaryFile[] = |
| 18 FILE_PATH_LITERAL("temp_custom_dictionary.txt"); | 19 FILE_PATH_LITERAL("temp_custom_dictionary.txt"); |
| 19 } // namespace | 20 } // namespace |
| 20 | 21 |
| 21 class SpellCheckTest : public testing::Test { | 22 class SpellCheckTest : public testing::Test { |
| 23 public: |
| 24 SpellCheckTest() |
| 25 : file_thread_(ChromeThread::FILE, &message_loop_), |
| 26 io_thread_(ChromeThread::IO, &message_loop_) {} |
| 27 |
| 28 protected: |
| 29 MessageLoop message_loop_; |
| 30 |
| 22 private: | 31 private: |
| 23 MessageLoop message_loop_; | 32 ChromeThread file_thread_; |
| 33 ChromeThread io_thread_; // To keep DCHECKs inside spell checker happy. |
| 24 }; | 34 }; |
| 25 | 35 |
| 26 // Represents a special initialization function used only for the unit tests | 36 // Represents a special initialization function used only for the unit tests |
| 27 // in this file. | 37 // in this file. |
| 28 extern void InitHunspellWithFiles(FILE* file_aff_hunspell, | 38 extern void InitHunspellWithFiles(FILE* file_aff_hunspell, |
| 29 FILE* file_dic_hunspell); | 39 FILE* file_dic_hunspell); |
| 30 | 40 |
| 31 FilePath GetHunspellDirectory() { | 41 FilePath GetHunspellDirectory() { |
| 32 FilePath hunspell_directory; | 42 FilePath hunspell_directory; |
| 33 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory)) | 43 if (!PathService::Get(base::DIR_SOURCE_ROOT, &hunspell_directory)) |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 // spellcheck" <http://crbug.com/13432>. | 274 // spellcheck" <http://crbug.com/13432>. |
| 265 {L"qwertyuiopasd", false, 0, 13}, | 275 {L"qwertyuiopasd", false, 0, 13}, |
| 266 {L"qwertyuiopasdf", false, 0, 14}, | 276 {L"qwertyuiopasdf", false, 0, 14}, |
| 267 }; | 277 }; |
| 268 | 278 |
| 269 FilePath hunspell_directory = GetHunspellDirectory(); | 279 FilePath hunspell_directory = GetHunspellDirectory(); |
| 270 ASSERT_FALSE(hunspell_directory.empty()); | 280 ASSERT_FALSE(hunspell_directory.empty()); |
| 271 | 281 |
| 272 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 282 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 273 hunspell_directory, "en-US", NULL, FilePath())); | 283 hunspell_directory, "en-US", NULL, FilePath())); |
| 284 spell_checker->Initialize(); |
| 285 message_loop_.RunAllPending(); |
| 274 | 286 |
| 275 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 287 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 276 size_t input_length = 0; | 288 size_t input_length = 0; |
| 277 if (kTestCases[i].input != NULL) { | 289 if (kTestCases[i].input != NULL) { |
| 278 input_length = wcslen(kTestCases[i].input); | 290 input_length = wcslen(kTestCases[i].input); |
| 279 } | 291 } |
| 280 int misspelling_start; | 292 int misspelling_start; |
| 281 int misspelling_length; | 293 int misspelling_length; |
| 282 bool result = spell_checker->SpellCheckWord( | 294 bool result = spell_checker->SpellCheckWord( |
| 283 WideToUTF16(kTestCases[i].input).c_str(), | 295 WideToUTF16(kTestCases[i].input).c_str(), |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 {L"jum", false, 0, 0, L"um"}, | 621 {L"jum", false, 0, 0, L"um"}, |
| 610 #endif // !OS_MACOSX | 622 #endif // !OS_MACOSX |
| 611 // TODO (Sidchat): add many more examples. | 623 // TODO (Sidchat): add many more examples. |
| 612 }; | 624 }; |
| 613 | 625 |
| 614 FilePath hunspell_directory = GetHunspellDirectory(); | 626 FilePath hunspell_directory = GetHunspellDirectory(); |
| 615 ASSERT_FALSE(hunspell_directory.empty()); | 627 ASSERT_FALSE(hunspell_directory.empty()); |
| 616 | 628 |
| 617 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 629 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 618 hunspell_directory, "en-US", NULL, FilePath())); | 630 hunspell_directory, "en-US", NULL, FilePath())); |
| 631 spell_checker->Initialize(); |
| 632 message_loop_.RunAllPending(); |
| 619 | 633 |
| 620 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 634 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 621 std::vector<string16> suggestions; | 635 std::vector<string16> suggestions; |
| 622 size_t input_length = 0; | 636 size_t input_length = 0; |
| 623 if (kTestCases[i].input != NULL) { | 637 if (kTestCases[i].input != NULL) { |
| 624 input_length = wcslen(kTestCases[i].input); | 638 input_length = wcslen(kTestCases[i].input); |
| 625 } | 639 } |
| 626 int misspelling_start; | 640 int misspelling_start; |
| 627 int misspelling_length; | 641 int misspelling_length; |
| 628 bool result = spell_checker->SpellCheckWord( | 642 bool result = spell_checker->SpellCheckWord( |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 L"th\x1EC3 truy c\x1EADp va h\x1EEFu d\x1EE5ng h\x01A1n." | 896 L"th\x1EC3 truy c\x1EADp va h\x1EEFu d\x1EE5ng h\x01A1n." |
| 883 }, | 897 }, |
| 884 }; | 898 }; |
| 885 | 899 |
| 886 FilePath hunspell_directory = GetHunspellDirectory(); | 900 FilePath hunspell_directory = GetHunspellDirectory(); |
| 887 ASSERT_FALSE(hunspell_directory.empty()); | 901 ASSERT_FALSE(hunspell_directory.empty()); |
| 888 | 902 |
| 889 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 903 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 890 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 904 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 891 hunspell_directory, kTestCases[i].language, NULL, FilePath())); | 905 hunspell_directory, kTestCases[i].language, NULL, FilePath())); |
| 906 spell_checker->Initialize(); |
| 907 message_loop_.RunAllPending(); |
| 892 | 908 |
| 893 size_t input_length = 0; | 909 size_t input_length = 0; |
| 894 if (kTestCases[i].input != NULL) | 910 if (kTestCases[i].input != NULL) |
| 895 input_length = wcslen(kTestCases[i].input); | 911 input_length = wcslen(kTestCases[i].input); |
| 896 | 912 |
| 897 int misspelling_start = 0; | 913 int misspelling_start = 0; |
| 898 int misspelling_length = 0; | 914 int misspelling_length = 0; |
| 899 bool result = spell_checker->SpellCheckWord( | 915 bool result = spell_checker->SpellCheckWord( |
| 900 WideToUTF16(kTestCases[i].input).c_str(), | 916 WideToUTF16(kTestCases[i].input).c_str(), |
| 901 static_cast<int>(input_length), | 917 static_cast<int>(input_length), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 919 {L"Googleplex"}, | 935 {L"Googleplex"}, |
| 920 {L"Googler"}, | 936 {L"Googler"}, |
| 921 }; | 937 }; |
| 922 | 938 |
| 923 FilePath custom_dictionary_file(kTempCustomDictionaryFile); | 939 FilePath custom_dictionary_file(kTempCustomDictionaryFile); |
| 924 FilePath hunspell_directory = GetHunspellDirectory(); | 940 FilePath hunspell_directory = GetHunspellDirectory(); |
| 925 ASSERT_FALSE(hunspell_directory.empty()); | 941 ASSERT_FALSE(hunspell_directory.empty()); |
| 926 | 942 |
| 927 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 943 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 928 hunspell_directory, "en-US", NULL, custom_dictionary_file)); | 944 hunspell_directory, "en-US", NULL, custom_dictionary_file)); |
| 945 spell_checker->Initialize(); |
| 946 message_loop_.RunAllPending(); |
| 929 | 947 |
| 930 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 948 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 931 // Add the word to spellchecker. | 949 // Add the word to spellchecker. |
| 932 spell_checker->AddWord(WideToUTF16(kTestCases[i].word_to_add)); | 950 spell_checker->AddWord(WideToUTF16(kTestCases[i].word_to_add)); |
| 933 | 951 |
| 934 // Now check whether it is added to Spellchecker. | 952 // Now check whether it is added to Spellchecker. |
| 935 std::vector<string16> suggestions; | 953 std::vector<string16> suggestions; |
| 936 size_t input_length = 0; | 954 size_t input_length = 0; |
| 937 if (kTestCases[i].word_to_add != NULL) { | 955 if (kTestCases[i].word_to_add != NULL) { |
| 938 input_length = wcslen(kTestCases[i].word_to_add); | 956 input_length = wcslen(kTestCases[i].word_to_add); |
| 939 } | 957 } |
| 940 int misspelling_start; | 958 int misspelling_start; |
| 941 int misspelling_length; | 959 int misspelling_length; |
| 942 bool result = spell_checker->SpellCheckWord( | 960 bool result = spell_checker->SpellCheckWord( |
| 943 WideToUTF16(kTestCases[i].word_to_add).c_str(), | 961 WideToUTF16(kTestCases[i].word_to_add).c_str(), |
| 944 static_cast<int>(input_length), | 962 static_cast<int>(input_length), |
| 945 0, | 963 0, |
| 946 &misspelling_start, | 964 &misspelling_start, |
| 947 &misspelling_length, | 965 &misspelling_length, |
| 948 &suggestions); | 966 &suggestions); |
| 949 | 967 |
| 950 // Check for spelling. | 968 // Check for spelling. |
| 951 EXPECT_TRUE(result); | 969 EXPECT_TRUE(result); |
| 952 } | 970 } |
| 953 | 971 |
| 954 // Now initialize another spellchecker to see that AddToWord is permanent. | 972 // Now initialize another spellchecker to see that AddToWord is permanent. |
| 955 scoped_refptr<SpellChecker> spell_checker_new(new SpellChecker( | 973 scoped_refptr<SpellChecker> spell_checker_new(new SpellChecker( |
| 956 hunspell_directory, "en-US", NULL, custom_dictionary_file)); | 974 hunspell_directory, "en-US", NULL, custom_dictionary_file)); |
| 975 spell_checker->Initialize(); |
| 976 message_loop_.RunAllPending(); |
| 957 | 977 |
| 958 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 978 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 959 // Now check whether it is added to Spellchecker. | 979 // Now check whether it is added to Spellchecker. |
| 960 std::vector<string16> suggestions; | 980 std::vector<string16> suggestions; |
| 961 size_t input_length = 0; | 981 size_t input_length = 0; |
| 962 if (kTestCases[i].word_to_add != NULL) { | 982 if (kTestCases[i].word_to_add != NULL) { |
| 963 input_length = wcslen(kTestCases[i].word_to_add); | 983 input_length = wcslen(kTestCases[i].word_to_add); |
| 964 } | 984 } |
| 965 int misspelling_start; | 985 int misspelling_start; |
| 966 int misspelling_length; | 986 int misspelling_length; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 990 {L"Googleplex"}, | 1010 {L"Googleplex"}, |
| 991 {L"Googler"}, | 1011 {L"Googler"}, |
| 992 }; | 1012 }; |
| 993 | 1013 |
| 994 FilePath custom_dictionary_file(kTempCustomDictionaryFile); | 1014 FilePath custom_dictionary_file(kTempCustomDictionaryFile); |
| 995 FilePath hunspell_directory = GetHunspellDirectory(); | 1015 FilePath hunspell_directory = GetHunspellDirectory(); |
| 996 ASSERT_FALSE(hunspell_directory.empty()); | 1016 ASSERT_FALSE(hunspell_directory.empty()); |
| 997 | 1017 |
| 998 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 1018 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 999 hunspell_directory, "en-US", NULL, custom_dictionary_file)); | 1019 hunspell_directory, "en-US", NULL, custom_dictionary_file)); |
| 1020 spell_checker->Initialize(); |
| 1021 message_loop_.RunAllPending(); |
| 1000 | 1022 |
| 1001 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 1023 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 1002 // Add the word to spellchecker. | 1024 // Add the word to spellchecker. |
| 1003 spell_checker->AddWord(WideToUTF16(kTestCases[i].word_to_add)); | 1025 spell_checker->AddWord(WideToUTF16(kTestCases[i].word_to_add)); |
| 1004 } | 1026 } |
| 1005 | 1027 |
| 1006 // Now check to see whether the custom words are suggested for | 1028 // Now check to see whether the custom words are suggested for |
| 1007 // misspelled but similar words. | 1029 // misspelled but similar words. |
| 1008 static const struct { | 1030 static const struct { |
| 1009 // A string to be tested. | 1031 // A string to be tested. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1076 {"noen", ""}, | 1098 {"noen", ""}, |
| 1077 {"what", ""}, | 1099 {"what", ""}, |
| 1078 }; | 1100 }; |
| 1079 | 1101 |
| 1080 FilePath hunspell_directory = GetHunspellDirectory(); | 1102 FilePath hunspell_directory = GetHunspellDirectory(); |
| 1081 ASSERT_FALSE(hunspell_directory.empty()); | 1103 ASSERT_FALSE(hunspell_directory.empty()); |
| 1082 | 1104 |
| 1083 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 1105 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 1084 hunspell_directory, "en-US", NULL, FilePath())); | 1106 hunspell_directory, "en-US", NULL, FilePath())); |
| 1085 spell_checker->EnableAutoSpellCorrect(true); | 1107 spell_checker->EnableAutoSpellCorrect(true); |
| 1108 spell_checker->Initialize(); |
| 1109 message_loop_.RunAllPending(); |
| 1086 | 1110 |
| 1087 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 1111 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 1088 string16 misspelled_word(UTF8ToUTF16(kTestCases[i].input)); | 1112 string16 misspelled_word(UTF8ToUTF16(kTestCases[i].input)); |
| 1089 string16 expected_autocorrect_word( | 1113 string16 expected_autocorrect_word( |
| 1090 UTF8ToUTF16(kTestCases[i].expected_result)); | 1114 UTF8ToUTF16(kTestCases[i].expected_result)); |
| 1091 string16 autocorrect_word = spell_checker->GetAutoCorrectionWord( | 1115 string16 autocorrect_word = spell_checker->GetAutoCorrectionWord( |
| 1092 misspelled_word, 0); | 1116 misspelled_word, 0); |
| 1093 | 1117 |
| 1094 // Check for spelling. | 1118 // Check for spelling. |
| 1095 EXPECT_EQ(expected_autocorrect_word, autocorrect_word); | 1119 EXPECT_EQ(expected_autocorrect_word, autocorrect_word); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1111 {"moer", false}, | 1135 {"moer", false}, |
| 1112 {"watre", false}, | 1136 {"watre", false}, |
| 1113 {"noen", false}, | 1137 {"noen", false}, |
| 1114 }; | 1138 }; |
| 1115 | 1139 |
| 1116 FilePath hunspell_directory = GetHunspellDirectory(); | 1140 FilePath hunspell_directory = GetHunspellDirectory(); |
| 1117 ASSERT_FALSE(hunspell_directory.empty()); | 1141 ASSERT_FALSE(hunspell_directory.empty()); |
| 1118 | 1142 |
| 1119 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( | 1143 scoped_refptr<SpellChecker> spell_checker(new SpellChecker( |
| 1120 hunspell_directory, "en-US", NULL, FilePath())); | 1144 hunspell_directory, "en-US", NULL, FilePath())); |
| 1145 spell_checker->Initialize(); |
| 1146 message_loop_.RunAllPending(); |
| 1121 | 1147 |
| 1122 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | 1148 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 1123 string16 word(UTF8ToUTF16(kTestCases[i].input)); | 1149 string16 word(UTF8ToUTF16(kTestCases[i].input)); |
| 1124 std::vector<string16> suggestions; | 1150 std::vector<string16> suggestions; |
| 1125 size_t input_length = 0; | 1151 size_t input_length = 0; |
| 1126 if (kTestCases[i].input != NULL) { | 1152 if (kTestCases[i].input != NULL) { |
| 1127 input_length = word.length(); | 1153 input_length = word.length(); |
| 1128 } | 1154 } |
| 1129 int misspelling_start; | 1155 int misspelling_start; |
| 1130 int misspelling_length; | 1156 int misspelling_length; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1164 doc_tag, | 1190 doc_tag, |
| 1165 &misspelling_start, | 1191 &misspelling_start, |
| 1166 &misspelling_length, | 1192 &misspelling_length, |
| 1167 &suggestions); | 1193 &suggestions); |
| 1168 | 1194 |
| 1169 // The word should now show be spelled wrong again | 1195 // The word should now show be spelled wrong again |
| 1170 EXPECT_EQ(kTestCases[i].input_result, result); | 1196 EXPECT_EQ(kTestCases[i].input_result, result); |
| 1171 } | 1197 } |
| 1172 } // Test IgnoreWords_EN_US | 1198 } // Test IgnoreWords_EN_US |
| 1173 #endif // OS_MACOSX | 1199 #endif // OS_MACOSX |
| OLD | NEW |