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 22 package fvt.uno.sw; 23 24 import static org.openoffice.test.common.Testspace.*; 25 26 import java.io.File; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.Assert; 32 import org.openoffice.test.common.FileUtil; 33 import org.openoffice.test.uno.UnoApp; 34 35 import testlib.uno.SWUtil; 36 37 import com.sun.star.text.XTextDocument; 38 import com.sun.star.text.XTextCursor; 39 import com.sun.star.text.XText; 40 import com.sun.star.beans.XPropertySet; 41 import com.sun.star.beans.PropertyValue; 42 import com.sun.star.frame.*; 43 import com.sun.star.uno.UnoRuntime; 44 import com.sun.star.lang.XComponent; 45 46 47 public class DocumentTest { 48 UnoApp unoApp = new UnoApp(); 49 XTextDocument textDocument = null; 50 File temp = null; 51 String workingFilePath = ""; 52 String workingTemplatePath = ""; 53 54 @Before setUp()55 public void setUp() throws Exception { 56 unoApp.start(); 57 58 FileUtil.deleteFile(getPath("temp")); 59 temp = new File(getPath("temp")); 60 temp.mkdirs(); 61 62 //copy sample file to temp folder 63 workingFilePath = prepareData("uno/sw/DocumentTest.odt"); 64 workingTemplatePath = prepareData("uno/sw/DocumentTest.ott"); 65 } 66 67 @After tearDown()68 public void tearDown() throws Exception { 69 unoApp.close(); 70 } 71 /** 72 * test new document and close document 73 * @throws Exception 74 */ 75 @Test testNewAndCloseDocument()76 public void testNewAndCloseDocument() throws Exception 77 { 78 XComponent component = unoApp.newDocument("swriter"); 79 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 80 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument); 81 String title = xTitle.getTitle(); 82 Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled")); 83 unoApp.closeDocument(component); 84 XDesktop xDesktop = unoApp.getDesktop(); 85 XFrame xFrame = (xDesktop == null) ? null : xDesktop.getCurrentFrame(); 86 XController xCtrl = (xFrame == null) ? null : xFrame.getController(); 87 XModel xModel = (xCtrl == null) ? null : xCtrl.getModel(); 88 Assert.assertTrue("Document has been closed.",xModel==null); 89 } 90 /** 91 * test new document from template 92 * @throws Exception 93 */ 94 @Test testNewDocumentFromTemplate()95 public void testNewDocumentFromTemplate() throws Exception 96 { 97 XComponent component = SWUtil.newDocumentFromTemplate(workingTemplatePath,unoApp); 98 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 99 XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument); 100 XText xText = textDocument.getText(); 101 XTextCursor xTextCursor = xText.createTextCursor(); 102 xTextCursor.gotoEnd(true); 103 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 104 String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName"); 105 Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle); 106 Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled")); 107 } 108 109 /** 110 * test save document as odt 111 * @throws Exception 112 */ 113 @Test testSaveDocument()114 public void testSaveDocument() throws Exception 115 { 116 XComponent component = unoApp.loadDocument(workingFilePath); 117 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 118 XText xText = textDocument.getText(); 119 XTextCursor xTextCursor = xText.createTextCursor(); 120 xTextCursor.gotoEnd(true); 121 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 122 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1"); 123 SWUtil.save(textDocument); 124 unoApp.closeDocument(textDocument); 125 component = unoApp.loadDocument(workingFilePath); 126 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 127 xText = textDocument.getText(); 128 xTextCursor = xText.createTextCursor(); 129 xTextCursor.gotoEnd(true); 130 xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 131 Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName")); 132 } 133 134 /** 135 * test save document as doc 136 * @throws Exception 137 */ 138 @Test testSaveAsDocument()139 public void testSaveAsDocument() throws Exception 140 { 141 File saveAsFile = new File(workingFilePath + ".doc"); 142 XComponent component = unoApp.loadDocument(workingFilePath); 143 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 144 XText xText = textDocument.getText(); 145 XTextCursor xTextCursor = xText.createTextCursor(); 146 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 147 148 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1"); 149 xText.insertString(xTextCursor, "test Save odt as doc.", false); 150 SWUtil.saveAsDoc(textDocument, FileUtil.getUrl(saveAsFile)); 151 Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists()); 152 } 153 154 /** 155 * test export document as pdf 156 * @throws Exception 157 */ 158 @Test testExportAsPDF()159 public void testExportAsPDF() throws Exception 160 { 161 File saveAsFile = new File(workingFilePath + ".pdf"); 162 XComponent component = unoApp.loadDocument(workingFilePath); 163 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 164 165 XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component); 166 PropertyValue[] storeProps = new PropertyValue[3]; 167 168 storeProps[0] = new PropertyValue(); 169 storeProps[0].Name = "Overwrite"; 170 storeProps[0].Value = new Boolean(true); 171 172 storeProps[1] = new PropertyValue(); 173 storeProps[1].Name = "FilterName"; 174 storeProps[1].Value = "writer_pdf_Export"; 175 176 storeProps[2] = new PropertyValue(); 177 storeProps[2].Name = "CompressionMode"; 178 storeProps[2].Value = "1"; 179 180 xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps); 181 182 Assert.assertTrue("Export document as PDF.", saveAsFile.exists()); 183 } 184 185 /** 186 * test save document as template 187 * @throws Exception 188 */ 189 @Test testSaveAsTemplate()190 public void testSaveAsTemplate() throws Exception 191 { 192 File saveAsFile = new File(workingFilePath + ".ott"); 193 XComponent component = unoApp.loadDocument(workingFilePath); 194 textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component); 195 XText xText = textDocument.getText(); 196 XTextCursor xTextCursor = xText.createTextCursor(); 197 xTextCursor.gotoEnd(true); 198 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor); 199 200 xPropertySet.setPropertyValue("ParaStyleName", "Heading 1"); 201 202 XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component); 203 xStorable.store(); 204 205 PropertyValue[] storeProps = new PropertyValue[3]; 206 storeProps[0] = new PropertyValue(); 207 storeProps[0].Name="TemplateName"; 208 storeProps[0].Value="MyNewCreatedTemplate"; 209 210 storeProps[1] = new PropertyValue(); 211 storeProps[1].Name="TemplateRegionName"; 212 storeProps[1].Value="My Templates"; 213 214 storeProps[2] = new PropertyValue(); 215 storeProps[2].Name="AsTemplate"; 216 storeProps[2].Value=new Boolean(true); 217 218 xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps); 219 unoApp.closeDocument(textDocument); 220 Assert.assertTrue("Export document as ott.", saveAsFile.exists()); 221 } 222 223 } 224