107d7dbdcSHerbert Dürr /************************************************************** 207d7dbdcSHerbert Dürr * 307d7dbdcSHerbert Dürr * Licensed to the Apache Software Foundation (ASF) under one 407d7dbdcSHerbert Dürr * or more contributor license agreements. See the NOTICE file 507d7dbdcSHerbert Dürr * distributed with this work for additional information 607d7dbdcSHerbert Dürr * regarding copyright ownership. The ASF licenses this file 707d7dbdcSHerbert Dürr * to you under the Apache License, Version 2.0 (the 807d7dbdcSHerbert Dürr * "License"); you may not use this file except in compliance 907d7dbdcSHerbert Dürr * with the License. You may obtain a copy of the License at 1007d7dbdcSHerbert Dürr * 1107d7dbdcSHerbert Dürr * http://www.apache.org/licenses/LICENSE-2.0 1207d7dbdcSHerbert Dürr * 1307d7dbdcSHerbert Dürr * Unless required by applicable law or agreed to in writing, 1407d7dbdcSHerbert Dürr * software distributed under the License is distributed on an 1507d7dbdcSHerbert Dürr * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1607d7dbdcSHerbert Dürr * KIND, either express or implied. See the License for the 1707d7dbdcSHerbert Dürr * specific language governing permissions and limitations 1807d7dbdcSHerbert Dürr * under the License. 1907d7dbdcSHerbert Dürr * 2007d7dbdcSHerbert Dürr *************************************************************/ 2107d7dbdcSHerbert Dürr 22eba4d44aSLiu Zhe package fvt.uno.sw; 23eba4d44aSLiu Zhe 24eba4d44aSLiu Zhe import static org.openoffice.test.common.Testspace.*; 25eba4d44aSLiu Zhe 26eba4d44aSLiu Zhe import java.io.File; 27eba4d44aSLiu Zhe 28eba4d44aSLiu Zhe import org.junit.After; 29eba4d44aSLiu Zhe import org.junit.Before; 30eba4d44aSLiu Zhe import org.junit.Test; 31eba4d44aSLiu Zhe import org.junit.Assert; 32eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil; 33eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp; 34eba4d44aSLiu Zhe 35*1ff9903bSLi Feng Wang import testlib.uno.SWUtil; 36*1ff9903bSLi Feng Wang 37eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument; 38eba4d44aSLiu Zhe import com.sun.star.text.XTextCursor; 39eba4d44aSLiu Zhe import com.sun.star.text.XText; 40eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet; 41eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue; 42eba4d44aSLiu Zhe import com.sun.star.frame.*; 43eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime; 44eba4d44aSLiu Zhe import com.sun.star.lang.XComponent; 45eba4d44aSLiu Zhe 46eba4d44aSLiu Zhe 47eba4d44aSLiu Zhe public class DocumentTest { 48eba4d44aSLiu Zhe UnoApp unoApp = new UnoApp(); 49eba4d44aSLiu Zhe XTextDocument textDocument = null; 50eba4d44aSLiu Zhe File temp = null; 51eba4d44aSLiu Zhe String workingFilePath = ""; 52eba4d44aSLiu Zhe String workingTemplatePath = ""; 53eba4d44aSLiu Zhe 54eba4d44aSLiu Zhe @Before 55eba4d44aSLiu Zhe public void setUp() throws Exception { 56eba4d44aSLiu Zhe unoApp.start(); 57eba4d44aSLiu Zhe 58eba4d44aSLiu Zhe FileUtil.deleteFile(getPath("temp")); 59eba4d44aSLiu Zhe temp = new File(getPath("temp")); 60eba4d44aSLiu Zhe temp.mkdirs(); 61eba4d44aSLiu Zhe 62eba4d44aSLiu Zhe //copy sample file to temp folder 63eba4d44aSLiu Zhe workingFilePath = prepareData("uno/sw/DocumentTest.odt"); 64eba4d44aSLiu Zhe workingTemplatePath = prepareData("uno/sw/DocumentTest.ott"); 65eba4d44aSLiu Zhe } 66eba4d44aSLiu Zhe 67eba4d44aSLiu Zhe @After 68eba4d44aSLiu Zhe public void tearDown() throws Exception { 69eba4d44aSLiu Zhe unoApp.close(); 70eba4d44aSLiu Zhe } 71eba4d44aSLiu Zhe /** 72*1ff9903bSLi Feng Wang * test new document and close document 73eba4d44aSLiu Zhe * @throws Exception 74eba4d44aSLiu Zhe */ 75eba4d44aSLiu Zhe @Test 76*1ff9903bSLi Feng Wang public void testNewAndCloseDocument() throws Exception 77eba4d44aSLiu Zhe { 78eba4d44aSLiu Zhe XComponent component = unoApp.newDocument("swriter"); 79eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 80eba4d44aSLiu Zhe XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument); 81eba4d44aSLiu Zhe String title = xTitle.getTitle(); 82eba4d44aSLiu Zhe Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled")); 83*1ff9903bSLi Feng Wang unoApp.closeDocument(component); 84*1ff9903bSLi Feng Wang XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel(); 85*1ff9903bSLi Feng Wang Assert.assertTrue("Document has been closed.",xModel==null); 86eba4d44aSLiu Zhe } 87eba4d44aSLiu Zhe /** 88eba4d44aSLiu Zhe * test new document from template 89eba4d44aSLiu Zhe * @throws Exception 90eba4d44aSLiu Zhe */ 91eba4d44aSLiu Zhe @Test 92eba4d44aSLiu Zhe public void testNewDocumentFromTemplate() throws Exception 93eba4d44aSLiu Zhe { 94*1ff9903bSLi Feng Wang XComponent component = SWUtil.newDocumentFromTemplate(workingTemplatePath,unoApp); 95eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 96eba4d44aSLiu Zhe XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument); 97*1ff9903bSLi Feng Wang XText xText = textDocument.getText(); 98eba4d44aSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 99eba4d44aSLiu Zhe xTextCursor.gotoEnd(true); 100eba4d44aSLiu Zhe XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 101eba4d44aSLiu Zhe String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName"); 102eba4d44aSLiu Zhe Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle); 103eba4d44aSLiu Zhe Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled")); 104eba4d44aSLiu Zhe } 105eba4d44aSLiu Zhe 106eba4d44aSLiu Zhe /** 107eba4d44aSLiu Zhe * test save document as odt 108eba4d44aSLiu Zhe * @throws Exception 109eba4d44aSLiu Zhe */ 110eba4d44aSLiu Zhe @Test 111eba4d44aSLiu Zhe public void testSaveDocument() throws Exception 112eba4d44aSLiu Zhe { 113eba4d44aSLiu Zhe XComponent component = unoApp.loadDocument(workingFilePath); 114eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 115eba4d44aSLiu Zhe XText xText = textDocument.getText(); 116eba4d44aSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 117eba4d44aSLiu Zhe xTextCursor.gotoEnd(true); 118eba4d44aSLiu Zhe XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 119eba4d44aSLiu Zhe xPropertySet.setPropertyValue("ParaStyleName", "Heading 1"); 120*1ff9903bSLi Feng Wang SWUtil.save(textDocument); 121eba4d44aSLiu Zhe unoApp.closeDocument(textDocument); 122eba4d44aSLiu Zhe component = unoApp.loadDocument(workingFilePath); 123eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 124eba4d44aSLiu Zhe xText = textDocument.getText(); 125eba4d44aSLiu Zhe xTextCursor = xText.createTextCursor(); 126eba4d44aSLiu Zhe xTextCursor.gotoEnd(true); 127eba4d44aSLiu Zhe xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 128eba4d44aSLiu Zhe Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName")); 129eba4d44aSLiu Zhe } 130eba4d44aSLiu Zhe 131eba4d44aSLiu Zhe /** 132eba4d44aSLiu Zhe * test save document as doc 133eba4d44aSLiu Zhe * @throws Exception 134eba4d44aSLiu Zhe */ 135eba4d44aSLiu Zhe @Test 136eba4d44aSLiu Zhe public void testSaveAsDocument() throws Exception 137eba4d44aSLiu Zhe { 138eba4d44aSLiu Zhe File saveAsFile = new File(workingFilePath + ".doc"); 139eba4d44aSLiu Zhe XComponent component = unoApp.loadDocument(workingFilePath); 140eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 141eba4d44aSLiu Zhe XText xText = textDocument.getText(); 142eba4d44aSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 143eba4d44aSLiu Zhe XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 144eba4d44aSLiu Zhe 145eba4d44aSLiu Zhe xPropertySet.setPropertyValue("ParaStyleName", "Heading 1"); 146eba4d44aSLiu Zhe xText.insertString(xTextCursor, "test Save odt as doc.", false); 147*1ff9903bSLi Feng Wang SWUtil.saveAsDoc(textDocument, FileUtil.getUrl(saveAsFile)); 148eba4d44aSLiu Zhe Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists()); 149eba4d44aSLiu Zhe } 150eba4d44aSLiu Zhe 151eba4d44aSLiu Zhe /** 152eba4d44aSLiu Zhe * test export document as pdf 153eba4d44aSLiu Zhe * @throws Exception 154eba4d44aSLiu Zhe */ 155eba4d44aSLiu Zhe @Test 156eba4d44aSLiu Zhe public void testExportAsPDF() throws Exception 157eba4d44aSLiu Zhe { 158eba4d44aSLiu Zhe File saveAsFile = new File(workingFilePath + ".pdf"); 159eba4d44aSLiu Zhe XComponent component = unoApp.loadDocument(workingFilePath); 160eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 161eba4d44aSLiu Zhe 162eba4d44aSLiu Zhe XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component); 163eba4d44aSLiu Zhe PropertyValue[] storeProps = new PropertyValue[3]; 164eba4d44aSLiu Zhe 165eba4d44aSLiu Zhe storeProps[0] = new PropertyValue(); 166eba4d44aSLiu Zhe storeProps[0].Name = "Overwrite"; 167eba4d44aSLiu Zhe storeProps[0].Value = new Boolean(true); 168eba4d44aSLiu Zhe 169eba4d44aSLiu Zhe storeProps[1] = new PropertyValue(); 170eba4d44aSLiu Zhe storeProps[1].Name = "FilterName"; 171eba4d44aSLiu Zhe storeProps[1].Value = "writer_pdf_Export"; 172eba4d44aSLiu Zhe 173eba4d44aSLiu Zhe storeProps[2] = new PropertyValue(); 174eba4d44aSLiu Zhe storeProps[2].Name = "CompressionMode"; 175eba4d44aSLiu Zhe storeProps[2].Value = "1"; 176eba4d44aSLiu Zhe 177eba4d44aSLiu Zhe xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps); 178eba4d44aSLiu Zhe 179eba4d44aSLiu Zhe Assert.assertTrue("Export document as PDF.", saveAsFile.exists()); 180eba4d44aSLiu Zhe } 181eba4d44aSLiu Zhe 182eba4d44aSLiu Zhe /** 183eba4d44aSLiu Zhe * test save document as template 184eba4d44aSLiu Zhe * @throws Exception 185eba4d44aSLiu Zhe */ 186eba4d44aSLiu Zhe @Test 187eba4d44aSLiu Zhe public void testSaveAsTemplate() throws Exception 188eba4d44aSLiu Zhe { 189eba4d44aSLiu Zhe File saveAsFile = new File(workingFilePath + ".ott"); 190eba4d44aSLiu Zhe XComponent component = unoApp.loadDocument(workingFilePath); 191eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 192eba4d44aSLiu Zhe XText xText = textDocument.getText(); 193eba4d44aSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 194eba4d44aSLiu Zhe xTextCursor.gotoEnd(true); 195eba4d44aSLiu Zhe XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 196eba4d44aSLiu Zhe 197eba4d44aSLiu Zhe xPropertySet.setPropertyValue("ParaStyleName", "Heading 1"); 198eba4d44aSLiu Zhe 199eba4d44aSLiu Zhe XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component); 200eba4d44aSLiu Zhe xStorable.store(); 201eba4d44aSLiu Zhe 202eba4d44aSLiu Zhe PropertyValue[] storeProps = new PropertyValue[3]; 203eba4d44aSLiu Zhe storeProps[0] = new PropertyValue(); 204eba4d44aSLiu Zhe storeProps[0].Name="TemplateName"; 205eba4d44aSLiu Zhe storeProps[0].Value="MyNewCreatedTemplate"; 206eba4d44aSLiu Zhe 207eba4d44aSLiu Zhe storeProps[1] = new PropertyValue(); 208eba4d44aSLiu Zhe storeProps[1].Name="TemplateRegionName"; 209eba4d44aSLiu Zhe storeProps[1].Value="My Templates"; 210eba4d44aSLiu Zhe 211eba4d44aSLiu Zhe storeProps[2] = new PropertyValue(); 212eba4d44aSLiu Zhe storeProps[2].Name="AsTemplate"; 213eba4d44aSLiu Zhe storeProps[2].Value=new Boolean(true); 214eba4d44aSLiu Zhe 215eba4d44aSLiu Zhe xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps); 216eba4d44aSLiu Zhe unoApp.closeDocument(textDocument); 217*1ff9903bSLi Feng Wang Assert.assertTrue("Export document as ott.", saveAsFile.exists()); 218eba4d44aSLiu Zhe } 219eba4d44aSLiu Zhe 220eba4d44aSLiu Zhe } 221