Tizen 2.1 base
[external/enchant.git] / unittests / Enchant.Net.Tests / BrokerTests.cs
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 using System;\r
23 using System.Collections.Generic;\r
24 using System.IO;\r
25 using Microsoft.Win32;\r
26 using NUnit.Framework;\r
27 \r
28 namespace Enchant.Tests\r
29 {\r
30         [TestFixture]\r
31         public class BrokerTests\r
32         {\r
33                 #region Setup/Teardown\r
34 \r
35                 [SetUp]\r
36                 public void Setup()\r
37                 {\r
38                         oldRegistryValue = (string)\r
39                                                                                                  Registry.GetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config", "Data_Dir", null);\r
40                         tempdir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());\r
41 \r
42                         Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config", "Data_Dir", tempdir, RegistryValueKind.String);\r
43                 }\r
44 \r
45                 [TearDown]\r
46                 public void Teardown()\r
47                 {\r
48                         if (oldRegistryValue == null)\r
49                         {\r
50                                 Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Enchant").OpenSubKey("Config", true).DeleteValue(\r
51                                         "Data_Dir");\r
52                         }\r
53                         else\r
54                         {\r
55                                 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config",\r
56                                                                                                         "Data_Dir",\r
57                                                                                                         oldRegistryValue,\r
58                                                                                                         RegistryValueKind.String);\r
59                         }\r
60                         while (Directory.Exists(tempdir))\r
61                         {\r
62                                 Directory.Delete(tempdir, true);\r
63                         }\r
64                 }\r
65 \r
66                 #endregion\r
67 \r
68                 private string tempdir;\r
69                 private string oldRegistryValue;\r
70 \r
71                 [TestFixtureSetUp]\r
72                 public void FixtureSetup()\r
73                 {\r
74                         string providerDir = Path.Combine(Path.Combine(\r
75                                                                                                                                                                         Directory.GetCurrentDirectory(), "lib"), "enchant");\r
76                         if (!Directory.Exists(providerDir))\r
77                         {\r
78                                 Directory.CreateDirectory(providerDir);\r
79                         }\r
80                         File.Copy("libenchant_ispell.dll",\r
81                                                                 Path.Combine(providerDir, "libenchant_ispell.dll"), true);\r
82                         File.Copy("libenchant_myspell.dll",\r
83                                                                 Path.Combine(providerDir, "libenchant_myspell.dll"), true);\r
84             InstallDictionary("myspell", new string[] { "en_US.aff", "en_US.dic" });\r
85                 }\r
86 \r
87         static private void InstallDictionary(string provider, IEnumerable<string> files)\r
88         {\r
89             string dictionarySourceDir =\r
90                     Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(\r
91                                              Directory.GetCurrentDirectory(), ".."), ".."),\r
92                                              "lib"), "share"),\r
93                                               "enchant"), provider);\r
94 \r
95             string dictionaryDestDir = Path.Combine(Path.Combine(Path.Combine(\r
96                                                     Directory.GetCurrentDirectory(), "share"), "enchant"),\r
97                                                     provider);\r
98 \r
99             if (!Directory.Exists(dictionaryDestDir))\r
100             {\r
101                 Directory.CreateDirectory(dictionaryDestDir);\r
102             }\r
103 \r
104             foreach (string file in files)\r
105             {\r
106                 File.Copy(Path.Combine(dictionarySourceDir, file),\r
107                           Path.Combine(dictionaryDestDir, file), true);\r
108 \r
109             }\r
110         }\r
111 \r
112                 [TestFixtureTearDown]\r
113                 public void FixtureTearDown()\r
114                 {\r
115                         Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "lib"), true);\r
116                         Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "share"), true);\r
117                 }\r
118 \r
119                 [Test]\r
120                 public void Construct()\r
121                 {\r
122                         Assert.IsNotNull(new Broker());\r
123                 }\r
124 \r
125                 [Test]\r
126         // this test may fail because it picks up dictionaries from the\r
127         // open office installation present on the machine\r
128                 public void Dictionaries()\r
129                 {\r
130                         using (Broker broker = new Broker())\r
131                         {\r
132                                 IEnumerable<DictionaryInfo> dictionaries = broker.Dictionaries;\r
133                                 Assert.IsNotNull(dictionaries);\r
134                                 int count = 0;\r
135                                 foreach (DictionaryInfo info in dictionaries)\r
136                                 {\r
137                                         Console.WriteLine("Language:{0}\tName:{1}\tDescription:{2}\tFile:{3}",\r
138                                                                                                                 info.Language,\r
139                                                                                                                 info.Provider.Name,\r
140                                                                                                                 info.Provider.Description,\r
141                                                                                                                 info.Provider.File);\r
142                                         Assert.IsNotEmpty(info.Language);\r
143                                         Assert.IsNotEmpty(info.Provider.Name);\r
144                                         Assert.IsNotEmpty(info.Provider.Description);\r
145                                         Assert.IsNotEmpty(info.Provider.File);\r
146                                         ++count;\r
147                                 }\r
148                                 Assert.AreEqual(1, count);\r
149                         }\r
150                 }\r
151 \r
152                 [Test]\r
153                 public void DictionaryExists()\r
154                 {\r
155                         using (Broker broker = new Broker())\r
156                         {\r
157                                 Assert.IsFalse(broker.DictionaryExists("qaa"));\r
158                                 Assert.IsTrue(broker.DictionaryExists("en_US"));\r
159                         }\r
160                 }\r
161 \r
162                 [Test]\r
163                 public void Providers()\r
164                 {\r
165                         using (Broker broker = new Broker())\r
166                         {\r
167                                 IEnumerable<ProviderInfo> providers = broker.Providers;\r
168                                 Assert.IsNotNull(providers);\r
169                                 int count = 0;\r
170                                 foreach (ProviderInfo info in providers)\r
171                                 {\r
172                                         Console.WriteLine("Name:{0}\tDescription:{1}\tFile:{2}",\r
173                                                                                                                 info.Name,\r
174                                                                                                                 info.Description,\r
175                                                                                                                 info.File);\r
176                                         Assert.IsNotEmpty(info.Name);\r
177                                         Assert.IsNotEmpty(info.Description);\r
178                                         Assert.IsNotEmpty(info.File);\r
179                                         ++count;\r
180                                 }\r
181                                 Assert.AreEqual(2, count);\r
182                         }\r
183                 }\r
184 \r
185                 [Test]\r
186                 public void RequestDictionary()\r
187                 {\r
188                         using (Broker broker = new Broker())\r
189                         {\r
190                                 Dictionary dictionary = broker.RequestDictionary("en_US");\r
191                                 Assert.IsNotNull(dictionary);\r
192                         }\r
193                 }\r
194 \r
195         [Test]\r
196         public void RequestDictionary_CachingEnabled_DictionaryReRequested_SameReference()\r
197         {\r
198             using (Broker broker = new Broker())\r
199             {\r
200                 broker.CacheDictionaries = true;\r
201                 Dictionary dictionaryFirstRequest = broker.RequestDictionary("en_US");\r
202                 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");\r
203 \r
204                 Assert.AreSame(dictionaryFirstRequest, dictionarySecondRequest);\r
205             }\r
206         }\r
207 \r
208         [Test]\r
209         public void RequestDictionary_CachingEnabled_DictionaryDisposedThenReRequested_DifferentReference()\r
210         {\r
211             using (Broker broker = new Broker())\r
212             {\r
213                 broker.CacheDictionaries = true;\r
214                 Dictionary dictionaryFirstRequest;\r
215                 using (dictionaryFirstRequest = broker.RequestDictionary("en_US")) {}\r
216                 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");\r
217 \r
218                     Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);\r
219             }\r
220         }\r
221 \r
222         [Test]\r
223         public void RequestDictionary_CachingDisabled_DictionaryReRequested_DifferentReference()\r
224         {\r
225             using (Broker broker = new Broker())\r
226             {\r
227                 broker.CacheDictionaries = false;\r
228                 Dictionary dictionaryFirstRequest = broker.RequestDictionary("en_US");\r
229                 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");\r
230 \r
231                 Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);\r
232             }\r
233         }\r
234 \r
235         [Test]\r
236         public void RequestDictionary_CachingDisabled_DictionaryDisposedThenReRequested_DifferentReference()\r
237         {\r
238             using (Broker broker = new Broker())\r
239             {\r
240                 broker.CacheDictionaries = false;\r
241                 Dictionary dictionaryFirstRequest;\r
242                 using (dictionaryFirstRequest = broker.RequestDictionary("en_US"))\r
243                 {\r
244                 }\r
245                 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");\r
246 \r
247                 Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);\r
248             }\r
249         }\r
250 \r
251         [Test]\r
252         [ExpectedException(typeof(ObjectDisposedException))]\r
253         public void Dispose_UseDictionaryAfterBrokerDisposed_Throws()\r
254         {\r
255             Dictionary dictionary;\r
256             using (Broker broker = new Broker())\r
257             {\r
258                 dictionary = broker.RequestDictionary("en_US");\r
259             }\r
260             DictionaryInfo info = dictionary.Information;\r
261         }\r
262 \r
263         [Test]\r
264                 [ExpectedException(typeof (ApplicationException))]\r
265                 public void RequestDictionary_DictionaryDoesNotExist_Throws()\r
266                 {\r
267                         using (Broker broker = new Broker())\r
268                         {\r
269                                 broker.RequestDictionary("qaa");\r
270                         }\r
271                 }\r
272 \r
273                 [Test]\r
274                 public void RequestPwlDictionary()\r
275                 {\r
276                         string filename = Path.GetTempFileName();\r
277                         using (Broker broker = new Broker())\r
278                         {\r
279                                 using (Dictionary dictionary = broker.RequestPwlDictionary(filename))\r
280                                 {\r
281                                         Assert.IsNotNull(dictionary);\r
282                                         File.Delete(filename);\r
283                                 }\r
284                         }\r
285                 }\r
286 \r
287                 [Test]\r
288                 public void SetOrdering()\r
289                 {\r
290                         using (Broker broker = new Broker())\r
291                         {\r
292                                 broker.SetOrdering("en_US", "aspell, myspell, ispell");\r
293                         }\r
294                 }\r
295 \r
296         [Test]\r
297         public void DictionaryKeepsBrokerAlive()\r
298         {\r
299             WeakReference brokerReference;\r
300             Dictionary dictionary = GetDictionaryAllowingBrokerToGoOutOfScope(out brokerReference);\r
301             GC.Collect();\r
302             GC.WaitForPendingFinalizers();\r
303             Assert.IsTrue(brokerReference.IsAlive);\r
304             GC.KeepAlive(dictionary);\r
305         }\r
306 \r
307             private static Dictionary GetDictionaryAllowingBrokerToGoOutOfScope(out WeakReference brokerReference) \r
308         {\r
309                 Broker broker = new Broker();\r
310             brokerReference = new WeakReference(broker);\r
311                 return broker.RequestDictionary("en_US");\r
312             }\r
313 \r
314             [Test]\r
315         public void Finalize_DictionaryGoesOutOfScope_Finalized()\r
316         {\r
317             using (Broker broker = new Broker())\r
318             {\r
319                 broker.CacheDictionaries = true;\r
320                 WeakReference dictionaryReference = GetDictionaryReference(broker);\r
321                 GC.Collect();\r
322                 GC.WaitForPendingFinalizers();\r
323                 Assert.IsFalse(dictionaryReference.IsAlive);\r
324             }\r
325         }\r
326 \r
327         //this will allow the dictionary object to go out of scope\r
328             private static WeakReference GetDictionaryReference(Broker broker) \r
329         {\r
330             Dictionary dictionary = broker.RequestDictionary("en_US");\r
331             return new WeakReference(dictionary);\r
332         }\r
333 \r
334         [Test]\r
335         public void Default_ReturnsNonNull()\r
336         {\r
337             Assert.IsNotNull(Broker.Default);\r
338         }\r
339 \r
340         [Test]\r
341         public void Default_CalledTwice_ReturnsSame()\r
342         {\r
343             Assert.AreSame(Broker.Default, Broker.Default);\r
344         }\r
345 \r
346         [Test]\r
347         public void Default_Disposed_ReturnsNonNull()\r
348         {\r
349             Broker.Default.Dispose();\r
350             Assert.IsNotNull(Broker.Default);\r
351         }\r
352 \r
353         [Test]\r
354         public void Default_Disposed_ReturnsNewObject()\r
355         {\r
356             Broker originalBroker = Broker.Default;\r
357             originalBroker.Dispose();\r
358 \r
359             Assert.AreNotSame(originalBroker, Broker.Default);\r
360         }\r
361 \r
362         }\r
363 }\r