Tizen 2.1 base
[external/enchant.git] / unittests / Enchant.Net.Tests / DictionaryTests.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 System.Threading;\r
26 using Microsoft.Win32;\r
27 using NUnit.Framework;\r
28 \r
29 namespace Enchant.Tests\r
30 {\r
31         [TestFixture]\r
32         public class DictionaryTests\r
33         {\r
34                 #region Setup/Teardown\r
35 \r
36                 [SetUp]\r
37                 public void Setup()\r
38                 {\r
39                         oldRegistryValue = (string) 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                         broker = new Broker();\r
44                         dictionary = broker.RequestDictionary("en_US");\r
45                 }\r
46 \r
47                 [TearDown]\r
48                 public void Teardown()\r
49                 {\r
50                         if (oldRegistryValue == null)\r
51                         {\r
52                                 Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Enchant").OpenSubKey("Config", true).DeleteValue(\r
53                                         "Data_Dir");\r
54                         }\r
55                         else\r
56                         {\r
57                                 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config",\r
58                                                                                                         "Data_Dir",\r
59                                                                                                         oldRegistryValue,\r
60                                                                                                         RegistryValueKind.String);\r
61                         }\r
62 \r
63                         dictionary.Dispose();\r
64                         broker.Dispose();\r
65                         while (Directory.Exists(tempdir))\r
66                         {\r
67                                 Directory.Delete(tempdir, true);\r
68                         }\r
69                 }\r
70 \r
71                 #endregion\r
72 \r
73                 private Broker broker;\r
74                 private Dictionary dictionary;\r
75                 private string tempdir;\r
76                 private string oldRegistryValue;\r
77 \r
78                 [TestFixtureSetUp]\r
79                 public void FixtureSetup()\r
80                 {\r
81                         string providerDir = Path.Combine(Path.Combine(\r
82                                                                  Directory.GetCurrentDirectory(), "lib"), "enchant");\r
83                         if (!Directory.Exists(providerDir))\r
84                         {\r
85                                 Directory.CreateDirectory(providerDir);\r
86                         }\r
87                         File.Copy("libenchant_ispell.dll",\r
88                                                                 Path.Combine(providerDir, "libenchant_ispell.dll"), true);\r
89                         File.Copy("libenchant_myspell.dll",\r
90                                                                 Path.Combine(providerDir, "libenchant_myspell.dll"), true);\r
91             InstallDictionary("myspell", new string[]{"en_US.aff", "en_US.dic"});\r
92                 }\r
93 \r
94         static private void InstallDictionary(string provider, IEnumerable<string> files)\r
95         {\r
96             string dictionarySourceDir =\r
97                     Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(\r
98                                              Directory.GetCurrentDirectory(), ".."), ".."),\r
99                                              "lib"), "share"),\r
100                                               "enchant"), provider);\r
101 \r
102             string dictionaryDestDir = Path.Combine(Path.Combine(Path.Combine(\r
103                                                     Directory.GetCurrentDirectory(), "share"), "enchant"),\r
104                                                     provider);\r
105 \r
106             if (!Directory.Exists(dictionaryDestDir))\r
107             {\r
108                 Directory.CreateDirectory(dictionaryDestDir);\r
109             }\r
110 \r
111             foreach (string file in files)\r
112             {\r
113                 File.Copy(Path.Combine(dictionarySourceDir, file),\r
114                           Path.Combine(dictionaryDestDir, file), true);\r
115 \r
116             }\r
117         }\r
118         \r
119                 [TestFixtureTearDown]\r
120                 public void FixtureTearDown()\r
121                 {\r
122                         Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "lib"), true);\r
123                         Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "share"), true);\r
124                 }\r
125 \r
126                 [Test]\r
127                 public void Add()\r
128                 {\r
129                         Assert.IsFalse(dictionary.Check("Googled"));\r
130                         dictionary.Add("Googled");\r
131                         Assert.IsTrue(dictionary.Check("Googled"));\r
132                 }\r
133 \r
134                 [Test]\r
135                 public void AddToSession(string word)\r
136                 {\r
137                         Assert.IsFalse(dictionary.Check("list"));\r
138                         dictionary.AddToSession("list");\r
139                         Assert.IsTrue(dictionary.Check("list"));\r
140                 }\r
141 \r
142                 [Test]\r
143                 public void Check()\r
144                 {\r
145                         Assert.IsTrue(dictionary.Check("hello"));\r
146                         Assert.IsFalse(dictionary.Check("helo"));\r
147                 }\r
148 \r
149                 [Test]\r
150                 public void Information()\r
151                 {\r
152                         DictionaryInfo info = dictionary.Information;\r
153                         Console.WriteLine("Language:{0}\tName:{1}\tDescription:{2}\tFile:{3}",\r
154                                                                                                 info.Language,\r
155                                                                                                 info.Provider.Name,\r
156                                                                                                 info.Provider.Description,\r
157                                                                                                 info.Provider.File);\r
158                         Assert.AreEqual("en_US", info.Language);\r
159                         Assert.IsNotEmpty(info.Provider.Name);\r
160                         Assert.IsNotEmpty(info.Provider.Description);\r
161                         Assert.IsNotEmpty(info.Provider.File);\r
162                 }\r
163 \r
164                 [Test]\r
165                 public void IsAdded()\r
166                 {\r
167                         Assert.IsFalse(dictionary.IsAdded("list"));\r
168                         dictionary.AddToSession("list");\r
169                         Assert.IsTrue(dictionary.IsAdded("list"));\r
170                 }\r
171 \r
172                 [Test]\r
173                 public void IsRemoved()\r
174                 {\r
175                         Assert.IsFalse(dictionary.IsRemoved("list"));\r
176                         dictionary.RemoveFromSession("list");\r
177                         Assert.IsTrue(dictionary.IsRemoved("list"));\r
178                 }\r
179 \r
180                 [Test]\r
181                 public void Remove()\r
182                 {\r
183                         Assert.IsTrue(dictionary.Check("world"));\r
184                         dictionary.Remove("world");\r
185                         Assert.IsFalse(dictionary.Check("world"));\r
186                 }\r
187 \r
188                 [Test]\r
189                 public void RemoveFromSession()\r
190                 {\r
191                         Assert.IsTrue(dictionary.Check("hello"));\r
192                         dictionary.RemoveFromSession("hello");\r
193                         Assert.IsFalse(dictionary.Check("hello"));\r
194                 }\r
195 \r
196                 [Test]\r
197                 public void StoreReplacement()\r
198                 {\r
199                         dictionary.StoreReplacement("theirs", "their's");\r
200                 }\r
201 \r
202                 [Test]\r
203                 public void Suggest()\r
204                 {\r
205                         List<string> suggestions = new List<string>(dictionary.Suggest("helo"));\r
206                         Assert.Contains("hello", suggestions);\r
207                 }\r
208 \r
209         [Test]\r
210         public void Dispose_Called_SendsDisposedEvent()\r
211         {\r
212             bool disposedEventCalled = false;\r
213             dictionary.Disposed += delegate\r
214                                    { disposedEventCalled = true; };\r
215             dictionary.Dispose();\r
216             Assert.IsTrue(disposedEventCalled);\r
217         }\r
218         }\r
219 }