1 package testcase.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.lang.XComponent;
22 
23 
24 public class DocumentTest {
25 	UnoApp unoApp = new UnoApp();
26 	XTextDocument textDocument = null;
27 	File temp = null;
28 	String workingFilePath = "";
29 	String workingTemplatePath = "";
30 
31 	/**
32 	 * @throws java.lang.Exception
33 	 */
34 	@Before
35 	public void setUp() throws Exception {
36 		unoApp.start();
37 
38 		FileUtil.deleteFile(getPath("temp"));
39 		temp = new File(getPath("temp"));
40 		temp.mkdirs();
41 
42 		//copy sample file to temp folder
43 		String originalFilePath = prepareData("testcase/uno/sw/DocumentTest.odt");
44 		String originalTemplatePath = prepareData("testcase/uno/sw/DocumentTest.ott");
45 		workingFilePath = temp + "/DocumentTest.odt";
46 		workingTemplatePath = temp + "/DocumentTest.ott";
47 		FileUtil.copyFile(new File(originalFilePath), new File(workingFilePath));
48 		FileUtil.copyFile(new File(originalTemplatePath), new File(workingTemplatePath));
49 	}
50 
51 	@After
52 	public void tearDown() throws Exception {
53 		unoApp.close();
54 	}
55 
56 	private XComponent newDocumentFromTemplate(String templatePath) throws Exception
57 	{
58 		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
59 		PropertyValue[] pros = new PropertyValue[1];
60 		pros[0] = new PropertyValue();
61 		pros[0].Name = "AsTemplate";
62 		pros[0].Value = new Boolean(true);
63 
64 		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
65 		return component;
66 	}
67 
68 
69 	@Test
70 	public void testNewDocument() throws Exception
71 	{
72 		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());
73 		XComponent component = componentLoader.loadComponentFromURL("private:factory/" + "swriter", "_blank", 0, new PropertyValue[0]);
74 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
75 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
76 		String title = xTitle.getTitle();
77 		Assert.assertEquals("New Document title start with \"Untitled\"",true, title.startsWith("Untitled"));
78 		unoApp.closeDocument(textDocument);
79 	}
80 
81 
82 	@Test
83 	public void testNewDocumentFromTemplate() throws Exception
84 	{
85 		XComponent component = this.newDocumentFromTemplate(workingTemplatePath);
86 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
87 		XText xText = textDocument.getText();
88 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
89 		xText = textDocument.getText();
90 		XTextCursor xTextCursor = xText.createTextCursor();
91 		xTextCursor.gotoEnd(true);
92 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
93 		String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
94 
95         Assert.assertEquals("new document from template, heading style in template is remained. ", "Heading 1", paraStyle);
96 
97         Assert.assertEquals("new document from template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
98 
99         unoApp.closeDocument(textDocument);
100 	}
101 
102 	@Test
103 	public void testSaveDocument() throws Exception
104 	{
105 		XComponent component = unoApp.loadDocument(workingFilePath);
106 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
107 		XText xText = textDocument.getText();
108 		XTextCursor xTextCursor = xText.createTextCursor();
109 		xTextCursor.gotoEnd(true);
110 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
111 
112 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
113 
114 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
115         xStorable.store();
116         unoApp.closeDocument(textDocument);
117 
118         component = unoApp.loadDocument(workingFilePath);
119 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
120 		xText = textDocument.getText();
121 		xTextCursor = xText.createTextCursor();
122 		xTextCursor.gotoEnd(true);
123 		xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
124 
125         Assert.assertEquals("Modify plain text to heading 1 style. ", "Heading 1", (String)xPropertySet.getPropertyValue("ParaStyleName"));
126         unoApp.closeDocument(textDocument);
127 	}
128 
129 	@Test
130 	public void testSaveAsDocument() throws Exception
131 	{
132 		File saveAsFile = new File(workingFilePath + ".doc");
133 		XComponent component = unoApp.loadDocument(workingFilePath);
134 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
135 		XText xText = textDocument.getText();
136 		XTextCursor xTextCursor = xText.createTextCursor();
137 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
138 
139 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
140 		xText.insertString(xTextCursor, "test Save odt as doc.", false);
141 
142 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
143         PropertyValue[] storeProps = new PropertyValue[2];
144 
145         storeProps[0] = new PropertyValue();
146         storeProps[0].Name = "Overwrite";
147         storeProps[0].Value = new Boolean(true);
148 
149         storeProps[1] = new PropertyValue();
150         storeProps[1].Name = "FilterName";
151         storeProps[1].Value = "MS Word 97";
152 
153         xStorable.storeAsURL(FileUtil.getUrl(saveAsFile), storeProps);
154         Assert.assertTrue("Save odt document as doc the file exist: " + saveAsFile.getAbsolutePath(), saveAsFile.exists());
155         unoApp.closeDocument(textDocument);
156 	}
157 
158 	@Test
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         unoApp.closeDocument(textDocument);
185 	}
186 
187 	@Test
188 	public void testSaveAsTemplate() throws Exception
189 	{
190 		File saveAsFile = new File(workingFilePath + ".ott");
191 		XComponent component = unoApp.loadDocument(workingFilePath);
192 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
193 		XText xText = textDocument.getText();
194 		XTextCursor xTextCursor = xText.createTextCursor();
195 		xTextCursor.gotoEnd(true);
196 		XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
197 
198 		xPropertySet.setPropertyValue("ParaStyleName", "Heading 1");
199 
200 		XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, component);
201         xStorable.store();
202 
203 		PropertyValue[] storeProps = new PropertyValue[3];
204 		storeProps[0] = new PropertyValue();
205 		storeProps[0].Name="TemplateName";
206 		storeProps[0].Value="MyNewCreatedTemplate";
207 
208 		storeProps[1] = new PropertyValue();
209 		storeProps[1].Name="TemplateRegionName";
210 		storeProps[1].Value="My Templates";
211 
212 		storeProps[2] = new PropertyValue();
213 		storeProps[2].Name="AsTemplate";
214 		storeProps[2].Value=new Boolean(true);
215 
216 		xStorable.storeToURL(FileUtil.getUrl(saveAsFile), storeProps);
217 		unoApp.closeDocument(textDocument);
218 
219 		component = this.newDocumentFromTemplate(saveAsFile.getAbsolutePath());
220 		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
221 		XTitle xTitle = (XTitle)UnoRuntime.queryInterface(XTitle.class, textDocument);
222 		xText = textDocument.getText();
223 		xTextCursor = xText.createTextCursor();
224 		xTextCursor.gotoEnd(true);
225 		xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
226 		String paraStyle = (String)xPropertySet.getPropertyValue("ParaStyleName");
227 		Assert.assertEquals("Save document as template, heading style is remained. ", "Heading 1", paraStyle);
228         Assert.assertEquals("Save document as template, title start with \"Untitled\".", true, xTitle.getTitle().startsWith("Untitled"));
229         unoApp.closeDocument(textDocument);
230 	}
231 
232 }
233