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;
23f18db294SLiu Zhe 
24f18db294SLiu Zhe import static org.openoffice.test.common.Testspace.*;
25f18db294SLiu Zhe 
26f18db294SLiu Zhe import java.io.File;
27f18db294SLiu Zhe 
28f18db294SLiu Zhe import org.junit.After;
29f18db294SLiu Zhe import org.junit.Before;
30f18db294SLiu Zhe import org.junit.Test;
31f18db294SLiu Zhe import org.junit.Assert;
32f18db294SLiu Zhe import org.openoffice.test.common.FileUtil;
33f18db294SLiu Zhe import org.openoffice.test.uno.UnoApp;
34f18db294SLiu Zhe 
351ff9903bSLi Feng Wang import testlib.uno.SWUtil;
361ff9903bSLi Feng Wang 
37f18db294SLiu Zhe import com.sun.star.text.XTextDocument;
38f18db294SLiu Zhe import com.sun.star.text.XTextCursor;
39f18db294SLiu Zhe import com.sun.star.text.XText;
40f18db294SLiu Zhe import com.sun.star.beans.XPropertySet;
41f18db294SLiu Zhe import com.sun.star.beans.PropertyValue;
42f18db294SLiu Zhe import com.sun.star.frame.*;
43f18db294SLiu Zhe import com.sun.star.uno.UnoRuntime;
44f18db294SLiu Zhe import com.sun.star.lang.XComponent;
45f18db294SLiu Zhe 
46f18db294SLiu Zhe 
47f18db294SLiu Zhe public class DocumentTest {
48f18db294SLiu Zhe 	UnoApp unoApp = new UnoApp();
49f18db294SLiu Zhe 	XTextDocument textDocument = null;
50f18db294SLiu Zhe 	File temp = null;
51f18db294SLiu Zhe 	String workingFilePath = "";
52f18db294SLiu Zhe 	String workingTemplatePath = "";
531ff9903bSLi Feng Wang 
54f18db294SLiu Zhe 	@Before
setUp()55f18db294SLiu Zhe 	public void setUp() throws Exception {
56f18db294SLiu Zhe 		unoApp.start();
57f18db294SLiu Zhe 
58f18db294SLiu Zhe 		FileUtil.deleteFile(getPath("temp"));
59f18db294SLiu Zhe 		temp = new File(getPath("temp"));
60f18db294SLiu Zhe 		temp.mkdirs();
61f18db294SLiu Zhe 
62f18db294SLiu Zhe 		//copy sample file to temp folder
63deb45f49SLiu Zhe 		workingFilePath = prepareData("uno/sw/DocumentTest.odt");
64deb45f49SLiu Zhe 		workingTemplatePath = prepareData("uno/sw/DocumentTest.ott");
65f18db294SLiu Zhe 	}
66f18db294SLiu Zhe 
67f18db294SLiu Zhe 	@After
tearDown()68f18db294SLiu Zhe 	public void tearDown() throws Exception {
69f18db294SLiu Zhe 		unoApp.close();
70f18db294SLiu Zhe 	}
7190a166ceSLiu Zhe 	/**
721ff9903bSLi Feng Wang 	 * test new document and close document
7390a166ceSLiu Zhe 	 * @throws Exception
7490a166ceSLiu Zhe 	 */
7590a166ceSLiu Zhe 	@Test
testNewAndCloseDocument()761ff9903bSLi Feng Wang 	public void testNewAndCloseDocument() throws Exception
7790a166ceSLiu Zhe 	{
7890a166ceSLiu Zhe 		XComponent component = unoApp.newDocument("swriter");
79f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
80f18db294SLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
81f18db294SLiu Zhe 		String title = xTitle.getTitle();
82f18db294SLiu Zhe 		Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
831ff9903bSLi Feng Wang 		unoApp.closeDocument(component);
84*e4b54efcSHerbert Dürr 		XDesktop xDesktop = unoApp.getDesktop();
85*e4b54efcSHerbert Dürr 		XFrame xFrame     = (xDesktop == null) ? null : xDesktop.getCurrentFrame();
86*e4b54efcSHerbert Dürr 		XController xCtrl = (xFrame == null)   ? null : xFrame.getController();
87*e4b54efcSHerbert Dürr 		XModel xModel     = (xCtrl == null)    ? null : xCtrl.getModel();
88*e4b54efcSHerbert Dürr 		Assert.assertTrue("Document has been closed.",xModel==null);
891ff9903bSLi Feng Wang 	}
9090a166ceSLiu Zhe 	/**
9190a166ceSLiu Zhe 	 * test new document from template
9290a166ceSLiu Zhe 	 * @throws Exception
9390a166ceSLiu Zhe 	 */
94f18db294SLiu Zhe 	@Test
testNewDocumentFromTemplate()95f18db294SLiu Zhe 	public void testNewDocumentFromTemplate() throws Exception
961ff9903bSLi Feng Wang 	{
971ff9903bSLi Feng Wang 		XComponent component = SWUtil.newDocumentFromTemplate(workingTemplatePath,unoApp);
98f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
99f18db294SLiu Zhe 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
1001ff9903bSLi Feng Wang 		XText xText = textDocument.getText();
101f18db294SLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
102f18db294SLiu Zhe 		xTextCursor.gotoEnd(true);
103f18db294SLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
1041ff9903bSLi Feng Wang 		String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
1051ff9903bSLi Feng Wang         Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
106f18db294SLiu Zhe         Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
107f18db294SLiu Zhe 	}
108f18db294SLiu Zhe 
10990a166ceSLiu Zhe 	/**
11090a166ceSLiu Zhe 	 * test save document as odt
11190a166ceSLiu Zhe 	 * @throws Exception
11290a166ceSLiu Zhe 	 */
113f18db294SLiu Zhe 	@Test
testSaveDocument()114f18db294SLiu Zhe 	public void testSaveDocument() throws Exception
115f18db294SLiu Zhe 	{
116f18db294SLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
117f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
118f18db294SLiu Zhe 		XText xText = textDocument.getText();
119f18db294SLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
120f18db294SLiu Zhe 		xTextCursor.gotoEnd(true);
1211ff9903bSLi Feng Wang 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
1221ff9903bSLi Feng Wang 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
1231ff9903bSLi Feng Wang 		SWUtil.save(textDocument);
1241ff9903bSLi Feng Wang         unoApp.closeDocument(textDocument);
125f18db294SLiu Zhe         component = unoApp.loadDocument(workingFilePath);
126f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
127f18db294SLiu Zhe 		xText = textDocument.getText();
128f18db294SLiu Zhe 		xTextCursor = xText.createTextCursor();
129f18db294SLiu Zhe 		xTextCursor.gotoEnd(true);
1301ff9903bSLi Feng Wang 		xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
131f18db294SLiu Zhe         Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
132f18db294SLiu Zhe 	}
133f18db294SLiu Zhe 
13490a166ceSLiu Zhe 	/**
13590a166ceSLiu Zhe 	 * test save document as doc
13690a166ceSLiu Zhe 	 * @throws Exception
13790a166ceSLiu Zhe 	 */
138f18db294SLiu Zhe 	@Test
testSaveAsDocument()139f18db294SLiu Zhe 	public void testSaveAsDocument() throws Exception
140f18db294SLiu Zhe 	{
141f18db294SLiu Zhe 		File saveAsFile = new File(workingFilePath + ".doc");
142f18db294SLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
143f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
144f18db294SLiu Zhe 		XText xText = textDocument.getText();
145f18db294SLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
146f18db294SLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
147f18db294SLiu Zhe 
148f18db294SLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
149f18db294SLiu Zhe 		xText.insertString(xTextCursor, "test Save odt as doc.", false);
1501ff9903bSLi Feng Wang 		SWUtil.saveAsDoc(textDocument, FileUtil.getUrl(saveAsFile));
151f18db294SLiu Zhe         Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
152f18db294SLiu Zhe 	}
153f18db294SLiu Zhe 
15490a166ceSLiu Zhe 	/**
15590a166ceSLiu Zhe 	 * test export document as pdf
15690a166ceSLiu Zhe 	 * @throws Exception
15790a166ceSLiu Zhe 	 */
158f18db294SLiu Zhe 	@Test
testExportAsPDF()159f18db294SLiu Zhe 	public void testExportAsPDF() throws Exception
160f18db294SLiu Zhe 	{
161f18db294SLiu Zhe 		File saveAsFile = new File(workingFilePath + ".pdf");
162f18db294SLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
163f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
164f18db294SLiu Zhe 
165f18db294SLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
166f18db294SLiu Zhe         PropertyValue[] storeProps = new PropertyValue[3];
167f18db294SLiu Zhe 
168f18db294SLiu Zhe         storeProps[0] = new PropertyValue();
169f18db294SLiu Zhe         storeProps[0].Name = "Overwrite";
170f18db294SLiu Zhe         storeProps[0].Value = new Boolean(true);
171f18db294SLiu Zhe 
172f18db294SLiu Zhe         storeProps[1] = new PropertyValue();
173f18db294SLiu Zhe         storeProps[1].Name = "FilterName";
174f18db294SLiu Zhe         storeProps[1].Value = "writer_pdf_Export";
175f18db294SLiu Zhe 
176f18db294SLiu Zhe         storeProps[2] = new PropertyValue();
177f18db294SLiu Zhe         storeProps[2].Name = "CompressionMode";
178f18db294SLiu Zhe         storeProps[2].Value = "1";
179f18db294SLiu Zhe 
180f18db294SLiu Zhe         xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
181f18db294SLiu Zhe 
182f18db294SLiu Zhe         Assert.assertTrue("Export document as PDF.", saveAsFile.exists());
183f18db294SLiu Zhe 	}
184f18db294SLiu Zhe 
18590a166ceSLiu Zhe 	/**
18690a166ceSLiu Zhe 	 * test save document as template
18790a166ceSLiu Zhe 	 * @throws Exception
18890a166ceSLiu Zhe 	 */
189f18db294SLiu Zhe 	@Test
testSaveAsTemplate()190f18db294SLiu Zhe 	public void testSaveAsTemplate() throws Exception
191f18db294SLiu Zhe 	{
192f18db294SLiu Zhe 		File saveAsFile = new File(workingFilePath + ".ott");
193f18db294SLiu Zhe 		XComponent component = unoApp.loadDocument(workingFilePath);
194f18db294SLiu Zhe 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
195f18db294SLiu Zhe 		XText xText = textDocument.getText();
196f18db294SLiu Zhe 		XTextCursor xTextCursor = xText.createTextCursor();
197f18db294SLiu Zhe 		xTextCursor.gotoEnd(true);
198f18db294SLiu Zhe 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
199f18db294SLiu Zhe 
200f18db294SLiu Zhe 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
201f18db294SLiu Zhe 
202f18db294SLiu Zhe 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
203f18db294SLiu Zhe         xStorable.store();
204f18db294SLiu Zhe 
205f18db294SLiu Zhe 		PropertyValue[] storeProps = new PropertyValue[3];
206f18db294SLiu Zhe 		storeProps[0] = new PropertyValue();
207f18db294SLiu Zhe 		storeProps[0].Name="TemplateName";
208f18db294SLiu Zhe 		storeProps[0].Value="MyNewCreatedTemplate";
209f18db294SLiu Zhe 
210f18db294SLiu Zhe 		storeProps[1] = new PropertyValue();
211f18db294SLiu Zhe 		storeProps[1].Name="TemplateRegionName";
212f18db294SLiu Zhe 		storeProps[1].Value="My Templates";
213f18db294SLiu Zhe 
214f18db294SLiu Zhe 		storeProps[2] = new PropertyValue();
215f18db294SLiu Zhe 		storeProps[2].Name="AsTemplate";
216f18db294SLiu Zhe 		storeProps[2].Value=new Boolean(true);
217f18db294SLiu Zhe 
218f18db294SLiu Zhe 		xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
2191ff9903bSLi Feng Wang 		unoApp.closeDocument(textDocument);
2201ff9903bSLi Feng Wang 		Assert.assertTrue("Export document as ott.", saveAsFile.exists());
221f18db294SLiu Zhe 	}
222f18db294SLiu Zhe 
223f18db294SLiu Zhe }
224