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