PageRenderTime 46ms CodeModel.GetById 29ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/test/xml/gui/VFSFileListTest.java

#
Java | 279 lines | 195 code | 59 blank | 25 comment | 0 complexity | 886ab468841af9ad29787b8dff878cce MD5 | raw file
  1/*
  2 * VFSFileListTest.java
  3 * :folding=explicit:collapseFolds=1:
  4 *
  5 * Copyright (C) 2010 Eric Le Lay
  6 *
  7 * The XML plugin is licensed under the GNU General Public License, with
  8 * the following exception:
  9 *
 10 * "Permission is granted to link this code with software released under
 11 * the Apache license version 1.1, for example used by the Xerces XML
 12 * parser package."
 13 */
 14package xml.gui;
 15
 16// {{{ jUnit imports 
 17import java.util.concurrent.TimeUnit;
 18
 19import org.junit.*;
 20import static org.junit.Assert.*;
 21import static org.junit.Assume.*;
 22
 23import org.fest.swing.fixture.*;
 24import org.fest.swing.core.*;
 25import org.fest.swing.data.TableCell;
 26import org.fest.swing.finder.*;
 27import org.fest.swing.edt.*;
 28import org.fest.swing.timing.*;
 29import org.fest.swing.core.matcher.JButtonMatcher;
 30
 31import static org.fest.assertions.Assertions.*;
 32
 33import org.gjt.sp.jedit.testframework.Log;
 34
 35import static org.gjt.sp.jedit.testframework.TestUtils.*;
 36import static org.gjt.sp.jedit.testframework.EBFixture.*;
 37import org.gjt.sp.jedit.testframework.PluginOptionsFixture;
 38import org.gjt.sp.jedit.testframework.TestUtils;
 39
 40// }}}
 41
 42import org.gjt.sp.jedit.jEdit;
 43import org.gjt.sp.jedit.EBMessage;
 44import org.gjt.sp.jedit.textarea.JEditTextArea;
 45import org.gjt.sp.jedit.Buffer;
 46
 47import java.io.*;
 48import java.util.regex.Pattern;
 49import javax.swing.text.*;
 50import javax.swing.*;
 51
 52import java.awt.event.KeyEvent;
 53import java.awt.event.InputEvent;
 54import org.gjt.sp.jedit.gui.CompletionPopup;
 55
 56/**
 57 * unit tests for VFSFileList
 58 * $Id: VFSFileListTest.java 21325 2012-03-11 11:03:44Z kerik-sf $
 59 */
 60public class VFSFileListTest{
 61	private static File testData;
 62	
 63    @BeforeClass
 64    public static void setUpjEdit() throws IOException{
 65        TestUtils.beforeClass();
 66        testData = new File(System.getProperty("test_data")).getCanonicalFile();
 67        assertTrue(testData.exists());
 68    }
 69    
 70    @AfterClass
 71    public static void tearDownjEdit() {
 72        TestUtils.afterClass();
 73    }
 74    
 75    @Test
 76    public void testEnabledDisabled() throws IOException{
 77    	
 78    	final VFSFileList selector = new VFSFileList(view(),"xml.translate.xml-inputs");
 79    	selector.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
 80    	
 81    	final FrameFixture frame = new FrameFixture(TestUtils.robot(),Containers.frameFor(selector));
 82    	frame.show();
 83    	
 84		frame.list("xml.translate.xml-inputs").requireEnabled();
 85		frame.button("xml.translate.xml-inputs.add").requireEnabled();
 86		
 87		GuiActionRunner.execute(new GuiTask(){
 88				protected void executeInEDT(){
 89					selector.setEnabled(false);
 90				}
 91		});
 92		frame.list("xml.translate.xml-inputs").requireDisabled();
 93		frame.button("xml.translate.xml-inputs.add").requireDisabled();
 94		
 95		GuiActionRunner.execute(new GuiTask(){
 96				protected void executeInEDT(){
 97					selector.setEnabled(true);
 98				}
 99		});
100		frame.list("xml.translate.xml-inputs").requireEnabled();
101		frame.button("xml.translate.xml-inputs.add").requireEnabled();
102
103		frame.close();
104    }
105    
106    
107    @Test
108    public void testAPI() throws IOException{
109    	final File f = new File(testData, "simple/actions.xml");
110    	
111    	final VFSFileList selector = new VFSFileList(view(),"xml.translate.xml-inputs");
112    	selector.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
113    	
114    	final FrameFixture frame = new FrameFixture(TestUtils.robot(),Containers.frameFor(selector));
115    	frame.show();
116    	
117		assertFalse(selector.itemsExist());
118		assertEquals(0,selector.getItemCount());
119		assertNull(selector.getSelectedItem());
120
121		GuiActionRunner.execute(new GuiTask(){
122				protected void executeInEDT(){
123					selector.setItems(new String[]{f.getPath()});
124				}
125		});
126		
127		assertThat(frame.list("xml.translate.xml-inputs").contents()).containsOnly(f.getPath());
128		assertTrue(selector.itemsExist());
129		assertEquals(1,selector.getItemCount());
130		assertThat(frame.list("xml.translate.xml-inputs").contents()).isEqualTo(selector.getItems());
131		assertNull(selector.getSelectedItem());
132		
133		try{
134			selector.getItem(2);
135			fail("Should throw an IllegalArgumentException");
136		}catch(IllegalArgumentException e){
137			//pass
138		}
139		try{
140			selector.getItem(-1);
141			fail("Should throw an IllegalArgumentException");
142		}catch(IllegalArgumentException e){
143			//pass
144		}
145		
146		frame.close();
147    }
148
149    @Test
150    public void testOpenFile(){
151    	final File f = new File(testData, "simple/actions.xml");
152    	final VFSFileList selector = new VFSFileList(view(),"xml.translate.xml-inputs");
153    	selector.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
154    	
155		assumeTrue(!view().getBuffer().getPath().equals(f.getPath()));
156
157    	final FrameFixture frame = new FrameFixture(TestUtils.robot(),Containers.frameFor(selector));
158    	frame.show();
159    	
160		GuiActionRunner.execute(new GuiTask(){
161				protected void executeInEDT(){
162					selector.setItems(new String[]{f.getPath()});
163				}
164		});
165		frame.list("xml.translate.xml-inputs").selectItem(0);
166		frame.list("xml.translate.xml-inputs").showPopupMenu().menuItemWithPath("Open file").click();
167		
168		assertEquals(f.getPath(),view().getBuffer().getPath());
169		
170		frame.close();
171		jEdit.closeBuffer(view(),view().getBuffer());
172    }
173    
174    @Test
175    public void testAddRemove(){
176    	final File xml = new File(testData, "simple/actions.xml");
177    	File xsd = new File(testData, "simple/actions.xsd");
178    	final VFSFileList selector = new VFSFileList(view(),"xml.translate.xml-inputs");
179    	selector.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
180    	final FrameFixture frame = new FrameFixture(TestUtils.robot(),Containers.frameFor(selector));
181     	frame.show();
182   	
183		GuiActionRunner.execute(new GuiTask(){
184				protected void executeInEDT(){
185					selector.setItems(new String[]{xml.getPath()});
186				}
187		});
188
189		frame.list("xml.translate.xml-inputs").showPopupMenu().menuItemWithPath("Add").click();
190		
191		DialogFixture browseDialog = findDialogByTitle("File Browser - Open");
192		Pause.pause(1000);
193		browseDialog.button("up").click();
194		Pause.pause(1000);
195		browseDialog.table("file").cell(
196			browseDialog.table("file").cell(xsd.getParentFile().getName())).doubleClick();
197		Pause.pause(1000);
198		browseDialog.table("file").selectCell(
199			browseDialog.table("file").cell(xsd.getName()));
200		browseDialog.button("ok").click();
201
202		assertThat(frame.list("xml.translate.xml-inputs").contents()).contains(xsd.getPath());
203		
204		frame.list("xml.translate.xml-inputs").selectItem(0);
205		
206		frame.button("xml.translate.xml-inputs.add").click();
207		
208		browseDialog = findDialogByTitle("File Browser - Open");
209		Pause.pause(1000);
210		browseDialog.button("up").click();
211		Pause.pause(1000);
212		browseDialog.table("file").cell(
213			browseDialog.table("file").cell(xml.getParentFile().getName())).doubleClick();
214		Pause.pause(1000);
215		browseDialog.table("file").selectCell(
216			browseDialog.table("file").cell(xml.getName()));
217		browseDialog.button("ok").click();
218
219		assertThat(frame.list("xml.translate.xml-inputs").contents()).contains(xml.getPath());
220		assertEquals(3,frame.list("xml.translate.xml-inputs").contents().length);
221		
222		frame.list("xml.translate.xml-inputs").selectItem(0);
223		frame.button("xml.translate.xml-inputs.remove").requireEnabled();
224		
225		frame.button("xml.translate.xml-inputs.remove").click();
226		frame.button("xml.translate.xml-inputs.remove").click();
227		frame.button("xml.translate.xml-inputs.remove").click();
228
229		frame.button("xml.translate.xml-inputs.remove").requireDisabled();
230		
231		frame.close();
232		jEdit.closeBuffer(view(),view().getBuffer());
233    }
234
235    @Test
236    public void testUpDown(){
237    	final File f = new File(testData, "simple/actions.xml");
238    	final File xsd = new File(testData, "simple/actions.xsd");
239
240    	final VFSFileList selector = new VFSFileList(view(),"xml.translate.xml-inputs");
241    	selector.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
242    	
243    	final FrameFixture frame = new FrameFixture(TestUtils.robot(),Containers.frameFor(selector));
244    	frame.show();
245    	
246		frame.button("xml.translate.xml-inputs.up").requireDisabled();
247		frame.button("xml.translate.xml-inputs.down").requireDisabled();
248    	
249    	
250		GuiActionRunner.execute(new GuiTask(){
251				protected void executeInEDT(){
252					selector.setItems(new String[]{f.getPath(),xsd.getPath()});
253				}
254		});
255		
256		//no selection
257		frame.button("xml.translate.xml-inputs.up").requireDisabled();
258		frame.button("xml.translate.xml-inputs.down").requireDisabled();
259		
260		// selected last item
261		frame.list("xml.translate.xml-inputs").selectItem(1);
262		frame.button("xml.translate.xml-inputs.up").requireEnabled();
263		frame.button("xml.translate.xml-inputs.down").requireDisabled();
264
265		// move it on top
266		frame.button("xml.translate.xml-inputs.up").click();
267		assertEquals(xsd.getPath(),selector.getItem(0));
268		assertEquals(f.getPath(),selector.getItem(1));
269		frame.button("xml.translate.xml-inputs.up").requireDisabled();
270		frame.button("xml.translate.xml-inputs.down").requireEnabled();
271
272		// move it down
273		frame.button("xml.translate.xml-inputs.down").click();
274		assertEquals(f.getPath(),selector.getItem(0));
275		assertEquals(xsd.getPath(),selector.getItem(1));
276		frame.button("xml.translate.xml-inputs.down").requireDisabled();
277    }
278
279}