Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
|
43 |
package org.openide.text; |
44 |
|
45 |
|
46 |
|
47 |
import java.beans.PropertyChangeListener; |
48 |
import java.io.IOException; |
49 |
import java.util.ArrayList; |
50 |
import javax.swing.text.*; |
51 |
import org.netbeans.junit.*; |
52 |
import org.openide.awt.UndoRedo; |
53 |
import org.openide.util.Exceptions; |
54 |
|
55 |
/** |
56 |
* Bug 21237 - Redo should be possible after save. |
57 |
* |
58 |
* @author Ernie Rael |
59 |
*/ |
60 |
public class UndoRedoAfterSaveTest extends NbTestCase implements CloneableEditorSupport.Env { |
61 |
static { |
62 |
System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); |
63 |
} |
64 |
/** the support to work with */ |
65 |
private CES support; |
66 |
// Env variables |
67 |
private String content = "Hello"; |
68 |
private boolean valid = true; |
69 |
private boolean modified = false; |
70 |
/** if not null contains message why this document cannot be modified */ |
71 |
private String cannotBeModified; |
72 |
private java.util.Date date = new java.util.Date (); |
73 |
private java.util.List<PropertyChangeListener> propL = new ArrayList<PropertyChangeListener>(); |
74 |
private java.beans.VetoableChangeListener vetoL; |
75 |
|
76 |
/** Creates new TextTest */ |
77 |
public UndoRedoAfterSaveTest (String s) { |
78 |
super(s); |
79 |
} |
80 |
|
81 |
protected javax.swing.text.EditorKit createEditorKit() { |
82 |
return new NbLikeEditorKit(); |
83 |
} |
84 |
|
85 |
UndoRedo.Manager ur() { |
86 |
return support.getUndoRedo(); |
87 |
} |
88 |
|
89 |
@Override |
90 |
protected void setUp () { |
91 |
support = new CES (this, org.openide.util.Lookup.EMPTY); |
92 |
} |
93 |
|
94 |
public void testRedoAfterSave() throws Exception { |
95 |
content = ""; |
96 |
StyledDocument d = support.openDocument(); |
97 |
d.insertString(d.getLength(), "a", null); |
98 |
|
99 |
d.insertString(d.getLength(), "b", null); |
100 |
|
101 |
assertEquals("insert: data", "ab", d.getText(0, d.getLength())); |
102 |
assertTrue("insert: modified", support.isModified()); |
103 |
assertTrue("insert: can undo", ur().canUndo()); |
104 |
assertFalse("insert: no redo", ur().canRedo()); |
105 |
|
106 |
ur().undo(); |
107 |
assertEquals("undo: data", "a", d.getText(0, d.getLength())); |
108 |
assertTrue("undo: modified", support.isModified()); |
109 |
assertTrue("undo: can undo", ur().canUndo()); |
110 |
assertTrue("undo: can redo", ur().canRedo()); |
111 |
|
112 |
support.saveDocument (); |
113 |
assertFalse("save: not modified", support.isModified()); |
114 |
assertTrue("save: can undo", ur().canUndo()); |
115 |
assertTrue("save: can redo", ur().canRedo()); |
116 |
|
117 |
ur().redo(); |
118 |
assertEquals("redo: data", "ab", d.getText(0, d.getLength())); |
119 |
assertTrue("redo: modified", support.isModified()); |
120 |
assertTrue("redo: can undo", ur().canUndo()); |
121 |
assertFalse("redo: no redo", ur().canRedo()); |
122 |
} |
123 |
|
124 |
public void testUndoAfterSaveSimple() throws Exception { |
125 |
content = ""; |
126 |
StyledDocument d = support.openDocument(); |
127 |
d.insertString(d.getLength(), "a", null); |
128 |
|
129 |
support.saveDocument (); |
130 |
assertFalse("save: not modified", support.isModified()); |
131 |
assertTrue("save: can undo", ur().canUndo()); |
132 |
assertFalse("save: no redo", ur().canRedo()); |
133 |
|
134 |
ur().undo(); |
135 |
assertEquals("undo1, before savePoint: data", "", d.getText(0, d.getLength())); |
136 |
|
137 |
// CES BUG??? |
138 |
assertTrue("undo1, before savePoint: modified", support.isModified()); |
139 |
assertFalse("undo1, before savePoint: can undo", ur().canUndo()); |
140 |
assertTrue("undo1, before savePoint: can redo", ur().canRedo()); |
141 |
} |
142 |
|
143 |
// |
144 |
// Explore the failing situation |
145 |
// |
146 |
// The first undo() after save fails isModified() and canUndo() fail. |
147 |
// After another undo, only isModified() fails. |
148 |
// |
149 |
public void testUndoAfterSaveExplore() throws Exception { |
150 |
content = ""; |
151 |
StyledDocument d = support.openDocument(); |
152 |
d.insertString(d.getLength(), "a", null); |
153 |
|
154 |
support.saveDocument (); |
155 |
assertFalse("save: not modified", support.isModified()); |
156 |
assertTrue("save: can undo", ur().canUndo()); |
157 |
assertFalse("save: no redo", ur().canRedo()); |
158 |
|
159 |
d.insertString(d.getLength(), "b", null); |
160 |
|
161 |
assertEquals("insert, after savePoint: data", "ab", d.getText(0, d.getLength())); |
162 |
assertTrue("insert, after savePoint: modified", support.isModified()); |
163 |
assertTrue("insert, after savePoint: can undo", ur().canUndo()); |
164 |
assertFalse("insert, after savePoint: no redo", ur().canRedo()); |
165 |
|
166 |
support.saveDocument (); |
167 |
assertFalse("save2: not modified", support.isModified()); |
168 |
assertTrue("save2: can undo", ur().canUndo()); |
169 |
assertFalse("save2: no redo", ur().canRedo()); |
170 |
|
171 |
ur().undo(); |
172 |
assertEquals("undo1, before savePoint2: data", "a", d.getText(0, d.getLength())); |
173 |
|
174 |
// THIS WILL FAIL |
175 |
assertTrue("undo1, before savePoint2: modified", support.isModified()); |
176 |
// THIS WILL FAIL |
177 |
assertFalse("undo1, before savePoint2: can undo", ur().canUndo()); |
178 |
assertTrue("undo1, before savePoint2: can redo", ur().canRedo()); |
179 |
|
180 |
ur().undo(); |
181 |
assertEquals("undo2, before savePoint2: data", "", d.getText(0, d.getLength())); |
182 |
|
183 |
// THIS WILL FAIL |
184 |
assertTrue("undo2, before savePoint2: modified", support.isModified()); |
185 |
assertFalse("undo2, before savePoint2: can undo", ur().canUndo()); |
186 |
assertTrue("undo2, before savePoint2: can redo", ur().canRedo()); |
187 |
} |
188 |
|
189 |
// |
190 |
// Two tests with only a single line difference |
191 |
// This is a lengthier test than the first one |
192 |
// |
193 |
|
194 |
public void testUndoAfterSave() throws Exception { |
195 |
doUndoAfterSaveCommon(false); |
196 |
} |
197 |
|
198 |
public void testUndoAfterSaveErrorCase() throws Exception { |
199 |
doUndoAfterSaveCommon(true); |
200 |
} |
201 |
|
202 |
private void doUndoAfterSaveCommon(boolean doFailCase) throws Exception { |
203 |
content = ""; |
204 |
StyledDocument d = support.openDocument(); |
205 |
d.insertString(d.getLength(), "a", null); |
206 |
|
207 |
support.saveDocument (); |
208 |
assertFalse("save: not modified", support.isModified()); |
209 |
assertTrue("save: can undo", ur().canUndo()); |
210 |
assertFalse("save: no redo", ur().canRedo()); |
211 |
|
212 |
ur().undo(); |
213 |
assertEquals("undo1, before savePoint: data", "", d.getText(0, d.getLength())); |
214 |
|
215 |
if(doFailCase) { |
216 |
// **************************************************************** |
217 |
// CES BUG??? |
218 |
assertTrue("undo1, before savePoint: modified", support.isModified()); |
219 |
// **************************************************************** |
220 |
} |
221 |
|
222 |
assertFalse("undo1, before savePoint: can undo", ur().canUndo()); |
223 |
assertTrue("undo1, before savePoint: can redo", ur().canRedo()); |
224 |
|
225 |
ur().redo(); // back to the save point |
226 |
|
227 |
d.insertString(d.getLength(), "b", null); |
228 |
|
229 |
assertEquals("insert, after savePoint: data", "ab", d.getText(0, d.getLength())); |
230 |
assertTrue("insert, after savePoint: modified", support.isModified()); |
231 |
assertTrue("insert, after savePoint: can undo", ur().canUndo()); |
232 |
assertFalse("insert, after savePoint: no redo", ur().canRedo()); |
233 |
|
234 |
ur().undo(); |
235 |
assertEquals("undo, at savePoint: data", "a", d.getText(0, d.getLength())); |
236 |
assertFalse("undo, at savePoint: not modified", support.isModified()); |
237 |
assertTrue("undo, at savePoint: can undo", ur().canUndo()); |
238 |
assertTrue("undo, at savePoint: can redo", ur().canRedo()); |
239 |
|
240 |
ur().undo(); |
241 |
assertEquals("undo2, before savePoint: data", "", d.getText(0, d.getLength())); |
242 |
|
243 |
if(doFailCase) { |
244 |
// **************************************************************** |
245 |
// CES BUG??? |
246 |
assertTrue("undo2, before savePoint: modified", support.isModified()); |
247 |
// **************************************************************** |
248 |
} |
249 |
|
250 |
assertFalse("undo2, before savePoint: can undo", ur().canUndo()); |
251 |
assertTrue("undo2, before savePoint: can redo", ur().canRedo()); |
252 |
|
253 |
ur().redo(); |
254 |
assertEquals("redo, at savePoint: data", "a", d.getText(0, d.getLength())); |
255 |
assertFalse("redo, at savePoint: not modified", support.isModified()); |
256 |
assertTrue("redo, at savePoint: can undo", ur().canUndo()); |
257 |
assertTrue("redo, at savePoint: can redo", ur().canRedo()); |
258 |
} |
259 |
|
260 |
// |
261 |
// Implementation of the CloneableEditorSupport.Env |
262 |
// |
263 |
|
264 |
@Override |
265 |
public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { |
266 |
propL.add (l); |
267 |
} |
268 |
@Override |
269 |
public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { |
270 |
propL.remove (l); |
271 |
} |
272 |
|
273 |
@Override |
274 |
public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { |
275 |
assertNull ("This is the first veto listener", vetoL); |
276 |
vetoL = l; |
277 |
} |
278 |
@Override |
279 |
public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { |
280 |
assertEquals ("Removing the right veto one", vetoL, l); |
281 |
vetoL = null; |
282 |
} |
283 |
|
284 |
@Override |
285 |
public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { |
286 |
return support; |
287 |
} |
288 |
|
289 |
@Override |
290 |
public String getMimeType() { |
291 |
return "text/plain"; |
292 |
} |
293 |
|
294 |
@Override |
295 |
public java.util.Date getTime() { |
296 |
return date; |
297 |
} |
298 |
|
299 |
@Override |
300 |
public java.io.InputStream inputStream() throws java.io.IOException { |
301 |
return new java.io.ByteArrayInputStream (content.getBytes ()); |
302 |
} |
303 |
@Override |
304 |
public java.io.OutputStream outputStream() throws java.io.IOException { |
305 |
class ContentStream extends java.io.ByteArrayOutputStream { |
306 |
@Override |
307 |
public void close () throws java.io.IOException { |
308 |
super.close (); |
309 |
content = new String (toByteArray ()); |
310 |
} |
311 |
} |
312 |
|
313 |
return new ContentStream (); |
314 |
} |
315 |
|
316 |
@Override |
317 |
public boolean isValid() { |
318 |
return valid; |
319 |
} |
320 |
|
321 |
@Override |
322 |
public boolean isModified() { |
323 |
return modified; |
324 |
} |
325 |
|
326 |
@Override |
327 |
public void markModified() throws java.io.IOException { |
328 |
if (cannotBeModified != null) { |
329 |
IOException e = new IOException (); |
330 |
Exceptions.attachLocalizedMessage(e, cannotBeModified); |
331 |
throw e; |
332 |
} |
333 |
|
334 |
modified = true; |
335 |
} |
336 |
|
337 |
@Override |
338 |
public void unmarkModified() { |
339 |
modified = false; |
340 |
} |
341 |
|
342 |
/** Implementation of the CES */ |
343 |
private final class CES extends CloneableEditorSupport { |
344 |
public boolean plain; |
345 |
|
346 |
|
347 |
public CES (Env env, org.openide.util.Lookup l) { |
348 |
super (env, l); |
349 |
} |
350 |
|
351 |
@Override |
352 |
protected String messageName() { |
353 |
return "Name"; |
354 |
} |
355 |
|
356 |
@Override |
357 |
protected String messageOpened() { |
358 |
return "Opened"; |
359 |
} |
360 |
|
361 |
@Override |
362 |
protected String messageOpening() { |
363 |
return "Opening"; |
364 |
} |
365 |
|
366 |
@Override |
367 |
protected String messageSave() { |
368 |
return "Save"; |
369 |
} |
370 |
|
371 |
@Override |
372 |
protected String messageToolTip() { |
373 |
return "ToolTip"; |
374 |
} |
375 |
|
376 |
@Override |
377 |
protected javax.swing.text.EditorKit createEditorKit() { |
378 |
if (plain) { |
379 |
return super.createEditorKit (); |
380 |
} else { |
381 |
return UndoRedoAfterSaveTest.this.createEditorKit (); |
382 |
} |
383 |
} |
384 |
} // end of CES |
385 |
|
386 |
} |