001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package bookmarks;
043:
044: import java.awt.event.KeyEvent;
045: import javax.swing.text.Document;
046: import javax.swing.text.Element;
047: import org.netbeans.jellytools.EditorOperator;
048: import org.netbeans.jemmy.operators.JEditorPaneOperator;
049: import org.netbeans.lib.editor.bookmarks.api.BookmarkList;
050:
051: /**
052: * Test of typing at begining/end and other typing tests.
053: *
054: * @author Miloslav Metelka
055: */
056: public class BookmarksPersistenceTest extends EditorBookmarksTestCase {
057:
058: public BookmarksPersistenceTest(String testMethodName) {
059: super (testMethodName);
060: }
061:
062: public void testPersistence() {
063: int[] bookmarkLines = new int[] { 1, 7, 9 };
064:
065: openDefaultProject();
066:
067: openDefaultSampleFile();
068: try {
069:
070: EditorOperator editorOper = getDefaultSampleEditorOperator();
071: JEditorPaneOperator txtOper = editorOper.txtEditorPane();
072: Document doc = txtOper.getDocument();
073:
074: for (int i = 0; i < bookmarkLines.length; i++) {
075: editorOper.setCaretPosition(getLineOffset(doc,
076: bookmarkLines[i]));
077: txtOper.pushKey(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK
078: | KeyEvent.SHIFT_DOWN_MASK);
079: }
080:
081: } finally {
082: closeFileWithDiscard();
083: }
084:
085: openDefaultSampleFile();
086: try {
087:
088: EditorOperator editorOper = getDefaultSampleEditorOperator();
089: JEditorPaneOperator txtOper = editorOper.txtEditorPane();
090: Document doc = txtOper.getDocument();
091: BookmarkList bml = BookmarkList.get(doc);
092: checkBookmarksAtLines(bml, bookmarkLines);
093:
094: } finally {
095: closeFileWithDiscard();
096: }
097: }
098:
099: public void testBookmarkMove() {
100: int bookmarkLine = 14;
101: int lineToDelete = 12;
102:
103: openDefaultProject();
104:
105: openDefaultSampleFile();
106: try {
107: EditorOperator editorOper = getDefaultSampleEditorOperator();
108: JEditorPaneOperator txtOper = editorOper.txtEditorPane();
109: Document doc = txtOper.getDocument();
110: editorOper
111: .setCaretPosition(getLineOffset(doc, bookmarkLine));
112: txtOper.pushKey(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK
113: | KeyEvent.SHIFT_DOWN_MASK);
114: editorOper
115: .setCaretPosition(getLineOffset(doc, lineToDelete));
116: txtOper.pushKey(KeyEvent.VK_E, KeyEvent.CTRL_MASK);
117: doc = txtOper.getDocument();
118: BookmarkList bml = BookmarkList.get(doc);
119: checkBookmarksAtLines(bml, new int[] { bookmarkLine - 1 });
120: } finally {
121: closeFileWithDiscard();
122: }
123: }
124:
125: public void testBookmarkMerge() {
126: int[] bookmarkLines = new int[] { 9, 10, 11 };
127:
128: openDefaultProject();
129:
130: openDefaultSampleFile();
131: try {
132:
133: EditorOperator editorOper = getDefaultSampleEditorOperator();
134: JEditorPaneOperator txtOper = editorOper.txtEditorPane();
135: Document doc = txtOper.getDocument();
136: for (int i = 0; i < bookmarkLines.length; i++) {
137: editorOper.setCaretPosition(getLineOffset(doc,
138: bookmarkLines[i]));
139: txtOper.pushKey(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK
140: | KeyEvent.SHIFT_DOWN_MASK);
141: }
142: editorOper.setCaretPosition(getLineOffset(doc,
143: bookmarkLines[0]));
144: txtOper.pushKey(KeyEvent.VK_E, KeyEvent.CTRL_DOWN_MASK);
145: txtOper.pushKey(KeyEvent.VK_E, KeyEvent.CTRL_DOWN_MASK);
146: BookmarkList bml = BookmarkList.get(doc);
147: checkBookmarksAtLines(bml, new int[] { bookmarkLines[0] });
148: } finally {
149: closeFileWithDiscard();
150: }
151: }
152:
153: public void testNextBookmark() {
154: int[] bookmarkLines = new int[] { 9, 10, 11 };
155: int[] expectedLines = new int[] { 9, 10, 11, 9 };
156:
157: openDefaultProject();
158:
159: openDefaultSampleFile();
160: try {
161:
162: EditorOperator editorOper = getDefaultSampleEditorOperator();
163: JEditorPaneOperator txtOper = editorOper.txtEditorPane();
164: Document doc = txtOper.getDocument();
165: for (int i = 0; i < bookmarkLines.length; i++) {
166: editorOper.setCaretPosition(getLineOffset(doc,
167: bookmarkLines[i]));
168: txtOper.pushKey(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK
169: | KeyEvent.SHIFT_DOWN_MASK);
170: }
171: editorOper.setCaretPosition(getLineOffset(doc, 1));
172: for (int i = 0; i < expectedLines.length; i++) {
173: txtOper.pushKey(KeyEvent.VK_COMMA,
174: KeyEvent.CTRL_DOWN_MASK
175: | KeyEvent.SHIFT_DOWN_MASK);
176: int j = expectedLines[i];
177: int actLine = getLineIndex(doc, txtOper
178: .getCaretPosition());
179: assertEquals("Caret is at bad location", j, actLine);
180: }
181: } finally {
182: closeFileWithDiscard();
183: }
184: }
185:
186: public void testPreviousBookmark() {
187: int[] bookmarkLines = new int[] { 9, 10, 11 };
188: int[] expectedLines = new int[] { 11, 10, 9, 11 };
189:
190: openDefaultProject();
191:
192: openDefaultSampleFile();
193: try {
194:
195: EditorOperator editorOper = getDefaultSampleEditorOperator();
196: JEditorPaneOperator txtOper = editorOper.txtEditorPane();
197: Document doc = txtOper.getDocument();
198: for (int i = 0; i < bookmarkLines.length; i++) {
199: editorOper.setCaretPosition(getLineOffset(doc,
200: bookmarkLines[i]));
201: txtOper.pushKey(KeyEvent.VK_M, KeyEvent.CTRL_DOWN_MASK
202: | KeyEvent.SHIFT_DOWN_MASK);
203: }
204: editorOper.setCaretPosition(getLineOffset(doc, 14));
205: for (int i = 0; i < expectedLines.length; i++) {
206: txtOper.pushKey(KeyEvent.VK_PERIOD,
207: KeyEvent.CTRL_DOWN_MASK
208: | KeyEvent.SHIFT_DOWN_MASK);
209: int j = expectedLines[i];
210: int actLine = getLineIndex(doc, txtOper
211: .getCaretPosition());
212: assertEquals("Caret is at bad location", j, actLine);
213: }
214: } finally {
215: closeFileWithDiscard();
216: }
217: }
218:
219: private void checkBookmarksAtLines(BookmarkList bml,
220: int[] expectedLineIndexes) {
221: assertEquals("Invalid bookmark count",
222: expectedLineIndexes.length, bml.getBookmarkCount());
223: for (int i = 0; i < expectedLineIndexes.length; i++) {
224: int expectedLineIndex = expectedLineIndexes[i];
225: int lineIndex = bml.getBookmark(i).getLineIndex();
226: assertEquals("Bookmark line index " + lineIndex
227: + " differs from expected " + expectedLineIndex,
228: lineIndex, expectedLineIndex);
229: }
230: }
231:
232: private int getLineOffset(Document doc, int lineIndex) {
233: Element root = doc.getDefaultRootElement();
234: return root.getElement(lineIndex).getStartOffset();
235: }
236:
237: private int getLineIndex(Document doc, int offset) {
238: Element root = doc.getDefaultRootElement();
239: return root.getElementIndex(offset);
240: }
241: }
|