1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package fvt.uno.sw.bookmark; 23 24 import static org.junit.Assert.assertArrayEquals; 25 import static testlib.uno.SWUtil.*; 26 27 import org.junit.After; 28 import org.junit.AfterClass; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.openoffice.test.uno.UnoApp; 32 33 import com.sun.star.container.XNameAccess; 34 import com.sun.star.text.ControlCharacter; 35 import com.sun.star.text.XBookmarksSupplier; 36 import com.sun.star.text.XText; 37 import com.sun.star.text.XTextContent; 38 import com.sun.star.text.XTextCursor; 39 import com.sun.star.text.XTextDocument; 40 import com.sun.star.text.XTextRange; 41 import com.sun.star.uno.UnoRuntime; 42 43 44 public class CheckBookmarks { 45 private static final UnoApp app = new UnoApp(); 46 47 private XTextDocument document = null; 48 49 private String[] initBookmarkNames= new String[]{"bookmark1", "bookmark2", "bookmark3"}; 50 51 private String[] initBookmarkContents= new String[]{"bookmark1 content", "bookmark2 content", "bookmark3 content!!!!!!!"}; 52 53 @Before setUp()54 public void setUp() throws Exception { 55 app.start(); 56 document = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter")); 57 XText xText = document.getText(); 58 XTextCursor xTextCursor = xText.createTextCursor(); 59 xTextCursor.setString("Contents"); 60 /** 61 * insert bookmark into document 62 */ 63 for (int i = 0; i < initBookmarkNames.length; i++) { 64 xTextCursor.gotoEnd(false); 65 XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xTextCursor); 66 xText.insertControlCharacter(xTextRange, ControlCharacter.PARAGRAPH_BREAK, false); 67 xTextCursor.gotoEnd(false); 68 xTextCursor.setString(initBookmarkContents[i]); 69 insertBookmark(document, xTextCursor, initBookmarkNames[i]); 70 } 71 } 72 73 @After tearDown()74 public void tearDown() { 75 app.closeDocument(document); 76 } 77 78 @AfterClass tearDownConnection()79 public static void tearDownConnection() throws Exception { 80 app.close(); 81 } 82 /** 83 * get bookmark content 84 * @param xBookmarks 85 * @return 86 * @throws Exception 87 */ getBookmarkContents(XNameAccess xBookmarks)88 private static String[] getBookmarkContents(XNameAccess xBookmarks) throws Exception { 89 String[] bookmarkNames = xBookmarks.getElementNames(); 90 String[] bookmarkContents = new String[bookmarkNames.length]; 91 for (int i = 0; i < bookmarkNames.length; i++) { 92 Object xBookmark = xBookmarks.getByName(bookmarkNames[i]); 93 XTextContent xBookmarkAsContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xBookmark); 94 bookmarkContents[i] = xBookmarkAsContent.getAnchor().getString(); 95 } 96 97 return bookmarkContents; 98 } 99 /** 100 * verify inserted bookmark by compare their name and content 101 * @throws Exception 102 */ 103 @Test createBookmark()104 public void createBookmark() throws Exception { 105 XNameAccess xBookmarks = ((XBookmarksSupplier)UnoRuntime.queryInterface(XBookmarksSupplier.class, document)).getBookmarks(); 106 assertArrayEquals("Bookmark name list:", initBookmarkNames, xBookmarks.getElementNames()); 107 assertArrayEquals("Bookmark content list:", initBookmarkContents, getBookmarkContents(xBookmarks)); 108 } 109 /** 110 * verify bookmark modify 111 * @throws Exception 112 */ 113 @Test updateBookmarkContent()114 public void updateBookmarkContent() throws Exception { 115 String[] expectedBookmarkNames= new String[]{"bookmark1", "bookmark2", "bookmark3"}; 116 String[] expectedBookmarkContents= new String[]{"bookmark1 content", "bookmark2 content", "bookmark3 cont"}; 117 // Delete some content 118 XText xText = document.getText(); 119 XTextCursor xTextCursor = xText.createTextCursor(); 120 xTextCursor.gotoEnd(false); 121 xTextCursor.goLeft((short)10, true); 122 xTextCursor.setString("new"); 123 124 // Let's see the bookmarks 125 XNameAccess xBookmarks = ((XBookmarksSupplier)UnoRuntime.queryInterface(XBookmarksSupplier.class, document)).getBookmarks(); 126 assertArrayEquals("Bookmark name list after updating some content:", expectedBookmarkNames, xBookmarks.getElementNames()); 127 assertArrayEquals("Bookmark content list after updating some content:", expectedBookmarkContents, getBookmarkContents(xBookmarks)); 128 } 129 /** 130 * verify bookmark remove 131 * @throws Exception 132 */ 133 @Test removeBookmark()134 public void removeBookmark() throws Exception { 135 String[] expectedBookmarkNames= new String[]{"bookmark2", "bookmark3"}; 136 String[] expectedBookmarkContents= new String[]{"tent", "bookmark3 content!!!!!!!"}; 137 // Delete some content 138 XText xText = document.getText(); 139 XTextCursor xTextCursor = xText.createTextCursor(); 140 xTextCursor.goRight((short)40, true); 141 xTextCursor.setString(""); 142 143 // Let's see the bookmarks 144 XNameAccess xBookmarks = ((XBookmarksSupplier)UnoRuntime.queryInterface(XBookmarksSupplier.class, document)).getBookmarks(); 145 assertArrayEquals("Bookmark name list after deleting some content:", expectedBookmarkNames, xBookmarks.getElementNames()); 146 assertArrayEquals("Bookmark content list after deleting some content:", expectedBookmarkContents, getBookmarkContents(xBookmarks)); 147 } 148 149 } 150