Tizen 2.1 base
[external/enchant.git] / unittests / EnchantDictionaryTestFixture.h
1 /* Copyright (c) 2007 Eric Scott Albright\r
2  * \r
3  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  * of this software and associated documentation files (the "Software"), to deal\r
5  * in the Software without restriction, including without limitation the rights\r
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  * copies of the Software, and to permit persons to whom the Software is\r
8  * furnished to do so, subject to the following conditions:\r
9  * \r
10  * The above copyright notice and this permission notice shall be included in\r
11  * all copies or substantial portions of the Software.\r
12  * \r
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19  * THE SOFTWARE.\r
20  */\r
21 \r
22 #ifndef __ENCHANTDICTIONARYTESTFIXTURE\r
23 #define __ENCHANTDICTIONARYTESTFIXTURE\r
24 \r
25 #if defined(_MSC_VER)\r
26 #pragma once\r
27 #pragma warning(push)\r
28 #pragma warning(disable: 4996) //The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name.\r
29 #endif\r
30 \r
31 #include "../EnchantBrokerTestFixture.h"\r
32 #include <io.h>\r
33 #include <sys/types.h>\r
34 #include <sys/stat.h>\r
35 #include <fcntl.h>\r
36 #include <vector>\r
37 \r
38 static EnchantDict*\r
39 MockProviderRequestEmptyMockDictionary(EnchantProvider *, const char *)\r
40 {\r
41     EnchantDict* dict = g_new0(EnchantDict, 1);\r
42     dict->user_data = NULL;\r
43     dict->check = NULL;\r
44     dict->suggest = NULL;\r
45     dict->add_to_personal = NULL;\r
46     dict->add_to_session = NULL;\r
47     dict->store_replacement = NULL;\r
48         \r
49     return dict;\r
50 }\r
51 \r
52 static void EmptyDictionary_ProviderConfiguration (EnchantProvider * me, const char *)\r
53 {\r
54      me->request_dict = MockProviderRequestEmptyMockDictionary;\r
55      me->dispose_dict = MockProviderDisposeDictionary;\r
56 }\r
57 \r
58 \r
59 static char**\r
60 MockDictionarySuggest (EnchantDict * , \r
61                        const char *const word,\r
62                                size_t len, \r
63                        size_t * out_n_suggs)\r
64 {\r
65     char **sugg_arr = NULL;\r
66 \r
67     *out_n_suggs = 4;\r
68 \r
69         sugg_arr = g_new0 (char *, *out_n_suggs + 1);\r
70     for(size_t i=0; i<*out_n_suggs;++i){\r
71         if(len == -1) {\r
72             sugg_arr[i] = g_strdup (word);\r
73         }\r
74         else {\r
75             sugg_arr[i] = g_strndup (word, (gsize)len);\r
76         }\r
77         sugg_arr[i][0] = 'a' + (char)i;\r
78     }\r
79     return sugg_arr;\r
80 }\r
81 \r
82 static EnchantDict*\r
83 MockProviderRequestBasicMockDictionary(EnchantProvider *me, const char *tag)\r
84 {\r
85     \r
86     EnchantDict* dict = MockProviderRequestEmptyMockDictionary(me, tag);\r
87     dict->suggest = MockDictionarySuggest;\r
88     return dict;\r
89 }\r
90 \r
91 \r
92 static void BasicDictionary_ProviderConfiguration (EnchantProvider * me, const char *)\r
93 {\r
94      me->request_dict = MockProviderRequestBasicMockDictionary;\r
95      me->dispose_dict = MockProviderDisposeDictionary;\r
96      me->free_string_list = MockProviderFreeStringList;\r
97 }\r
98 \r
99 struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture\r
100 {\r
101     EnchantDict* _dict;\r
102     EnchantDict* _pwl;\r
103     std::string _pwlFileName;\r
104     std::string origLangEnv;\r
105     bool hasLangEnv;\r
106     std::string languageTag;\r
107 \r
108     //Setup\r
109     EnchantDictionaryTestFixture(ConfigureHook userConfiguration=BasicDictionary_ProviderConfiguration, const std::string& languageTag="qaa"):\r
110         EnchantBrokerTestFixture(userConfiguration),\r
111         languageTag(languageTag)\r
112     {\r
113         InitializeTestDictionary();\r
114         _pwl = RequestPersonalDictionary();\r
115         _pwlFileName = GetLastPersonalDictionaryFileName();\r
116 \r
117         bool hasLangEnv = (g_getenv("LANG") != NULL);\r
118         if(hasLangEnv)\r
119         {\r
120             origLangEnv = std::string(g_getenv("LANG"));\r
121         }\r
122     }\r
123 \r
124     //Teardown\r
125     ~EnchantDictionaryTestFixture(){\r
126         FreeTestDictionary();\r
127         FreeDictionary(_pwl);\r
128         ResetLocale();\r
129     }\r
130 \r
131     void SetLocale(const std::string& locale)    \r
132     {\r
133         g_setenv("LANG", locale.c_str(), TRUE);\r
134     }\r
135 \r
136     void ResetLocale()\r
137     {\r
138         if(hasLangEnv)\r
139         {\r
140             g_setenv("LANG", origLangEnv.c_str(), TRUE);\r
141         }\r
142         else{\r
143             g_unsetenv("LANG");\r
144         }\r
145     }\r
146 \r
147     void ReloadTestDictionary()\r
148     {\r
149         FreeTestDictionary();\r
150         InitializeTestDictionary();\r
151     }\r
152 \r
153     void InitializeTestDictionary()\r
154     {\r
155         _dict = enchant_broker_request_dict(_broker, languageTag.c_str());\r
156     }\r
157 \r
158     void FreeTestDictionary()\r
159     {\r
160         FreeDictionary(_dict);\r
161     }\r
162 \r
163     bool PersonalWordListFileHasContents()\r
164     {\r
165         return FileHasContents(GetPersonalDictFileName());\r
166     }\r
167 \r
168     bool ExcludeFileHasContents()\r
169     {\r
170         return FileHasContents(GetExcludeDictFileName());\r
171     }\r
172 \r
173         bool BrokerPWLFileHasContents()\r
174     {\r
175         return FileHasContents(_pwlFileName);\r
176     }\r
177 \r
178     bool FileHasContents(const std::string & filename)\r
179     {\r
180         bool hasContents = false;\r
181         int fd = g_open(filename.c_str(), O_RDONLY, S_IREAD );\r
182         if(fd == -1){\r
183             return false;\r
184         }\r
185 \r
186         char c;\r
187         while(read(fd, &c, 1) == 1 && !hasContents){\r
188             switch(c)\r
189             {\r
190             case '\n':\r
191             case '\r':\r
192             case ' ':\r
193                 break;\r
194             default:\r
195                 hasContents = true;\r
196             }\r
197         }\r
198         close(fd);\r
199 \r
200         return hasContents;\r
201     }\r
202 \r
203     std::string GetPersonalDictFileName(){\r
204         return AddToPath(GetTempUserEnchantDir(), "qaa.dic");\r
205     }\r
206 \r
207         std::string GetExcludeDictFileName(){\r
208         return AddToPath(GetTempUserEnchantDir(), "qaa.exc");\r
209     }\r
210 \r
211     void SetErrorOnMockDictionary(const std::string& error)\r
212     {\r
213         enchant_dict_set_error(_dict, error.c_str());\r
214     }\r
215 \r
216     void FreeStringList(char** list)\r
217     {\r
218         if(list)\r
219         {\r
220             enchant_dict_free_string_list(_dict, list);\r
221         }\r
222     }\r
223 \r
224     bool IsWordInSession(const std::string& word){\r
225         return enchant_dict_is_in_session(_dict, word.c_str(), word.size())!=0;\r
226     }\r
227 \r
228     bool IsWordInDictionary(const std::string& word){\r
229         return enchant_dict_check(_dict, word.c_str(), word.size())==0;\r
230     }\r
231 \r
232     void RemoveWordFromDictionary(const std::string& word)\r
233     {\r
234          enchant_dict_remove(_dict, word.c_str(), word.size());\r
235     }\r
236 \r
237     void AddWordToDictionary(const std::string& word)\r
238     {\r
239                 enchant_dict_add(_dict, word.c_str(), word.size());\r
240     }\r
241 \r
242     void AddWordsToDictionary(const std::vector<const std::string>& sWords)\r
243     {\r
244         for(std::vector<const std::string>::const_iterator itWord = sWords.begin();\r
245                                                               itWord != sWords.end();\r
246                                                               ++itWord){\r
247             AddWordToDictionary(*itWord);\r
248         }\r
249     }\r
250 \r
251         void ExternalAddWordToDictionary(const std::string& word)\r
252     {\r
253         ExternalAddWordToFile(word, GetPersonalDictFileName());\r
254     }\r
255 \r
256     void ExternalAddWordToExclude(const std::string& word)\r
257     {\r
258         ExternalAddWordToFile(word, GetExcludeDictFileName());\r
259     }\r
260 \r
261     static void ExternalAddWordToFile(const std::string& word, const std::string& filename)\r
262     {\r
263         Sleep(1000); // FAT systems have a 2 second resolution\r
264                      // NTFS is appreciably faster but no specs on what it is exactly\r
265                      // c runtime library's time_t has a 1 second resolution\r
266         FILE * f = g_fopen(filename.c_str(), "at");\r
267                 if(f)\r
268                 {\r
269                         fputc('\n', f);\r
270                         fputs(word.c_str(), f);\r
271                         fclose(f);\r
272                 }\r
273     }\r
274 \r
275     void ExternalAddNewLineToDictionary()\r
276     {\r
277         Sleep(1000); // FAT systems have a 2 second resolution\r
278                      // NTFS is appreciably faster but no specs on what it is exactly\r
279                      // c runtime library's time_t has a 1 second resolution\r
280         FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "at");\r
281                 if(f) \r
282                 {\r
283                     fputc('\n', f);\r
284                         fclose(f);\r
285                 }\r
286     }\r
287 \r
288         void ExternalAddWordsToDictionary(const std::vector<const std::string>& sWords)\r
289     {\r
290         Sleep(1000); // FAT systems have a 2 second resolution\r
291                      // NTFS is appreciably faster but no specs on what it is exactly\r
292                      // c runtime library's time_t has a 1 second resolution\r
293         FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "at");\r
294                 if(f) \r
295                 {\r
296                         for(std::vector<const std::string>::const_iterator \r
297                                 itWord = sWords.begin();\r
298                 itWord != sWords.end(); ++itWord)\r
299             {\r
300                 if(itWord != sWords.begin()){\r
301                                     fputc('\n', f);\r
302                 }\r
303                                 fputs(itWord->c_str(), f);\r
304                         }\r
305                         fclose(f);\r
306                 }\r
307     }\r
308 \r
309    std::vector<std::string> GetExpectedSuggestions(const std::string& s, size_t begin = 0)\r
310     {\r
311         size_t cSuggestions;\r
312         char** expectedSuggestions = MockDictionarySuggest (_dict, \r
313                                                             s.c_str(),\r
314                                                                     s.size(), \r
315                                                             &cSuggestions);\r
316 \r
317         std::vector<std::string> result;\r
318         if(expectedSuggestions != NULL && begin < cSuggestions){\r
319             result.insert(result.begin(), expectedSuggestions+begin, expectedSuggestions+cSuggestions);\r
320             FreeStringList(expectedSuggestions);\r
321         }\r
322 \r
323         return result;\r
324     }\r
325 \r
326 \r
327     std::vector<const std::string> GetSuggestionsFromWord(const std::string& word)\r
328     {\r
329         std::vector<const std::string> result;\r
330 \r
331         size_t cSuggestions;\r
332         char** suggestions = enchant_dict_suggest(_dict, word.c_str(), word.size(), &cSuggestions);\r
333 \r
334         if(suggestions != NULL){\r
335             result.insert(result.begin(), suggestions, suggestions+cSuggestions);\r
336         }\r
337 \r
338         FreeStringList(suggestions);\r
339         \r
340         return result;\r
341     }\r
342 \r
343     std::vector<const std::string> GetSuggestions(const std::string& s)\r
344     {\r
345         return GetSuggestionsFromWord("helo");\r
346     }\r
347 };\r
348 #if defined(_MSC_VER)\r
349 #pragma warning(pop)\r
350 #endif\r
351 \r
352 #endif\r