/plugins/SpellCheck/tags/SpellCheckR007/src/test/cswilly/jeditPlugins/spell/SpellCheckPluginTest.java

# · Java · 489 lines · 342 code · 85 blank · 62 comment · 6 complexity · 0a1afcf58c0de0e4c93b1286264462e9 MD5 · raw file

  1. /*
  2. * $Revision: 20171 $
  3. * $Date: 2011-10-30 10:05:25 +0100 (Sun, 30 Oct 2011) $
  4. * $Author: kerik-sf $
  5. *
  6. * Copyright (C) 2008 Eric Le Lay
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package cswilly.jeditPlugins.spell;
  23. //{{{ Imports
  24. //{{{ Java Classpath
  25. import javax.swing.*;
  26. import javax.swing.tree.TreePath;
  27. import java.util.concurrent.atomic.AtomicReference;
  28. //}}}
  29. //{{{ jEdit
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.jedit.options.PluginOptions;
  32. import org.gjt.sp.jedit.gui.EnhancedDialog;
  33. import org.gjt.sp.jedit.textarea.Selection;
  34. //}}}
  35. //{{{ junit
  36. //annotations
  37. import org.junit.*;
  38. //usual classes
  39. import static org.junit.Assert.*;
  40. //}}}
  41. //{{{ FEST...
  42. import org.fest.swing.fixture.*;
  43. import org.fest.swing.core.*;
  44. import org.fest.swing.finder.WindowFinder;
  45. import org.fest.swing.timing.Pause;
  46. import org.fest.swing.edt.*;
  47. //}}}
  48. import cswilly.spell.ValidationDialog;
  49. import cswilly.spell.SpellException;
  50. //{{{ FEST...
  51. import errorlist.ErrorSource;
  52. import errorlist.ErrorSourceUpdate;
  53. //}}}
  54. ///}}}
  55. import org.gjt.sp.jedit.testframework.*;
  56. import org.gjt.sp.jedit.testframework.TestUtils;
  57. import static org.gjt.sp.jedit.testframework.TestUtils.*;
  58. import static cswilly.jeditPlugins.spell.TestUtils.ENV_ASPELL_EXE;
  59. /**
  60. * test high-level functions of the plugin:
  61. * - Option Pane
  62. * - Spell-check on save
  63. * - interactive spell-checking
  64. * - stop()
  65. * - spellCheck with lang...
  66. * - set buffer's language
  67. */
  68. public class SpellCheckPluginTest
  69. {
  70. @BeforeClass
  71. public static void setUpjEdit(){
  72. System.err.println("beforeClass");
  73. TestUtils.beforeClass();
  74. }
  75. @AfterClass
  76. public static void tearDownjEdit(){
  77. System.err.println("afterClass");
  78. TestUtils.afterClass();
  79. }
  80. @Test
  81. public void testOptions(){
  82. System.err.println("testOptions");
  83. PluginOptionsFixture optionsDialog = TestUtils.pluginOptions();
  84. //TestUtils.selectPath(optionsDialog.tree(),new String[]{"Plugins","Spell Check","General"});
  85. JPanelFixture pane = optionsDialog.optionPane("Spell Check/General","spellcheck.general");
  86. optionsDialog.close();
  87. //fail("I don't want to succeed");
  88. //don't do that, as it quits the VM
  89. //TestUtils.jeditFrame().menuItem(AbstractButtonTextMatcher.withText(JMenuItem.class,"Exit")).select();
  90. // try{
  91. // runJeditThread.join(10000);
  92. // }catch(InterruptedException ie){
  93. // fail("don't interrupt me !");
  94. // }
  95. // assertTrue(!runJeditThread.isAlive());
  96. }
  97. @Test
  98. public void testInteractiveSpellCheck(){
  99. System.err.println("testInteractiveSpellCheck");
  100. String exePath = System.getProperty(ENV_ASPELL_EXE);
  101. assertTrue("Forgot to set env. variable '"+ENV_ASPELL_EXE+"'",exePath!=null);
  102. jEdit.setProperty(AspellEngineManager.ASPELL_EXE_PROP,exePath);
  103. jEdit.setProperty(SpellCheckPlugin.MAIN_LANGUAGE_PROP,"en");
  104. jEdit.setProperty(SpellCheckPlugin.ENGINE_MANAGER_PROP,"Aspell");
  105. final View view = TestUtils.view();
  106. final Buffer buff = TestUtils.newFile();
  107. buff.insert(0,"The quick brown foxe");
  108. //Pause.pause(120000);
  109. Thread spellThread = new Thread(){
  110. public void run(){
  111. SpellCheckPlugin.checkBuffer(view,buff);
  112. }
  113. };
  114. spellThread.start();
  115. DialogFixture spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(5000).using(TestUtils.robot());
  116. spellDialog.list().selectItem("fox");
  117. spellDialog.button("Change").click();
  118. try{
  119. spellThread.join(5000);
  120. }catch(InterruptedException ie){}
  121. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  122. assertEquals("The quick brown fox",buff.getText(0,buff.getLength()));
  123. }
  124. @Test
  125. public void testShowCustomLangSpellDialog(){
  126. System.err.println("testShowCustomLangSpellDialog");
  127. String exePath = System.getProperty(ENV_ASPELL_EXE);
  128. assertTrue("Forgot to set env. variable '"+ENV_ASPELL_EXE+"'",exePath!=null);
  129. jEdit.setProperty(AspellEngineManager.ASPELL_EXE_PROP,exePath);
  130. jEdit.setProperty(SpellCheckPlugin.MAIN_LANGUAGE_PROP,"en-w_accents");
  131. final Buffer buff = TestUtils.newFile();
  132. System.err.println("hello");
  133. GuiActionRunner.execute(new GuiTask(){
  134. protected void executeInEDT(){
  135. buff.setProperty(SpellCheckPlugin.BUFFER_LANGUAGE_PROP,"en");
  136. buff.insert(0,"Les licences de la plupart des logiciels\nsont concues pour vous enlever toute libertée");
  137. }
  138. });
  139. final AtomicReference<SpellException> except = new AtomicReference<SpellException>(null);
  140. Thread spellThread = new Thread(){
  141. public void run(){
  142. try{
  143. SpellCheckPlugin.showCustomLangSpellDialog(TestUtils.view(),buff);
  144. }catch(SpellException spe){
  145. except.set(spe);
  146. spe.printStackTrace(System.err);
  147. }
  148. }
  149. };
  150. spellThread.start();
  151. DialogFixture langDialog = WindowFinder.findDialog(EnhancedDialog.class).withTimeout(5000).using(TestUtils.robot());
  152. Pause.pause(1000);//let dictionaries be loaded
  153. langDialog.comboBox().selectItem("fr");
  154. langDialog.button(org.fest.swing.core.matcher.JButtonMatcher.withText("OK")).click();
  155. DialogFixture spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(20000).using(TestUtils.robot());
  156. Pause.pause(1000);//let dictionaries be loaded
  157. assertEquals("fr",buff.getProperty(SpellCheckPlugin.BUFFER_LANGUAGE_PROP));
  158. spellDialog.list().selectItem("conçues");
  159. spellDialog.button("Change").click();
  160. Pause.pause(1000);//let dictionaries be loaded
  161. spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(5000).using(TestUtils.robot());
  162. spellDialog.list().selectItem("liberté");
  163. spellDialog.button("Change").click();
  164. try{spellThread.join(5000);}catch(InterruptedException ie){}
  165. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  166. assertEquals(null,except.get());
  167. assertEquals("Les licences de la plupart des logiciels\nsont conçues pour vous enlever toute liberté",buff.getText(0,buff.getLength()));
  168. assertEquals("en",buff.getProperty(SpellCheckPlugin.BUFFER_LANGUAGE_PROP));
  169. }
  170. @Test
  171. public void testSpellCheckOnSave(){
  172. System.err.println("testSpellCheckOnSave");
  173. String testsDir = System.getProperty(ENV_TESTS_DIR);
  174. assertTrue("Forgot to set env. variable '"+ENV_TESTS_DIR+"'",testsDir!=null);
  175. String exePath = System.getProperty(ENV_ASPELL_EXE);
  176. assertTrue("Forgot to set env. variable '"+ENV_ASPELL_EXE+"'",exePath!=null);
  177. String path = testsDir+"/spellTest.txt";
  178. jEdit.setProperty(AspellEngineManager.ASPELL_EXE_PROP,exePath);
  179. jEdit.setProperty(SpellCheckPlugin.MAIN_LANGUAGE_PROP,"en");
  180. jEdit.setBooleanProperty(SpellCheckPlugin.SPELLCHECK_ON_SAVE_PROP,true);
  181. jEdit.setProperty(SpellCheckPlugin.ENGINE_MANAGER_PROP,"Aspell");
  182. TestUtils.openFile(path);
  183. TestUtils.jEditFrame().menuItemWithPath("File","Save").click();
  184. final AtomicReference<Boolean> atr = new AtomicReference<Boolean>(Boolean.FALSE);
  185. EBComponent eb = new EBComponent(){
  186. public void handleMessage(EBMessage ebm){
  187. if(ebm instanceof ErrorSourceUpdate){
  188. System.err.println("got error update : "+ebm);
  189. if(ErrorSourceUpdate.ERROR_SOURCE_ADDED.equals(((ErrorSourceUpdate)ebm).getWhat()))
  190. atr.set(Boolean.TRUE);
  191. }
  192. }
  193. };
  194. EditBus.addToBus(eb);
  195. Pause.pause(10000);
  196. assertEquals(Boolean.TRUE,atr.get());
  197. ErrorSource[] sources = ErrorSource.getErrorSources();
  198. ErrorSource spellError = null;
  199. for(int i=0;i<sources.length;i++){
  200. if(sources[i] instanceof ErrorListValidator){
  201. spellError = sources[i];
  202. }
  203. }
  204. assertNotNull(spellError);
  205. assertTrue(6==spellError.getFileErrorCount(path));
  206. EditBus.removeFromBus(eb);
  207. }
  208. @Test
  209. public void testStop(){
  210. System.err.println("testStop");
  211. final PluginJAR jar = jEdit.getPlugin(SpellCheckPlugin.class.getName()).getPluginJAR();
  212. try{
  213. SwingUtilities.invokeAndWait(
  214. new Runnable(){
  215. public void run(){
  216. jar.deactivatePlugin(false);
  217. }
  218. });
  219. }catch(Exception ie){}
  220. ErrorSource[] sources = ErrorSource.getErrorSources();
  221. ErrorSource spellError = null;
  222. for(int i=0;i<sources.length;i++){
  223. if(sources[i] instanceof ErrorListValidator){
  224. spellError = sources[i];
  225. }
  226. }
  227. assertNull(spellError);
  228. }
  229. @Test
  230. public void testMultipleBuffers(){
  231. System.err.println("testMultipleBuffers");
  232. String exePath = System.getProperty(ENV_ASPELL_EXE);
  233. assertTrue("Forgot to set env. variable '"+ENV_ASPELL_EXE+"'",exePath!=null);
  234. jEdit.setProperty(AspellEngineManager.ASPELL_EXE_PROP,exePath);
  235. jEdit.setProperty(SpellCheckPlugin.MAIN_LANGUAGE_PROP,"en");
  236. jEdit.setProperty(SpellCheckPlugin.ENGINE_MANAGER_PROP,"Aspell");
  237. final View view = TestUtils.view();
  238. TestUtils.newFile();
  239. final Buffer buffer1 = view.getBuffer();
  240. try{
  241. SwingUtilities.invokeAndWait(new Runnable(){
  242. public void run(){
  243. buffer1.insert(0,"The wiek comes to an end");
  244. }
  245. });
  246. }catch(Exception ie){}
  247. TestUtils.newFile();
  248. try{
  249. SwingUtilities.invokeAndWait(new Runnable(){
  250. public void run(){
  251. view.splitHorizontally();
  252. }
  253. });
  254. }catch(Exception ie){}
  255. final String oldText1 = buffer1.getText(0,buffer1.getLength());
  256. final Buffer buffer2 = buffer1.getNext();
  257. Pause.pause(2000);
  258. GuiActionRunner.execute(new GuiTask(){
  259. protected void executeInEDT(){
  260. view.getEditPanes()[0].setBuffer(buffer1);
  261. EditPane pane2 = view.getEditPanes()[1];
  262. pane2.setBuffer(buffer2);
  263. buffer2.insert(0,"The qwick brown foxe");
  264. pane2.getTextArea().setSelection(new Selection.Range(0,10));
  265. }
  266. });
  267. //select the second pane and spell-check in the first
  268. //(throws an exception)
  269. final AtomicReference<Boolean> except = new AtomicReference<Boolean>(Boolean.FALSE);
  270. Thread spellThread = new Thread(){
  271. public void run(){
  272. try{
  273. SpellCheckPlugin.checkBuffer(view,buffer1);
  274. }catch(IllegalArgumentException iae){
  275. except.set(Boolean.TRUE);
  276. try{
  277. SpellCheckPlugin.checkBuffer(view,buffer2);
  278. }catch(Exception e){
  279. e.printStackTrace(System.err);
  280. }
  281. }
  282. }
  283. };
  284. spellThread.start();
  285. DialogFixture spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(5000).using(TestUtils.robot());
  286. assertEquals("SpellCheckPlugin didn't throw an exception on view/buffer mismatch",Boolean.TRUE,except.get());
  287. Pause.pause(1000);
  288. spellDialog.list().selectItem("quick");
  289. spellDialog.button("Change").click();
  290. //update : foxe not spell-checked as it's out of selection
  291. try{
  292. spellThread.join(5000);
  293. }catch(InterruptedException ie){}
  294. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  295. assertEquals("The quick brown foxe",buffer2.getText(0,buffer2.getLength()));
  296. assertEquals("The wiek comes to an end",buffer1.getText(0,buffer1.getLength()));
  297. }
  298. @Test
  299. public void testMarkupModes(){
  300. System.err.println("testMarkupModes");
  301. String exePath = System.getProperty(ENV_ASPELL_EXE);
  302. assertTrue("Forgot to set env. variable '"+ENV_ASPELL_EXE+"'",exePath!=null);
  303. String testDir = System.getProperty(ENV_TESTS_DIR);
  304. assertTrue("Forgot to set env. variable '"+ENV_TESTS_DIR+"'",testDir!=null);
  305. jEdit.setProperty(AspellEngineManager.ASPELL_EXE_PROP,exePath);
  306. jEdit.setProperty(SpellCheckPlugin.MAIN_LANGUAGE_PROP,"en");
  307. final View view = TestUtils.view();
  308. Pause.pause(5000);
  309. view.unsplit();
  310. final Buffer buff = TestUtils.openFile(testDir+"/latex-file.tex");
  311. //with "none" filter
  312. jEdit.setProperty(AspellEngineManager.ASPELL_MARKUP_MODE_PROP,
  313. AspellEngineManager.AspellMarkupMode.NO_MARKUP_MODE.toString());
  314. Thread spellThread = new Thread(){
  315. public void run(){
  316. SpellCheckPlugin.checkBuffer(view,buff);
  317. }
  318. };
  319. spellThread.start();
  320. DialogFixture spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(10000).using(TestUtils.robot());
  321. spellDialog.textBox("originalWord").requireText("documentclass");
  322. spellDialog.button("Cancel").click();
  323. try{
  324. spellThread.join(5000);
  325. }catch(InterruptedException ie){}
  326. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  327. //with manual markup mode
  328. jEdit.setProperty(AspellEngineManager.ASPELL_MARKUP_MODE_PROP,
  329. AspellEngineManager.AspellMarkupMode.MANUAL_MARKUP_MODE.toString());
  330. assertEquals("tex",
  331. jEdit.getProperty(AspellEngineManager.FILTERS_PROP+".latex"));
  332. spellThread = new Thread(){
  333. public void run(){
  334. SpellCheckPlugin.checkBuffer(view,buff);
  335. }
  336. };
  337. spellThread.start();
  338. spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(5000).using(TestUtils.robot());
  339. spellDialog.textBox("originalWord").requireText("mispelled");
  340. spellDialog.button("Cancel").click();
  341. try{
  342. spellThread.join(5000);
  343. }catch(InterruptedException ie){}
  344. }
  345. @Test
  346. public void testIgnoreAll(){
  347. System.err.println("testIgnoreAll");
  348. String exePath = System.getProperty(ENV_ASPELL_EXE);
  349. assertTrue("Forgot to set env. variable '"+ENV_ASPELL_EXE+"'",exePath!=null);
  350. String testDir = System.getProperty(ENV_TESTS_DIR);
  351. assertTrue("Forgot to set env. variable '"+ENV_TESTS_DIR+"'",testDir!=null);
  352. jEdit.setProperty(AspellEngineManager.ASPELL_EXE_PROP,exePath);
  353. jEdit.setProperty(SpellCheckPlugin.MAIN_LANGUAGE_PROP,"en");
  354. final View view = TestUtils.view();
  355. final Buffer buffer = TestUtils.newFile();
  356. GuiActionRunner.execute(new GuiTask(){
  357. protected void executeInEDT(){
  358. buffer.insert(0,"The wiek comes to an end\nIndeed it's the end of the wiek");
  359. }});
  360. final String oldText = buffer.getText(0,buffer.getLength());
  361. Pause.pause(1000);
  362. Thread spellThread = new Thread(){
  363. public void run(){
  364. SpellCheckPlugin.checkBuffer(view,buffer);
  365. }
  366. };
  367. spellThread.start();
  368. DialogFixture spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(10000).using(TestUtils.robot());
  369. spellDialog.textBox("originalWord").requireText("wiek");
  370. spellDialog.button("Ignore All").click();
  371. try{
  372. spellThread.join(5000);
  373. }catch(InterruptedException ie){}
  374. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  375. assertEquals(oldText,buffer.getText(0,buffer.getLength()));
  376. //persistent accross invocations ?
  377. spellThread = new Thread(){
  378. public void run(){
  379. SpellCheckPlugin.checkBuffer(view,buffer);
  380. }
  381. };
  382. spellThread.start();
  383. try{
  384. spellThread.join(5000);
  385. }catch(InterruptedException ie){}
  386. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  387. //test clear...
  388. TestUtils.jEditFrame().menuItemWithPath("Plugins","Spell Check","Clear Ignored Words").click();
  389. spellThread = new Thread(){
  390. public void run(){
  391. SpellCheckPlugin.checkBuffer(view,buffer);
  392. }
  393. };
  394. spellThread.start();
  395. spellDialog = WindowFinder.findDialog(ValidationDialog.class).withTimeout(10000).using(TestUtils.robot());
  396. spellDialog.textBox("originalWord").requireText("wiek");
  397. spellDialog.button("Cancel").click();
  398. try{
  399. spellThread.join(5000);
  400. }catch(InterruptedException ie){}
  401. assertTrue("spell-checking didn't finish", !spellThread.isAlive());
  402. assertEquals(oldText,buffer.getText(0,buffer.getLength()));
  403. }
  404. }