View | Details | Raw Unified | Return to issue 120703
Collapse All | Expand All

(-)testuno/source/testcase/uno/sw/breaks/CheckBreaks.java (+297 lines)
Line 0 Link Here
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
package testcase.uno.sw.breaks;
22
23
import static org.openoffice.test.common.Testspace.*;
24
25
import java.io.File;
26
27
import org.junit.After;
28
import org.junit.Before;
29
import org.junit.Test;
30
import org.junit.Assert;
31
import org.openoffice.test.common.FileUtil;
32
import org.openoffice.test.uno.UnoApp;
33
34
import testlib.uno.SWUtil;
35
36
import com.sun.star.text.XTextDocument;
37
import com.sun.star.text.XTextCursor;
38
import com.sun.star.text.XText;
39
import com.sun.star.text.ControlCharacter;
40
import com.sun.star.text.XTextViewCursor;
41
import com.sun.star.text.XPageCursor;
42
import com.sun.star.text.XTextViewCursorSupplier;
43
import com.sun.star.style.BreakType;
44
import com.sun.star.beans.XPropertySet;
45
import com.sun.star.frame.*;
46
import com.sun.star.uno.UnoRuntime;
47
import com.sun.star.lang.XComponent;
48
49
public class CheckBreaks {
50
	UnoApp unoApp = new UnoApp();
51
	XTextDocument textDocument = null;
52
	File temp = null;
53
	String tempFilePathODT = "";	
54
	String tempFilePathDOC = "";
55
	
56
	/**
57
	 * @throws java.lang.Exception
58
	 */
59
	@Before
60
	public void setUp() throws Exception {
61
		unoApp.start();
62
		
63
		FileUtil.deleteFile(getPath("temp"));
64
		temp = new File(getPath("temp"));
65
		temp.mkdirs();		
66
		
67
		tempFilePathODT = temp + "/tempFilePathODT.odt";
68
		tempFilePathDOC = temp + "/tempFilePathDOC.doc";		
69
	}
70
71
	@After
72
	public void tearDown() throws Exception {
73
		unoApp.close();
74
	}	
75
	
76
	/**
77
	 * test line break can be inserted and deleted.
78
	 * when save it as odt file, close and reopen, line break can be inserted and deleted.
79
	 * when save it as doc file, close and reopen, line break can be inserted and deleted.
80
	 * \n represent line break
81
	 * @throws Exception
82
	 */
83
	@Test
84
	public void testInsertDeleteLineBreak() throws Exception
85
	{
86
		XComponent xComponent = unoApp.newDocument("swriter");
87
		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);		
88
		this.insertNewLine(textDocument, "Line1", true);
89
		this.insertNewLine(textDocument, "Line2", false);
90
		this.insertNewLine(textDocument, "Line3", false);
91
		this.insertNewLine(textDocument, "Line4", false);
92
		
93
		int lineCount = this.getLineCount(textDocument);
94
		Assert.assertEquals("Line break is inserted when new document.",4,lineCount);
95
		this.deleteLineBreak(textDocument);
96
		lineCount = this.getLineCount(textDocument);
97
		Assert.assertEquals("Line break is deleted when new document", 3,lineCount);
98
		
99
		//save to odt, test the line break
100
		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));		
101
		unoApp.closeDocument(xComponent);	
102
		xComponent = unoApp.loadDocument(tempFilePathODT);
103
		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
104
		lineCount = this.getLineCount(textDocument);
105
		Assert.assertEquals("Line breaks when open saved odt file.",3,lineCount);
106
		this.insertNewLine(textDocument, "Line added when open saved odt file", false);
107
		lineCount = this.getLineCount(textDocument);
108
		Assert.assertEquals("Line break is inserted when open saved odt file.",4,lineCount);
109
		this.deleteLineBreak(textDocument);
110
		lineCount = this.getLineCount(textDocument);
111
		Assert.assertEquals("Line break is deleted when open saved odt file.",3,lineCount);
112
		
113
		//save to doc, test the line break
114
		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
115
		unoApp.closeDocument(xComponent);
116
		xComponent = unoApp.loadDocument(tempFilePathDOC);
117
		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);		
118
		lineCount = this.getLineCount(textDocument);
119
		Assert.assertEquals("Line breaks when open saved doc file.",3,lineCount);
120
		this.insertNewLine(textDocument, "Line added when open saved doc file", false);
121
		lineCount = this.getLineCount(textDocument);
122
		Assert.assertEquals("Line break is inserted when open saved doc file.",4,lineCount);
123
		this.deleteLineBreak(textDocument);
124
		lineCount = this.getLineCount(textDocument);
125
		Assert.assertEquals("Line break is deleted when open saved doc file.",3,lineCount);
126
		
127
		unoApp.closeDocument(xComponent);		
128
	}
129
	
130
	@Test
131
	public void testInsertDeletePageBreak() throws Exception
132
	{
133
		//new document, test page break
134
		XComponent xComponent = unoApp.newDocument("swriter");
135
		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);		
136
		this.insertNewPage(textDocument, "Page1", true);
137
		this.insertNewPage(textDocument, "Page2", false);
138
		this.insertNewPage(textDocument, "Page3", false);		
139
		int pageCount = SWUtil.getPageCount(textDocument);
140
		Assert.assertEquals("page break is inserted when new document", 3,pageCount);
141
		
142
		this.deleteFirstPage(textDocument);
143
		pageCount = SWUtil.getPageCount(textDocument);
144
		Assert.assertEquals("page break is deleted when new document", 2,pageCount);
145
		
146
		//save as odt, test page break
147
		SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT));		
148
		unoApp.closeDocument(xComponent);	
149
		xComponent = unoApp.loadDocument(tempFilePathODT);
150
		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
151
		pageCount = SWUtil.getPageCount(textDocument);
152
		Assert.assertEquals("Page breaks after open saved odt file", 2,pageCount);		
153
		this.insertNewPage(textDocument, "Page4", false);
154
		pageCount = SWUtil.getPageCount(textDocument);
155
		Assert.assertEquals("page break is inserted after open saved odt file", 3,pageCount);		
156
		
157
		this.deleteFirstPage(textDocument);
158
		pageCount = SWUtil.getPageCount(textDocument);
159
		Assert.assertEquals("page break is deleted after open saved odt file.", 2,pageCount);
160
		
161
		//save as doc, test page break
162
		SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC));
163
		unoApp.closeDocument(xComponent);	
164
		xComponent = unoApp.loadDocument(tempFilePathDOC);
165
		textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent);
166
		pageCount = SWUtil.getPageCount(textDocument);
167
		Assert.assertEquals("Page breaks after open saved doc file", 2,pageCount);
168
		
169
		this.deleteFirstPage(textDocument);
170
		pageCount = SWUtil.getPageCount(textDocument);
171
		Assert.assertEquals("page break is deleted after open saved doc file.", 1,pageCount);		
172
		
173
		this.insertNewPage(textDocument, "Page5", false);		
174
		pageCount = SWUtil.getPageCount(textDocument);
175
		Assert.assertEquals("page break is inserted after open saved doc file", 2,pageCount);			
176
			
177
		unoApp.closeDocument(xComponent);			
178
	}	
179
	
180
	
181
	
182
	
183
	/**
184
	 * insert a new page at the end of the document
185
	 * @param xTextDocument
186
	 * @param pageContent
187
	 * @param isFirstPage
188
	 * @throws Exception
189
	 */
190
	private void insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage) throws Exception
191
	{	
192
		XText xText = document.getText();
193
		XTextCursor textCursor = xText.createTextCursor();
194
		textCursor.gotoEnd(false);
195
		System.out.println("The content before insert " + pageContent + ":" + xText.getString());
196
		if(!isFirstPage)
197
		{			
198
			XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
199
			        XPropertySet.class, textCursor);
200
			xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);		
201
			document.getText().insertControlCharacter(textCursor,ControlCharacter.PARAGRAPH_BREAK,false); 		
202
		}			
203
		document.getText().insertString(textCursor, pageContent, false);
204
	}
205
	
206
	/**
207
	 * delete the first page of the document
208
	 * @param document
209
	 * @throws Exception
210
	 */
211
	private void deleteFirstPage(XTextDocument document) throws Exception
212
	{	
213
		XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, document);
214
        XController xController = xModel.getCurrentController();
215
        XTextViewCursorSupplier xTextViewCursorSupplier =
216
                (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
217
        XTextViewCursor textViewCursor = xTextViewCursorSupplier.getViewCursor();
218
219
        XPageCursor pageCursor = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class, textViewCursor);
220
221
        // Move the cursor to the start of the document
222
        textViewCursor.gotoStart(false);
223
224
        pageCursor.jumpToFirstPage();
225
        XTextCursor textCursor = textViewCursor.getText().createTextCursorByRange(textViewCursor.getStart());
226
227
        pageCursor.jumpToEndOfPage();        
228
        textCursor.gotoRange(textViewCursor.getEnd(), true); 
229
        //System.out.println("deleted: " + textCursor.getString());
230
        textCursor.setString("");
231
		
232
     // Page contents cleared, now delete the page break at the start
233
        textCursor.collapseToStart();
234
        if(textCursor.goRight((short) 1, true)){
235
        	//System.out.println("page break deleted: " + textCursor.getString());
236
        	textCursor.setString("");
237
        }       
238
		
239
	}	
240
	
241
	
242
	/**
243
	 * insert a new line at the end of the document.
244
	 * @param xText
245
	 * @param lineContent
246
	 * @param isFirstLine
247
	 * @throws Exception
248
	 */
249
	private void insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine) throws Exception
250
	{
251
		XText xText = xTextDocument.getText();
252
		XTextCursor xTextCursor = xText.createTextCursor();
253
		xTextCursor.gotoEnd(false);
254
		if(!isFirstLine)
255
		{
256
			xText.insertControlCharacter(xTextCursor, ControlCharacter.LINE_BREAK, false);			
257
		}
258
		
259
		xText.insertString(xTextCursor, lineContent, false);
260
	}
261
	
262
	/**
263
	 * delete first line break
264
	 * @param xText
265
	 * @throws Exception
266
	 */
267
	private void deleteLineBreak(XTextDocument xTextDocument) throws Exception
268
	{
269
		XText xText = xTextDocument.getText();
270
		String content = xText.getString();
271
		content = content.replaceFirst("\n", "");
272
		xText.setString(content);
273
	}
274
	
275
	/**
276
	 * get line count of all text.
277
	 * \n represent line break. 
278
	 * @param xText
279
	 * @return count of line breaks
280
	 * @throws Exception
281
	 */
282
	private int getLineCount(XTextDocument xTextDocument) throws Exception
283
	{
284
		int count = 1;	
285
		XText xText = xTextDocument.getText();
286
		String content = xText.getString();
287
		int index = content.indexOf("\n");
288
		while(index >=0)
289
		{
290
			count ++;
291
			content = content.substring(index+1);
292
			index = content.indexOf("\n");
293
		}
294
		return count;
295
	}
296
297
}
(-)testuno/source/testcase/uno/sw/DocumentTest.java (-2 / +41 lines)
Lines 18-24 Link Here
18
import com.sun.star.beans.PropertyValue;
18
import com.sun.star.beans.PropertyValue;
19
import com.sun.star.frame.*;
19
import com.sun.star.frame.*;
20
import com.sun.star.uno.UnoRuntime;
20
import com.sun.star.uno.UnoRuntime;
21
import com.sun.star.util.XCloseable;
21
import com.sun.star.lang.XComponent;
22
import com.sun.star.lang.XComponent;
23
import com.sun.star.container.XEnumerationAccess;
24
import com.sun.star.container.XEnumeration;
22
25
23
26
24
public class DocumentTest {
27
public class DocumentTest {
Lines 64-72 Link Here
64
		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
67
		XComponent component = componentLoader.loadComponentFromURL(FileUtil.getUrl(workingTemplatePath), "_blank", 0,pros);
65
		return component;
68
		return component;
66
	}
69
	}
70
	
67
71
68
72
	/**
73
	 * test close document
74
	 * @throws Exception
75
	 */
69
	@Test
76
	@Test
77
	public void testCloseDocument() throws Exception
78
	{
79
		XComponent component = unoApp.newDocument("swriter");
80
		unoApp.closeDocument(component);
81
		XModel xModel = unoApp.getDesktop().getCurrentFrame().getController().getModel();		
82
		Assert.assertTrue("Document has been closed.",xModel==null);
83
	}
84
	
85
	/**
86
	 * test new document
87
	 * @throws Exception
88
	 */
89
	@Test
70
	public void testNewDocument() throws Exception
90
	public void testNewDocument() throws Exception
71
	{
91
	{
72
		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());		
92
		XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, unoApp.getDesktop());		
Lines 78-84 Link Here
78
		unoApp.closeDocument(textDocument);
98
		unoApp.closeDocument(textDocument);
79
	}
99
	}
80
	
100
	
81
	
101
	/**
102
	 * test new document from template
103
	 * @throws Exception
104
	 */
82
	@Test
105
	@Test
83
	public void testNewDocumentFromTemplate() throws Exception
106
	public void testNewDocumentFromTemplate() throws Exception
84
	{		
107
	{		
Lines 99-104 Link Here
99
        unoApp.closeDocument(textDocument);
122
        unoApp.closeDocument(textDocument);
100
	}
123
	}
101
	
124
	
125
	/**
126
	 * test save document as odt
127
	 * @throws Exception
128
	 */
102
	@Test
129
	@Test
103
	public void testSaveDocument() throws Exception
130
	public void testSaveDocument() throws Exception
104
	{			
131
	{			
Lines 126-131 Link Here
126
        unoApp.closeDocument(textDocument);
153
        unoApp.closeDocument(textDocument);
127
	}
154
	}
128
	
155
	
156
	/**
157
	 * test save document as doc
158
	 * @throws Exception
159
	 */
129
	@Test
160
	@Test
130
	public void testSaveAsDocument() throws Exception
161
	public void testSaveAsDocument() throws Exception
131
	{		
162
	{		
Lines 155-160 Link Here
155
        unoApp.closeDocument(textDocument);
186
        unoApp.closeDocument(textDocument);
156
	}
187
	}
157
	
188
	
189
	/**
190
	 * test export document as pdf
191
	 * @throws Exception
192
	 */
158
	@Test
193
	@Test
159
	public void testExportAsPDF() throws Exception
194
	public void testExportAsPDF() throws Exception
160
	{		
195
	{		
Lines 184-189 Link Here
184
        unoApp.closeDocument(textDocument);
219
        unoApp.closeDocument(textDocument);
185
	}
220
	}
186
	
221
	
222
	/**
223
	 * test save document as template
224
	 * @throws Exception
225
	 */
187
	@Test
226
	@Test
188
	public void testSaveAsTemplate() throws Exception
227
	public void testSaveAsTemplate() throws Exception
189
	{
228
	{
(-)testuno/source/testlib/uno/sw/SWUtil.java (+39 lines)
Line 0 Link Here
1
package testlib.uno.sw;
2
3
import org.openoffice.test.uno.UnoApp;
4
5
import com.sun.star.beans.PropertyValue;
6
import com.sun.star.frame.XStorable;
7
import com.sun.star.text.XTextDocument;
8
import com.sun.star.uno.UnoRuntime;
9
import com.sun.star.lang.XComponent;
10
11
public class SWUtil {
12
	
13
	private static final UnoApp app = new UnoApp();
14
	public static void saveAsDoc(XTextDocument document, String url) throws Exception {
15
		saveAs(document, "MS Word 97", url);
16
		
17
	}
18
19
	public static void saveAsODT(XTextDocument document, String url) throws Exception {
20
		saveAs(document, "writer8", url);
21
	}
22
	
23
	public static void saveAs(XTextDocument document, String filterValue, String url) throws Exception {
24
		XStorable store = UnoRuntime.queryInterface(XStorable.class, document);
25
		
26
		PropertyValue[] propsValue = new PropertyValue[1];
27
		propsValue[0] = new PropertyValue();
28
		propsValue[0].Name = "FilterName";
29
		propsValue[0].Value = filterValue;
30
		store.storeAsURL(url, propsValue);
31
		
32
	}
33
	
34
	public static XTextDocument newDocument() throws Exception {
35
		XComponent component = app.newDocument("swriter");
36
		return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, component);
37
		
38
	}
39
}
(-)testuno/source/testlib/uno/SWUtil.java (+38 lines)
Lines 25-40 Link Here
25
25
26
import com.sun.star.beans.PropertyValue;
26
import com.sun.star.beans.PropertyValue;
27
import com.sun.star.beans.XPropertySet;
27
import com.sun.star.beans.XPropertySet;
28
import com.sun.star.container.XEnumeration;
29
import com.sun.star.container.XEnumerationAccess;
28
import com.sun.star.container.XNamed;
30
import com.sun.star.container.XNamed;
29
import com.sun.star.document.XDocumentInfo;
31
import com.sun.star.document.XDocumentInfo;
30
import com.sun.star.document.XDocumentInfoSupplier;
32
import com.sun.star.document.XDocumentInfoSupplier;
31
import com.sun.star.frame.XStorable;
33
import com.sun.star.frame.XStorable;
32
import com.sun.star.io.IOException;
34
import com.sun.star.io.IOException;
33
import com.sun.star.lang.XMultiServiceFactory;
35
import com.sun.star.lang.XMultiServiceFactory;
36
import com.sun.star.style.BreakType;
37
import com.sun.star.text.ControlCharacter;
34
import com.sun.star.text.XText;
38
import com.sun.star.text.XText;
35
import com.sun.star.text.XTextContent;
39
import com.sun.star.text.XTextContent;
36
import com.sun.star.text.XTextCursor;
40
import com.sun.star.text.XTextCursor;
37
import com.sun.star.text.XTextDocument;
41
import com.sun.star.text.XTextDocument;
42
import com.sun.star.frame.XModel;
43
import com.sun.star.frame.XController;
38
import com.sun.star.uno.UnoRuntime;
44
import com.sun.star.uno.UnoRuntime;
39
45
40
public class SWUtil {
46
public class SWUtil {
Lines 133-136 Link Here
133
		xBookmarkAsNamed.setName(bookmarkName);
139
		xBookmarkAsNamed.setName(bookmarkName);
134
		document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true);
140
		document.getText().insertTextContent(textCursor, xBookmarkAsTextContent, true);
135
	}
141
	}
142
	
143
	/**
144
	 * insert page break in current cursor
145
	 * @param xText
146
	 * @param currentCursor
147
	 * @throws Exception
148
	 */
149
	public static void insertPageBreak(XText xText, XTextCursor currentCursor) throws Exception
150
	{
151
		XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface(
152
		        XPropertySet.class, currentCursor);
153
		xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER);		
154
	    xText.insertControlCharacter(currentCursor,ControlCharacter.PARAGRAPH_BREAK,false); 
155
	}	
156
	
157
	
158
	/**
159
	 * get page count
160
	 * @param document
161
	 * @return
162
	 * @throws Exception
163
	 */
164
	public static int getPageCount(XTextDocument document) throws Exception
165
	{
166
		XModel xmodel = (XModel)UnoRuntime.queryInterface(XModel.class, document);
167
		XController xcont = xmodel.getCurrentController();
168
169
		XPropertySet xps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcont);
170
		Integer pageCount = (Integer) xps.getPropertyValue("PageCount"); 
171
		return pageCount.intValue();
172
	}
173
	
136
}
174
}

Return to issue 120703