KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > InteractionsPaneTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36
37 import java.io.File JavaDoc;
38 import edu.rice.cs.drjava.DrJavaTestCase;
39 import edu.rice.cs.drjava.config.FileOption;
40 import edu.rice.cs.drjava.model.GlobalModel;
41 import edu.rice.cs.drjava.model.repl.*;
42 import edu.rice.cs.drjava.model.repl.DummyInteractionsListener;
43 import edu.rice.cs.drjava.model.repl.InteractionsDJDocument;
44 import edu.rice.cs.drjava.model.repl.InteractionsDocument;
45 import edu.rice.cs.drjava.model.repl.InteractionsDocumentTest.TestBeep;
46 import edu.rice.cs.drjava.model.repl.InteractionsModel;
47 import edu.rice.cs.drjava.model.repl.InteractionsModelTest.TestInteractionsModel;
48 import edu.rice.cs.util.swing.Utilities;
49 import edu.rice.cs.util.text.EditDocumentException;
50 import edu.rice.cs.util.CompletionMonitor;
51
52 /** Test functions of InteractionsPane.
53  * @version $Id: InteractionsPaneTest.java 3903 2006-07-05 20:03:06Z rcartwright $
54  */

55 public final class InteractionsPaneTest extends DrJavaTestCase {
56
57   protected volatile InteractionsDJDocument _adapter;
58   protected volatile InteractionsModel _model;
59   protected volatile InteractionsDocument _doc;
60   protected volatile InteractionsPane _pane;
61   protected volatile InteractionsController _controller;
62
63   /** Setup method for each JUnit test case. */
64   public void setUp() throws Exception JavaDoc {
65     super.setUp();
66     _adapter = new InteractionsDJDocument();
67     _model = new TestInteractionsModel(_adapter);
68     _doc = _model.getDocument();
69     _pane = new InteractionsPane(_adapter) {
70       public int getPromptPos() {
71        return _model.getDocument().getPromptPos();
72       }
73     };
74     // Make tests silent
75
_pane.setBeep(new TestBeep());
76     _controller = new InteractionsController(_model, _adapter, _pane);
77 // System.err.println("_controller = " + _controller);
78
}
79
80   public void tearDown() throws Exception JavaDoc {
81 // _controller = null;
82
// _doc = null;
83
// _model = null;
84
// _pane = null;
85
// _adapter = null;
86
super.tearDown();
87   }
88
89   /** Tests that this.setUp() puts the caret in the correct position. */
90   public void testInitialPosition() {
91     assertEquals("Initial caret not in the correct position.", _pane.getCaretPosition(), _doc.getPromptPos());
92   }
93
94   /** Tests that moving the caret left when it's already at the prompt will cycle it to the end of the line. */
95   public void testCaretMovementCyclesWhenAtPrompt() throws EditDocumentException {
96     _doc.append("test text", InteractionsDocument.DEFAULT_STYLE);
97     Utilities.invokeAndWait(new Runnable JavaDoc() {
98       public void run() {
99         _controller.moveToPrompt();
100         _controller.moveLeftAction.actionPerformed(null);
101       }
102     });
103     assertEquals("Caret was not cycled when moved left at the prompt.", _doc.getLength(), _pane.getCaretPosition());
104   }
105
106   /** Tests that moving the caret right when it's already at the end will cycle it to the prompt. */
107   public void testCaretMovementCyclesWhenAtEnd() throws EditDocumentException {
108     _doc.append("test text", InteractionsDocument.DEFAULT_STYLE);
109     Utilities.invokeAndWait(new Runnable JavaDoc() {
110       public void run() {
111         _controller.moveToEnd();
112         _controller.moveRightAction.actionPerformed(null);
113       }
114     });
115     assertEquals("Caret was not cycled when moved right at the end.", _doc.getPromptPos(), _pane.getCaretPosition());
116   }
117
118   /** Tests that moving the caret left when it's before the prompt will cycle it to the prompt. */
119   public void testLeftBeforePromptMovesToPrompt() {
120     Utilities.invokeAndWait(new Runnable JavaDoc() {
121       public void run() {
122         _pane.setCaretPosition(1);
123         _controller.moveLeftAction.actionPerformed(null);
124       }
125     });
126     assertEquals("Left arrow doesn't move to prompt when caret is before prompt.",
127                  _doc.getPromptPos(),
128                  _pane.getCaretPosition());
129   }
130
131   /** Tests that moving the caret right when it's before the prompt will cycle it to the end of the document. */
132   public void testRightBeforePromptMovesToEnd() {
133     Utilities.invokeAndWait(new Runnable JavaDoc() {
134       public void run() {
135         _pane.setCaretPosition(1);
136         _controller.moveRightAction.actionPerformed(null);
137       }
138     });
139     assertEquals("Right arrow doesn't move to end when caret is before prompt.",
140                  _doc.getLength(),
141                  _pane.getCaretPosition());
142   }
143
144   /** Tests that moving the caret up (recalling the previous command from history) will move the caret to the end
145    * of the document.
146    */

147   public void testHistoryRecallPrevMovesToEnd() {
148     Utilities.invokeAndWait(new Runnable JavaDoc() {
149       public void run() {
150       _pane.setCaretPosition(1);
151       _controller.historyPrevAction.actionPerformed(null);
152       }
153     });
154     assertEquals("Caret not moved to end on up arrow.", _doc.getLength(), _pane.getCaretPosition());
155   }
156
157   /** Tests that moving the caret down (recalling the next command from history) will move the caret to the end of
158    * the document.
159    */

160   public void testHistoryRecallNextMovesToEnd() {
161     Utilities.invokeAndWait(new Runnable JavaDoc() {
162       public void run() {
163         _pane.setCaretPosition(1);
164         _controller.historyNextAction.actionPerformed(null);
165       }
166     });
167     assertEquals("Caret not moved to end on down arrow.", _doc.getLength(), _pane.getCaretPosition());
168   }
169
170   public void testCaretStaysAtEndDuringInteraction() throws EditDocumentException {
171     
172     _doc.setInProgress(true);
173 // System.err.println(_pane.getCaretPosition());
174
_doc.append("simulated output", InteractionsDocument.DEFAULT_STYLE);
175     Utilities.clearEventQueue();
176     _doc.setInProgress(false);
177 // System.err.println(_pane.getCaretPosition());
178
// System.err.println("Document = |" + _doc.getDocText(0, _doc.getLength()) + "|");
179
assertEquals("Caret is at the end after output while in progress.",
180                  _doc.getLength(),
181                  _pane.getCaretPosition());
182   }
183
184   /** Tests that the caret catches up to the prompt if it is before it and
185    * output is displayed.
186    */

187   public void testCaretMovesUpToPromptAfterInsert() throws EditDocumentException {
188     _doc.append("typed text", InteractionsDocument.DEFAULT_STYLE);
189     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(1); } });
190 // System.err.println("caretPostion = " + _pane.getCaretPosition());
191
_doc.insertBeforeLastPrompt("simulated output", InteractionsDocument.DEFAULT_STYLE);
192 // System.err.println("caretPostion = " + _pane.getCaretPosition());
193
Utilities.clearEventQueue();
194 // System.err.println("caretPostion = " + _pane.getCaretPosition());
195
assertEquals("Caret is at the prompt after output inserted.", _doc.getPromptPos(), _pane.getCaretPosition());
196
197     _doc.insertPrompt();
198     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(1); } });
199     _doc.insertBeforeLastPrompt("simulated output", InteractionsDocument.DEFAULT_STYLE);
200     Utilities.clearEventQueue();
201     assertEquals("Caret is at the end after output inserted.", _doc.getPromptPos(), _pane.getCaretPosition());
202   }
203
204   /**
205    * Tests that the caret is moved properly when the current interaction
206    * is cleared.
207    */

208   public void testClearCurrentInteraction() throws EditDocumentException {
209     _doc.append("typed text", InteractionsDocument.DEFAULT_STYLE);
210     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _controller.moveToEnd(); } });
211
212     _doc.clearCurrentInteraction();
213     Utilities.clearEventQueue();
214     assertEquals("Caret is at the prompt after output cleared.", _doc.getPromptPos(), _pane.getCaretPosition());
215     assertEquals("Prompt is at the end after output cleared.", _doc.getLength(), _doc.getPromptPos());
216   }
217
218   /** Tests that the InteractionsPane cannot be edited before the prompt. */
219   public void testCannotEditBeforePrompt() throws EditDocumentException {
220     _doc.acquireWriteLock();
221     int origLength = 0;
222     try {
223       origLength = _doc.getLength();
224       _doc.insertText(1, "typed text", InteractionsDocument.DEFAULT_STYLE);
225     }
226     finally { _doc.releaseWriteLock(); }
227     assertEquals("Document should not have changed.", origLength, _doc.getLength());
228   }
229
230   /** Tests that the caret is put in the correct position after an insert. */
231   public void testCaretUpdatedOnInsert() throws EditDocumentException {
232     _doc.append("typed text", InteractionsDocument.DEFAULT_STYLE);
233     final int pos = _doc.getLength() - 5;
234     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(pos); } });
235
236     // Insert text before the prompt
237
_doc.insertBeforeLastPrompt("aa", InteractionsDocument.DEFAULT_STYLE);
238      Utilities.clearEventQueue();
239     assertEquals("caret should be in correct position", pos + 2, _pane.getCaretPosition());
240
241     // Move caret to prompt and insert more text
242
Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(_doc.getPromptPos()); } });
243     _doc.insertBeforeLastPrompt("b", InteractionsDocument.DEFAULT_STYLE);
244     Utilities.clearEventQueue();
245     assertEquals("caret should be at prompt", _doc.getPromptPos(), _pane.getCaretPosition());
246
247     // Move caret before prompt and insert more text
248
Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(0); } });
249     _doc.insertBeforeLastPrompt("ccc", InteractionsDocument.DEFAULT_STYLE);
250     Utilities.clearEventQueue();
251     assertEquals("caret should be at prompt", _doc.getPromptPos(), _pane.getCaretPosition());
252
253     // Move caret after prompt and insert more text
254
final int newPos = _doc.getPromptPos();
255     // simulate a keystroke by putting caret just *after* pos of insert
256
Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(newPos+1); } });
257     _doc.insertText(newPos, "d", InteractionsDocument.DEFAULT_STYLE);
258     Utilities.clearEventQueue();
259     assertEquals("caret should be immediately after the d", newPos + 1, _pane.getCaretPosition());
260   }
261
262 // public void testSystemIn() {
263
// final Object lock = new Object();
264
// final StringBuffer buf = new StringBuffer();
265
// synchronized(_controller._inputEnteredAction) {
266
// new Thread("Testing System.in") {
267
// public void run() {
268
// synchronized(_controller._inputEnteredAction) {
269
// _controller._inputEnteredAction.notify();
270
// synchronized(lock) {
271
// buf.append(_controller._inputListener.getConsoleInput());
272
// }
273
// }
274
// }
275
// }.start();
276
// try {
277
// _controller._inputEnteredAction.wait();
278
// }
279
// catch (InterruptedException ie) {
280
// }
281
// }
282
// try {
283
// Thread.sleep(2000);
284
// }
285
// catch (InterruptedException ie) {
286
// }
287
// _controller._insertNewlineAction.actionPerformed(null);
288
// _controller._inputEnteredAction.actionPerformed(null);
289
// synchronized(lock) {
290
// assertEquals("Should have returned the correct text.", "\n\n", buf.toString());
291
// }
292
// }
293

294   public void testSystemIn() {
295     final Object JavaDoc bufLock = new Object JavaDoc();
296     final StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
297     
298     final CompletionMonitor completionMonitor = new CompletionMonitor();
299     
300     _controller.addConsoleStateListener(new InteractionsController.ConsoleStateListener() {
301       public void consoleInputStarted(InteractionsController c) {
302         completionMonitor.set();
303       }
304       public void consoleInputCompleted(String JavaDoc text, InteractionsController c) {
305         // do not assert the text here since it won't be called from the testing thread.
306
// It is called on the following thread that calls getConsoleInput()
307
}
308     });
309     
310     new Thread JavaDoc("Testing System.in") {
311       public void run() {
312         synchronized(bufLock) {
313           String JavaDoc s = _controller.getInputListener().getConsoleInput();
314           buf.append(s);
315         }
316       }
317     }.start();
318     
319     // Wait for console input to begin
320
completionMonitor.waitOne();
321         
322     _controller.insertConsoleText("test-text");
323     _controller.interruptConsoleInput();
324     
325     // Make sure the buffer 'buf' is updated
326
synchronized(bufLock) {
327       assertEquals("Should have returned the correct text.", "test-text\n", buf.toString());
328     }
329   }
330   
331   /** Fields used in a closure in testPromptList */
332   private volatile int _firstPrompt, _secondPrompt, _size;
333   private volatile boolean _resetDone;
334   
335   public void testPromptListClearedOnReset() throws Exception JavaDoc {
336     // Can't use the fields declared in setUp - it doesn't use a real InteractionsModel
337
final MainFrame _mf = new MainFrame();
338     final Object JavaDoc _resetLock = new Object JavaDoc();
339     
340     Utilities.clearEventQueue();
341     GlobalModel gm = _mf.getModel();
342     _controller = _mf.getInteractionsController();
343     _model = gm.getInteractionsModel();
344     _adapter = gm.getSwingInteractionsDocument();
345     _doc = gm.getInteractionsDocument();
346     _pane = _mf.getInteractionsPane();
347     
348     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.resetPrompts(); } });
349     
350     Utilities.clearEventQueue();
351
352 // System.err.println(_pane.getPromptList());
353
assertEquals("PromptList before insert should contain 0 elements", 0, _pane.getPromptList().size());
354         
355     // Insert some text
356
_doc.append("5", InteractionsDocument.NUMBER_RETURN_STYLE);
357
358     Utilities.invokeAndWait(new Runnable JavaDoc() { public void run() { _pane.setCaretPosition(_doc.getLength()); } });
359 // System.err.println(_pane.getPromptList());
360

361     Utilities.clearEventQueue();
362     
363     assertEquals("PromptList after insert should contain 1 element", 1, _pane.getPromptList().size());
364     assertEquals("First prompt should be saved as being at position",
365                  _model.getStartUpBanner().length() + InteractionsDocument.DEFAULT_PROMPT.length(),
366                  (int)_pane.getPromptList().get(0)); //needs cast to prevent ambiguity
367

368     _doc.insertPrompt();
369     Utilities.clearEventQueue();
370     
371     assertEquals("PromptList has length 2", 2, _pane.getPromptList().size());
372     
373     Utilities.invokeAndWait(new Runnable JavaDoc() {
374       public void run() {
375         _pane.setCaretPosition(_doc.getLength());
376         _firstPrompt = (int) _pane.getPromptList().get(0); // cast prevents ambiguity
377
_secondPrompt = (int) _pane.getPromptList().get(1); // cast prevents ambiguity
378
}
379     });
380     
381     assertEquals("PromptList after insertion of new prompt should contain 2 elements", 2, _pane.getPromptList().size());
382     assertEquals("First prompt should be saved as being at position",
383                  _model.getStartUpBanner().length() + InteractionsDocument.DEFAULT_PROMPT.length(),
384                  _firstPrompt);
385     assertEquals("Second prompt should be saved as being at position",
386                  _model.getStartUpBanner().length() + InteractionsDocument.DEFAULT_PROMPT.length() * 2 + 1,
387                  _secondPrompt);
388     
389     synchronized(_resetLock) { _resetDone = false; }
390     _model.addListener(new DummyInteractionsListener() {
391       public void interpreterReady(File JavaDoc wd) {
392         synchronized(_resetLock) {
393           _resetDone = true;
394           _resetLock.notifyAll();
395         }
396       }});
397       
398     _model.resetInterpreter(FileOption.NULL_FILE);
399  
400     /* Wait until reset has finished. */
401     synchronized(_resetLock) { while (! _resetDone) _resetLock.wait(); }
402     
403     Utilities.clearEventQueue();
404     
405     Utilities.invokeAndWait(new Runnable JavaDoc() {
406       public void run() { _size = _pane.getPromptList().size(); }
407     });
408     
409 // System.err.println(_pane.getPromptList());
410

411     assertEquals("PromptList after reset should contain one element", 1, _size);
412   }
413     
414 }
415
Popular Tags