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