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
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 		for (int i = 0; i < initBookmarkNames.length; i++) {
62 			xTextCursor.gotoEnd(false);
63 			XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(XTextRange.class, xTextCursor);
64 			xText.insertControlCharacter(xTextRange, ControlCharacter.PARAGRAPH_BREAK, false);
65 			xTextCursor.gotoEnd(false);
66 			xTextCursor.setString(initBookmarkContents[i]);
67 			insertBookmark(document, xTextCursor, initBookmarkNames[i]);
68 		}
69 	}
70 
71 	@After
72 	public void tearDown() {
73 		app.closeDocument(document);
74 	}
75 
76 	@AfterClass
77 	public static void tearDownConnection() throws Exception {
78 		app.close();
79 	}
80 
81 	private static String[] getBookmarkContents(XNameAccess xBookmarks) throws Exception {
82 		String[] bookmarkNames = xBookmarks.getElementNames();
83 		String[] bookmarkContents = new String[bookmarkNames.length];
84 		for (int i = 0; i < bookmarkNames.length; i++) {
85 			Object xBookmark = xBookmarks.getByName(bookmarkNames[i]);
86 			XTextContent xBookmarkAsContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, xBookmark);
87 			bookmarkContents[i] = xBookmarkAsContent.getAnchor().getString();
88 		}
89 
90 		return bookmarkContents;
91 	}
92 
93 	@Test
94 	public void createBookmark() throws Exception {
95 		XNameAccess xBookmarks = ((XBookmarksSupplier)UnoRuntime.queryInterface(XBookmarksSupplier.class, document)).getBookmarks();
96 		assertArrayEquals("Bookmark name list:", initBookmarkNames, xBookmarks.getElementNames());
97 		assertArrayEquals("Bookmark content list:", initBookmarkContents, getBookmarkContents(xBookmarks));
98 	}
99 
100 	@Test
101 	public void updateBookmarkContent() throws Exception {
102 		String[] expectedBookmarkNames= new String[]{"bookmark1", "bookmark2", "bookmark3"};
103 		String[] expectedBookmarkContents= new String[]{"bookmark1 content", "bookmark2 content", "bookmark3 cont"};
104 		// Delete some content
105 		XText xText = document.getText();
106 		XTextCursor xTextCursor = xText.createTextCursor();
107 		xTextCursor.gotoEnd(false);
108 		xTextCursor.goLeft((short)10, true);
109 		xTextCursor.setString("new");
110 
111 		// Let's see the bookmarks
112 		XNameAccess xBookmarks = ((XBookmarksSupplier)UnoRuntime.queryInterface(XBookmarksSupplier.class, document)).getBookmarks();
113 		assertArrayEquals("Bookmark name list after updating some content:", expectedBookmarkNames, xBookmarks.getElementNames());
114 		assertArrayEquals("Bookmark content list after updating some content:", expectedBookmarkContents, getBookmarkContents(xBookmarks));
115 	}
116 
117 	@Test
118 	public void removeBookmark() throws Exception {
119 		String[] expectedBookmarkNames= new String[]{"bookmark2", "bookmark3"};
120 		String[] expectedBookmarkContents= new String[]{"tent", "bookmark3 content!!!!!!!"};
121 		// Delete some content
122 		XText xText = document.getText();
123 		XTextCursor xTextCursor = xText.createTextCursor();
124 		xTextCursor.goRight((short)40, true);
125 		xTextCursor.setString("");
126 
127 		// Let's see the bookmarks
128 		XNameAccess xBookmarks = ((XBookmarksSupplier)UnoRuntime.queryInterface(XBookmarksSupplier.class, document)).getBookmarks();
129 		assertArrayEquals("Bookmark name list after deleting some content:", expectedBookmarkNames, xBookmarks.getElementNames());
130 		assertArrayEquals("Bookmark content list after deleting some content:", expectedBookmarkContents, getBookmarkContents(xBookmarks));
131 	}
132 
133 }
134