1 33 34 package edu.rice.cs.drjava.ui; 35 36 37 import java.io.File ; 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 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 64 public void setUp() throws Exception { 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 _pane.setBeep(new TestBeep()); 76 _controller = new InteractionsController(_model, _adapter, _pane); 77 } 79 80 public void tearDown() throws Exception { 81 super.tearDown(); 87 } 88 89 90 public void testInitialPosition() { 91 assertEquals("Initial caret not in the correct position.", _pane.getCaretPosition(), _doc.getPromptPos()); 92 } 93 94 95 public void testCaretMovementCyclesWhenAtPrompt() throws EditDocumentException { 96 _doc.append("test text", InteractionsDocument.DEFAULT_STYLE); 97 Utilities.invokeAndWait(new Runnable () { 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 107 public void testCaretMovementCyclesWhenAtEnd() throws EditDocumentException { 108 _doc.append("test text", InteractionsDocument.DEFAULT_STYLE); 109 Utilities.invokeAndWait(new Runnable () { 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 119 public void testLeftBeforePromptMovesToPrompt() { 120 Utilities.invokeAndWait(new Runnable () { 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 132 public void testRightBeforePromptMovesToEnd() { 133 Utilities.invokeAndWait(new Runnable () { 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 147 public void testHistoryRecallPrevMovesToEnd() { 148 Utilities.invokeAndWait(new Runnable () { 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 160 public void testHistoryRecallNextMovesToEnd() { 161 Utilities.invokeAndWait(new Runnable () { 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 _doc.append("simulated output", InteractionsDocument.DEFAULT_STYLE); 175 Utilities.clearEventQueue(); 176 _doc.setInProgress(false); 177 assertEquals("Caret is at the end after output while in progress.", 180 _doc.getLength(), 181 _pane.getCaretPosition()); 182 } 183 184 187 public void testCaretMovesUpToPromptAfterInsert() throws EditDocumentException { 188 _doc.append("typed text", InteractionsDocument.DEFAULT_STYLE); 189 Utilities.invokeAndWait(new Runnable () { public void run() { _pane.setCaretPosition(1); } }); 190 _doc.insertBeforeLastPrompt("simulated output", InteractionsDocument.DEFAULT_STYLE); 192 Utilities.clearEventQueue(); 194 assertEquals("Caret is at the prompt after output inserted.", _doc.getPromptPos(), _pane.getCaretPosition()); 196 197 _doc.insertPrompt(); 198 Utilities.invokeAndWait(new Runnable () { 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 208 public void testClearCurrentInteraction() throws EditDocumentException { 209 _doc.append("typed text", InteractionsDocument.DEFAULT_STYLE); 210 Utilities.invokeAndWait(new Runnable () { 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 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 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 () { public void run() { _pane.setCaretPosition(pos); } }); 235 236 _doc.insertBeforeLastPrompt("aa", InteractionsDocument.DEFAULT_STYLE); 238 Utilities.clearEventQueue(); 239 assertEquals("caret should be in correct position", pos + 2, _pane.getCaretPosition()); 240 241 Utilities.invokeAndWait(new Runnable () { 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 Utilities.invokeAndWait(new Runnable () { 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 final int newPos = _doc.getPromptPos(); 255 Utilities.invokeAndWait(new Runnable () { 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 294 public void testSystemIn() { 295 final Object bufLock = new Object (); 296 final StringBuilder buf = new StringBuilder (); 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 text, InteractionsController c) { 305 } 308 }); 309 310 new Thread ("Testing System.in") { 311 public void run() { 312 synchronized(bufLock) { 313 String s = _controller.getInputListener().getConsoleInput(); 314 buf.append(s); 315 } 316 } 317 }.start(); 318 319 completionMonitor.waitOne(); 321 322 _controller.insertConsoleText("test-text"); 323 _controller.interruptConsoleInput(); 324 325 synchronized(bufLock) { 327 assertEquals("Should have returned the correct text.", "test-text\n", buf.toString()); 328 } 329 } 330 331 332 private volatile int _firstPrompt, _secondPrompt, _size; 333 private volatile boolean _resetDone; 334 335 public void testPromptListClearedOnReset() throws Exception { 336 final MainFrame _mf = new MainFrame(); 338 final Object _resetLock = new Object (); 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 () { public void run() { _pane.resetPrompts(); } }); 349 350 Utilities.clearEventQueue(); 351 352 assertEquals("PromptList before insert should contain 0 elements", 0, _pane.getPromptList().size()); 354 355 _doc.append("5", InteractionsDocument.NUMBER_RETURN_STYLE); 357 358 Utilities.invokeAndWait(new Runnable () { public void run() { _pane.setCaretPosition(_doc.getLength()); } }); 359 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)); 368 _doc.insertPrompt(); 369 Utilities.clearEventQueue(); 370 371 assertEquals("PromptList has length 2", 2, _pane.getPromptList().size()); 372 373 Utilities.invokeAndWait(new Runnable () { 374 public void run() { 375 _pane.setCaretPosition(_doc.getLength()); 376 _firstPrompt = (int) _pane.getPromptList().get(0); _secondPrompt = (int) _pane.getPromptList().get(1); } 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 wd) { 392 synchronized(_resetLock) { 393 _resetDone = true; 394 _resetLock.notifyAll(); 395 } 396 }}); 397 398 _model.resetInterpreter(FileOption.NULL_FILE); 399 400 401 synchronized(_resetLock) { while (! _resetDone) _resetLock.wait(); } 402 403 Utilities.clearEventQueue(); 404 405 Utilities.invokeAndWait(new Runnable () { 406 public void run() { _size = _pane.getPromptList().size(); } 407 }); 408 409 411 assertEquals("PromptList after reset should contain one element", 1, _size); 412 } 413 414 } 415 | Popular Tags |