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.field; 22 23 import static org.junit.Assert.assertTrue; 24 25 import org.junit.After; 26 import org.junit.AfterClass; 27 import org.junit.Before; 28 import org.junit.BeforeClass; 29 import org.junit.Test; 30 import org.openoffice.test.common.Testspace; 31 import org.openoffice.test.uno.UnoApp; 32 33 import testlib.uno.SWUtil; 34 35 import com.sun.star.beans.XPropertySet; 36 import com.sun.star.container.XEnumeration; 37 import com.sun.star.container.XEnumerationAccess; 38 import com.sun.star.lang.XMultiServiceFactory; 39 import com.sun.star.text.XTextDocument; 40 import com.sun.star.text.XTextField; 41 import com.sun.star.text.XTextFieldsSupplier; 42 import com.sun.star.uno.UnoRuntime; 43 public class SubjectField { 44 45 private static final UnoApp app = new UnoApp(); 46 private static XTextDocument odtDocument = null; 47 private static XTextDocument docDocument = null; 48 private static String odtSample = "uno/sw/field/SubjectField.odt"; 49 private static String docSample = "uno/sw/field/Test_Sample.doc"; 50 51 private static String odtSaveAsDocSample = "uno/sw/field/odtSaveAsDOC.doc"; 52 private static String docSaveAsODTSample = "uno/sw/field/docSaveAsODT.odt"; 53 54 @Before 55 public void setUpDocument() throws Exception { 56 57 } 58 59 @After 60 public void tearDownDocument() { 61 62 63 } 64 65 @BeforeClass 66 public static void setUpConnection() throws Exception { 67 app.start(); 68 } 69 70 @AfterClass 71 public static void tearDownConnection() throws InterruptedException, 72 Exception { 73 app.close(); 74 } 75 76 /** 77 * 78 * Test Subject Field Can created and Saved in odt file 79 * 1.launch a odt document 80 * 2.Create a page count field at end of this page 81 * 3.Save and Reopen this document 82 * 4.Save it as doc format and reload 83 * @throws Throwable 84 */ 85 @Test 86 public void testSubjectFieldODT() throws Throwable { 87 odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app); 88 89 90 String subjectStr = "Test Subject Field"; 91 createSubjectFiled(odtDocument); 92 93 assertTrue("Verify Subject field is created use exsit subject", isContainSubjectField(odtDocument, "UNO API Subject")); 94 assertTrue("Verify Subject field is created use exsit subject, can get text from document", 95 isContainSubjectText(odtDocument, "UNO API Subject")); 96 //Set subject 97 SWUtil.setDocumentProperty(odtDocument, "Subject", subjectStr); 98 //Verfiy after set subject 99 assertTrue("Verify Subject field is created, can get text from document", 100 isContainSubjectText(odtDocument, subjectStr)); 101 102 odtDocument = SWUtil.saveAndReload(odtDocument, app); 103 //verify after save and reload 104 assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(odtDocument, subjectStr)); 105 assertTrue("Verify Subject field is created, can get text from document after saved.", 106 isContainSubjectText(odtDocument, subjectStr)); 107 SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample)); 108 app.closeDocument(odtDocument); 109 docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app); 110 assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(docDocument, subjectStr)); 111 assertTrue("Verify Subject field is created, after saved to doc format, can get text from document", 112 isContainSubjectText(docDocument, subjectStr)); 113 app.closeDocument(docDocument); 114 } 115 116 /** 117 * Test Subject Field Can created and Saved in Doc file 118 * 1.launch a doc document 119 * 2.Create a Subject field at end of this page 120 * 3.Save and Reopen this document, check Subject field 121 * 3.Save as odt format and reload 122 * @throws Throwable 123 */ 124 // @Test 125 // public void testPageCountFieldDOC() throws Throwable { 126 // docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app); 127 // String subjectStr = "Test Subject Field"; 128 // SWUtil.setDocumentProperty(docDocument, "Subject", subjectStr); 129 // createSubjectFiled(docDocument); 130 // 131 // assertTrue("Verify Subject field is created.", isContainSubjectField(docDocument, subjectStr)); 132 // assertTrue("Verify Subject field is created, can get text from document", 133 // isContainSubjectText(docDocument, subjectStr)); 134 // 135 // docDocument = SWUtil.saveAndReload(docDocument, app); 136 // //verify after save and reload 137 // assertTrue("Verify Subject field is created, after save, still exist.", isContainSubjectField(docDocument, subjectStr)); 138 // assertTrue("Verify Subject field is created, can get text from document after saved.", 139 // isContainSubjectText(docDocument, subjectStr)); 140 // SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample)); 141 // app.closeDocument(docDocument); 142 // odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app); 143 // assertTrue("Verify Subject field is created, after saved to doc format, field still exist.", isContainSubjectField(odtDocument, subjectStr)); 144 // assertTrue("Verify Subject field is created, after saved to doc format, can get text from document", 145 // isContainSubjectText(odtDocument, subjectStr)); 146 // app.closeDocument(odtDocument); 147 // } 148 149 150 151 /** 152 * Create a subject field at start of this document 153 * @param document 154 * @throws Exception 155 */ 156 private void createSubjectFiled(XTextDocument document) throws Exception { 157 158 XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 159 XTextField subjectField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.docinfo.Subject")); 160 161 162 SWUtil.moveCuror2Start(document); 163 document.getText().insertTextContent(document.getText().getStart(), subjectField, true); 164 165 } 166 167 168 /** 169 * Check is contain subject content at start of this document 170 * 171 * @param document 172 * @param content 173 * @return 174 */ 175 private boolean isContainSubjectText(XTextDocument document, String content) { 176 String documentString = document.getText().getString().trim(); 177 return documentString.indexOf(content) == 0; 178 } 179 180 /** 181 * Check is contain subject field 182 * @param document 183 * @throws Exception 184 */ 185 private boolean isContainSubjectField(XTextDocument document, String content) throws Exception { 186 XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 187 XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 188 XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 189 while (enumeration.hasMoreElements()) { 190 Object field = enumeration.nextElement(); 191 XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 192 String strContent = (String) props.getPropertyValue("Content"); 193 return content.equals(strContent); 194 195 } 196 return false; 197 198 } 199 200 } 201