Tizen 2.1 base
[external/enchant.git] / unittests / broker / enchant_broker_list_dicts_tests.cpp
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 #include <UnitTest++.h>\r
23 #include <enchant.h>\r
24 #include "../EnchantBrokerTestFixture.h"\r
25 #include <vector>\r
26 \r
27 \r
28 void EnchantDictionaryDescribeCallback (const char * const lang_tag,\r
29                                                           const char * const provider_name,\r
30                                                           const char * const provider_desc,\r
31                                                           const char * const provider_file,\r
32                                                           void * user_data)\r
33 {\r
34     std::vector<DictionaryDescription>* dictionaryList = reinterpret_cast<std::vector<DictionaryDescription>*>(user_data);\r
35     dictionaryList->push_back(DictionaryDescription(lang_tag, provider_name, provider_desc, provider_file));\r
36 }\r
37 \r
38 static void * global_user_data;\r
39 void EnchantDictionaryDescribeAssignUserDataToStaticCallback (const char * const,\r
40                                                                               const char * const,\r
41                                                                               const char * const,\r
42                                                                               const char * const,\r
43                                                                               void * user_data)\r
44 {\r
45   global_user_data = user_data;\r
46 }\r
47 \r
48 static bool freeStringListCalled;\r
49 static void FreeStringList (EnchantProvider * me, char **str_list)\r
50 {\r
51    freeStringListCalled = true;\r
52     \r
53    return MockProviderFreeStringList(me, str_list);\r
54 }\r
55 \r
56 static bool listDictionariesCalled;\r
57 static char** ListDictionaries (EnchantProvider * me, size_t * out_n_dicts)\r
58 {\r
59    listDictionariesCalled = true;\r
60     \r
61    return MockEnGbProviderListDictionaries(me, out_n_dicts);\r
62 }\r
63 \r
64 struct EnchantBrokerListDictionaries_TestFixtureBase : EnchantBrokerTestFixture\r
65 {\r
66     //Setup\r
67     EnchantBrokerListDictionaries_TestFixtureBase(ConfigureHook userConfiguration):\r
68             EnchantBrokerTestFixture(userConfiguration)\r
69     {\r
70         listDictionariesCalled=false; \r
71         freeStringListCalled=false;\r
72         global_user_data = NULL;\r
73     }\r
74     std::vector<DictionaryDescription> _dictionaryList;\r
75 };\r
76 \r
77 static void List_Dictionaries_ProviderConfiguration (EnchantProvider * me, const char *)\r
78 {\r
79      me->list_dicts=ListDictionaries;\r
80      me->free_string_list = FreeStringList;\r
81 }\r
82 \r
83 \r
84 struct EnchantBrokerListDictionaries_TestFixture : EnchantBrokerListDictionaries_TestFixtureBase\r
85 {\r
86     //Setup\r
87     EnchantBrokerListDictionaries_TestFixture():\r
88             EnchantBrokerListDictionaries_TestFixtureBase(List_Dictionaries_ProviderConfiguration)\r
89     { }\r
90 };\r
91 \r
92 static void List_Dictionaries_ProviderConfigurationLacksFreeStringList (EnchantProvider * me, const char *)\r
93 {\r
94      me->list_dicts=ListDictionaries;\r
95      me->free_string_list = NULL;\r
96 }\r
97 \r
98 struct EnchantBrokerListDictionaries_ProviderLacksFreeStringList_TestFixture : EnchantBrokerListDictionaries_TestFixtureBase\r
99 {\r
100     //Setup\r
101     EnchantBrokerListDictionaries_ProviderLacksFreeStringList_TestFixture():\r
102             EnchantBrokerListDictionaries_TestFixtureBase(List_Dictionaries_ProviderConfigurationLacksFreeStringList)\r
103     { }\r
104 };\r
105 \r
106 static char ** \r
107 InvalidTagEnGbListDictionaries (EnchantProvider *, \r
108                                           size_t * out_n_dicts)\r
109 {\r
110     *out_n_dicts = 2;\r
111     char** out_list = g_new0 (char *, *out_n_dicts + 1);\r
112     out_list[0] = g_strdup ("en_GB");\r
113     out_list[1] = g_strdup ("en-GB");\r
114 \r
115     return out_list;\r
116 }\r
117 \r
118 static void List_Dictionaries_InvalidTag_ProviderConfiguration (EnchantProvider * me, const char *)\r
119 {\r
120      me->list_dicts=InvalidTagEnGbListDictionaries;\r
121      me->free_string_list = FreeStringList;\r
122 }\r
123 \r
124 struct EnchantBrokerListDictionaries_ProviderReturnsInvalidTag_TestFixture : EnchantBrokerListDictionaries_TestFixtureBase\r
125 {\r
126     //Setup\r
127     EnchantBrokerListDictionaries_ProviderReturnsInvalidTag_TestFixture():\r
128             EnchantBrokerListDictionaries_TestFixtureBase(List_Dictionaries_InvalidTag_ProviderConfiguration)\r
129     { }\r
130 };\r
131 \r
132 \r
133 static char ** \r
134 DuplicateEnGbListDictionaries (EnchantProvider *, \r
135                                           size_t * out_n_dicts)\r
136 {\r
137     *out_n_dicts = 2;\r
138     char** out_list = g_new0 (char *, *out_n_dicts + 1);\r
139     out_list[0] = g_strdup ("en_GB");\r
140     out_list[1] = g_strdup ("en_GB");\r
141 \r
142     return out_list;\r
143 }\r
144 \r
145 static void List_Dictionaries_ProviderConfigurationDuplicateTags (EnchantProvider * me, const char *)\r
146 {\r
147      me->list_dicts=DuplicateEnGbListDictionaries;\r
148      me->free_string_list = MockProviderFreeStringList;\r
149 }\r
150 \r
151 struct EnchantBrokerListDictionaries_ProviderDuplicateTags_TestFixture : EnchantBrokerListDictionaries_TestFixtureBase\r
152 {\r
153     //Setup\r
154     EnchantBrokerListDictionaries_ProviderDuplicateTags_TestFixture():\r
155             EnchantBrokerListDictionaries_TestFixtureBase(List_Dictionaries_ProviderConfigurationDuplicateTags)\r
156     { }\r
157 };\r
158 \r
159 /**\r
160  * enchant_broker_list_dicts\r
161  * @broker: A non-null #EnchantBroker\r
162  * @fn: A non-null #EnchantDictDescribeFn\r
163  * @user_data: Optional user-data\r
164  *\r
165  * Enumerates the dictionaries available from\r
166  * all Enchant providers.\r
167  */\r
168 \r
169 /////////////////////////////////////////////////////////////////////////////\r
170 // Test Normal Operation\r
171 \r
172 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
173              EnchantBrokerListDictionaries_UserDataNotNull_PassedThrough)\r
174 {\r
175     char* userData = "some user data";\r
176     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeAssignUserDataToStaticCallback, userData);\r
177     CHECK_EQUAL(userData, global_user_data);\r
178 }\r
179 \r
180 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
181              EnchantBrokerListDictionaries_UserDataNull_PassedThrough)\r
182 {\r
183     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeAssignUserDataToStaticCallback, NULL);\r
184     CHECK_EQUAL((void*)NULL, global_user_data);\r
185 }\r
186 \r
187 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
188              EnchantBrokerListDictionaries_listDictionariesCalledOnProvider)\r
189 {\r
190     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
191     CHECK(listDictionariesCalled);\r
192 }\r
193 \r
194 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
195              EnchantBrokerListDictionaries_freeStringListCalledOnProvider)\r
196 {\r
197     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
198     CHECK(freeStringListCalled);\r
199 }\r
200 \r
201 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
202              EnchantBrokerListDictionaries_dictionaryListHasDictionariesFromProvider)\r
203 {\r
204     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
205     CHECK_EQUAL((unsigned int)1, _dictionaryList.size());\r
206     if(_dictionaryList.size()>0){\r
207         CHECK(_dictionaryList[0].DataIsComplete());\r
208         CHECK_EQUAL(std::string("en_GB"), _dictionaryList[0].LanguageTag);\r
209         CHECK_EQUAL(std::string("mock"), _dictionaryList[0].Name);\r
210         CHECK_EQUAL(std::string("Mock Provider"), _dictionaryList[0].Description);\r
211     }\r
212 }\r
213 \r
214 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture, \r
215              EnchantBrokerListDictionaries_HasPreviousError_ErrorCleared)\r
216 {\r
217     SetErrorOnMockProvider("something bad happened");\r
218 \r
219     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
220 \r
221     CHECK_EQUAL((void*)NULL, (void*)enchant_broker_get_error(_broker));\r
222 }\r
223 \r
224 /////////////////////////////////////////////////////////////////////////////\r
225 // Test Error Conditions\r
226 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
227              EnchantBrokerListDictionaries_NullBroker_DoNotCallDescribeFunction)\r
228 {\r
229     enchant_broker_list_dicts(NULL, EnchantDictionaryDescribeAssignUserDataToStaticCallback, NULL);\r
230     CHECK(!listDictionariesCalled);\r
231 }\r
232 \r
233 TEST_FIXTURE(EnchantBrokerListDictionaries_TestFixture,\r
234              EnchantBrokerListDictionaries_NullCallback_DoNotCallDescribeFunction)\r
235 {\r
236     enchant_broker_list_dicts(_broker, NULL, NULL);\r
237     CHECK(!listDictionariesCalled);\r
238 }\r
239 \r
240 TEST_FIXTURE(EnchantBrokerTestFixture,\r
241              EnchantBrokerListDictionaries_ProviderLacksListDictionaries_CallbackNeverCalled)\r
242 {\r
243     void* userData = "some user data";\r
244     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeAssignUserDataToStaticCallback, userData);\r
245     CHECK_EQUAL((void*)NULL, global_user_data);\r
246 }\r
247 \r
248 TEST_FIXTURE(EnchantBrokerListDictionaries_ProviderDuplicateTags_TestFixture,\r
249              EnchantBrokerListDictionaries_ProviderDuplicateTags_CallbackCalledOnlyOncePerUniqueTag)\r
250 {\r
251     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
252     CHECK_EQUAL((unsigned int)1, _dictionaryList.size());\r
253 }\r
254 \r
255 TEST_FIXTURE(EnchantBrokerListDictionaries_ProviderLacksFreeStringList_TestFixture,\r
256              EnchantBrokerListDictionaries_freeStringListCalledOnProvider)\r
257 {\r
258     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
259     CHECK(!freeStringListCalled);\r
260 }\r
261 \r
262 \r
263 TEST_FIXTURE(EnchantBrokerListDictionaries_ProviderReturnsInvalidTag_TestFixture,\r
264              EnchantBrokerListDictionaries_ProviderReturnsInvalidTag)\r
265 {\r
266     enchant_broker_list_dicts(_broker, EnchantDictionaryDescribeCallback, &_dictionaryList);\r
267     CHECK_EQUAL((unsigned int)1, _dictionaryList.size());\r
268 }