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.paragraph;
23 
24 import static org.junit.Assert.*;
25 
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openoffice.test.common.FileUtil;
30 import org.openoffice.test.common.Testspace;
31 import org.openoffice.test.uno.UnoApp;
32 
33 import com.sun.star.style.DropCapFormat;
34 import com.sun.star.text.*;
35 import com.sun.star.beans.*;
36 import com.sun.star.frame.XStorable;
37 import com.sun.star.uno.UnoRuntime;
38 
39 public class ParagraphDropcap {
40 	private static final UnoApp app = new UnoApp();
41 	XText xText = null;
42 
43 	@Before
setUp()44 	public void setUp() throws Exception {
45 		app.start();
46 
47 	}
48 
49 	@After
tearDown()50 	public void tearDown() throws Exception {
51 		app.close();
52 	}
53 	/*
54 	 * test paragraph background color
55 	 * 1.new a text document
56 	 * 2.insert some text
57 	 * 3.set paragraph drop
58 	 * 4.save and close the document
59 	 * 5.reload the saved document and check the paragraph drop cap
60 	 */
61 	@Test
testParagraphDropcapSetting()62 	public void testParagraphDropcapSetting() throws Exception {
63 
64 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
65 		xText = xTextDocument.getText();
66 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
67 				"Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!");
68 		// create text cursor for selecting and formatting text
69 		XTextCursor xTextCursor = xText.createTextCursor();
70 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
71 		//set paragraph dropcaps
72 		DropCapFormat dropcapFormat=new DropCapFormat();
73 		dropcapFormat.Lines=3;
74 		dropcapFormat.Distance=101;
75 		dropcapFormat.Count=9;
76 		xCursorProps.setPropertyValue("DropCapFormat",dropcapFormat);
77 		//xCursorProps.setPropertyValue("DropCapWholeWord",true);
78 		//save to odt
79 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
80 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
81 		aStoreProperties_odt[0] = new PropertyValue();
82 		aStoreProperties_odt[1] = new PropertyValue();
83 		aStoreProperties_odt[0].Name = "Override";
84 		aStoreProperties_odt[0].Value = true;
85 		aStoreProperties_odt[1].Name = "FilterName";
86 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
87 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
88 		//save to doc
89 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
90 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
91 		aStoreProperties_doc[0] = new PropertyValue();
92 		aStoreProperties_doc[1] = new PropertyValue();
93 		aStoreProperties_doc[0].Name = "Override";
94 		aStoreProperties_doc[0].Value = true;
95 		aStoreProperties_doc[1].Name = "FilterName";
96 		aStoreProperties_doc[1].Value = "MS Word 97";
97 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
98 		app.closeDocument(xTextDocument);
99 
100 		//reopen the document
101 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
102 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
103 		//verify paragraph dropcap
104 		DropCapFormat dropcapFormat_assert_odt = (DropCapFormat) UnoRuntime.queryInterface(DropCapFormat.class,xCursorProps_Assert_odt.getPropertyValue("DropCapFormat"));
105 		assertEquals("assert paragraph dropcaps",9,dropcapFormat_assert_odt.Count);
106 		assertEquals("assert paragraph dropcaps",3,dropcapFormat_assert_odt.Lines);
107 		assertEquals("assert paragraph dropcaps",101,dropcapFormat_assert_odt.Distance);
108 
109 		//reopen the document
110 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
111 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
112 		//verify paragraph dropcap
113 		DropCapFormat dropcapFormat_assert_doc = (DropCapFormat) UnoRuntime.queryInterface(DropCapFormat.class,xCursorProps_Assert_doc.getPropertyValue("DropCapFormat"));
114 		assertEquals("assert paragraph dropcaps",9,dropcapFormat_assert_doc.Count);
115 		assertEquals("assert paragraph dropcaps",3,dropcapFormat_assert_doc.Lines);
116 		assertEquals("assert paragraph dropcaps",101,dropcapFormat_assert_doc.Distance);
117 	}
118 	@Test
testParagraphDropcapWholewordSetting()119 	public void testParagraphDropcapWholewordSetting() throws Exception {
120 
121 		XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, app.newDocument("swriter"));// new a text document
122 		xText = xTextDocument.getText();
123 		xText.setString("we are Chinese,they are American.we are all living in one earth!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!" +
124 				"Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!Hello,world!");
125 		// create text cursor for selecting and formatting text
126 		XTextCursor xTextCursor = xText.createTextCursor();
127 		XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);
128 		//set paragraph dropcaps
129 		DropCapFormat dropcapFormat=new DropCapFormat();
130 		dropcapFormat.Lines=3;
131 		dropcapFormat.Distance=101;
132 		xCursorProps.setPropertyValue("DropCapFormat",dropcapFormat);
133 		xCursorProps.setPropertyValue("DropCapWholeWord",true);
134 		//save to odt
135 		XStorable xStorable_odt = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
136 		PropertyValue[] aStoreProperties_odt = new PropertyValue[2];
137 		aStoreProperties_odt[0] = new PropertyValue();
138 		aStoreProperties_odt[1] = new PropertyValue();
139 		aStoreProperties_odt[0].Name = "Override";
140 		aStoreProperties_odt[0].Value = true;
141 		aStoreProperties_odt[1].Name = "FilterName";
142 		aStoreProperties_odt[1].Value = "StarOffice XML (Writer)";
143 		xStorable_odt.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.odt")), aStoreProperties_odt);
144 		//save to doc
145 		XStorable xStorable_doc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xTextDocument);
146 		PropertyValue[] aStoreProperties_doc = new PropertyValue[2];
147 		aStoreProperties_doc[0] = new PropertyValue();
148 		aStoreProperties_doc[1] = new PropertyValue();
149 		aStoreProperties_doc[0].Name = "Override";
150 		aStoreProperties_doc[0].Value = true;
151 		aStoreProperties_doc[1].Name = "FilterName";
152 		aStoreProperties_doc[1].Value = "MS Word 97";
153 		xStorable_doc.storeToURL(FileUtil.getUrl(Testspace.getPath("output/test.doc")), aStoreProperties_doc);
154 		app.closeDocument(xTextDocument);
155 
156 		//reopen the document
157 		XTextDocument assertDocument_odt=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.odt")));
158 		XPropertySet xCursorProps_Assert_odt = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_odt.getText().createTextCursor());
159 		//verify paragraph dropcap
160 		DropCapFormat dropcapFormat_assert_odt = (DropCapFormat) UnoRuntime.queryInterface(DropCapFormat.class,xCursorProps_Assert_odt.getPropertyValue("DropCapFormat"));
161 		assertEquals("assert paragraph dropcaps",true,xCursorProps_Assert_odt.getPropertyValue("DropCapWholeWord"));
162 		assertEquals("assert paragraph dropcaps",3,dropcapFormat_assert_odt.Lines);
163 		assertEquals("assert paragraph dropcaps",101,dropcapFormat_assert_odt.Distance);
164 
165 		//reopen the document
166 		XTextDocument assertDocument_doc=(XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, app.loadDocument(Testspace.getPath("output/test.doc")));
167 		XPropertySet xCursorProps_Assert_doc = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, assertDocument_doc.getText().createTextCursor());
168 		//verify paragraph dropcap
169 		DropCapFormat dropcapFormat_assert_doc = (DropCapFormat) UnoRuntime.queryInterface(DropCapFormat.class,xCursorProps_Assert_doc.getPropertyValue("DropCapFormat"));
170 		assertEquals("assert paragraph dropcaps",3,dropcapFormat_assert_doc.Lines);
171 		assertEquals("assert paragraph dropcaps",101,dropcapFormat_assert_doc.Distance);
172 		//when save to doc,the DropCapWholeWord option will disable,and enable dropcapFormat.count
173 		assertEquals("assert paragraph dropcaps",false,xCursorProps_Assert_doc.getPropertyValue("DropCapWholeWord"));
174 	}
175 }
176