1 19 20 package bookmarks; 21 22 import java.awt.event.KeyEvent ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.Element ; 25 import org.netbeans.jellytools.EditorOperator; 26 import org.netbeans.jemmy.operators.JEditorPaneOperator; 27 import org.netbeans.lib.editor.bookmarks.api.BookmarkList; 28 29 34 public class BookmarksPersistenceTest extends EditorBookmarksTestCase { 35 36 public BookmarksPersistenceTest(String testMethodName) { 37 super(testMethodName); 38 } 39 40 public void testPersistence() { 41 int[] bookmarkLines = new int[] { 1, 7, 9 }; 42 43 openDefaultProject(); 44 45 openDefaultSampleFile(); 46 try { 47 48 EditorOperator editorOper = getDefaultSampleEditorOperator(); 49 JEditorPaneOperator txtOper = editorOper.txtEditorPane(); 50 Document doc = txtOper.getDocument(); 51 52 for (int i = 0; i < bookmarkLines.length; i++) { 53 editorOper.setCaretPosition(getLineOffset(doc, bookmarkLines[i])); 54 txtOper.pushKey(KeyEvent.VK_F2, KeyEvent.CTRL_MASK); 55 } 56 57 } finally { 58 closeFileWithDiscard(); 59 } 60 61 openDefaultSampleFile(); 62 try { 63 64 EditorOperator editorOper = getDefaultSampleEditorOperator(); 65 JEditorPaneOperator txtOper = editorOper.txtEditorPane(); 66 Document doc = txtOper.getDocument(); 67 BookmarkList bml = BookmarkList.get(doc); 68 checkBookmarksAtLines(bml, bookmarkLines); 69 70 } finally { 71 closeFileWithDiscard(); 72 } 73 } 74 75 public void testBookmarkMove() { 76 int bookmarkLine = 14; 77 int lineToDelete = 12; 78 79 openDefaultProject(); 80 81 openDefaultSampleFile(); 82 try { 83 EditorOperator editorOper = getDefaultSampleEditorOperator(); 84 JEditorPaneOperator txtOper = editorOper.txtEditorPane(); 85 Document doc = txtOper.getDocument(); 86 editorOper.setCaretPosition(getLineOffset(doc, bookmarkLine)); 87 txtOper.pushKey(KeyEvent.VK_F2, KeyEvent.CTRL_MASK); 88 editorOper.setCaretPosition(getLineOffset(doc,lineToDelete)); 89 txtOper.pushKey(KeyEvent.VK_E, KeyEvent.CTRL_MASK); 90 doc = txtOper.getDocument(); 91 BookmarkList bml = BookmarkList.get(doc); 92 checkBookmarksAtLines(bml, new int[]{bookmarkLine-1}); 93 } finally { 94 closeFileWithDiscard(); 95 } 96 } 97 98 public void testBookmarkMerge() { 99 int[] bookmarkLines = new int[] { 9, 10, 11 }; 100 101 openDefaultProject(); 102 103 openDefaultSampleFile(); 104 try { 105 106 EditorOperator editorOper = getDefaultSampleEditorOperator(); 107 JEditorPaneOperator txtOper = editorOper.txtEditorPane(); 108 Document doc = txtOper.getDocument(); 109 for (int i = 0; i < bookmarkLines.length; i++) { 110 editorOper.setCaretPosition(getLineOffset(doc, bookmarkLines[i])); 111 txtOper.pushKey(KeyEvent.VK_F2, KeyEvent.CTRL_MASK); 112 } 113 editorOper.setCaretPosition(getLineOffset(doc, bookmarkLines[0])); 114 txtOper.pushKey(KeyEvent.VK_E, KeyEvent.CTRL_MASK); 115 txtOper.pushKey(KeyEvent.VK_E, KeyEvent.CTRL_MASK); 116 BookmarkList bml = BookmarkList.get(doc); 117 checkBookmarksAtLines(bml, new int[]{bookmarkLines[0]}); 118 } finally { 119 closeFileWithDiscard(); 120 } 121 } 122 123 public void testNextBookmark() { 124 int[] bookmarkLines = new int[] { 9, 10, 11 }; 125 int[] expectedLines = new int[] { 9, 10, 11, 9}; 126 127 openDefaultProject(); 128 129 openDefaultSampleFile(); 130 try { 131 132 EditorOperator editorOper = getDefaultSampleEditorOperator(); 133 JEditorPaneOperator txtOper = editorOper.txtEditorPane(); 134 Document doc = txtOper.getDocument(); 135 for (int i = 0; i < bookmarkLines.length; i++) { 136 editorOper.setCaretPosition(getLineOffset(doc, bookmarkLines[i])); 137 txtOper.pushKey(KeyEvent.VK_F2, KeyEvent.CTRL_MASK); 138 } 139 editorOper.setCaretPosition(getLineOffset(doc,1)); 140 for (int i = 0; i < expectedLines.length; i++) { 141 txtOper.pushKey(KeyEvent.VK_F2); 142 int j = expectedLines[i]; 143 int actLine = getLineIndex(doc, txtOper.getCaretPosition()); 144 assertEquals("Caret is at bad location", j, actLine); 145 } 146 } finally { 147 closeFileWithDiscard(); 148 } 149 } 150 151 public void testPreviousBookmark() { 152 int[] bookmarkLines = new int[] { 9, 10, 11 }; 153 int[] expectedLines = new int[] { 11, 10, 9, 11}; 154 155 openDefaultProject(); 156 157 openDefaultSampleFile(); 158 try { 159 160 EditorOperator editorOper = getDefaultSampleEditorOperator(); 161 JEditorPaneOperator txtOper = editorOper.txtEditorPane(); 162 Document doc = txtOper.getDocument(); 163 for (int i = 0; i < bookmarkLines.length; i++) { 164 editorOper.setCaretPosition(getLineOffset(doc, bookmarkLines[i])); 165 txtOper.pushKey(KeyEvent.VK_F2, KeyEvent.CTRL_MASK); 166 } 167 editorOper.setCaretPosition(getLineOffset(doc,14)); 168 for (int i = 0; i < expectedLines.length; i++) { 169 txtOper.pushKey(KeyEvent.VK_F2,KeyEvent.SHIFT_MASK); 170 int j = expectedLines[i]; 171 int actLine = getLineIndex(doc, txtOper.getCaretPosition()); 172 assertEquals("Caret is at bad location", j, actLine); 173 } 174 } finally { 175 closeFileWithDiscard(); 176 } 177 } 178 179 180 181 private void checkBookmarksAtLines(BookmarkList bml, int[] expectedLineIndexes) { 182 assertEquals("Invalid bookmark count", expectedLineIndexes.length, bml.getBookmarkCount()); 183 for (int i = 0; i < expectedLineIndexes.length; i++) { 184 int expectedLineIndex = expectedLineIndexes[i]; 185 int lineIndex = bml.getBookmark(i).getLineIndex(); 186 assertEquals("Bookmark line index " + lineIndex 187 + " differs from expected " + expectedLineIndex, 188 lineIndex, 189 expectedLineIndex 190 ); 191 } 192 } 193 194 195 private int getLineOffset(Document doc, int lineIndex) { 196 Element root = doc.getDefaultRootElement(); 197 return root.getElement(lineIndex).getStartOffset(); 198 } 199 200 private int getLineIndex(Document doc, int offset) { 201 Element root = doc.getDefaultRootElement(); 202 return root.getElementIndex(offset); 203 } 204 } 205 | Popular Tags |