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 |
assertTrue("undo1, before savePoint: modified", support.isModified()); |
138 |
assertFalse("undo1, before savePoint: can undo", ur().canUndo()); |
139 |
assertTrue("undo1, before savePoint: can redo", ur().canRedo()); |
140 |
} |
141 |
|
142 |
public void testUndoAfterSaveExplore() throws Exception { |
143 |
content = ""; |
144 |
StyledDocument d = support.openDocument(); |
145 |
d.insertString(d.getLength(), "a", null); |
146 |
|
147 |
support.saveDocument (); |
148 |
assertFalse("save: not modified", support.isModified()); |
149 |
assertTrue("save: can undo", ur().canUndo()); |
150 |
assertFalse("save: no redo", ur().canRedo()); |
151 |
|
152 |
d.insertString(d.getLength(), "b", null); |
153 |
assertEquals("insert, after savePoint: data", "ab", d.getText(0, d.getLength())); |
154 |
assertTrue("insert, after savePoint: modified", support.isModified()); |
155 |
assertTrue("insert, after savePoint: can undo", ur().canUndo()); |
156 |
assertFalse("insert, after savePoint: no redo", ur().canRedo()); |
157 |
|
158 |
support.saveDocument (); |
159 |
assertFalse("save2: not modified", support.isModified()); |
160 |
assertTrue("save2: can undo", ur().canUndo()); |
161 |
assertFalse("save2: no redo", ur().canRedo()); |
162 |
|
163 |
ur().undo(); |
164 |
assertEquals("undo1, before savePoint2: data", "a", d.getText(0, d.getLength())); |
165 |
assertTrue("undo1, before savePoint2: modified", support.isModified()); |
166 |
assertTrue("undo1, before savePoint2: can undo", ur().canUndo()); |
167 |
assertTrue("undo1, before savePoint2: can redo", ur().canRedo()); |
168 |
|
169 |
ur().undo(); |
170 |
assertEquals("undo2, before savePoint2: data", "", d.getText(0, d.getLength())); |
171 |
assertTrue("undo2, before savePoint2: modified", support.isModified()); |
172 |
assertFalse("undo2, before savePoint2: can undo", ur().canUndo()); |
173 |
assertTrue("undo2, before savePoint2: can redo", ur().canRedo()); |
174 |
|
175 |
ur().redo(); |
176 |
assertEquals("redo1, before savePoint2: data", "a", d.getText(0, d.getLength())); |
177 |
assertTrue("redo1, before savePoint2: modified", support.isModified()); |
178 |
assertTrue("redo1, before savePoint2: can undo", ur().canUndo()); |
179 |
assertTrue("redo1, before savePoint2: can redo", ur().canRedo()); |
180 |
} |
181 |
|
182 |
public void testUndoAfterSave() throws Exception { |
183 |
content = ""; |
184 |
StyledDocument d = support.openDocument(); |
185 |
d.insertString(d.getLength(), "a", null); |
186 |
|
187 |
support.saveDocument (); |
188 |
assertFalse("save: not modified", support.isModified()); |
189 |
assertTrue("save: can undo", ur().canUndo()); |
190 |
assertFalse("save: no redo", ur().canRedo()); |
191 |
|
192 |
ur().undo(); |
193 |
assertEquals("undo1, before savePoint: data", "", d.getText(0, d.getLength())); |
194 |
assertTrue("undo1, before savePoint: modified", support.isModified()); |
195 |
assertFalse("undo1, before savePoint: can undo", ur().canUndo()); |
196 |
assertTrue("undo1, before savePoint: can redo", ur().canRedo()); |
197 |
|
198 |
ur().redo(); // back to the save point |
199 |
|
200 |
d.insertString(d.getLength(), "b", null); |
201 |
|
202 |
assertEquals("insert, after savePoint: data", "ab", d.getText(0, d.getLength())); |
203 |
assertTrue("insert, after savePoint: modified", support.isModified()); |
204 |
assertTrue("insert, after savePoint: can undo", ur().canUndo()); |
205 |
assertFalse("insert, after savePoint: no redo", ur().canRedo()); |
206 |
|
207 |
ur().undo(); |
208 |
assertEquals("undo, at savePoint: data", "a", d.getText(0, d.getLength())); |
209 |
assertFalse("undo, at savePoint: not modified", support.isModified()); |
210 |
assertTrue("undo, at savePoint: can undo", ur().canUndo()); |
211 |
assertTrue("undo, at savePoint: can redo", ur().canRedo()); |
212 |
|
213 |
ur().undo(); |
214 |
assertEquals("undo2, before savePoint: data", "", d.getText(0, d.getLength())); |
215 |
assertTrue("undo2, before savePoint: modified", support.isModified()); |
216 |
assertFalse("undo2, before savePoint: can undo", ur().canUndo()); |
217 |
assertTrue("undo2, before savePoint: can redo", ur().canRedo()); |
218 |
|
219 |
ur().redo(); |
220 |
assertEquals("redo, at savePoint: data", "a", d.getText(0, d.getLength())); |
221 |
assertFalse("redo, at savePoint: not modified", support.isModified()); |
222 |
assertTrue("redo, at savePoint: can undo", ur().canUndo()); |
223 |
assertTrue("redo, at savePoint: can redo", ur().canRedo()); |
224 |
} |
225 |
|
226 |
// |
227 |
// Implementation of the CloneableEditorSupport.Env |
228 |
// |
229 |
|
230 |
@Override |
231 |
public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { |
232 |
propL.add (l); |
233 |
} |
234 |
@Override |
235 |
public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { |
236 |
propL.remove (l); |
237 |
} |
238 |
|
239 |
@Override |
240 |
public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { |
241 |
assertNull ("This is the first veto listener", vetoL); |
242 |
vetoL = l; |
243 |
} |
244 |
@Override |
245 |
public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { |
246 |
assertEquals ("Removing the right veto one", vetoL, l); |
247 |
vetoL = null; |
248 |
} |
249 |
|
250 |
@Override |
251 |
public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { |
252 |
return support; |
253 |
} |
254 |
|
255 |
@Override |
256 |
public String getMimeType() { |
257 |
return "text/plain"; |
258 |
} |
259 |
|
260 |
@Override |
261 |
public java.util.Date getTime() { |
262 |
return date; |
263 |
} |
264 |
|
265 |
@Override |
266 |
public java.io.InputStream inputStream() throws java.io.IOException { |
267 |
return new java.io.ByteArrayInputStream (content.getBytes ()); |
268 |
} |
269 |
@Override |
270 |
public java.io.OutputStream outputStream() throws java.io.IOException { |
271 |
class ContentStream extends java.io.ByteArrayOutputStream { |
272 |
@Override |
273 |
public void close () throws java.io.IOException { |
274 |
super.close (); |
275 |
content = new String (toByteArray ()); |
276 |
} |
277 |
} |
278 |
|
279 |
return new ContentStream (); |
280 |
} |
281 |
|
282 |
@Override |
283 |
public boolean isValid() { |
284 |
return valid; |
285 |
} |
286 |
|
287 |
@Override |
288 |
public boolean isModified() { |
289 |
return modified; |
290 |
} |
291 |
|
292 |
@Override |
293 |
public void markModified() throws java.io.IOException { |
294 |
if (cannotBeModified != null) { |
295 |
IOException e = new IOException (); |
296 |
Exceptions.attachLocalizedMessage(e, cannotBeModified); |
297 |
throw e; |
298 |
} |
299 |
|
300 |
modified = true; |
301 |
} |
302 |
|
303 |
@Override |
304 |
public void unmarkModified() { |
305 |
modified = false; |
306 |
} |
307 |
|
308 |
/** Implementation of the CES */ |
309 |
private final class CES extends CloneableEditorSupport { |
310 |
public boolean plain; |
311 |
|
312 |
|
313 |
public CES (Env env, org.openide.util.Lookup l) { |
314 |
super (env, l); |
315 |
} |
316 |
|
317 |
@Override |
318 |
protected String messageName() { |
319 |
return "Name"; |
320 |
} |
321 |
|
322 |
@Override |
323 |
protected String messageOpened() { |
324 |
return "Opened"; |
325 |
} |
326 |
|
327 |
@Override |
328 |
protected String messageOpening() { |
329 |
return "Opening"; |
330 |
} |
331 |
|
332 |
@Override |
333 |
protected String messageSave() { |
334 |
return "Save"; |
335 |
} |
336 |
|
337 |
@Override |
338 |
protected String messageToolTip() { |
339 |
return "ToolTip"; |
340 |
} |
341 |
|
342 |
@Override |
343 |
protected javax.swing.text.EditorKit createEditorKit() { |
344 |
if (plain) { |
345 |
return super.createEditorKit (); |
346 |
} else { |
347 |
return UndoRedoAfterSaveTest.this.createEditorKit (); |
348 |
} |
349 |
} |
350 |
} // end of CES |
351 |
|
352 |
} |