xref: /AOO41X/test/testuno/source/fvt/uno/sw/DocumentTest.java (revision 6a6ec68d792bd477e5b23798f42a1a9de0925497)
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
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
68     public void tearDown() throws Exception {
69         unoApp.close();
70     }
71     /**
72      * test new document and close document
73      * @throws Exception
74      */
75     @Test
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         XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();
85         Assert.assertTrue("Document has been closed.",xModel==null);
86     }
87     /**
88      * test new document from template
89      * @throws Exception
90      */
91     @Test
92     public void testNewDocumentFromTemplate() throws Exception
93     {
94         XComponent component = SWUtil.newDocumentFromTemplate(workingTemplatePath,unoApp);
95         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
96         XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
97         XText xText = textDocument.getText();
98         XTextCursor xTextCursor = xText.createTextCursor();
99         xTextCursor.gotoEnd(true);
100         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
101         String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
102         Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
103         Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
104     }
105 
106     /**
107      * test save document as odt
108      * @throws Exception
109      */
110     @Test
111     public void testSaveDocument() throws Exception
112     {
113         XComponent component = unoApp.loadDocument(workingFilePath);
114         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
115         XText xText = textDocument.getText();
116         XTextCursor xTextCursor = xText.createTextCursor();
117         xTextCursor.gotoEnd(true);
118         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
119         xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
120         SWUtil.save(textDocument);
121         unoApp.closeDocument(textDocument);
122         component = unoApp.loadDocument(workingFilePath);
123         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
124         xText = textDocument.getText();
125         xTextCursor = xText.createTextCursor();
126         xTextCursor.gotoEnd(true);
127         xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
128         Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
129     }
130 
131     /**
132      * test save document as doc
133      * @throws Exception
134      */
135     @Test
136     public void testSaveAsDocument() throws Exception
137     {
138         File saveAsFile = new File(workingFilePath + ".doc");
139         XComponent component = unoApp.loadDocument(workingFilePath);
140         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
141         XText xText = textDocument.getText();
142         XTextCursor xTextCursor = xText.createTextCursor();
143         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
144 
145         xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
146         xText.insertString(xTextCursor, "test Save odt as doc.", false);
147         SWUtil.saveAsDoc(textDocument, FileUtil.getUrl(saveAsFile));
148         Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
149     }
150 
151     /**
152      * test export document as pdf
153      * @throws Exception
154      */
155     @Test
156     public void testExportAsPDF() throws Exception
157     {
158         File saveAsFile = new File(workingFilePath + ".pdf");
159         XComponent component = unoApp.loadDocument(workingFilePath);
160         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
161 
162         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
163         PropertyValue[] storeProps = new PropertyValue[3];
164 
165         storeProps[0] = new PropertyValue();
166         storeProps[0].Name = "Overwrite";
167         storeProps[0].Value = new Boolean(true);
168 
169         storeProps[1] = new PropertyValue();
170         storeProps[1].Name = "FilterName";
171         storeProps[1].Value = "writer_pdf_Export";
172 
173         storeProps[2] = new PropertyValue();
174         storeProps[2].Name = "CompressionMode";
175         storeProps[2].Value = "1";
176 
177         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
178 
179         Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
180     }
181 
182     /**
183      * test save document as template
184      * @throws Exception
185      */
186     @Test
187     public void testSaveAsTemplate() throws Exception
188     {
189         File saveAsFile = new File(workingFilePath + ".ott");
190         XComponent component = unoApp.loadDocument(workingFilePath);
191         textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
192         XText xText = textDocument.getText();
193         XTextCursor xTextCursor = xText.createTextCursor();
194         xTextCursor.gotoEnd(true);
195         XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
196 
197         xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
198 
199         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
200         xStorable.store();
201 
202         PropertyValue[] storeProps = new PropertyValue[3];
203         storeProps[0] = new PropertyValue();
204         storeProps[0].Name="TemplateName";
205         storeProps[0].Value="MyNewCreatedTemplate";
206 
207         storeProps[1] = new PropertyValue();
208         storeProps[1].Name="TemplateRegionName";
209         storeProps[1].Value="My Templates";
210 
211         storeProps[2] = new PropertyValue();
212         storeProps[2].Name="AsTemplate";
213         storeProps[2].Value=new Boolean(true);
214 
215         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
216         unoApp.closeDocument(textDocument);
217         Assert.assertTrue("Export document as ott.", saveAsFile.exists());
218     }
219 
220 }
221