1a624bbf9SLiu Zhe /************************************************************** 2a624bbf9SLiu Zhe * 3a624bbf9SLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4a624bbf9SLiu Zhe * or more contributor license agreements. See the NOTICE file 5a624bbf9SLiu Zhe * distributed with this work for additional information 6a624bbf9SLiu Zhe * regarding copyright ownership. The ASF licenses this file 7a624bbf9SLiu Zhe * to you under the Apache License, Version 2.0 (the 8a624bbf9SLiu Zhe * "License"); you may not use this file except in compliance 9a624bbf9SLiu Zhe * with the License. You may obtain a copy of the License at 10a624bbf9SLiu Zhe * 11a624bbf9SLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12a624bbf9SLiu Zhe * 13a624bbf9SLiu Zhe * Unless required by applicable law or agreed to in writing, 14a624bbf9SLiu Zhe * software distributed under the License is distributed on an 15a624bbf9SLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16a624bbf9SLiu Zhe * KIND, either express or implied. See the License for the 17a624bbf9SLiu Zhe * specific language governing permissions and limitations 18a624bbf9SLiu Zhe * under the License. 19a624bbf9SLiu Zhe * 20a624bbf9SLiu Zhe *************************************************************/ 21eba4d44aSLiu Zhe package fvt.uno.sw.field; 22a624bbf9SLiu Zhe 23a624bbf9SLiu Zhe import static org.junit.Assert.*; 24a624bbf9SLiu Zhe 25a624bbf9SLiu Zhe import org.junit.After; 26a624bbf9SLiu Zhe import org.junit.AfterClass; 27a624bbf9SLiu Zhe import org.junit.Before; 28a624bbf9SLiu Zhe import org.junit.BeforeClass; 29a624bbf9SLiu Zhe import org.junit.Test; 30a624bbf9SLiu Zhe 31a624bbf9SLiu Zhe import org.openoffice.test.common.Testspace; 32a624bbf9SLiu Zhe import org.openoffice.test.uno.UnoApp; 33a624bbf9SLiu Zhe 34a624bbf9SLiu Zhe import testlib.uno.SWUtil; 35a624bbf9SLiu Zhe 36a624bbf9SLiu Zhe import com.sun.star.beans.XPropertySet; 37a624bbf9SLiu Zhe import com.sun.star.container.XEnumeration; 38a624bbf9SLiu Zhe import com.sun.star.container.XEnumerationAccess; 39a624bbf9SLiu Zhe import com.sun.star.lang.XMultiServiceFactory; 40a624bbf9SLiu Zhe import com.sun.star.text.XTextDocument; 41a624bbf9SLiu Zhe import com.sun.star.text.XTextField; 42a624bbf9SLiu Zhe import com.sun.star.text.XTextFieldsSupplier; 43a624bbf9SLiu Zhe import com.sun.star.uno.UnoRuntime; 44a624bbf9SLiu Zhe public class TitleField { 45a624bbf9SLiu Zhe 46a624bbf9SLiu Zhe private static final UnoApp app = new UnoApp(); 47a624bbf9SLiu Zhe private static XTextDocument odtDocument = null; 48a624bbf9SLiu Zhe private static XTextDocument docDocument = null; 49a624bbf9SLiu Zhe private static String odtSample = "uno/sw/field/Test_Sample.odt"; 50a624bbf9SLiu Zhe private static String docSample = "uno/sw/field/Test_Sample.doc"; 51a624bbf9SLiu Zhe 52a624bbf9SLiu Zhe private static String odtSaveAsDocSample = "uno/sw/field/odtSaveAsDoc.doc"; 53a624bbf9SLiu Zhe private static String docSaveAsODTSample = "uno/sw/field/docSaveAsODT.odt"; 54a624bbf9SLiu Zhe 55a624bbf9SLiu Zhe @Before 56a624bbf9SLiu Zhe public void setUpDocument() throws Exception { 57a624bbf9SLiu Zhe 58a624bbf9SLiu Zhe } 59a624bbf9SLiu Zhe 60a624bbf9SLiu Zhe @After 61a624bbf9SLiu Zhe public void tearDownDocument() { 62a624bbf9SLiu Zhe 63a624bbf9SLiu Zhe 64a624bbf9SLiu Zhe } 65a624bbf9SLiu Zhe 66a624bbf9SLiu Zhe @BeforeClass 67a624bbf9SLiu Zhe public static void setUpConnection() throws Exception { 68a624bbf9SLiu Zhe app.start(); 69a624bbf9SLiu Zhe } 70a624bbf9SLiu Zhe 71a624bbf9SLiu Zhe @AfterClass 72a624bbf9SLiu Zhe public static void tearDownConnection() throws InterruptedException, 73a624bbf9SLiu Zhe Exception { 74a624bbf9SLiu Zhe app.close(); 75a624bbf9SLiu Zhe } 76a624bbf9SLiu Zhe 77a624bbf9SLiu Zhe /** 78a624bbf9SLiu Zhe * 79a624bbf9SLiu Zhe * Test Title Field Can created and Saved in odt file 80a624bbf9SLiu Zhe * 1.launch a odt document 81a624bbf9SLiu Zhe * 2.Create a title field at end of this page 82a624bbf9SLiu Zhe * 3.Save and Reopen this document 83a624bbf9SLiu Zhe * 4.Save it as doc format and reload 84a624bbf9SLiu Zhe * @throws Throwable 85a624bbf9SLiu Zhe */ 86a624bbf9SLiu Zhe @Test 87a624bbf9SLiu Zhe public void testTitleFieldODT() throws Throwable { 88a624bbf9SLiu Zhe odtDocument = SWUtil.openDocument(Testspace.prepareData(odtSample), app); 89a624bbf9SLiu Zhe 90a624bbf9SLiu Zhe 91a624bbf9SLiu Zhe String titleStr = "Title Test"; 92a624bbf9SLiu Zhe createTitleFiled(odtDocument); 93a624bbf9SLiu Zhe 94a624bbf9SLiu Zhe assertTrue("Verify Title field is created use exsit Title", isContainTitleField(odtDocument, titleStr)); 95a624bbf9SLiu Zhe assertTrue("Verify Title field is created use exsit Title, can get text from document", 96a624bbf9SLiu Zhe isContainTitleText(odtDocument, titleStr)); 97a624bbf9SLiu Zhe //Set Title 98a624bbf9SLiu Zhe SWUtil.setDocumentProperty(odtDocument, "Title", titleStr); 99a624bbf9SLiu Zhe //Verfiy after set Title 100a624bbf9SLiu Zhe assertTrue("Verify Title field is created, can get text from document", 101a624bbf9SLiu Zhe isContainTitleText(odtDocument, titleStr)); 102a624bbf9SLiu Zhe 103a624bbf9SLiu Zhe odtDocument = SWUtil.saveAndReload(odtDocument, app); 104a624bbf9SLiu Zhe //verify after save and reload 105a624bbf9SLiu Zhe assertTrue("Verify Title field is created, after save, still exist.", isContainTitleField(odtDocument, titleStr)); 106a624bbf9SLiu Zhe assertTrue("Verify Title field is created, can get text from document after saved.", 107a624bbf9SLiu Zhe isContainTitleText(odtDocument, titleStr)); 108a624bbf9SLiu Zhe SWUtil.saveAsDoc(odtDocument, Testspace.getUrl(odtSaveAsDocSample)); 109a624bbf9SLiu Zhe app.closeDocument(odtDocument); 110a624bbf9SLiu Zhe docDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(odtSaveAsDocSample), app); 111a624bbf9SLiu Zhe assertTrue("Verify Title field is created, after saved to doc format, field still exist.", isContainTitleField(docDocument, titleStr)); 112a624bbf9SLiu Zhe assertTrue("Verify Title field is created, after saved to doc format, can get text from document", 113a624bbf9SLiu Zhe isContainTitleText(docDocument, titleStr)); 114a624bbf9SLiu Zhe app.closeDocument(docDocument); 115a624bbf9SLiu Zhe } 116a624bbf9SLiu Zhe 117a624bbf9SLiu Zhe /** 118a624bbf9SLiu Zhe * Test Title Field Can created and Saved in Doc file 119a624bbf9SLiu Zhe * 1.launch a doc document 120a624bbf9SLiu Zhe * 2.Create a Title field at end of this page 121a624bbf9SLiu Zhe * 3.Save and Reopen this document, check Title field 122a624bbf9SLiu Zhe * 3.Save as odt format and reload 123a624bbf9SLiu Zhe * @throws Throwable 124a624bbf9SLiu Zhe */ 125*3ecf08d2SLi Feng Wang // @Test 126*3ecf08d2SLi Feng Wang // public void testPageCountFieldDOC() throws Throwable { 127*3ecf08d2SLi Feng Wang // docDocument = SWUtil.openDocument(Testspace.prepareData(docSample), app); 128*3ecf08d2SLi Feng Wang // String TitleStr = "Title Test"; 129*3ecf08d2SLi Feng Wang // SWUtil.setDocumentProperty(docDocument, "Title", TitleStr); 130*3ecf08d2SLi Feng Wang // createTitleFiled(docDocument); 131*3ecf08d2SLi Feng Wang // 132*3ecf08d2SLi Feng Wang // assertTrue("Verify Title field is created.", isContainTitleField(docDocument, TitleStr)); 133*3ecf08d2SLi Feng Wang // assertTrue("Verify Title field is created, can get text from document", 134*3ecf08d2SLi Feng Wang // isContainTitleText(docDocument, TitleStr)); 135*3ecf08d2SLi Feng Wang // 136*3ecf08d2SLi Feng Wang // docDocument = SWUtil.saveAndReload(docDocument, app); 137*3ecf08d2SLi Feng Wang // //verify after save and reload 138*3ecf08d2SLi Feng Wang // assertTrue("Verify Title field is created, after save, still exist.", isContainTitleField(docDocument, TitleStr)); 139*3ecf08d2SLi Feng Wang // assertTrue("Verify Title field is created, can get text from document after saved.", 140*3ecf08d2SLi Feng Wang // isContainTitleText(docDocument, TitleStr)); 141*3ecf08d2SLi Feng Wang // SWUtil.saveAsODT(docDocument, Testspace.getUrl(docSaveAsODTSample)); 142*3ecf08d2SLi Feng Wang // app.closeDocument(docDocument); 143*3ecf08d2SLi Feng Wang // odtDocument = SWUtil.openDocumentFromURL(Testspace.getUrl(docSaveAsODTSample), app); 144*3ecf08d2SLi Feng Wang // assertTrue("Verify Title field is created, after saved to doc format, field still exist.", isContainTitleField(odtDocument, TitleStr)); 145*3ecf08d2SLi Feng Wang // assertTrue("Verify Title field is created, after saved to doc format, can get text from document", 146*3ecf08d2SLi Feng Wang // isContainTitleText(odtDocument, TitleStr)); 147*3ecf08d2SLi Feng Wang // app.closeDocument(odtDocument); 148*3ecf08d2SLi Feng Wang // } 149a624bbf9SLiu Zhe 150a624bbf9SLiu Zhe 151a624bbf9SLiu Zhe 152a624bbf9SLiu Zhe /** 153a624bbf9SLiu Zhe * Create a Title field at start of this document 154a624bbf9SLiu Zhe * @param document 155a624bbf9SLiu Zhe * @throws Exception 156a624bbf9SLiu Zhe */ 157a624bbf9SLiu Zhe private void createTitleFiled(XTextDocument document) throws Exception { 158a624bbf9SLiu Zhe 159a624bbf9SLiu Zhe XMultiServiceFactory sevriceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, document); 160a624bbf9SLiu Zhe XTextField TitleField = (XTextField)UnoRuntime.queryInterface(XTextField.class, sevriceFactory.createInstance("com.sun.star.text.textfield.docinfo.Title")); 161a624bbf9SLiu Zhe 162a624bbf9SLiu Zhe 163a624bbf9SLiu Zhe SWUtil.moveCuror2Start(document); 164a624bbf9SLiu Zhe document.getText().insertTextContent(document.getText().getStart(), TitleField, true); 165a624bbf9SLiu Zhe 166a624bbf9SLiu Zhe } 167a624bbf9SLiu Zhe 168a624bbf9SLiu Zhe 169a624bbf9SLiu Zhe /** 170a624bbf9SLiu Zhe * Check is contain Title content at start of this document 171a624bbf9SLiu Zhe * 172a624bbf9SLiu Zhe * @param document 173a624bbf9SLiu Zhe * @param content 174a624bbf9SLiu Zhe * @return 175a624bbf9SLiu Zhe */ 176a624bbf9SLiu Zhe private boolean isContainTitleText(XTextDocument document, String content) { 177a624bbf9SLiu Zhe String documentString = document.getText().getString().trim(); 178a624bbf9SLiu Zhe return documentString.indexOf(content) == 0; 179a624bbf9SLiu Zhe } 180a624bbf9SLiu Zhe 181a624bbf9SLiu Zhe /** 182a624bbf9SLiu Zhe * Check is contain Title field 183a624bbf9SLiu Zhe * @param document 184a624bbf9SLiu Zhe * @throws Exception 185a624bbf9SLiu Zhe */ 186a624bbf9SLiu Zhe private boolean isContainTitleField(XTextDocument document, String content) throws Exception { 1874c280f34SLinyi Li XTextFieldsSupplier fieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, document); 188a624bbf9SLiu Zhe XEnumerationAccess xEnumeratedFields = fieldsSupplier.getTextFields(); 189a624bbf9SLiu Zhe XEnumeration enumeration = xEnumeratedFields.createEnumeration(); 190a624bbf9SLiu Zhe while (enumeration.hasMoreElements()) { 191a624bbf9SLiu Zhe Object field = enumeration.nextElement(); 192a624bbf9SLiu Zhe XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, field); 193a624bbf9SLiu Zhe String strContent = (String) props.getPropertyValue("CurrentPresentation"); 194a624bbf9SLiu Zhe return content.equals(strContent); 195a624bbf9SLiu Zhe 196a624bbf9SLiu Zhe } 197a624bbf9SLiu Zhe return false; 198a624bbf9SLiu Zhe 199a624bbf9SLiu Zhe } 200a624bbf9SLiu Zhe } 201