xref: /AOO41X/test/testuno/source/fvt/uno/sw/DocumentTest.java (revision ff0525f24f03981d56b7579b645949f111420994)
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 com.sun.star.text.XTextDocument;
36 import com.sun.star.text.XTextCursor;
37 import com.sun.star.text.XText;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.frame.*;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.util.XCloseable;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.container.XEnumerationAccess;
45 import com.sun.star.container.XEnumeration;
46 
47 
48 public class DocumentTest {
49     UnoApp unoApp = new UnoApp();
50     XTextDocument textDocument = null;
51     File temp = null;
52     String workingFilePath = "";
53     String workingTemplatePath = "";
54 
55     /**
56      * @throws java.lang.Exception
57      */
58     @Before
59     public void setUp() throws Exception {
60         unoApp.start();
61 
62         FileUtil.deleteFile(getPath("temp"));
63         temp = new File(getPath("temp"));
64         temp.mkdirs();
65 
66         //copy sample file to temp folder
67         workingFilePath = prepareData("uno/sw/DocumentTest.odt");
68         workingTemplatePath = prepareData("uno/sw/DocumentTest.ott");
69     }
70 
71     @After
72     public void tearDown() throws Exception {
73         unoApp.close();
74     }
75 
76     private XComponent newDocumentFromTemplate(String templatePath) throws Exception
77     {
78         XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
79         PropertyValue[] pros = new PropertyValue[1];
80         pros[0] = new PropertyValue();
81         pros[0].Name = "AsTemplate";
82         pros[0].Value = new Boolean(true);
83 
84         XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
85         return component;
86     }
87 
88 
89     /**
90      * test close document
91      * @throws Exception
92      */
93     @Test
94     public void testCloseDocument() throws Exception
95     {
96         XComponent component = unoApp.newDocument("swriter");
97         unoApp.closeDocument(component);
98         XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();
99         Assert.assertTrue("Document has been closed.",xModel==null);
100     }
101 
102     /**
103      * test new document
104      * @throws Exception
105      */
106     @Test
107     public void testNewDocument() throws Exception
108     {
109         XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
110         XComponent component = componentLoader.loadComponentFromURL("private:factory/" + "swriter", "_blank", 0, new PropertyValue[0]);
111         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
112         XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
113         String title = xTitle.getTitle();
114         Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
115         unoApp.closeDocument(textDocument);
116     }
117 
118     /**
119      * test new document from template
120      * @throws Exception
121      */
122     @Test
123     public void testNewDocumentFromTemplate() throws Exception
124     {
125         XComponent component = this.newDocumentFromTemplate(workingTemplatePath);
126         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
127         XText xText = textDocument.getText();
128         XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
129         xText = textDocument.getText();
130         XTextCursor xTextCursor = xText.createTextCursor();
131         xTextCursor.gotoEnd(true);
132         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
133         String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
134 
135         Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
136 
137         Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
138 
139         unoApp.closeDocument(textDocument);
140     }
141 
142     /**
143      * test save document as odt
144      * @throws Exception
145      */
146     @Test
147     public void testSaveDocument() throws Exception
148     {
149         XComponent component = unoApp.loadDocument(workingFilePath);
150         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
151         XText xText = textDocument.getText();
152         XTextCursor xTextCursor = xText.createTextCursor();
153         xTextCursor.gotoEnd(true);
154         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
155 
156         xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
157 
158         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
159         xStorable.store();
160         unoApp.closeDocument(textDocument);
161 
162         component = unoApp.loadDocument(workingFilePath);
163         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
164         xText = textDocument.getText();
165         xTextCursor = xText.createTextCursor();
166         xTextCursor.gotoEnd(true);
167         xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
168 
169         Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
170         unoApp.closeDocument(textDocument);
171     }
172 
173     /**
174      * test save document as doc
175      * @throws Exception
176      */
177     @Test
178     public void testSaveAsDocument() throws Exception
179     {
180         File saveAsFile = new File(workingFilePath + ".doc");
181         XComponent component = unoApp.loadDocument(workingFilePath);
182         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
183         XText xText = textDocument.getText();
184         XTextCursor xTextCursor = xText.createTextCursor();
185         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
186 
187         xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
188         xText.insertString(xTextCursor, "test Save odt as doc.", false);
189 
190         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
191         PropertyValue[] storeProps = new PropertyValue[2];
192 
193         storeProps[0] = new PropertyValue();
194         storeProps[0].Name = "Overwrite";
195         storeProps[0].Value = new Boolean(true);
196 
197         storeProps[1] = new PropertyValue();
198         storeProps[1].Name = "FilterName";
199         storeProps[1].Value = "MS Word 97";
200 
201         xStorable.storeAsURL(FileUtil.getUrl(saveAsFile), storeProps);
202         Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
203         unoApp.closeDocument(textDocument);
204     }
205 
206     /**
207      * test export document as pdf
208      * @throws Exception
209      */
210     @Test
211     public void testExportAsPDF() throws Exception
212     {
213         File saveAsFile = new File(workingFilePath + ".pdf");
214         XComponent component = unoApp.loadDocument(workingFilePath);
215         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
216 
217         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
218         PropertyValue[] storeProps = new PropertyValue[3];
219 
220         storeProps[0] = new PropertyValue();
221         storeProps[0].Name = "Overwrite";
222         storeProps[0].Value = new Boolean(true);
223 
224         storeProps[1] = new PropertyValue();
225         storeProps[1].Name = "FilterName";
226         storeProps[1].Value = "writer_pdf_Export";
227 
228         storeProps[2] = new PropertyValue();
229         storeProps[2].Name = "CompressionMode";
230         storeProps[2].Value = "1";
231 
232         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
233 
234         Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
235 
236         unoApp.closeDocument(textDocument);
237     }
238 
239     /**
240      * test save document as template
241      * @throws Exception
242      */
243     @Test
244     public void testSaveAsTemplate() throws Exception
245     {
246         File saveAsFile = new File(workingFilePath + ".ott");
247         XComponent component = unoApp.loadDocument(workingFilePath);
248         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
249         XText xText = textDocument.getText();
250         XTextCursor xTextCursor = xText.createTextCursor();
251         xTextCursor.gotoEnd(true);
252         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
253 
254         xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
255 
256         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
257         xStorable.store();
258 
259         PropertyValue[] storeProps = new PropertyValue[3];
260         storeProps[0] = new PropertyValue();
261         storeProps[0].Name="TemplateName";
262         storeProps[0].Value="MyNewCreatedTemplate";
263 
264         storeProps[1] = new PropertyValue();
265         storeProps[1].Name="TemplateRegionName";
266         storeProps[1].Value="My Templates";
267 
268         storeProps[2] = new PropertyValue();
269         storeProps[2].Name="AsTemplate";
270         storeProps[2].Value=new Boolean(true);
271 
272         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
273         unoApp.closeDocument(textDocument);
274 
275         component = this.newDocumentFromTemplate(saveAsFile.getAbsolutePath());
276         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
277         XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
278         xText = textDocument.getText();
279         xTextCursor = xText.createTextCursor();
280         xTextCursor.gotoEnd(true);
281         xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
282         String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
283         Assert.assertEquals("Save document as template, heading style is remained. ", "Heading 1", paraStyle);
284         Assert.assertEquals("Save document as template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
285         unoApp.closeDocument(textDocument);
286     }
287 
288 }
289