1 /* Copyright (c) 2007 Eric Scott Albright
\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
10 * The above copyright notice and this permission notice shall be included in
\r
11 * all copies or substantial portions of the Software.
\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
23 using System.Collections.Generic;
\r
25 using Microsoft.Win32;
\r
26 using NUnit.Framework;
\r
28 namespace Enchant.Tests
\r
31 public class BrokerTests
\r
33 #region Setup/Teardown
\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
42 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config", "Data_Dir", tempdir, RegistryValueKind.String);
\r
46 public void Teardown()
\r
48 if (oldRegistryValue == null)
\r
50 Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Enchant").OpenSubKey("Config", true).DeleteValue(
\r
55 Registry.SetValue(@"HKEY_CURRENT_USER\Software\Enchant\Config",
\r
58 RegistryValueKind.String);
\r
60 while (Directory.Exists(tempdir))
\r
62 Directory.Delete(tempdir, true);
\r
68 private string tempdir;
\r
69 private string oldRegistryValue;
\r
72 public void FixtureSetup()
\r
74 string providerDir = Path.Combine(Path.Combine(
\r
75 Directory.GetCurrentDirectory(), "lib"), "enchant");
\r
76 if (!Directory.Exists(providerDir))
\r
78 Directory.CreateDirectory(providerDir);
\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
87 static private void InstallDictionary(string provider, IEnumerable<string> files)
\r
89 string dictionarySourceDir =
\r
90 Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(
\r
91 Directory.GetCurrentDirectory(), ".."), ".."),
\r
93 "enchant"), provider);
\r
95 string dictionaryDestDir = Path.Combine(Path.Combine(Path.Combine(
\r
96 Directory.GetCurrentDirectory(), "share"), "enchant"),
\r
99 if (!Directory.Exists(dictionaryDestDir))
\r
101 Directory.CreateDirectory(dictionaryDestDir);
\r
104 foreach (string file in files)
\r
106 File.Copy(Path.Combine(dictionarySourceDir, file),
\r
107 Path.Combine(dictionaryDestDir, file), true);
\r
112 [TestFixtureTearDown]
\r
113 public void FixtureTearDown()
\r
115 Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "lib"), true);
\r
116 Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "share"), true);
\r
120 public void Construct()
\r
122 Assert.IsNotNull(new Broker());
\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
130 using (Broker broker = new Broker())
\r
132 IEnumerable<DictionaryInfo> dictionaries = broker.Dictionaries;
\r
133 Assert.IsNotNull(dictionaries);
\r
135 foreach (DictionaryInfo info in dictionaries)
\r
137 Console.WriteLine("Language:{0}\tName:{1}\tDescription:{2}\tFile:{3}",
\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
148 Assert.AreEqual(1, count);
\r
153 public void DictionaryExists()
\r
155 using (Broker broker = new Broker())
\r
157 Assert.IsFalse(broker.DictionaryExists("qaa"));
\r
158 Assert.IsTrue(broker.DictionaryExists("en_US"));
\r
163 public void Providers()
\r
165 using (Broker broker = new Broker())
\r
167 IEnumerable<ProviderInfo> providers = broker.Providers;
\r
168 Assert.IsNotNull(providers);
\r
170 foreach (ProviderInfo info in providers)
\r
172 Console.WriteLine("Name:{0}\tDescription:{1}\tFile:{2}",
\r
176 Assert.IsNotEmpty(info.Name);
\r
177 Assert.IsNotEmpty(info.Description);
\r
178 Assert.IsNotEmpty(info.File);
\r
181 Assert.AreEqual(2, count);
\r
186 public void RequestDictionary()
\r
188 using (Broker broker = new Broker())
\r
190 Dictionary dictionary = broker.RequestDictionary("en_US");
\r
191 Assert.IsNotNull(dictionary);
\r
196 public void RequestDictionary_CachingEnabled_DictionaryReRequested_SameReference()
\r
198 using (Broker broker = new Broker())
\r
200 broker.CacheDictionaries = true;
\r
201 Dictionary dictionaryFirstRequest = broker.RequestDictionary("en_US");
\r
202 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
\r
204 Assert.AreSame(dictionaryFirstRequest, dictionarySecondRequest);
\r
209 public void RequestDictionary_CachingEnabled_DictionaryDisposedThenReRequested_DifferentReference()
\r
211 using (Broker broker = new Broker())
\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
218 Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
\r
223 public void RequestDictionary_CachingDisabled_DictionaryReRequested_DifferentReference()
\r
225 using (Broker broker = new Broker())
\r
227 broker.CacheDictionaries = false;
\r
228 Dictionary dictionaryFirstRequest = broker.RequestDictionary("en_US");
\r
229 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
\r
231 Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
\r
236 public void RequestDictionary_CachingDisabled_DictionaryDisposedThenReRequested_DifferentReference()
\r
238 using (Broker broker = new Broker())
\r
240 broker.CacheDictionaries = false;
\r
241 Dictionary dictionaryFirstRequest;
\r
242 using (dictionaryFirstRequest = broker.RequestDictionary("en_US"))
\r
245 Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
\r
247 Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
\r
252 [ExpectedException(typeof(ObjectDisposedException))]
\r
253 public void Dispose_UseDictionaryAfterBrokerDisposed_Throws()
\r
255 Dictionary dictionary;
\r
256 using (Broker broker = new Broker())
\r
258 dictionary = broker.RequestDictionary("en_US");
\r
260 DictionaryInfo info = dictionary.Information;
\r
264 [ExpectedException(typeof (ApplicationException))]
\r
265 public void RequestDictionary_DictionaryDoesNotExist_Throws()
\r
267 using (Broker broker = new Broker())
\r
269 broker.RequestDictionary("qaa");
\r
274 public void RequestPwlDictionary()
\r
276 string filename = Path.GetTempFileName();
\r
277 using (Broker broker = new Broker())
\r
279 using (Dictionary dictionary = broker.RequestPwlDictionary(filename))
\r
281 Assert.IsNotNull(dictionary);
\r
282 File.Delete(filename);
\r
288 public void SetOrdering()
\r
290 using (Broker broker = new Broker())
\r
292 broker.SetOrdering("en_US", "aspell, myspell, ispell");
\r
297 public void DictionaryKeepsBrokerAlive()
\r
299 WeakReference brokerReference;
\r
300 Dictionary dictionary = GetDictionaryAllowingBrokerToGoOutOfScope(out brokerReference);
\r
302 GC.WaitForPendingFinalizers();
\r
303 Assert.IsTrue(brokerReference.IsAlive);
\r
304 GC.KeepAlive(dictionary);
\r
307 private static Dictionary GetDictionaryAllowingBrokerToGoOutOfScope(out WeakReference brokerReference)
\r
309 Broker broker = new Broker();
\r
310 brokerReference = new WeakReference(broker);
\r
311 return broker.RequestDictionary("en_US");
\r
315 public void Finalize_DictionaryGoesOutOfScope_Finalized()
\r
317 using (Broker broker = new Broker())
\r
319 broker.CacheDictionaries = true;
\r
320 WeakReference dictionaryReference = GetDictionaryReference(broker);
\r
322 GC.WaitForPendingFinalizers();
\r
323 Assert.IsFalse(dictionaryReference.IsAlive);
\r
327 //this will allow the dictionary object to go out of scope
\r
328 private static WeakReference GetDictionaryReference(Broker broker)
\r
330 Dictionary dictionary = broker.RequestDictionary("en_US");
\r
331 return new WeakReference(dictionary);
\r
335 public void Default_ReturnsNonNull()
\r
337 Assert.IsNotNull(Broker.Default);
\r
341 public void Default_CalledTwice_ReturnsSame()
\r
343 Assert.AreSame(Broker.Default, Broker.Default);
\r
347 public void Default_Disposed_ReturnsNonNull()
\r
349 Broker.Default.Dispose();
\r
350 Assert.IsNotNull(Broker.Default);
\r
354 public void Default_Disposed_ReturnsNewObject()
\r
356 Broker originalBroker = Broker.Default;
\r
357 originalBroker.Dispose();
\r
359 Assert.AreNotSame(originalBroker, Broker.Default);
\r