1*eba4d44aSLiu Zhe /************************************************************** 2*eba4d44aSLiu Zhe * 3*eba4d44aSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4*eba4d44aSLiu Zhe * or more contributor license agreements. See the NOTICE file 5*eba4d44aSLiu Zhe * distributed with this work for additional information 6*eba4d44aSLiu Zhe * regarding copyright ownership. The ASF licenses this file 7*eba4d44aSLiu Zhe * to you under the Apache License, Version 2.0 (the 8*eba4d44aSLiu Zhe * "License"); you may not use this file except in compliance 9*eba4d44aSLiu Zhe * with the License. You may obtain a copy of the License at 10*eba4d44aSLiu Zhe * 11*eba4d44aSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12*eba4d44aSLiu Zhe * 13*eba4d44aSLiu Zhe * Unless required by applicable law or agreed to in writing, 14*eba4d44aSLiu Zhe * software distributed under the License is distributed on an 15*eba4d44aSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*eba4d44aSLiu Zhe * KIND, either express or implied. See the License for the 17*eba4d44aSLiu Zhe * specific language governing permissions and limitations 18*eba4d44aSLiu Zhe * under the License. 19*eba4d44aSLiu Zhe * 20*eba4d44aSLiu Zhe *************************************************************/ 21*eba4d44aSLiu Zhe package fvt.uno.sw.breaks; 22*eba4d44aSLiu Zhe 23*eba4d44aSLiu Zhe import static org.openoffice.test.common.Testspace.*; 24*eba4d44aSLiu Zhe 25*eba4d44aSLiu Zhe import java.io.File; 26*eba4d44aSLiu Zhe 27*eba4d44aSLiu Zhe import org.junit.After; 28*eba4d44aSLiu Zhe import org.junit.Before; 29*eba4d44aSLiu Zhe import org.junit.Test; 30*eba4d44aSLiu Zhe import org.junit.Assert; 31*eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil; 32*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp; 33*eba4d44aSLiu Zhe 34*eba4d44aSLiu Zhe import testlib.uno.SWUtil; 35*eba4d44aSLiu Zhe 36*eba4d44aSLiu Zhe import com.sun.star.text.XTextDocument; 37*eba4d44aSLiu Zhe import com.sun.star.text.XTextCursor; 38*eba4d44aSLiu Zhe import com.sun.star.text.XText; 39*eba4d44aSLiu Zhe import com.sun.star.text.ControlCharacter; 40*eba4d44aSLiu Zhe import com.sun.star.text.XTextViewCursor; 41*eba4d44aSLiu Zhe import com.sun.star.text.XPageCursor; 42*eba4d44aSLiu Zhe import com.sun.star.text.XTextViewCursorSupplier; 43*eba4d44aSLiu Zhe import com.sun.star.style.BreakType; 44*eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet; 45*eba4d44aSLiu Zhe import com.sun.star.frame.*; 46*eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime; 47*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent; 48*eba4d44aSLiu Zhe 49*eba4d44aSLiu Zhe public class CheckBreaks { 50*eba4d44aSLiu Zhe UnoApp unoApp = new UnoApp(); 51*eba4d44aSLiu Zhe XTextDocument textDocument = null; 52*eba4d44aSLiu Zhe File temp = null; 53*eba4d44aSLiu Zhe String tempFilePathODT = ""; 54*eba4d44aSLiu Zhe String tempFilePathDOC = ""; 55*eba4d44aSLiu Zhe 56*eba4d44aSLiu Zhe /** 57*eba4d44aSLiu Zhe * @throws java.lang.Exception 58*eba4d44aSLiu Zhe */ 59*eba4d44aSLiu Zhe @Before setUp()60*eba4d44aSLiu Zhe public void setUp() throws Exception { 61*eba4d44aSLiu Zhe unoApp.start(); 62*eba4d44aSLiu Zhe 63*eba4d44aSLiu Zhe FileUtil.deleteFile(getPath("temp")); 64*eba4d44aSLiu Zhe temp = new File(getPath("temp")); 65*eba4d44aSLiu Zhe temp.mkdirs(); 66*eba4d44aSLiu Zhe 67*eba4d44aSLiu Zhe tempFilePathODT = temp + "/tempFilePathODT.odt"; 68*eba4d44aSLiu Zhe tempFilePathDOC = temp + "/tempFilePathDOC.doc"; 69*eba4d44aSLiu Zhe } 70*eba4d44aSLiu Zhe 71*eba4d44aSLiu Zhe @After tearDown()72*eba4d44aSLiu Zhe public void tearDown() throws Exception { 73*eba4d44aSLiu Zhe unoApp.close(); 74*eba4d44aSLiu Zhe } 75*eba4d44aSLiu Zhe 76*eba4d44aSLiu Zhe /** 77*eba4d44aSLiu Zhe * test line break can be inserted and deleted. 78*eba4d44aSLiu Zhe * when save it as odt file, close and reopen, line break can be inserted and deleted. 79*eba4d44aSLiu Zhe * when save it as doc file, close and reopen, line break can be inserted and deleted. 80*eba4d44aSLiu Zhe * \n represent line break 81*eba4d44aSLiu Zhe * @throws Exception 82*eba4d44aSLiu Zhe */ 83*eba4d44aSLiu Zhe @Test testInsertDeleteLineBreak()84*eba4d44aSLiu Zhe public void testInsertDeleteLineBreak() throws Exception 85*eba4d44aSLiu Zhe { 86*eba4d44aSLiu Zhe XComponent xComponent = unoApp.newDocument("swriter"); 87*eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent); 88*eba4d44aSLiu Zhe this.insertNewLine(textDocument, "Line1", true); 89*eba4d44aSLiu Zhe this.insertNewLine(textDocument, "Line2", false); 90*eba4d44aSLiu Zhe this.insertNewLine(textDocument, "Line3", false); 91*eba4d44aSLiu Zhe this.insertNewLine(textDocument, "Line4", false); 92*eba4d44aSLiu Zhe 93*eba4d44aSLiu Zhe int lineCount = this.getLineCount(textDocument); 94*eba4d44aSLiu Zhe Assert.assertEquals("Line break is inserted when new document.",4,lineCount); 95*eba4d44aSLiu Zhe this.deleteLineBreak(textDocument); 96*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 97*eba4d44aSLiu Zhe Assert.assertEquals("Line break is deleted when new document", 3,lineCount); 98*eba4d44aSLiu Zhe 99*eba4d44aSLiu Zhe //save to odt, test the line break 100*eba4d44aSLiu Zhe SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT)); 101*eba4d44aSLiu Zhe unoApp.closeDocument(xComponent); 102*eba4d44aSLiu Zhe xComponent = unoApp.loadDocument(tempFilePathODT); 103*eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent); 104*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 105*eba4d44aSLiu Zhe Assert.assertEquals("Line breaks when open saved odt file.",3,lineCount); 106*eba4d44aSLiu Zhe this.insertNewLine(textDocument, "Line added when open saved odt file", false); 107*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 108*eba4d44aSLiu Zhe Assert.assertEquals("Line break is inserted when open saved odt file.",4,lineCount); 109*eba4d44aSLiu Zhe this.deleteLineBreak(textDocument); 110*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 111*eba4d44aSLiu Zhe Assert.assertEquals("Line break is deleted when open saved odt file.",3,lineCount); 112*eba4d44aSLiu Zhe 113*eba4d44aSLiu Zhe //save to doc, test the line break 114*eba4d44aSLiu Zhe SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC)); 115*eba4d44aSLiu Zhe unoApp.closeDocument(xComponent); 116*eba4d44aSLiu Zhe xComponent = unoApp.loadDocument(tempFilePathDOC); 117*eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent); 118*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 119*eba4d44aSLiu Zhe Assert.assertEquals("Line breaks when open saved doc file.",3,lineCount); 120*eba4d44aSLiu Zhe this.insertNewLine(textDocument, "Line added when open saved doc file", false); 121*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 122*eba4d44aSLiu Zhe Assert.assertEquals("Line break is inserted when open saved doc file.",4,lineCount); 123*eba4d44aSLiu Zhe this.deleteLineBreak(textDocument); 124*eba4d44aSLiu Zhe lineCount = this.getLineCount(textDocument); 125*eba4d44aSLiu Zhe Assert.assertEquals("Line break is deleted when open saved doc file.",3,lineCount); 126*eba4d44aSLiu Zhe 127*eba4d44aSLiu Zhe unoApp.closeDocument(xComponent); 128*eba4d44aSLiu Zhe } 129*eba4d44aSLiu Zhe 130*eba4d44aSLiu Zhe @Test testInsertDeletePageBreak()131*eba4d44aSLiu Zhe public void testInsertDeletePageBreak() throws Exception 132*eba4d44aSLiu Zhe { 133*eba4d44aSLiu Zhe //new document, test page break 134*eba4d44aSLiu Zhe XComponent xComponent = unoApp.newDocument("swriter"); 135*eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent); 136*eba4d44aSLiu Zhe this.insertNewPage(textDocument, "Page1", true); 137*eba4d44aSLiu Zhe this.insertNewPage(textDocument, "Page2", false); 138*eba4d44aSLiu Zhe this.insertNewPage(textDocument, "Page3", false); 139*eba4d44aSLiu Zhe int pageCount = SWUtil.getPageCount(textDocument); 140*eba4d44aSLiu Zhe Assert.assertEquals("page break is inserted when new document", 3,pageCount); 141*eba4d44aSLiu Zhe 142*eba4d44aSLiu Zhe this.deleteFirstPage(textDocument); 143*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 144*eba4d44aSLiu Zhe Assert.assertEquals("page break is deleted when new document", 2,pageCount); 145*eba4d44aSLiu Zhe 146*eba4d44aSLiu Zhe //save as odt, test page break 147*eba4d44aSLiu Zhe SWUtil.saveAsODT(textDocument, FileUtil.getUrl(tempFilePathODT)); 148*eba4d44aSLiu Zhe unoApp.closeDocument(xComponent); 149*eba4d44aSLiu Zhe xComponent = unoApp.loadDocument(tempFilePathODT); 150*eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent); 151*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 152*eba4d44aSLiu Zhe Assert.assertEquals("Page breaks after open saved odt file", 2,pageCount); 153*eba4d44aSLiu Zhe this.insertNewPage(textDocument, "Page4", false); 154*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 155*eba4d44aSLiu Zhe Assert.assertEquals("page break is inserted after open saved odt file", 3,pageCount); 156*eba4d44aSLiu Zhe 157*eba4d44aSLiu Zhe this.deleteFirstPage(textDocument); 158*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 159*eba4d44aSLiu Zhe Assert.assertEquals("page break is deleted after open saved odt file.", 2,pageCount); 160*eba4d44aSLiu Zhe 161*eba4d44aSLiu Zhe //save as doc, test page break 162*eba4d44aSLiu Zhe SWUtil.saveAs(textDocument, "MS Word 97", FileUtil.getUrl(tempFilePathDOC)); 163*eba4d44aSLiu Zhe unoApp.closeDocument(xComponent); 164*eba4d44aSLiu Zhe xComponent = unoApp.loadDocument(tempFilePathDOC); 165*eba4d44aSLiu Zhe textDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,xComponent); 166*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 167*eba4d44aSLiu Zhe Assert.assertEquals("Page breaks after open saved doc file", 2,pageCount); 168*eba4d44aSLiu Zhe 169*eba4d44aSLiu Zhe this.deleteFirstPage(textDocument); 170*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 171*eba4d44aSLiu Zhe Assert.assertEquals("page break is deleted after open saved doc file.", 1,pageCount); 172*eba4d44aSLiu Zhe 173*eba4d44aSLiu Zhe this.insertNewPage(textDocument, "Page5", false); 174*eba4d44aSLiu Zhe pageCount = SWUtil.getPageCount(textDocument); 175*eba4d44aSLiu Zhe Assert.assertEquals("page break is inserted after open saved doc file", 2,pageCount); 176*eba4d44aSLiu Zhe 177*eba4d44aSLiu Zhe unoApp.closeDocument(xComponent); 178*eba4d44aSLiu Zhe } 179*eba4d44aSLiu Zhe 180*eba4d44aSLiu Zhe 181*eba4d44aSLiu Zhe 182*eba4d44aSLiu Zhe 183*eba4d44aSLiu Zhe /** 184*eba4d44aSLiu Zhe * insert a new page at the end of the document 185*eba4d44aSLiu Zhe * @param xTextDocument 186*eba4d44aSLiu Zhe * @param pageContent 187*eba4d44aSLiu Zhe * @param isFirstPage 188*eba4d44aSLiu Zhe * @throws Exception 189*eba4d44aSLiu Zhe */ insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage)190*eba4d44aSLiu Zhe private void insertNewPage(XTextDocument document, String pageContent, Boolean isFirstPage) throws Exception 191*eba4d44aSLiu Zhe { 192*eba4d44aSLiu Zhe XText xText = document.getText(); 193*eba4d44aSLiu Zhe XTextCursor textCursor = xText.createTextCursor(); 194*eba4d44aSLiu Zhe textCursor.gotoEnd(false); 195*eba4d44aSLiu Zhe System.out.println("The content before insert " + pageContent + ":" + xText.getString()); 196*eba4d44aSLiu Zhe if(!isFirstPage) 197*eba4d44aSLiu Zhe { 198*eba4d44aSLiu Zhe XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 199*eba4d44aSLiu Zhe XPropertySet.class, textCursor); 200*eba4d44aSLiu Zhe xCursorProps.setPropertyValue("BreakType", BreakType.PAGE_AFTER); 201*eba4d44aSLiu Zhe document.getText().insertControlCharacter(textCursor,ControlCharacter.PARAGRAPH_BREAK,false); 202*eba4d44aSLiu Zhe } 203*eba4d44aSLiu Zhe document.getText().insertString(textCursor, pageContent, false); 204*eba4d44aSLiu Zhe } 205*eba4d44aSLiu Zhe 206*eba4d44aSLiu Zhe /** 207*eba4d44aSLiu Zhe * delete the first page of the document 208*eba4d44aSLiu Zhe * @param document 209*eba4d44aSLiu Zhe * @throws Exception 210*eba4d44aSLiu Zhe */ deleteFirstPage(XTextDocument document)211*eba4d44aSLiu Zhe private void deleteFirstPage(XTextDocument document) throws Exception 212*eba4d44aSLiu Zhe { 213*eba4d44aSLiu Zhe XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, document); 214*eba4d44aSLiu Zhe XController xController = xModel.getCurrentController(); 215*eba4d44aSLiu Zhe XTextViewCursorSupplier xTextViewCursorSupplier = 216*eba4d44aSLiu Zhe (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController); 217*eba4d44aSLiu Zhe XTextViewCursor textViewCursor = xTextViewCursorSupplier.getViewCursor(); 218*eba4d44aSLiu Zhe 219*eba4d44aSLiu Zhe XPageCursor pageCursor = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class, textViewCursor); 220*eba4d44aSLiu Zhe 221*eba4d44aSLiu Zhe // Move the cursor to the start of the document 222*eba4d44aSLiu Zhe textViewCursor.gotoStart(false); 223*eba4d44aSLiu Zhe 224*eba4d44aSLiu Zhe pageCursor.jumpToFirstPage(); 225*eba4d44aSLiu Zhe XTextCursor textCursor = textViewCursor.getText().createTextCursorByRange(textViewCursor.getStart()); 226*eba4d44aSLiu Zhe 227*eba4d44aSLiu Zhe pageCursor.jumpToEndOfPage(); 228*eba4d44aSLiu Zhe textCursor.gotoRange(textViewCursor.getEnd(), true); 229*eba4d44aSLiu Zhe //System.out.println("deleted: " + textCursor.getString()); 230*eba4d44aSLiu Zhe textCursor.setString(""); 231*eba4d44aSLiu Zhe 232*eba4d44aSLiu Zhe // Page contents cleared, now delete the page break at the start 233*eba4d44aSLiu Zhe textCursor.collapseToStart(); 234*eba4d44aSLiu Zhe if(textCursor.goRight((short) 1, true)){ 235*eba4d44aSLiu Zhe //System.out.println("page break deleted: " + textCursor.getString()); 236*eba4d44aSLiu Zhe textCursor.setString(""); 237*eba4d44aSLiu Zhe } 238*eba4d44aSLiu Zhe 239*eba4d44aSLiu Zhe } 240*eba4d44aSLiu Zhe 241*eba4d44aSLiu Zhe 242*eba4d44aSLiu Zhe /** 243*eba4d44aSLiu Zhe * insert a new line at the end of the document. 244*eba4d44aSLiu Zhe * @param xText 245*eba4d44aSLiu Zhe * @param lineContent 246*eba4d44aSLiu Zhe * @param isFirstLine 247*eba4d44aSLiu Zhe * @throws Exception 248*eba4d44aSLiu Zhe */ insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine)249*eba4d44aSLiu Zhe private void insertNewLine(XTextDocument xTextDocument, String lineContent, Boolean isFirstLine) throws Exception 250*eba4d44aSLiu Zhe { 251*eba4d44aSLiu Zhe XText xText = xTextDocument.getText(); 252*eba4d44aSLiu Zhe XTextCursor xTextCursor = xText.createTextCursor(); 253*eba4d44aSLiu Zhe xTextCursor.gotoEnd(false); 254*eba4d44aSLiu Zhe if(!isFirstLine) 255*eba4d44aSLiu Zhe { 256*eba4d44aSLiu Zhe xText.insertControlCharacter(xTextCursor, ControlCharacter.LINE_BREAK, false); 257*eba4d44aSLiu Zhe } 258*eba4d44aSLiu Zhe 259*eba4d44aSLiu Zhe xText.insertString(xTextCursor, lineContent, false); 260*eba4d44aSLiu Zhe } 261*eba4d44aSLiu Zhe 262*eba4d44aSLiu Zhe /** 263*eba4d44aSLiu Zhe * delete first line break 264*eba4d44aSLiu Zhe * @param xText 265*eba4d44aSLiu Zhe * @throws Exception 266*eba4d44aSLiu Zhe */ deleteLineBreak(XTextDocument xTextDocument)267*eba4d44aSLiu Zhe private void deleteLineBreak(XTextDocument xTextDocument) throws Exception 268*eba4d44aSLiu Zhe { 269*eba4d44aSLiu Zhe XText xText = xTextDocument.getText(); 270*eba4d44aSLiu Zhe String content = xText.getString(); 271*eba4d44aSLiu Zhe content = content.replaceFirst("\n", ""); 272*eba4d44aSLiu Zhe xText.setString(content); 273*eba4d44aSLiu Zhe } 274*eba4d44aSLiu Zhe 275*eba4d44aSLiu Zhe /** 276*eba4d44aSLiu Zhe * get line count of all text. 277*eba4d44aSLiu Zhe * \n represent line break. 278*eba4d44aSLiu Zhe * @param xText 279*eba4d44aSLiu Zhe * @return count of line breaks 280*eba4d44aSLiu Zhe * @throws Exception 281*eba4d44aSLiu Zhe */ getLineCount(XTextDocument xTextDocument)282*eba4d44aSLiu Zhe private int getLineCount(XTextDocument xTextDocument) throws Exception 283*eba4d44aSLiu Zhe { 284*eba4d44aSLiu Zhe int count = 1; 285*eba4d44aSLiu Zhe XText xText = xTextDocument.getText(); 286*eba4d44aSLiu Zhe String content = xText.getString(); 287*eba4d44aSLiu Zhe int index = content.indexOf("\n"); 288*eba4d44aSLiu Zhe while(index >=0) 289*eba4d44aSLiu Zhe { 290*eba4d44aSLiu Zhe count ++; 291*eba4d44aSLiu Zhe content = content.substring(index+1); 292*eba4d44aSLiu Zhe index = content.indexOf("\n"); 293*eba4d44aSLiu Zhe } 294*eba4d44aSLiu Zhe return count; 295*eba4d44aSLiu Zhe } 296*eba4d44aSLiu Zhe 297*eba4d44aSLiu Zhe } 298