PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/BufferLocal/test/test/BufferLocalTest.java

#
Java | 379 lines | 273 code | 56 blank | 50 comment | 12 complexity | 61c3f6208b2577be06f107bac6e68c6e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package test;
  2. import java.io.File;
  3. import static java.awt.event.InputEvent.*;
  4. import static java.awt.event.KeyEvent.*;
  5. import javax.swing.*;
  6. import org.junit.*;
  7. import static org.junit.Assert.*;
  8. import org.fest.swing.fixture.*;
  9. import org.fest.swing.core.*;
  10. import org.fest.swing.finder.WindowFinder;
  11. import org.fest.swing.edt.*;
  12. import org.fest.swing.timing.Pause;
  13. import org.gjt.sp.jedit.testframework.Log;
  14. import org.gjt.sp.jedit.testframework.TestUtils;
  15. import org.gjt.sp.jedit.*;
  16. import org.gjt.sp.jedit.msg.PropertiesChanged;
  17. import org.gjt.sp.jedit.options.PluginOptions;
  18. public class BufferLocalTest {
  19. @BeforeClass
  20. public static void setUpjEdit() {
  21. TestUtils.beforeClass();
  22. }
  23. @AfterClass
  24. public static void tearDownjEdit() {
  25. TestUtils.afterClass();
  26. }
  27. //@Test
  28. public void testOptionPane() {
  29. Log.log( "testOptionPane" );
  30. // set the jEdit properties for the option pane to known values
  31. jEdit.setBooleanProperty( "bufferlocal.removeStale", false );
  32. jEdit.setIntegerProperty( "bufferlocal.staleTime", 30 );
  33. // open the plugin options and select the options pane
  34. TestUtils.jEditFrame().menuItemWithPath( "Plugins", "Plugin Options..." ).click();
  35. DialogFixture optionsDialog = WindowFinder.findDialog( PluginOptions.class ).withTimeout( 5000 ).using( TestUtils.robot() );
  36. TestUtils.selectPath( optionsDialog.tree(), new String[] {"Plugins", "BufferLocal"} );
  37. JPanelFixture pane = optionsDialog.panel( "bufferlocal" );
  38. assertTrue( "BufferLocal option pane not found", pane != null );
  39. // test that the checkbox is unchecked and then check the checkbox
  40. JCheckBoxFixture checkbox = pane.checkBox();
  41. assertTrue( "Cannot find checkbox in BufferLocal option pane", checkbox != null );
  42. checkbox.requireNotSelected();
  43. checkbox.click();
  44. // check that the spinner has the right value, then spin it to 45
  45. JSpinnerFixture spinner = pane.spinner();
  46. assertTrue( "Cannot find spinner in BufferLocal option pane", spinner != null );
  47. spinner.requireValue( 30 );
  48. spinner.increment( 15 );
  49. // click the OK button on the plugin options dialog
  50. optionsDialog.button(
  51. new GenericTypeMatcher<JButton>( JButton.class ) {
  52. public boolean isMatching( JButton button ) {
  53. return "OK".equals( button.getText() );
  54. }
  55. }
  56. ).click();
  57. // wait a second to make sure jEdit has time to save the properties
  58. Pause.pause( 1000 );
  59. // test that the properties were set correctly
  60. boolean checked = jEdit.getBooleanProperty( "bufferlocal.removeStale", false );
  61. int time = jEdit.getIntegerProperty( "bufferlocal.staleTime", 30 );
  62. assertTrue( "Close files checkbox is not checked and it should be", checked );
  63. assertTrue( "Minutes spinner value is not 45 and it should be 45", time == 45 );
  64. }
  65. //@Test
  66. public void testBufferMinder() {
  67. // open 2 files, then wait. The first file opened should be automatically
  68. // closed after 1 minute.
  69. jEdit.setBooleanProperty( "bufferlocal.removeStale", true );
  70. jEdit.setIntegerProperty( "bufferlocal.staleTime", 1 );
  71. Pause.pause( 1000 );
  72. EditBus.send( new PropertiesChanged( null ) );
  73. try {
  74. File fileA = File.createTempFile( "bufferlocal", ".test" );
  75. final Buffer bufferA = TestUtils.newFile();
  76. final String textA =
  77. "* This program is free software; you can redistribute it and/or\n"
  78. + "* modify it under the terms of the GNU General Public License\n"
  79. + "* as published by the Free Software Foundation; either version 2\n"
  80. + "\n"
  81. + "* of the License, or any later version.";
  82. try {
  83. SwingUtilities.invokeAndWait(
  84. new Runnable() {
  85. public void run() {
  86. bufferA.insert( 0, textA );
  87. }
  88. }
  89. );
  90. }
  91. catch ( Exception e ) {
  92. fail( "Unable to insert text into buffer" );
  93. }
  94. bufferA.save( TestUtils.view(), fileA.getAbsolutePath() );
  95. File fileB = File.createTempFile( "bufferlocal", ".test" );
  96. final Buffer bufferB = TestUtils.newFile();
  97. final String textB = "Wait... this test takes 65 seconds";
  98. try {
  99. SwingUtilities.invokeAndWait(
  100. new Runnable() {
  101. public void run() {
  102. bufferB.insert( 0, textB );
  103. }
  104. }
  105. );
  106. }
  107. catch ( Exception e ) {
  108. fail( "Unable to insert text into buffer" );
  109. }
  110. bufferB.save( TestUtils.view(), fileB.getAbsolutePath() );
  111. // wait for BufferLocal to do its thing
  112. Pause.pause( 65000 );
  113. // check that the first buffer is closed
  114. Buffer bufferA2 = jEdit._getBuffer( fileA.getAbsolutePath() );
  115. assertTrue( "Buffer should be closed and it isn't", bufferA2 == null );
  116. }
  117. catch ( Exception e ) {
  118. e.printStackTrace();
  119. fail( "Unable to create temp file" );
  120. }
  121. }
  122. @Test
  123. public void testPauseBufferMinder() {
  124. // open 2 files, then wait. The first file opened should be automatically
  125. // closed after 1 minute.
  126. jEdit.setBooleanProperty( "bufferlocal.removeStale", true );
  127. jEdit.setIntegerProperty( "bufferlocal.staleTime", 1 );
  128. Pause.pause( 1000 );
  129. EditBus.send( new PropertiesChanged( null ) );
  130. try {
  131. File fileA = File.createTempFile( "bufferlocal", ".test" );
  132. final Buffer bufferA = TestUtils.newFile();
  133. final String textA =
  134. "* This program is free software; you can redistribute it and/or\n"
  135. + "* modify it under the terms of the GNU General Public License\n"
  136. + "* as published by the Free Software Foundation; either version 2\n"
  137. + "\n"
  138. + "* of the License, or any later version.";
  139. try {
  140. SwingUtilities.invokeAndWait(
  141. new Runnable() {
  142. public void run() {
  143. bufferA.insert( 0, textA );
  144. }
  145. }
  146. );
  147. }
  148. catch ( Exception e ) {
  149. fail( "Unable to insert text into buffer" );
  150. }
  151. bufferA.save( TestUtils.view(), fileA.getAbsolutePath() );
  152. File fileB = File.createTempFile( "bufferlocal", ".test" );
  153. final Buffer bufferB = TestUtils.newFile();
  154. final String textB = "Wait... this test takes 70 seconds";
  155. try {
  156. SwingUtilities.invokeAndWait(
  157. new Runnable() {
  158. public void run() {
  159. bufferB.insert( 0, textB );
  160. }
  161. }
  162. );
  163. }
  164. catch ( Exception e ) {
  165. fail( "Unable to insert text into buffer" );
  166. }
  167. bufferB.save( TestUtils.view(), fileB.getAbsolutePath() );
  168. // here is the real test -- wait 30 seconds, iconify for 5 seconds,
  169. // deiconify, then wait 30 more seconds. The 5 seconds of iconified
  170. // time should be added to the start time for the buffer, so it
  171. // should not be closed after the last 30 seconds. It should be closed
  172. // after after waiting an additional 5 seconds.
  173. Pause.pause( 30000 );
  174. TestUtils.jEditFrame().iconify();
  175. Pause.pause( 5000 );
  176. TestUtils.jEditFrame().deiconify();
  177. Pause.pause( 30000 );
  178. // check that the first buffer is not closed yet
  179. Buffer bufferA2 = jEdit._getBuffer( fileA.getAbsolutePath() );
  180. assertTrue( "Buffer not should be closed and it is", bufferA2 != null );
  181. Pause.pause( 5000 );
  182. // check that the first buffer is closed now
  183. bufferA2 = jEdit._getBuffer( fileA.getAbsolutePath() );
  184. assertTrue( "Buffer should be closed and it isn't", bufferA2 == null );
  185. }
  186. catch ( Exception e ) {
  187. e.printStackTrace();
  188. fail( "Unable to create temp file" );
  189. }
  190. }
  191. @Test
  192. public void testRestoreSettings() {
  193. // create a file, set some buffer properties, close the file, reopen
  194. // the file, then confirm the buffer properties were restored
  195. try {
  196. File fileA = File.createTempFile( "bufferlocal", ".test" );
  197. final Buffer bufferA = TestUtils.newFile();
  198. final String textA =
  199. "* This program is free software; you can redistribute it and/or\n"
  200. + "* modify it under the terms of the GNU General Public License\n"
  201. + "* as published by the Free Software Foundation; either version 2\n"
  202. + "\n"
  203. + "* of the License, or any later version.";
  204. try {
  205. SwingUtilities.invokeAndWait(
  206. new Runnable() {
  207. public void run() {
  208. bufferA.insert( 0, textA );
  209. }
  210. }
  211. );
  212. }
  213. catch ( Exception e ) {
  214. fail( "Unable to insert text into buffer" );
  215. }
  216. bufferA.save( TestUtils.view(), fileA.getAbsolutePath() );
  217. // change the buffer settings
  218. TestUtils.jEditFrame().menuItemWithPath( "Utilities", "Buffer Options..." ).click();
  219. DialogFixture bufferDialog = TestUtils.findDialogByTitle( "Buffer Options" );
  220. assertTrue( "Can't find buffer dialog", bufferDialog != null );
  221. // set these programatically rather than with the gui since none of the comboboxes
  222. // in BufferOptionPane have names
  223. bufferA.setStringProperty( Buffer.ENCODING, "UTF-16" );
  224. bufferA.setIntegerProperty( "maxLineLength", 40 );
  225. bufferA.setIntegerProperty( "tabSize", 7 );
  226. bufferA.setIntegerProperty( "indentSize", 11 );
  227. bufferA.setBooleanProperty( "noTabs", false );
  228. // close and reopen the buffer
  229. jEdit._closeBuffer( TestUtils.view(), bufferA );
  230. // give buffer local time to store settings for this buffer
  231. Pause.pause( 1000 );
  232. // reopen the buffer
  233. Buffer bufferA2 = jEdit.openFile( TestUtils.view(), fileA.getAbsolutePath() );
  234. // give buffer local time to restore the settings for this buffer
  235. Pause.pause( 1000 );
  236. // check the buffer settings
  237. String encoding = bufferA2.getStringProperty( Buffer.ENCODING );
  238. int linelength = bufferA2.getIntegerProperty( "maxLineLength", 0 );
  239. int tabsize = bufferA2.getIntegerProperty( "tabSize", 0 );
  240. int indentsize = bufferA2.getIntegerProperty( "indentSize", 0 );
  241. boolean notabs = bufferA2.getBooleanProperty( "noTabs", true );
  242. assertTrue( "encoding is not UTF-16, it is " + encoding, "UTF-16".equals( encoding ) );
  243. assertTrue( "linelength is not 40, it is " + linelength, linelength == 40 );
  244. assertTrue( "tabsize is not 7, it is " + tabsize, tabsize == 7 );
  245. assertTrue( "indentsize is not 11, it is " + indentsize, indentsize == 11 );
  246. assertTrue( "notabs is not false", notabs == false );
  247. }
  248. catch ( Exception e ) {
  249. e.printStackTrace();
  250. fail( "Unable to create temp file" );
  251. }
  252. }
  253. @Test
  254. public void testChangeEncoding() {
  255. // create a file, set the character encoding to UTF-16, save the file,
  256. // reload with ISO-8859-1, close the file, reopen and confirm encoding
  257. // is still ISO-8859-1.
  258. try {
  259. File fileA = File.createTempFile( "bufferlocal", ".test" );
  260. final Buffer bufferA = TestUtils.newFile();
  261. final String textA =
  262. "* This program is free software; you can redistribute it and/or\n"
  263. + "* modify it under the terms of the GNU General Public License\n"
  264. + "* as published by the Free Software Foundation; either version 2\n"
  265. + "\n"
  266. + "* of the License, or any later version.";
  267. try {
  268. SwingUtilities.invokeAndWait(
  269. new Runnable() {
  270. public void run() {
  271. bufferA.insert( 0, textA );
  272. }
  273. }
  274. );
  275. }
  276. catch ( Exception e ) {
  277. fail( "Unable to insert text into buffer" );
  278. }
  279. bufferA.save( TestUtils.view(), fileA.getAbsolutePath() );
  280. // set these programatically rather than with the gui since none of the comboboxes
  281. // in BufferOptionPane have names
  282. bufferA.setStringProperty( Buffer.ENCODING, "UTF-16" );
  283. // close and reopen the buffer
  284. jEdit._closeBuffer( TestUtils.view(), bufferA );
  285. // give buffer local time to store settings for this buffer
  286. Pause.pause( 1000 );
  287. // reopen the buffer
  288. Buffer bufferA2 = jEdit.openFile( TestUtils.view(), fileA.getAbsolutePath() );
  289. // check the buffer settings, encoding should still be UTF-16
  290. String encoding = bufferA2.getStringProperty( Buffer.ENCODING );
  291. assertTrue( "encoding is not UTF-16, it is " + encoding, "UTF-16".equals( encoding ) );
  292. // reload the buffer with ISO-8895-1 encoding
  293. // this doesn't work, I don't know why:
  294. //TestUtils.jEditFrame().menuItemWithPath( "File", "Reload with Encoding", "More", "More", "ISO-8859-1" ).click();
  295. JMenuItem menuItem = new JMenuItem( "ISO-8859-1" );
  296. menuItem.setActionCommand( "encoding@ISO-8859-1" );
  297. org.gjt.sp.jedit.menu.ReloadWithEncodingProvider provider = new org.gjt.sp.jedit.menu.ReloadWithEncodingProvider();
  298. provider.update(jEdit.getActiveView().getJMenuBar().getMenu(0));
  299. java.awt.event.ActionEvent event = new java.awt.event.ActionEvent( menuItem, 0, "encoding@ISO-8859-1" );
  300. provider.actionPerformed( event );
  301. // give buffer local time to restore the settings for this buffer
  302. Pause.pause( 5000 );
  303. // check the buffer settings
  304. bufferA2 = jEdit.getActiveView().getBuffer();
  305. encoding = bufferA2.getStringProperty( Buffer.ENCODING );
  306. assertTrue( "encoding is not ISO-8859, it is " + encoding, "ISO-8859-1".equals( encoding ) );
  307. // close and reopen the buffer
  308. jEdit._closeBuffer( TestUtils.view(), bufferA );
  309. // give buffer local time to store settings for this buffer
  310. Pause.pause( 1000 );
  311. // reopen the buffer
  312. bufferA2 = jEdit.openFile( TestUtils.view(), fileA.getAbsolutePath() );
  313. // check the buffer settings, encoding should still be ISO-8859-1
  314. encoding = bufferA2.getStringProperty( Buffer.ENCODING );
  315. assertTrue( "encoding is not ISO-8859-1, it is " + encoding, "ISO-8859-1".equals( encoding ) );
  316. Pause.pause( 5000 );
  317. }
  318. catch ( Exception e ) {
  319. e.printStackTrace();
  320. fail( e.getMessage() );
  321. }
  322. }
  323. }