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