Author: liuzhe
Date: Wed Aug 22 02:55:55 2012
New Revision: 1375882

URL: http://svn.apache.org/viewvc?rev=1375882&view=rev
Log:
#120630# - [testuno]Test PageNumber field create and load in wirter
Patch by: Zong Dong Jun <[email protected]>
Review by: Liu Zhe <[email protected]>

Modified:
    
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/PageNumberField.java

Modified: 
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/PageNumberField.java
URL: 
http://svn.apache.org/viewvc/incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/PageNumberField.java?rev=1375882&r1=1375881&r2=1375882&view=diff
==============================================================================
--- 
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/PageNumberField.java
 (original)
+++ 
incubator/ooo/trunk/main/test/testuno/source/testcase/uno/sw/field/PageNumberField.java
 Wed Aug 22 02:55:55 2012
@@ -20,6 +20,7 @@
  *************************************************************/
 package testcase.uno.sw.field;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.After;
 import org.junit.AfterClass;
@@ -36,6 +37,7 @@ 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;
@@ -48,16 +50,14 @@ public class PageNumberField {
        private static XTextDocument odtDocument = null;
        private static XTextDocument docDocument = null;
        private  static String odtSample = 
"testcase/uno/sw/field/PageNumberFieldTest.odt";
-       private static String odtSampleWorking = "";
        private static String docSample = 
"testcase/uno/sw/field/PageNumberFieldTest.doc";
-       private static String docSampleWorking = "";
        
        private  static String odtSaveAsDocSample = 
"testcase/uno/sw/field/PageNumberFieldTest_1.doc";
        private static String docSaveAsODTSample = 
"testcase/uno/sw/field/PageNumberFieldTest_1.odt";
        
        @Before
        public void setUpDocument() throws Exception {
-               app.start();
+
        }
 
        @After
@@ -65,10 +65,9 @@ public class PageNumberField {
        
        
        }
-       
        @BeforeClass
        public static void setUpConnection() throws Exception {
-               
+               app.start();
        }
 
 
@@ -77,22 +76,34 @@ public class PageNumberField {
                        Exception {
                app.close();
        }
-       
        /**
         * There is a bug : Bug 120625
         * Test Page Number Field Can created and Saved in odt file
         * 1.launch a odt document
         * 2.Create a page number field at end of this page
         * 3.Save and Reopen this document
+        * 4.Save it as doc format and reload
         * @throws Throwable
         */
        @Test
-       @Ignore("Bug 120625")
+       @Ignore
        public void testPageNumberFieldODT() throws Throwable {
-               odtSampleWorking = Testspace.prepareData(odtSample);
-               odtDocument = SWUtil.openDocument(odtSampleWorking, app);
-               createPageNumberFiled(odtDocument, 3, "odt");
+               odtDocument = 
SWUtil.openDocument(Testspace.prepareData(odtSample), app);
+               createPageNumberFiled(odtDocument);
+               int pageNumber = getPageNumber(odtDocument);
+               assertEquals("Verify page number created in exist odt sample 
file.", 3, pageNumber);
+               odtDocument = SWUtil.saveAndReload(odtDocument, app);
+               assertTrue("Test page number field still exist after odt sample 
file saved", isContainPageNumberField(odtDocument));
+               pageNumber = getPageNumber(odtDocument);
+               assertEquals("Verify page number value still exist after 
saved.", 3, pageNumber);
+               SWUtil.saveAsDoc(odtDocument, 
Testspace.getUrl(odtSaveAsDocSample));
                app.closeDocument(odtDocument);
+               docDocument = 
SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
+               
+               assertTrue("Test page number field still exist after odt sample 
file save as doc format", isContainPageNumberField(docDocument));
+               pageNumber = getPageNumber(docDocument);
+               assertEquals("Verify page number value still exist after saved 
as doc format.", 3, pageNumber);
+               app.closeDocument(docDocument);
        }
        
        /**
@@ -101,79 +112,74 @@ public class PageNumberField {
         * 1.launch a doc document
         * 2.Create a page number field at end of this page
         * 3.Save and Reopen this document, check page number field
+        * 3.Save as odt format and reload
         * @throws Throwable
         */
        @Test
-       @Ignore("Bug 120625")
        public void testPageNumberFieldDOC() throws Throwable {
-               docSampleWorking = Testspace.prepareData(docSample);
-               docDocument = SWUtil.openDocument(docSampleWorking, app);
-               createPageNumberFiled(docDocument, 2, "odt");
+               docDocument = 
SWUtil.openDocument(Testspace.prepareData(docSample), app);
+               createPageNumberFiled(docDocument);
+               int pageNumber = getPageNumber(docDocument);
+               assertEquals("Verify page number created in exist doc sample 
file.", 2, pageNumber);
+               docDocument = SWUtil.saveAndReload(docDocument, app);
+               assertTrue("Test page number field still exist after doc sample 
file saved", isContainPageNumberField(docDocument));
+               pageNumber = getPageNumber(docDocument);
+               assertEquals("Verify page number value still exist after 
saved.", 2, pageNumber);
+               SWUtil.saveAsODT(docDocument, 
Testspace.getUrl(docSaveAsODTSample));
                app.closeDocument(docDocument);
-       }
-       
-       /**
-        * Test Page Number Field in odt file save to doc format works well
-        * 1.Launch the new saved file 
-        * 2.Check page number filed.
-        * @throws Throwable
-        */
-       @Test
-       public void testSavedDoc2ODTPageNumberField() throws Throwable {
                odtDocument = 
SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app);
-               getPageNumberField(odtDocument, 2);
+               
+               assertTrue("Test page number field still exist after doc sample 
file save as odt format", isContainPageNumberField(odtDocument));
+               pageNumber = getPageNumber(odtDocument);
+               assertEquals("Verify page number value still exist after saved 
as doc format.", 2, pageNumber);
                app.closeDocument(odtDocument);
        }
        
+
+       
        /**
-        * Test Page Number Field in doc file save to odt format works well
-        * 1.Launch the new saved file 
-        * 2.Check page number filed.
-        * @throws Throwable
+        * Create a page number field at end of this document
+        * @param document
+        * @throws Exception
         */
-       @Test
-       public void testSavedODT2DOCPageNumberField() throws Throwable {
-               docDocument = 
SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app);
-               getPageNumberField(docDocument, 2);
-               app.closeDocument(docDocument);
-       }
-       
-       private void createPageNumberFiled(XTextDocument document, int 
expectNumber, String saveAsFormat) throws Exception {
+       private void createPageNumberFiled(XTextDocument document) throws 
Exception {
                XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) 
UnoRuntime.queryInterface(XMultiServiceFactory.class, document);
                XTextField  pageNumberFiled = 
(XTextField)UnoRuntime.queryInterface(XTextField.class, 
sevriceFactory.createInstance("com.sun.star.text.textfield.PageNumber"));
                
                XPropertySet props = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, pageNumberFiled);
-               props.setPropertyValue("NumberingType", 4);//Set page number 
display as Arabic
-               
+               props.setPropertyValue("NumberingType", 
NumberingType.ARABIC);//Set page number display as Arabic
        
                SWUtil.moveCuror2End(document);
                
document.getText().insertTextContent(document.getText().getEnd(), 
pageNumberFiled, true);
-
+               
+               
+       }
+       /**
+        * Get the page number by getText
+        * This page number is at end of this document
+        * @param document
+        * @return
+        */
+       private int getPageNumber(XTextDocument document) {
+               try {
+                       Thread.sleep(1000); //sleep before get page number 
field, there is a bug:120625
+               } catch (InterruptedException e) {
+                       e.printStackTrace();
+               }
                String documentString = document.getText().getString().trim();
                int length = documentString.length();
                String strNum = String.valueOf(documentString.charAt(length 
-1)); 
                int number = Integer.valueOf(strNum);
-               assertEquals("Test Page Number field can insert correctly", 
expectNumber, number);
-               SWUtil.save(document);
-               if("odt".equals(saveAsFormat)) {
-                       SWUtil.saveAsDoc(document, 
Testspace.getUrl(odtSaveAsDocSample));
-                       
-                       
-               } else if ("doc".equals(saveAsFormat)) {
-                       SWUtil.saveAsDoc(document, 
Testspace.getUrl(docSaveAsODTSample));
-                       
-               }
-               app.closeDocument(document);
-               
-               //Verify after save.
-               document = SWUtil.openDocument(odtSampleWorking, app);
-               
-               getPageNumberField(document, expectNumber);
-               
-               
+               return number;
        }
+
        
-       private void getPageNumberField(XTextDocument document, int 
expectNumber) throws Exception {
+       /**
+        * Check is contain page number field
+        * @param document
+        * @throws Exception
+        */
+       private boolean isContainPageNumberField(XTextDocument document) throws 
Exception {
                XTextFieldsSupplier fieldsSupplier = 
UnoRuntime.queryInterface(XTextFieldsSupplier.class, document);
                XEnumerationAccess xEnumeratedFields = 
fieldsSupplier.getTextFields();
                XEnumeration enumeration = 
xEnumeratedFields.createEnumeration();
@@ -181,16 +187,10 @@ public class PageNumberField {
                                Object field =  enumeration.nextElement();
                                XPropertySet props = 
(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field);
                                short numberType = (Short) 
props.getPropertyValue("NumberingType");
-                               assertEquals("Verify page number field type is 
Arabic", 4, numberType);
+                           return numberType == 4;
                        
                }
-               
-               String documentString = document.getText().getString().trim();
-               int length = documentString.length();
-               String strNum = String.valueOf(documentString.charAt(length 
-1)); 
-               int number = Integer.valueOf(strNum);
-               assertEquals("Test Page Number field can insert and saved 
correctly", expectNumber, number);
-               
+               return false;
                
        }
 }


Reply via email to