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