190a166ceSLiu Zhe /**************************************************************
290a166ceSLiu Zhe  *
390a166ceSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
490a166ceSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
590a166ceSLiu Zhe  * distributed with this work for additional information
690a166ceSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
790a166ceSLiu Zhe  * to you under the Apache License, Version 2.0 (the
890a166ceSLiu Zhe  * "License"); you may not use this file except in compliance
990a166ceSLiu Zhe  * with the License.  You may obtain a copy of the License at
1090a166ceSLiu Zhe  *
1190a166ceSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
1290a166ceSLiu Zhe  *
1390a166ceSLiu Zhe  * Unless required by applicable law or agreed to in writing,
1490a166ceSLiu Zhe  * software distributed under the License is distributed on an
1590a166ceSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1690a166ceSLiu Zhe  * KIND, either express or implied.  See the License for the
1790a166ceSLiu Zhe  * specific language governing permissions and limitations
1890a166ceSLiu Zhe  * under the License.
1990a166ceSLiu Zhe  *
2090a166ceSLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe package fvt.uno.sw.breaks;
2290a166ceSLiu Zhe 
2390a166ceSLiu Zhe import static org.openoffice.test.common.Testspace.*;
2490a166ceSLiu Zhe 
2590a166ceSLiu Zhe import java.io.File;
2690a166ceSLiu Zhe 
2790a166ceSLiu Zhe import org.junit.After;
2890a166ceSLiu Zhe import org.junit.Before;
2990a166ceSLiu Zhe import org.junit.Test;
3090a166ceSLiu Zhe import org.junit.Assert;
3190a166ceSLiu Zhe import org.openoffice.test.common.FileUtil;
3290a166ceSLiu Zhe import org.openoffice.test.uno.UnoApp;
3390a166ceSLiu Zhe 
3490a166ceSLiu Zhe import testlib.uno.SWUtil;
3590a166ceSLiu Zhe 
3690a166ceSLiu Zhe import com.sun.star.text.XTextDocument;
3790a166ceSLiu Zhe import com.sun.star.text.XTextCursor;
3890a166ceSLiu Zhe import com.sun.star.text.XText;
3990a166ceSLiu Zhe import com.sun.star.text.ControlCharacter;
4090a166ceSLiu Zhe import com.sun.star.text.XTextViewCursor;
4190a166ceSLiu Zhe import com.sun.star.text.XPageCursor;
4290a166ceSLiu Zhe import com.sun.star.text.XTextViewCursorSupplier;
4390a166ceSLiu Zhe import com.sun.star.style.BreakType;
4490a166ceSLiu Zhe import com.sun.star.beans.XPropertySet;
4590a166ceSLiu Zhe import com.sun.star.frame.*;
4690a166ceSLiu Zhe import com.sun.star.uno.UnoRuntime;
4790a166ceSLiu Zhe import com.sun.star.lang.XComponent;
4890a166ceSLiu Zhe 
4990a166ceSLiu Zhe public class CheckBreaks {
5090a166ceSLiu Zhe 	UnoApp unoApp = new UnoApp();
5190a166ceSLiu Zhe 	XTextDocument textDocument = null;
5290a166ceSLiu Zhe 	File temp = null;
5390a166ceSLiu Zhe 	String tempFilePathODT = "";
5490a166ceSLiu Zhe 	String tempFilePathDOC = "";
5590a166ceSLiu Zhe 
5690a166ceSLiu Zhe 	/**
5790a166ceSLiu Zhe 	 * @throws java.lang.Exception
5890a166ceSLiu Zhe 	 */
5990a166ceSLiu Zhe 	@Before
setUp()6090a166ceSLiu Zhe 	public void setUp() throws Exception {
6190a166ceSLiu Zhe 		unoApp.start();
6290a166ceSLiu Zhe 
6390a166ceSLiu Zhe 		FileUtil.deleteFile(getPath("temp"));
6490a166ceSLiu Zhe 		temp = new File(getPath("temp"));
6590a166ceSLiu Zhe 		temp.mkdirs();
6690a166ceSLiu Zhe 
6790a166ceSLiu Zhe 		tempFilePathODT = temp + "/tempFilePathODT.odt";
6890a166ceSLiu Zhe 		tempFilePathDOC = temp + "/tempFilePathDOC.doc";
6990a166ceSLiu Zhe 	}
7090a166ceSLiu Zhe 
7190a166ceSLiu Zhe 	@After
tearDown()7290a166ceSLiu Zhe 	public void tearDown() throws Exception {
7390a166ceSLiu Zhe 		unoApp.close();
7490a166ceSLiu Zhe 	}
7590a166ceSLiu Zhe 
7690a166ceSLiu Zhe 	/**
7790a166ceSLiu Zhe 	 * test line break can be inserted and deleted.
7890a166ceSLiu Zhe 	 * when save it as odt file, close and reopen, line break can be inserted and deleted.
7990a166ceSLiu Zhe 	 * when save it as doc file, close and reopen, line break can be inserted and deleted.
8090a166ceSLiu Zhe 	 * \n represent line break
8190a166ceSLiu Zhe 	 * @throws Exception
8290a166ceSLiu Zhe 	 */
8390a166ceSLiu Zhe 	@Test
testInsertDeleteLineBreak()8490a166ceSLiu Zhe 	public void testInsertDeleteLineBreak() throws Exception
8590a166ceSLiu Zhe 	{
8690a166ceSLiu Zhe 		XComponent xComponent = unoApp.newDocument("swriter");
8790a166ceSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
8890a166ceSLiu Zhe 		this.insertNewLine(textDocument, "Line1", true);
8990a166ceSLiu Zhe 		this.insertNewLine(textDocument, "Line2", false);
9090a166ceSLiu Zhe 		this.insertNewLine(textDocument, "Line3", false);
9190a166ceSLiu Zhe 		this.insertNewLine(textDocument, "Line4", false);
9290a166ceSLiu Zhe 
9390a166ceSLiu Zhe 		int lineCount = this.getLineCount(textDocument);
9490a166ceSLiu Zhe 		Assert.assertEquals("Line break is inserted when new document.",4,lineCount);
9590a166ceSLiu Zhe 		this.deleteLineBreak(textDocument);
9690a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
9790a166ceSLiu Zhe 		Assert.assertEquals("Line break is deleted when new document", 3,lineCount);
9890a166ceSLiu Zhe 
9990a166ceSLiu Zhe 		//save to odt, test the line break
10090a166ceSLiu Zhe 		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));
10190a166ceSLiu Zhe 		unoApp.closeDocument(xComponent);
10290a166ceSLiu Zhe 		xComponent = unoApp.loadDocument(tempFilePathODT);
10390a166ceSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
10490a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
10590a166ceSLiu Zhe 		Assert.assertEquals("Line breaks when open saved odt file.",3,lineCount);
10690a166ceSLiu Zhe 		this.insertNewLine(textDocument, "Line added when open saved odt file", false);
10790a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
10890a166ceSLiu Zhe 		Assert.assertEquals("Line break is inserted when open saved odt file.",4,lineCount);
10990a166ceSLiu Zhe 		this.deleteLineBreak(textDocument);
11090a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
11190a166ceSLiu Zhe 		Assert.assertEquals("Line break is deleted when open saved odt file.",3,lineCount);
11290a166ceSLiu Zhe 
11390a166ceSLiu Zhe 		//save to doc, test the line break
11490a166ceSLiu Zhe 		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
11590a166ceSLiu Zhe 		unoApp.closeDocument(xComponent);
11690a166ceSLiu Zhe 		xComponent = unoApp.loadDocument(tempFilePathDOC);
11790a166ceSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
11890a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
11990a166ceSLiu Zhe 		Assert.assertEquals("Line breaks when open saved doc file.",3,lineCount);
12090a166ceSLiu Zhe 		this.insertNewLine(textDocument, "Line added when open saved doc file", false);
12190a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
12290a166ceSLiu Zhe 		Assert.assertEquals("Line break is inserted when open saved doc file.",4,lineCount);
12390a166ceSLiu Zhe 		this.deleteLineBreak(textDocument);
12490a166ceSLiu Zhe 		lineCount = this.getLineCount(textDocument);
12590a166ceSLiu Zhe 		Assert.assertEquals("Line break is deleted when open saved doc file.",3,lineCount);
12690a166ceSLiu Zhe 
12790a166ceSLiu Zhe 		unoApp.closeDocument(xComponent);
12890a166ceSLiu Zhe 	}
12990a166ceSLiu Zhe 
13090a166ceSLiu Zhe 	@Test
testInsertDeletePageBreak()13190a166ceSLiu Zhe 	public void testInsertDeletePageBreak() throws Exception
13290a166ceSLiu Zhe 	{
13390a166ceSLiu Zhe 		//new document, test page break
13490a166ceSLiu Zhe 		XComponent xComponent = unoApp.newDocument("swriter");
13590a166ceSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
13690a166ceSLiu Zhe 		this.insertNewPage(textDocument, "Page1", true);
13790a166ceSLiu Zhe 		this.insertNewPage(textDocument, "Page2", false);
13890a166ceSLiu Zhe 		this.insertNewPage(textDocument, "Page3", false);
13990a166ceSLiu Zhe 		int pageCount = SWUtil.getPageCount(textDocument);
14090a166ceSLiu Zhe 		Assert.assertEquals("page break is inserted when new document", 3,pageCount);
14190a166ceSLiu Zhe 
14290a166ceSLiu Zhe 		this.deleteFirstPage(textDocument);
14390a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
14490a166ceSLiu Zhe 		Assert.assertEquals("page break is deleted when new document", 2,pageCount);
14590a166ceSLiu Zhe 
14690a166ceSLiu Zhe 		//save as odt, test page break
14790a166ceSLiu Zhe 		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));
14890a166ceSLiu Zhe 		unoApp.closeDocument(xComponent);
14990a166ceSLiu Zhe 		xComponent = unoApp.loadDocument(tempFilePathODT);
15090a166ceSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
15190a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
15290a166ceSLiu Zhe 		Assert.assertEquals("Page breaks after open saved odt file", 2,pageCount);
15390a166ceSLiu Zhe 		this.insertNewPage(textDocument, "Page4", false);
15490a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
15590a166ceSLiu Zhe 		Assert.assertEquals("page break is inserted after open saved odt file", 3,pageCount);
15690a166ceSLiu Zhe 
15790a166ceSLiu Zhe 		this.deleteFirstPage(textDocument);
15890a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
15990a166ceSLiu Zhe 		Assert.assertEquals("page break is deleted after open saved odt file.", 2,pageCount);
16090a166ceSLiu Zhe 
16190a166ceSLiu Zhe 		//save as doc, test page break
16290a166ceSLiu Zhe 		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
16390a166ceSLiu Zhe 		unoApp.closeDocument(xComponent);
16490a166ceSLiu Zhe 		xComponent = unoApp.loadDocument(tempFilePathDOC);
16590a166ceSLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
16690a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
16790a166ceSLiu Zhe 		Assert.assertEquals("Page breaks after open saved doc file", 2,pageCount);
16890a166ceSLiu Zhe 
16990a166ceSLiu Zhe 		this.deleteFirstPage(textDocument);
17090a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
17190a166ceSLiu Zhe 		Assert.assertEquals("page break is deleted after open saved doc file.", 1,pageCount);
17290a166ceSLiu Zhe 
17390a166ceSLiu Zhe 		this.insertNewPage(textDocument, "Page5", false);
17490a166ceSLiu Zhe 		pageCount = SWUtil.getPageCount(textDocument);
17590a166ceSLiu Zhe 		Assert.assertEquals("page break is inserted after open saved doc file", 2,pageCount);
17690a166ceSLiu Zhe 
17790a166ceSLiu Zhe 		unoApp.closeDocument(xComponent);
17890a166ceSLiu Zhe 	}
17990a166ceSLiu Zhe 
18090a166ceSLiu Zhe 
18190a166ceSLiu Zhe 
18290a166ceSLiu Zhe 
18390a166ceSLiu Zhe 	/**
18490a166ceSLiu Zhe 	 * insert a new page at the end of the document
18590a166ceSLiu Zhe 	 * @param xTextDocument
18690a166ceSLiu Zhe 	 * @param pageContent
18790a166ceSLiu Zhe 	 * @param isFirstPage
18890a166ceSLiu Zhe 	 * @throws Exception
18990a166ceSLiu Zhe 	 */
insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage)19090a166ceSLiu Zhe 	private void insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage) throws Exception
19190a166ceSLiu Zhe 	{
19290a166ceSLiu Zhe 		XText xText = document.getText();
19390a166ceSLiu Zhe 		XTextCursor textCursor = xText.createTextCursor();
19490a166ceSLiu Zhe 		textCursor.gotoEnd(false);
19590a166ceSLiu Zhe 		System.out.println("The content before insert " + pageContent + ":" + xText.getString());
19690a166ceSLiu Zhe 		if(!isFirstPage)
19790a166ceSLiu Zhe 		{
19890a166ceSLiu Zhe 			XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
19990a166ceSLiu Zhe 			        XPropertySet.class, textCursor);
20090a166ceSLiu Zhe 			xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);
20190a166ceSLiu Zhe 			document.getText().insertControlCharacter(textCursor,ControlCharacter.PARAGRAPH_BREAK,false);
20290a166ceSLiu Zhe 		}
20390a166ceSLiu Zhe 		document.getText().insertString(textCursor, pageContent, false);
20490a166ceSLiu Zhe 	}
20590a166ceSLiu Zhe 
20690a166ceSLiu Zhe 	/**
20790a166ceSLiu Zhe 	 * delete the first page of the document
20890a166ceSLiu Zhe 	 * @param document
20990a166ceSLiu Zhe 	 * @throws Exception
21090a166ceSLiu Zhe 	 */
deleteFirstPage(XTextDocument document)21190a166ceSLiu Zhe 	private void deleteFirstPage(XTextDocument document) throws Exception
21290a166ceSLiu Zhe 	{
21390a166ceSLiu Zhe 		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, document);
21490a166ceSLiu Zhe         XController xController = xModel.getCurrentController();
21590a166ceSLiu Zhe         XTextViewCursorSupplier xTextViewCursorSupplier =
21690a166ceSLiu Zhe                 (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
21790a166ceSLiu Zhe         XTextViewCursor textViewCursor = xTextViewCursorSupplier.getViewCursor();
21890a166ceSLiu Zhe 
21990a166ceSLiu Zhe         XPageCursor pageCursor = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class, textViewCursor);
22090a166ceSLiu Zhe 
22190a166ceSLiu Zhe         // Move the cursor to the start of the document
22290a166ceSLiu Zhe         textViewCursor.gotoStart(false);
22390a166ceSLiu Zhe 
22490a166ceSLiu Zhe         pageCursor.jumpToFirstPage();
22590a166ceSLiu Zhe         XTextCursor textCursor = textViewCursor.getText().createTextCursorByRange(textViewCursor.getStart());
22690a166ceSLiu Zhe 
22790a166ceSLiu Zhe         pageCursor.jumpToEndOfPage();
22890a166ceSLiu Zhe         textCursor.gotoRange(textViewCursor.getEnd(), true);
22990a166ceSLiu Zhe         //System.out.println("deleted: " + textCursor.getString());
23090a166ceSLiu Zhe         textCursor.setString("");
23190a166ceSLiu Zhe 
23290a166ceSLiu Zhe      // Page contents cleared, now delete the page break at the start
23390a166ceSLiu Zhe         textCursor.collapseToStart();
23490a166ceSLiu Zhe         if(textCursor.goRight((short) 1, true)){
23590a166ceSLiu Zhe         	//System.out.println("page break deleted: " + textCursor.getString());
23690a166ceSLiu Zhe         	textCursor.setString("");
23790a166ceSLiu Zhe         }
23890a166ceSLiu Zhe 
23990a166ceSLiu Zhe 	}
24090a166ceSLiu Zhe 
24190a166ceSLiu Zhe 
24290a166ceSLiu Zhe 	/**
24390a166ceSLiu Zhe 	 * insert a new line at the end of the document.
24490a166ceSLiu Zhe 	 * @param xText
24590a166ceSLiu Zhe 	 * @param lineContent
24690a166ceSLiu Zhe 	 * @param isFirstLine
24790a166ceSLiu Zhe 	 * @throws Exception
24890a166ceSLiu Zhe 	 */
insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine)24990a166ceSLiu Zhe 	private void insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine) throws Exception
25090a166ceSLiu Zhe 	{
25190a166ceSLiu Zhe 		XText xText = xTextDocument.getText();
25290a166ceSLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
25390a166ceSLiu Zhe 		xTextCursor.gotoEnd(false);
25490a166ceSLiu Zhe 		if(!isFirstLine)
25590a166ceSLiu Zhe 		{
25690a166ceSLiu Zhe 			xText.insertControlCharacter(xTextCursor, ControlCharacter.LINE_BREAK, false);
25790a166ceSLiu Zhe 		}
25890a166ceSLiu Zhe 
25990a166ceSLiu Zhe 		xText.insertString(xTextCursor, lineContent, false);
26090a166ceSLiu Zhe 	}
26190a166ceSLiu Zhe 
26290a166ceSLiu Zhe 	/**
26390a166ceSLiu Zhe 	 * delete first line break
26490a166ceSLiu Zhe 	 * @param xText
26590a166ceSLiu Zhe 	 * @throws Exception
26690a166ceSLiu Zhe 	 */
deleteLineBreak(XTextDocument xTextDocument)26790a166ceSLiu Zhe 	private void deleteLineBreak(XTextDocument xTextDocument) throws Exception
26890a166ceSLiu Zhe 	{
26990a166ceSLiu Zhe 		XText xText = xTextDocument.getText();
27090a166ceSLiu Zhe 		String content = xText.getString();
27190a166ceSLiu Zhe 		content = content.replaceFirst("\n", "");
27290a166ceSLiu Zhe 		xText.setString(content);
27390a166ceSLiu Zhe 	}
27490a166ceSLiu Zhe 
27590a166ceSLiu Zhe 	/**
27690a166ceSLiu Zhe 	 * get line count of all text.
27790a166ceSLiu Zhe 	 * \n represent line break.
27890a166ceSLiu Zhe 	 * @param xText
27990a166ceSLiu Zhe 	 * @return count of line breaks
28090a166ceSLiu Zhe 	 * @throws Exception
28190a166ceSLiu Zhe 	 */
getLineCount(XTextDocument xTextDocument)28290a166ceSLiu Zhe 	private int getLineCount(XTextDocument xTextDocument) throws Exception
28390a166ceSLiu Zhe 	{
28490a166ceSLiu Zhe 		int count = 1;
28590a166ceSLiu Zhe 		XText xText = xTextDocument.getText();
28690a166ceSLiu Zhe 		String content = xText.getString();
28790a166ceSLiu Zhe 		int index = content.indexOf("\n");
28890a166ceSLiu Zhe 		while(index >=0)
28990a166ceSLiu Zhe 		{
29090a166ceSLiu Zhe 			count ++;
29190a166ceSLiu Zhe 			content = content.substring(index+1);
29290a166ceSLiu Zhe 			index = content.indexOf("\n");
29390a166ceSLiu Zhe 		}
29490a166ceSLiu Zhe 		return count;
29590a166ceSLiu Zhe 	}
29690a166ceSLiu Zhe 
29790a166ceSLiu Zhe }
298