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 package fvt.uno.sw.breaks;
22 
23 import static org.openoffice.test.common.Testspace.*;
24 
25 import java.io.File;
26 
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.Assert;
31 import org.openoffice.test.common.FileUtil;
32 import org.openoffice.test.uno.UnoApp;
33 
34 import testlib.uno.SWUtil;
35 
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.text.XTextCursor;
38 import com.sun.star.text.XText;
39 import com.sun.star.text.ControlCharacter;
40 import com.sun.star.text.XTextViewCursor;
41 import com.sun.star.text.XPageCursor;
42 import com.sun.star.text.XTextViewCursorSupplier;
43 import com.sun.star.style.BreakType;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.frame.*;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.lang.XComponent;
48 
49 public class CheckBreaks {
50 	UnoApp unoApp = new UnoApp();
51 	XTextDocument textDocument = null;
52 	File temp = null;
53 	String tempFilePathODT = "";
54 	String tempFilePathDOC = "";
55 
56 	/**
57 	 * @throws java.lang.Exception
58 	 */
59 	@Before
setUp()60 	public void setUp() throws Exception {
61 		unoApp.start();
62 
63 		FileUtil.deleteFile(getPath("temp"));
64 		temp = new File(getPath("temp"));
65 		temp.mkdirs();
66 
67 		tempFilePathODT = temp + "/tempFilePathODT.odt";
68 		tempFilePathDOC = temp + "/tempFilePathDOC.doc";
69 	}
70 
71 	@After
tearDown()72 	public void tearDown() throws Exception {
73 		unoApp.close();
74 	}
75 
76 	/**
77 	 * test line break can be inserted and deleted.
78 	 * when save it as odt file, close and reopen, line break can be inserted and deleted.
79 	 * when save it as doc file, close and reopen, line break can be inserted and deleted.
80 	 * \n represent line break
81 	 * @throws Exception
82 	 */
83 	@Test
testInsertDeleteLineBreak()84 	public void testInsertDeleteLineBreak() throws Exception
85 	{
86 		XComponent xComponent = unoApp.newDocument("swriter");
87 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
88 		this.insertNewLine(textDocument, "Line1", true);
89 		this.insertNewLine(textDocument, "Line2", false);
90 		this.insertNewLine(textDocument, "Line3", false);
91 		this.insertNewLine(textDocument, "Line4", false);
92 
93 		int lineCount = this.getLineCount(textDocument);
94 		Assert.assertEquals("Line break is inserted when new document.",4,lineCount);
95 		this.deleteLineBreak(textDocument);
96 		lineCount = this.getLineCount(textDocument);
97 		Assert.assertEquals("Line break is deleted when new document", 3,lineCount);
98 
99 		//save to odt, test the line break
100 		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));
101 		unoApp.closeDocument(xComponent);
102 		xComponent = unoApp.loadDocument(tempFilePathODT);
103 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
104 		lineCount = this.getLineCount(textDocument);
105 		Assert.assertEquals("Line breaks when open saved odt file.",3,lineCount);
106 		this.insertNewLine(textDocument, "Line added when open saved odt file", false);
107 		lineCount = this.getLineCount(textDocument);
108 		Assert.assertEquals("Line break is inserted when open saved odt file.",4,lineCount);
109 		this.deleteLineBreak(textDocument);
110 		lineCount = this.getLineCount(textDocument);
111 		Assert.assertEquals("Line break is deleted when open saved odt file.",3,lineCount);
112 
113 		//save to doc, test the line break
114 		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
115 		unoApp.closeDocument(xComponent);
116 		xComponent = unoApp.loadDocument(tempFilePathDOC);
117 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
118 		lineCount = this.getLineCount(textDocument);
119 		Assert.assertEquals("Line breaks when open saved doc file.",3,lineCount);
120 		this.insertNewLine(textDocument, "Line added when open saved doc file", false);
121 		lineCount = this.getLineCount(textDocument);
122 		Assert.assertEquals("Line break is inserted when open saved doc file.",4,lineCount);
123 		this.deleteLineBreak(textDocument);
124 		lineCount = this.getLineCount(textDocument);
125 		Assert.assertEquals("Line break is deleted when open saved doc file.",3,lineCount);
126 
127 		unoApp.closeDocument(xComponent);
128 	}
129 
130 	@Test
testInsertDeletePageBreak()131 	public void testInsertDeletePageBreak() throws Exception
132 	{
133 		//new document, test page break
134 		XComponent xComponent = unoApp.newDocument("swriter");
135 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
136 		this.insertNewPage(textDocument, "Page1", true);
137 		this.insertNewPage(textDocument, "Page2", false);
138 		this.insertNewPage(textDocument, "Page3", false);
139 		int pageCount = SWUtil.getPageCount(textDocument);
140 		Assert.assertEquals("page break is inserted when new document", 3,pageCount);
141 
142 		this.deleteFirstPage(textDocument);
143 		pageCount = SWUtil.getPageCount(textDocument);
144 		Assert.assertEquals("page break is deleted when new document", 2,pageCount);
145 
146 		//save as odt, test page break
147 		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));
148 		unoApp.closeDocument(xComponent);
149 		xComponent = unoApp.loadDocument(tempFilePathODT);
150 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
151 		pageCount = SWUtil.getPageCount(textDocument);
152 		Assert.assertEquals("Page breaks after open saved odt file", 2,pageCount);
153 		this.insertNewPage(textDocument, "Page4", false);
154 		pageCount = SWUtil.getPageCount(textDocument);
155 		Assert.assertEquals("page break is inserted after open saved odt file", 3,pageCount);
156 
157 		this.deleteFirstPage(textDocument);
158 		pageCount = SWUtil.getPageCount(textDocument);
159 		Assert.assertEquals("page break is deleted after open saved odt file.", 2,pageCount);
160 
161 		//save as doc, test page break
162 		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
163 		unoApp.closeDocument(xComponent);
164 		xComponent = unoApp.loadDocument(tempFilePathDOC);
165 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
166 		pageCount = SWUtil.getPageCount(textDocument);
167 		Assert.assertEquals("Page breaks after open saved doc file", 2,pageCount);
168 
169 		this.deleteFirstPage(textDocument);
170 		pageCount = SWUtil.getPageCount(textDocument);
171 		Assert.assertEquals("page break is deleted after open saved doc file.", 1,pageCount);
172 
173 		this.insertNewPage(textDocument, "Page5", false);
174 		pageCount = SWUtil.getPageCount(textDocument);
175 		Assert.assertEquals("page break is inserted after open saved doc file", 2,pageCount);
176 
177 		unoApp.closeDocument(xComponent);
178 	}
179 
180 
181 
182 
183 	/**
184 	 * insert a new page at the end of the document
185 	 * @param xTextDocument
186 	 * @param pageContent
187 	 * @param isFirstPage
188 	 * @throws Exception
189 	 */
insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage)190 	private void insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage) throws Exception
191 	{
192 		XText xText = document.getText();
193 		XTextCursor textCursor = xText.createTextCursor();
194 		textCursor.gotoEnd(false);
195 		System.out.println("The content before insert " + pageContent + ":" + xText.getString());
196 		if(!isFirstPage)
197 		{
198 			XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
199 			        XPropertySet.class, textCursor);
200 			xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
201 			document.getText().insertControlCharacter(textCursor,ControlCharacter.PARAGRAPH_BREAK,false);
202 		}
203 		document.getText().insertString(textCursor, pageContent, false);
204 	}
205 
206 	/**
207 	 * delete the first page of the document
208 	 * @param document
209 	 * @throws Exception
210 	 */
deleteFirstPage(XTextDocument document)211 	private void deleteFirstPage(XTextDocument document) throws Exception
212 	{
213 		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, document);
214         XController xController = xModel.getCurrentController();
215         XTextViewCursorSupplier xTextViewCursorSupplier =
216                 (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
217         XTextViewCursor textViewCursor = xTextViewCursorSupplier.getViewCursor();
218 
219         XPageCursor pageCursor = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class, textViewCursor);
220 
221         // Move the cursor to the start of the document
222         textViewCursor.gotoStart(false);
223 
224         pageCursor.jumpToFirstPage();
225         XTextCursor textCursor = textViewCursor.getText().createTextCursorByRange(textViewCursor.getStart());
226 
227         pageCursor.jumpToEndOfPage();
228         textCursor.gotoRange(textViewCursor.getEnd(), true);
229         //System.out.println("deleted: " + textCursor.getString());
230         textCursor.setString("");
231 
232      // Page contents cleared, now delete the page break at the start
233         textCursor.collapseToStart();
234         if(textCursor.goRight((short) 1, true)){
235         	//System.out.println("page break deleted: " + textCursor.getString());
236         	textCursor.setString("");
237         }
238 
239 	}
240 
241 
242 	/**
243 	 * insert a new line at the end of the document.
244 	 * @param xText
245 	 * @param lineContent
246 	 * @param isFirstLine
247 	 * @throws Exception
248 	 */
insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine)249 	private void insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine) throws Exception
250 	{
251 		XText xText = xTextDocument.getText();
252 		XTextCursor xTextCursor = xText.createTextCursor();
253 		xTextCursor.gotoEnd(false);
254 		if(!isFirstLine)
255 		{
256 			xText.insertControlCharacter(xTextCursor, ControlCharacter.LINE_BREAK, false);
257 		}
258 
259 		xText.insertString(xTextCursor, lineContent, false);
260 	}
261 
262 	/**
263 	 * delete first line break
264 	 * @param xText
265 	 * @throws Exception
266 	 */
deleteLineBreak(XTextDocument xTextDocument)267 	private void deleteLineBreak(XTextDocument xTextDocument) throws Exception
268 	{
269 		XText xText = xTextDocument.getText();
270 		String content = xText.getString();
271 		content = content.replaceFirst("\n", "");
272 		xText.setString(content);
273 	}
274 
275 	/**
276 	 * get line count of all text.
277 	 * \n represent line break.
278 	 * @param xText
279 	 * @return count of line breaks
280 	 * @throws Exception
281 	 */
getLineCount(XTextDocument xTextDocument)282 	private int getLineCount(XTextDocument xTextDocument) throws Exception
283 	{
284 		int count = 1;
285 		XText xText = xTextDocument.getText();
286 		String content = xText.getString();
287 		int index = content.indexOf("\n");
288 		while(index >=0)
289 		{
290 			count ++;
291 			content = content.substring(index+1);
292 			index = content.indexOf("\n");
293 		}
294 		return count;
295 	}
296 
297 }
298