PageRenderTime 38ms CodeModel.GetById 20ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

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

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