Kaydet (Commit) d79eaa7f authored tarafından Liu Zhe's avatar Liu Zhe

#120660 - [testuno]Test create, load, save Page Count field

Patch by: Zong Dong Jun <zongdj001@gmail.com>
Review by: Liu Zhe <aliuzhe@gmail.com>
üst 37b68025
This diff was suppressed by a .gitattributes entry.
/**************************************************************
*
* 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.field;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openoffice.test.common.Testspace;
import org.openoffice.test.uno.UnoApp;
import testlib.uno.SWUtil;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XEnumerationAccess;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.style.NumberingType;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextField;
import com.sun.star.text.XTextFieldsSupplier;
import com.sun.star.uno.UnoRuntime;
public class PageCountField {
private static final UnoApp app = new UnoApp();
private static XTextDocument odtDocument = null;
private static XTextDocument docDocument = null;
private static String odtSample = "testcase/uno/sw/field/PageCountField.odt";
private static String docSample = "testcase/uno/sw/field/PageCountField.doc";
private static String odtSaveAsDocSample = "testcase/uno/sw/field/PageCountFieldNewSave.doc";
private static String docSaveAsODTSample = "testcase/uno/sw/field/PageCountFieldNewSave.odt";
@Before
public void setUpDocument() throws Exception {
}
@After
public void tearDownDocument() {
}
@BeforeClass
public static void setUpConnection() throws Exception {
app.start();
}
@AfterClass
public static void tearDownConnection() throws InterruptedException,
Exception {
app.close();
}
/**
*
* Test Page count Field Can created and Saved in odt file
* 1.launch a odt document
* 2.Create a page count field at end of this page
* 3.Save and Reopen this document
* 4.Save it as doc format and reload
* @throws Throwable
*/
@Test
public void testPageCountFieldODT() throws Throwable {
odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app);
createPageCountField(odtDocument);
int PageCount = getPageCount(odtDocument);
assertEquals("Verify page count created in exist odt sample file.", 3, PageCount);
odtDocument = SWUtil.saveAndReload(odtDocument, app);
assertTrue("Test page count field still exist after odt sample file saved", isContainPageCountField(odtDocument));
PageCount = getPageCount(odtDocument);
assertEquals("Verify page count value still exist after saved.", 3, PageCount);
SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample));
app.closeDocument(odtDocument);
docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
assertTrue("Test page count field still exist after odt sample file save as doc format", isContainPageCountField(docDocument));
PageCount = getPageCount(docDocument);
assertEquals("Verify page count value still exist after saved as doc format.", 3, PageCount);
app.closeDocument(docDocument);
}
/**
* Test Page count Field Can created and Saved in Doc file
* 1.launch a doc document
* 2.Create a page count field at end of this page
* 3.Save and Reopen this document, check page count field
* 3.Save as odt format and reload
* @throws Throwable
*/
@Test
public void testPageCountFieldDOC() throws Throwable {
docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app);
createPageCountField(docDocument);
int PageCount = getPageCount(docDocument);
assertEquals("Verify page count created in exist doc sample file.", 4, PageCount);
docDocument = SWUtil.saveAndReload(docDocument, app);
assertTrue("Test page count field still exist after doc sample file saved", isContainPageCountField(docDocument));
PageCount = getPageCount(docDocument);
assertEquals("Verify page count value still exist after saved.", 4, PageCount);
SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample));
app.closeDocument(docDocument);
odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
assertTrue("Test page count field still exist after doc sample file save as odt format", isContainPageCountField(odtDocument));
PageCount = getPageCount(odtDocument);
assertEquals("Verify page count value still exist after saved as doc format.", 4, PageCount);
app.closeDocument(odtDocument);
}
/**
* Create a page count field at start of this document
* @param document
* @throws Exception
*/
private void createPageCountField(XTextDocument document) throws Exception {
XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
XTextField PageCountField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.PageCount"));
XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, PageCountField);
props.setPropertyValue("NumberingType", NumberingType.ARABIC);//Set page count display as Arabic
SWUtil.moveCuror2Start(document);
document.getText().insertTextContent(document.getText().getStart(), PageCountField, true);
}
/**
* Get the page count by getText
* This page count is at end of this document
* @param document
* @return
*/
private int getPageCount(XTextDocument document) {
String documentString = document.getText().getString().trim();
String strNum = String.valueOf(documentString.charAt(0));
int count = Integer.valueOf(strNum);
return count;
}
/**
* Check is contain page count field
* @param document
* @throws Exception
*/
private boolean isContainPageCountField(XTextDocument document) throws Exception {
XTextFieldsSupplier fieldsSupplier = UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields();
XEnumeration enumeration = xEnumeratedFields.createEnumeration();
while (enumeration.hasMoreElements()) {
Object field = enumeration.nextElement();
XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
short countType = (Short) props.getPropertyValue("NumberingType");
return countType == NumberingType.ARABIC;
}
return false;
}
}
......@@ -26,7 +26,6 @@ import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.openoffice.test.common.Testspace;
import org.openoffice.test.uno.UnoApp;
......@@ -86,7 +85,7 @@ public class PageNumberField {
* @throws Throwable
*/
@Test
@Ignore
public void testPageNumberFieldODT() throws Throwable {
odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app);
createPageNumberFiled(odtDocument);
......
......@@ -20,10 +20,14 @@
*************************************************************/
package testlib.uno;
import org.openoffice.test.uno.UnoApp;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
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;
......@@ -36,24 +40,20 @@ import com.sun.star.uno.UnoRuntime;
public class SWUtil {
public static void saveAsDoc(XTextDocument document, String url) throws IOException {
saveAs(document, "MS Word 97", url);
}
public static void saveAsODT(XTextDocument document, String url) throws IOException {
saveAs(document, "writer8", url);
}
public static void save(XTextDocument document) throws IOException {
XStorable store = UnoRuntime.queryInterface(XStorable.class, document);
store.store();
}
public static void saveAs(XTextDocument document, String filterValue, String url) throws IOException {
XStorable store = UnoRuntime.queryInterface(XStorable.class, document);
PropertyValue[] propsValue = new PropertyValue[1];
propsValue[0] = new PropertyValue();
propsValue[0].Name = "FilterName";
......@@ -62,21 +62,35 @@ public class SWUtil {
}
public static void save(XTextDocument document) throws IOException {
XStorable store = UnoRuntime.queryInterface(XStorable.class, document);
store.store();
}
public static XTextDocument saveAndReload(XTextDocument document, UnoApp app) throws Exception {
XStorable store = UnoRuntime.queryInterface(XStorable.class, document);
store.store();
String url = document.getURL();
app.closeDocument(document);
return openDocumentFromURL(url, app);
}
public static XTextDocument newDocument(UnoApp app) throws Exception {
return UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));
}
public static XTextDocument openDocumentFromURL(String url, UnoApp app) throws Exception {
return UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocumentFromURL(url));
}
public static XTextDocument openDocument(String filePath, UnoApp app) throws Exception {
return UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(filePath));
return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(filePath));
}
public static void moveCuror2End(XTextDocument document) {
XText xText = document.getText();
XTextCursor xTextCursor = xText.createTextCursor();
......@@ -89,6 +103,20 @@ public class SWUtil {
xTextCursor.gotoStart(false);
}
/**
* Set document properties. such as subject, title etc
* @param document - set document information on this document
* @param prop - document information, including "Subject" ,"Title", "Author", "Title", "KeyWords"
* @param propValue - value you want to set for prop
* @throws Exception
*/
public static void setDocumentProperty(XTextDocument document, String prop, String propValue) throws Exception {
XDocumentInfoSupplier docInfoSupplier = UnoRuntime.queryInterface(XDocumentInfoSupplier.class, document);
XDocumentInfo docInfo = docInfoSupplier.getDocumentInfo();
XPropertySet propsDocInfo = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, docInfo);
propsDocInfo.setPropertyValue(prop, propValue);
}
/**
* Insert a bookmark into text document
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment