You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openoffice.apache.org by li...@apache.org on 2012/08/27 05:31:49 UTC

svn commit: r1377561 - in /incubator/ooo/trunk/main/test/testuno/source: testcase/uno/sw/DocumentTest.java testcase/uno/sw/breaks/ testcase/uno/sw/breaks/CheckBreaks.java testlib/uno/SWUtil.java

Author: liuzhe
Date: Mon Aug 27 03:31:48 2012
New Revision: 1377561

URL: http://svn.apache.org/viewvc?rev=1377561&view=rev
Log:
#120703 - [testuno] - insert, delete of page break and line break
Patch by: Xiao Xiao Ting <ti...@gmail.com>
Review by: Liu Zhe <al...@gmail.com>

Added:
    incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/breaks/
    incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/breaks/CheckBreaks.java
Modified:
    incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/DocumentTest.java
    incubator/ooo/trunk/main/test/testuno/source/testlib/uno/SWUtil.java

Modified: incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/DocumentTest.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/DocumentTest.java?rev=1377561&r1=1377560&r2=1377561&view=diff
==============================================================================
--- incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/DocumentTest.java (original)
+++ incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/DocumentTest.java Mon Aug 27 03:31:48 2012
@@ -18,7 +18,10 @@ import com.sun.star.beans.XPropertySet;
 import com.sun.star.beans.PropertyValue;
 import com.sun.star.frame.*;
 import com.sun.star.uno.UnoRuntime;
+import com.sun.star.util.XCloseable;
 import com.sun.star.lang.XComponent;
+import com.sun.star.container.XEnumerationAccess;
+import com.sun.star.container.XEnumeration;
 
 
 public class DocumentTest {
@@ -64,8 +67,25 @@ public class DocumentTest {
 		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
 		return component;
 	}
+	
 
-
+	/**
+	 * test close document
+	 * @throws Exception
+	 */
+	@Test
+	public void testCloseDocument() throws Exception
+	{
+		XComponent component = unoApp.newDocument("swriter");
+		unoApp.closeDocument(component);
+		XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();		
+		Assert.assertTrue("Document has been closed.",xModel==null);
+	}
+	
+	/**
+	 * test new document
+	 * @throws Exception
+	 */
 	@Test
 	public void testNewDocument() throws Exception
 	{
@@ -78,7 +98,10 @@ public class DocumentTest {
 		unoApp.closeDocument(textDocument);
 	}
 	
-	
+	/**
+	 * test new document from template
+	 * @throws Exception
+	 */
 	@Test
 	public void testNewDocumentFromTemplate() throws Exception
 	{		
@@ -99,6 +122,10 @@ public class DocumentTest {
         unoApp.closeDocument(textDocument);
 	}
 	
+	/**
+	 * test save document as odt
+	 * @throws Exception
+	 */
 	@Test
 	public void testSaveDocument() throws Exception
 	{			
@@ -126,6 +153,10 @@ public class DocumentTest {
         unoApp.closeDocument(textDocument);
 	}
 	
+	/**
+	 * test save document as doc
+	 * @throws Exception
+	 */
 	@Test
 	public void testSaveAsDocument() throws Exception
 	{		
@@ -155,6 +186,10 @@ public class DocumentTest {
         unoApp.closeDocument(textDocument);
 	}
 	
+	/**
+	 * test export document as pdf
+	 * @throws Exception
+	 */
 	@Test
 	public void testExportAsPDF() throws Exception
 	{		
@@ -184,6 +219,10 @@ public class DocumentTest {
         unoApp.closeDocument(textDocument);
 	}
 	
+	/**
+	 * test save document as template
+	 * @throws Exception
+	 */
 	@Test
 	public void testSaveAsTemplate() throws Exception
 	{

Added: incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/breaks/CheckBreaks.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/breaks/CheckBreaks.java?rev=1377561&view=auto
==============================================================================
--- incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/breaks/CheckBreaks.java (added)
+++ incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/breaks/CheckBreaks.java Mon Aug 27 03:31:48 2012
@@ -0,0 +1,297 @@
+/**************************************************************
+ * 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * 
+ *************************************************************/
+package testcase.uno.sw.breaks;
+
+import static org.openoffice.test.common.Testspace.*;
+
+import java.io.File;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.Assert;
+import org.openoffice.test.common.FileUtil;
+import org.openoffice.test.uno.UnoApp;
+
+import testlib.uno.SWUtil;
+
+import com.sun.star.text.XTextDocument;
+import com.sun.star.text.XTextCursor;
+import com.sun.star.text.XText;
+import com.sun.star.text.ControlCharacter;
+import com.sun.star.text.XTextViewCursor;
+import com.sun.star.text.XPageCursor;
+import com.sun.star.text.XTextViewCursorSupplier;
+import com.sun.star.style.BreakType;
+import com.sun.star.beans.XPropertySet;
+import com.sun.star.frame.*;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.lang.XComponent;
+
+public class CheckBreaks {
+	UnoApp unoApp = new UnoApp();
+	XTextDocument textDocument = null;
+	File temp = null;
+	String tempFilePathODT = "";	
+	String tempFilePathDOC = "";
+	
+	/**
+	 * @throws java.lang.Exception
+	 */
+	@Before
+	public void setUp() throws Exception {
+		unoApp.start();
+		
+		FileUtil.deleteFile(getPath("temp"));
+		temp = new File(getPath("temp"));
+		temp.mkdirs();		
+		
+		tempFilePathODT = temp + "/tempFilePathODT.odt";
+		tempFilePathDOC = temp + "/tempFilePathDOC.doc";		
+	}
+
+	@After
+	public void tearDown() throws Exception {
+		unoApp.close();
+	}	
+	
+	/**
+	 * test line break can be inserted and deleted.
+	 * when save it as odt file, close and reopen, line break can be inserted and deleted.
+	 * when save it as doc file, close and reopen, line break can be inserted and deleted.
+	 * \n represent line break
+	 * @throws Exception
+	 */
+	@Test
+	public void testInsertDeleteLineBreak() throws Exception
+	{
+		XComponent xComponent = unoApp.newDocument("swriter");
+		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);		
+		this.insertNewLine(textDocument, "Line1", true);
+		this.insertNewLine(textDocument, "Line2", false);
+		this.insertNewLine(textDocument, "Line3", false);
+		this.insertNewLine(textDocument, "Line4", false);
+		
+		int lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line break is inserted when new document.",4,lineCount);
+		this.deleteLineBreak(textDocument);
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line break is deleted when new document", 3,lineCount);
+		
+		//save to odt, test the line break
+		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));		
+		unoApp.closeDocument(xComponent);	
+		xComponent = unoApp.loadDocument(tempFilePathODT);
+		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line breaks when open saved odt file.",3,lineCount);
+		this.insertNewLine(textDocument, "Line added when open saved odt file", false);
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line break is inserted when open saved odt file.",4,lineCount);
+		this.deleteLineBreak(textDocument);
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line break is deleted when open saved odt file.",3,lineCount);
+		
+		//save to doc, test the line break
+		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
+		unoApp.closeDocument(xComponent);
+		xComponent = unoApp.loadDocument(tempFilePathDOC);
+		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);		
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line breaks when open saved doc file.",3,lineCount);
+		this.insertNewLine(textDocument, "Line added when open saved doc file", false);
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line break is inserted when open saved doc file.",4,lineCount);
+		this.deleteLineBreak(textDocument);
+		lineCount = this.getLineCount(textDocument);
+		Assert.assertEquals("Line break is deleted when open saved doc file.",3,lineCount);
+		
+		unoApp.closeDocument(xComponent);		
+	}
+	
+	@Test
+	public void testInsertDeletePageBreak() throws Exception
+	{
+		//new document, test page break
+		XComponent xComponent = unoApp.newDocument("swriter");
+		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);		
+		this.insertNewPage(textDocument, "Page1", true);
+		this.insertNewPage(textDocument, "Page2", false);
+		this.insertNewPage(textDocument, "Page3", false);		
+		int pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("page break is inserted when new document", 3,pageCount);
+		
+		this.deleteFirstPage(textDocument);
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("page break is deleted when new document", 2,pageCount);
+		
+		//save as odt, test page break
+		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));		
+		unoApp.closeDocument(xComponent);	
+		xComponent = unoApp.loadDocument(tempFilePathODT);
+		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("Page breaks after open saved odt file", 2,pageCount);		
+		this.insertNewPage(textDocument, "Page4", false);
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("page break is inserted after open saved odt file", 3,pageCount);		
+		
+		this.deleteFirstPage(textDocument);
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("page break is deleted after open saved odt file.", 2,pageCount);
+		
+		//save as doc, test page break
+		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
+		unoApp.closeDocument(xComponent);	
+		xComponent = unoApp.loadDocument(tempFilePathDOC);
+		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("Page breaks after open saved doc file", 2,pageCount);
+		
+		this.deleteFirstPage(textDocument);
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("page break is deleted after open saved doc file.", 1,pageCount);		
+		
+		this.insertNewPage(textDocument, "Page5", false);		
+		pageCount = SWUtil.getPageCount(textDocument);
+		Assert.assertEquals("page break is inserted after open saved doc file", 2,pageCount);			
+			
+		unoApp.closeDocument(xComponent);			
+	}	
+	
+	
+	
+	
+	/**
+	 * insert a new page at the end of the document
+	 * @param xTextDocument
+	 * @param pageContent
+	 * @param isFirstPage
+	 * @throws Exception
+	 */
+	private void insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage) throws Exception
+	{	
+		XText xText = document.getText();
+		XTextCursor textCursor = xText.createTextCursor();
+		textCursor.gotoEnd(false);
+		System.out.println("The content before insert " + pageContent + ":" + xText.getString());
+		if(!isFirstPage)
+		{			
+			XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
+			        XPropertySet.class, textCursor);
+			xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);		
+			document.getText().insertControlCharacter(textCursor,ControlCharacter.PARAGRAPH_BREAK,false); 		
+		}			
+		document.getText().insertString(textCursor, pageContent, false);
+	}
+	
+	/**
+	 * delete the first page of the document
+	 * @param document
+	 * @throws Exception
+	 */
+	private void deleteFirstPage(XTextDocument document) throws Exception
+	{	
+		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, document);
+        XController xController = xModel.getCurrentController();
+        XTextViewCursorSupplier xTextViewCursorSupplier =
+                (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
+        XTextViewCursor textViewCursor = xTextViewCursorSupplier.getViewCursor();
+
+        XPageCursor pageCursor = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class, textViewCursor);
+
+        // Move the cursor to the start of the document
+        textViewCursor.gotoStart(false);
+
+        pageCursor.jumpToFirstPage();
+        XTextCursor textCursor = textViewCursor.getText().createTextCursorByRange(textViewCursor.getStart());
+
+        pageCursor.jumpToEndOfPage();        
+        textCursor.gotoRange(textViewCursor.getEnd(), true); 
+        //System.out.println("deleted: " + textCursor.getString());
+        textCursor.setString("");
+		
+     // Page contents cleared, now delete the page break at the start
+        textCursor.collapseToStart();
+        if(textCursor.goRight((short) 1, true)){
+        	//System.out.println("page break deleted: " + textCursor.getString());
+        	textCursor.setString("");
+        }       
+		
+	}	
+	
+	
+	/**
+	 * insert a new line at the end of the document.
+	 * @param xText
+	 * @param lineContent
+	 * @param isFirstLine
+	 * @throws Exception
+	 */
+	private void insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine) throws Exception
+	{
+		XText xText = xTextDocument.getText();
+		XTextCursor xTextCursor = xText.createTextCursor();
+		xTextCursor.gotoEnd(false);
+		if(!isFirstLine)
+		{
+			xText.insertControlCharacter(xTextCursor, ControlCharacter.LINE_BREAK, false);			
+		}
+		
+		xText.insertString(xTextCursor, lineContent, false);
+	}
+	
+	/**
+	 * delete first line break
+	 * @param xText
+	 * @throws Exception
+	 */
+	private void deleteLineBreak(XTextDocument xTextDocument) throws Exception
+	{
+		XText xText = xTextDocument.getText();
+		String content = xText.getString();
+		content = content.replaceFirst("\n", "");
+		xText.setString(content);
+	}
+	
+	/**
+	 * get line count of all text.
+	 * \n represent line break. 
+	 * @param xText
+	 * @return count of line breaks
+	 * @throws Exception
+	 */
+	private int getLineCount(XTextDocument xTextDocument) throws Exception
+	{
+		int count = 1;	
+		XText xText = xTextDocument.getText();
+		String content = xText.getString();
+		int index = content.indexOf("\n");
+		while(index >=0)
+		{
+			count ++;
+			content = content.substring(index+1);
+			index = content.indexOf("\n");
+		}
+		return count;
+	}
+
+}

Modified: incubator/ooo/trunk/main/test/testuno/source/testlib/uno/SWUtil.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testlib/uno/SWUtil.java?rev=1377561&r1=1377560&r2=1377561&view=diff
==============================================================================
--- incubator/ooo/trunk/main/test/testuno/source/testlib/uno/SWUtil.java (original)
+++ incubator/ooo/trunk/main/test/testuno/source/testlib/uno/SWUtil.java Mon Aug 27 03:31:48 2012
@@ -25,16 +25,22 @@ import org.openoffice.test.uno.UnoApp;
 
 import com.sun.star.beans.PropertyValue;
 import com.sun.star.beans.XPropertySet;
+import com.sun.star.container.XEnumeration;
+import com.sun.star.container.XEnumerationAccess;
 import com.sun.star.container.XNamed;
 import com.sun.star.document.XDocumentInfo;
 import com.sun.star.document.XDocumentInfoSupplier;
 import com.sun.star.frame.XStorable;
 import com.sun.star.io.IOException;
 import com.sun.star.lang.XMultiServiceFactory;
+import com.sun.star.style.BreakType;
+import com.sun.star.text.ControlCharacter;
 import com.sun.star.text.XText;
 import com.sun.star.text.XTextContent;
 import com.sun.star.text.XTextCursor;
 import com.sun.star.text.XTextDocument;
+import com.sun.star.frame.XModel;
+import com.sun.star.frame.XController;
 import com.sun.star.uno.UnoRuntime;
 
 public class SWUtil {
@@ -133,4 +139,50 @@ public class SWUtil {
 		xBookmarkAsNamed.setName(bookmarkName);
 		document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true);
 	}
+	
+	/**
+	 * insert column break in current cursor
+	 * @param xText
+	 * @param currentCursor
+	 * @throws Exception
+	 */
+	public static void insertColumnBreak(XText xText, XTextCursor currentCursor) throws Exception
+	{
+		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
+		        XPropertySet.class, currentCursor);
+		xCursorProps.setPropertyValue("BreakType", BreakType.COLUMN_AFTER);		
+	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 
+	}
+	
+	/**
+	 * insert page break in current cursor
+	 * @param xText
+	 * @param currentCursor
+	 * @throws Exception
+	 */
+	public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception
+	{
+		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
+		        XPropertySet.class, currentCursor);
+		xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);		
+	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 
+	}	
+	
+	
+	/**
+	 * get page count
+	 * @param document
+	 * @return
+	 * @throws Exception
+	 */
+	public static int getPageCount(XTextDocument document) throws Exception
+	{
+		XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document);
+		XController xcont = xmodel.getCurrentController();
+
+		XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont);
+		Integer pageCount = (Integer) xps.getPropertyValue("PageCount"); 
+		return pageCount.intValue();
+	}
+	
 }