Tizen 2.1 base
[external/enchant.git] / unittests / broker / enchant_broker_request_dict_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 \r
26 static bool requestDictionaryCalled;\r
27 static EnchantDict * RequestDictionary (EnchantProvider *me, const char *tag)\r
28 {\r
29     requestDictionaryCalled = true;\r
30     return MockEnGbAndQaaProviderRequestDictionary(me, tag);\r
31 }\r
32 \r
33 static void Request_Dictionary_ProviderConfiguration (EnchantProvider * me, const char *)\r
34 {\r
35      me->request_dict = RequestDictionary;\r
36      me->dispose_dict = MockProviderDisposeDictionary;\r
37 }\r
38 \r
39 struct EnchantBrokerRequestDictionary_TestFixture : EnchantBrokerTestFixture\r
40 {\r
41     //Setup\r
42     EnchantBrokerRequestDictionary_TestFixture():\r
43             EnchantBrokerTestFixture(Request_Dictionary_ProviderConfiguration)\r
44     { \r
45         _dict = NULL;\r
46         requestDictionaryCalled = false;\r
47     }\r
48 \r
49     //Teardown\r
50     ~EnchantBrokerRequestDictionary_TestFixture()\r
51     {\r
52         FreeDictionary(_dict);\r
53     }\r
54 \r
55     EnchantDict* _dict;\r
56 };\r
57 \r
58 /**\r
59  * enchant_broker_request_dict\r
60  * @broker: A non-null #EnchantBroker\r
61  * @tag: The non-null language tag you wish to request a dictionary for ("en_US", "de_DE", ...)\r
62  *\r
63  * Returns: An #EnchantDict, or %null if no suitable dictionary could be found.\r
64  */\r
65 \r
66 \r
67 \r
68 /////////////////////////////////////////////////////////////////////////////\r
69 // Test Normal Operation\r
70 \r
71 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
72              EnchantBrokerRequestDictionary_ProviderHas_CallsProvider)\r
73 {\r
74     _dict = enchant_broker_request_dict(_broker, "en_GB");\r
75     CHECK(_dict);\r
76     CHECK(requestDictionaryCalled);\r
77 }\r
78 \r
79 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
80              EnchantBrokerRequestDictionary_CalledTwice_CallsProviderOnceReturnsSame)\r
81 {\r
82     _dict = enchant_broker_request_dict(_broker, "en_GB");\r
83     requestDictionaryCalled = false;\r
84     EnchantDict* dict = enchant_broker_request_dict(_broker, "en_GB");\r
85     CHECK(!requestDictionaryCalled);\r
86 \r
87     CHECK_EQUAL(_dict, dict);\r
88 }\r
89 \r
90 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
91              EnchantBrokerRequestDictionary_ProviderDoesNotHave_CallsProvider)\r
92 {\r
93     _dict = enchant_broker_request_dict(_broker, "en");\r
94     CHECK_EQUAL((void*)NULL, (void*)_dict);\r
95     CHECK(requestDictionaryCalled);\r
96 }\r
97 \r
98 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
99              EnchantBrokerRequestDictionary_ProviderHasBase_CallsProvider)\r
100 {\r
101     _dict = enchant_broker_request_dict(_broker, "qaa_CA");\r
102     CHECK(_dict);\r
103     CHECK(requestDictionaryCalled);\r
104 }\r
105 \r
106 \r
107 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
108              EnchantBrokerRequestDictionary_WhitespaceSurroundingLanguageTag_Removed)\r
109 {\r
110     _dict = enchant_broker_request_dict(_broker, "\n\r en_GB \t\f");\r
111     CHECK(_dict);\r
112 }\r
113 \r
114 /* Vertical tab is not considered to be whitespace in glib!\r
115     See bug# 59388 http://bugzilla.gnome.org/show_bug.cgi?id=59388\r
116 */\r
117 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
118              EnchantBrokerRequestDictionary_VerticalTabBeforeLanguageTag_NotRemoved)\r
119 {\r
120   _dict = enchant_broker_request_dict(_broker, "\ven_GB");\r
121   CHECK_EQUAL((void*)NULL, (void*)_dict);\r
122 }\r
123 \r
124 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
125              EnchantBrokerRequestDictionary_VerticalTabAfterLanguageTag_NotRemoved)\r
126 {\r
127   _dict = enchant_broker_request_dict(_broker, "en_GB\v");\r
128   CHECK_EQUAL((void*)NULL, (void*)_dict);\r
129 }\r
130 \r
131 \r
132 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
133              EnchantBrokerRequestDictionary_AtSignInLanguageTag_RemovesToTail)\r
134 {\r
135     _dict = enchant_broker_request_dict(_broker, "en_GB@euro");\r
136     CHECK(_dict);\r
137 }\r
138 \r
139 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
140              EnchantBrokerRequestDictionary_PeriodInLanguageTag_RemovesToTail)\r
141 {\r
142     _dict = enchant_broker_request_dict(_broker, "en_GB.UTF-8");\r
143     CHECK(_dict);\r
144 }\r
145 \r
146 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
147              EnchantBrokerRequestDictionary_HyphensInLanguageTag_SubstitutedWithUnderscore)\r
148 {\r
149     _dict = enchant_broker_request_dict(_broker, "en-GB");\r
150     CHECK(_dict);\r
151 }\r
152 \r
153 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
154              EnchantBrokerRequestDictionary_DifferentCase_Finds)\r
155 {\r
156   _dict = enchant_broker_request_dict(_broker, "En_gb");\r
157   CHECK(_dict);\r
158 }\r
159 \r
160 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
161              EnchantBrokerRequestDictionary_DifferentCase_NoRegion_Finds)\r
162 {\r
163   _dict = enchant_broker_request_dict(_broker, "QAA");\r
164   CHECK(_dict);\r
165 }\r
166 \r
167 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
168              EnchantBrokerRequestDictionary_HasPreviousError_ErrorCleared)\r
169 {\r
170   SetErrorOnMockProvider("something bad happened");\r
171 \r
172   _dict = enchant_broker_request_dict(_broker, "en-GB");\r
173 \r
174   CHECK_EQUAL((void*)NULL, (void*)enchant_broker_get_error(_broker));\r
175 }\r
176 \r
177 // ordering of providers for request is tested by enchant_broker_set_ordering tests\r
178 \r
179 /////////////////////////////////////////////////////////////////////////////\r
180 // Test Error Conditions\r
181 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture,\r
182              EnchantBrokerRequestDictionary_NullBroker_NULL)\r
183 {\r
184     _dict = enchant_broker_request_dict(NULL, "en_GB");\r
185 \r
186     CHECK_EQUAL((void*)NULL, (void*)_dict);\r
187     CHECK(!requestDictionaryCalled);\r
188 }\r
189 \r
190 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture,\r
191              EnchantBrokerRequestDictionary_NullLanguageTag_NULL)\r
192 {\r
193     _dict = enchant_broker_request_dict(_broker, NULL);\r
194 \r
195     CHECK_EQUAL((void*)NULL, (void*)_dict);\r
196     CHECK(!requestDictionaryCalled);\r
197 }\r
198 \r
199 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture,\r
200              EnchantBrokerRequestDictionary_EmptyLanguageTag_NULL)\r
201 {\r
202     _dict = enchant_broker_request_dict(_broker, "");\r
203 \r
204     CHECK_EQUAL((void*)NULL, _dict);\r
205     CHECK(!requestDictionaryCalled);\r
206 \r
207 }\r
208 \r
209 TEST_FIXTURE(EnchantBrokerRequestDictionary_TestFixture, \r
210              EnchantBrokerRequestDictionary_InvalidTag_NULL_ErrorSet)\r
211 {\r
212     _dict = enchant_broker_request_dict(_broker, "en~US");\r
213     CHECK_EQUAL((void*)NULL, _dict);\r
214     CHECK(NULL != enchant_broker_get_error(_broker));\r
215 }\r
216 \r
217 TEST_FIXTURE(EnchantBrokerTestFixture,\r
218              EnchantBrokerRequestDictionary_ProviderLacksListDictionaries_CallbackNeverCalled)\r
219 {\r
220     requestDictionaryCalled = false;\r
221     EnchantDict* dict = enchant_broker_request_dict(_broker, "en_GB");\r
222 \r
223     CHECK_EQUAL((void*)NULL, dict);\r
224     CHECK(!requestDictionaryCalled);\r
225 }\r