10d8ff32eSLiu Zhe /**************************************************************
20d8ff32eSLiu Zhe  *
30d8ff32eSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
40d8ff32eSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
50d8ff32eSLiu Zhe  * distributed with this work for additional information
60d8ff32eSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
70d8ff32eSLiu Zhe  * to you under the Apache License, Version 2.0 (the
80d8ff32eSLiu Zhe  * "License"); you may not use this file except in compliance
90d8ff32eSLiu Zhe  * with the License.  You may obtain a copy of the License at
100d8ff32eSLiu Zhe  *
110d8ff32eSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
120d8ff32eSLiu Zhe  *
130d8ff32eSLiu Zhe  * Unless required by applicable law or agreed to in writing,
140d8ff32eSLiu Zhe  * software distributed under the License is distributed on an
150d8ff32eSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
160d8ff32eSLiu Zhe  * KIND, either express or implied.  See the License for the
170d8ff32eSLiu Zhe  * specific language governing permissions and limitations
180d8ff32eSLiu Zhe  * under the License.
190d8ff32eSLiu Zhe  *
200d8ff32eSLiu Zhe  *************************************************************/
210d8ff32eSLiu Zhe 
220d8ff32eSLiu Zhe 
23*eba4d44aSLiu Zhe package fvt.uno.sc.cell;
240d8ff32eSLiu Zhe 
250d8ff32eSLiu Zhe import static org.junit.Assert.assertEquals;
260d8ff32eSLiu Zhe 
270d8ff32eSLiu Zhe import java.util.Arrays;
280d8ff32eSLiu Zhe import java.util.Collection;
290d8ff32eSLiu Zhe 
300d8ff32eSLiu Zhe import org.junit.After;
310d8ff32eSLiu Zhe import org.junit.AfterClass;
320d8ff32eSLiu Zhe import org.junit.Before;
330d8ff32eSLiu Zhe import org.junit.BeforeClass;
340d8ff32eSLiu Zhe import org.junit.Test;
350d8ff32eSLiu Zhe import org.junit.runner.RunWith;
360d8ff32eSLiu Zhe import org.junit.runners.Parameterized;
370d8ff32eSLiu Zhe import org.junit.runners.Parameterized.Parameters;
380d8ff32eSLiu Zhe import org.openoffice.test.uno.UnoApp;
390d8ff32eSLiu Zhe 
400d8ff32eSLiu Zhe import testlib.uno.SCUtil;
410d8ff32eSLiu Zhe import testlib.uno.TestUtil;
420d8ff32eSLiu Zhe import testlib.uno.CellInfo;
430d8ff32eSLiu Zhe 
440d8ff32eSLiu Zhe import com.sun.star.lang.XComponent;
450d8ff32eSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
460d8ff32eSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
470d8ff32eSLiu Zhe import com.sun.star.table.BorderLine;
480d8ff32eSLiu Zhe import com.sun.star.table.XCell;
490d8ff32eSLiu Zhe 
500d8ff32eSLiu Zhe 
510d8ff32eSLiu Zhe /**
520d8ff32eSLiu Zhe  *  Check the cell border setting can be applied and saved
530d8ff32eSLiu Zhe  *
540d8ff32eSLiu Zhe  */
550d8ff32eSLiu Zhe @RunWith(value = Parameterized.class)
560d8ff32eSLiu Zhe public class CellBorder {
570d8ff32eSLiu Zhe 	//create a class to implement Equals method for BorderLine
580d8ff32eSLiu Zhe 	private class BorderLineWithEqualsFunction{
590d8ff32eSLiu Zhe 		private int Color;
600d8ff32eSLiu Zhe 		private short InnerLineWidth;
610d8ff32eSLiu Zhe 		private short LineDistance;
620d8ff32eSLiu Zhe 		private short OuterLineWidth;
630d8ff32eSLiu Zhe 
BorderLineWithEqualsFunction(BorderLine borderLine)640d8ff32eSLiu Zhe 		public BorderLineWithEqualsFunction(BorderLine borderLine) {
650d8ff32eSLiu Zhe 			this.Color = borderLine.Color;
660d8ff32eSLiu Zhe 			this.InnerLineWidth = borderLine.InnerLineWidth;
670d8ff32eSLiu Zhe 			this.LineDistance = borderLine.LineDistance;
680d8ff32eSLiu Zhe 			this.OuterLineWidth = borderLine.OuterLineWidth;
690d8ff32eSLiu Zhe 		}
700d8ff32eSLiu Zhe 
equals(Object obj)710d8ff32eSLiu Zhe 		public boolean equals(Object obj) {
720d8ff32eSLiu Zhe 			if (!(obj instanceof BorderLineWithEqualsFunction)) {
730d8ff32eSLiu Zhe 				return false;
740d8ff32eSLiu Zhe 			}
750d8ff32eSLiu Zhe 			BorderLineWithEqualsFunction borderLine = (BorderLineWithEqualsFunction) obj;
760d8ff32eSLiu Zhe 			return this.Color == borderLine.Color
770d8ff32eSLiu Zhe 					&& this.InnerLineWidth == borderLine.InnerLineWidth
780d8ff32eSLiu Zhe 					&& this.LineDistance == borderLine.LineDistance
790d8ff32eSLiu Zhe 					&& this.OuterLineWidth == borderLine.OuterLineWidth;
800d8ff32eSLiu Zhe 		}
810d8ff32eSLiu Zhe 
hashCode()820d8ff32eSLiu Zhe 		public int hashCode() {
830d8ff32eSLiu Zhe 	       int result = 17;
840d8ff32eSLiu Zhe 	       result = 37 * result + (int) this.Color;
850d8ff32eSLiu Zhe 	       result = 37 * result + (short) this.InnerLineWidth;
860d8ff32eSLiu Zhe 	       result = 37 * result + (short) this.LineDistance;
870d8ff32eSLiu Zhe 	       result = 37 * result + (short) this.OuterLineWidth;
880d8ff32eSLiu Zhe 	       return result;
890d8ff32eSLiu Zhe 	    }
900d8ff32eSLiu Zhe 	}
910d8ff32eSLiu Zhe 
920d8ff32eSLiu Zhe 	private BorderLine expected;
930d8ff32eSLiu Zhe 	private BorderLine borderLine;
940d8ff32eSLiu Zhe 	private String fileType;
950d8ff32eSLiu Zhe 
960d8ff32eSLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
970d8ff32eSLiu Zhe 
980d8ff32eSLiu Zhe 	XComponent scComponent = null;
990d8ff32eSLiu Zhe 	XSpreadsheetDocument scDocument = null;
1000d8ff32eSLiu Zhe 
1010d8ff32eSLiu Zhe 	@Parameters
data()1020d8ff32eSLiu Zhe 	public static Collection<Object[]> data() throws Exception {
1030d8ff32eSLiu Zhe 		int[] colorList = TestUtil.randColorList(3);
1040d8ff32eSLiu Zhe 
1050d8ff32eSLiu Zhe 		return Arrays.asList(new Object[][] {
1060d8ff32eSLiu Zhe 			//{inner line (pt), distance (pt), outer line (pt), color number, inner line (pt), distance (pt), outer line (pt), file type}
1070d8ff32eSLiu Zhe 			{0, 0, 1, 0xFF0000, 0, 0, 1, "ods"},
1080d8ff32eSLiu Zhe 			{0, 0, 0.5, 0x00FF00, 0, 0, 0.5, "ods"},
1090d8ff32eSLiu Zhe 			{0, 0, 2.5, 0x0000FF, 0, 0, 2.5, "ods"},
1100d8ff32eSLiu Zhe 			{0, 0, 5, 0x0000FF, 0, 0, 5, "ods"},
1110d8ff32eSLiu Zhe 			{0.05, 0.05, 0.05, colorList[0], 0.05, 0.05, 0.05, "ods"},
1120d8ff32eSLiu Zhe 			{1.0, 0.5, 1.0, colorList[1], 1.0, 0.5, 1.0, "ods"},
1130d8ff32eSLiu Zhe 			{5, 2, 5, colorList[2], 5, 2, 5, "ods"},
1140d8ff32eSLiu Zhe 			{0, 0, 4, 0xFF0000, 0, 0, 5, "xls"},
1150d8ff32eSLiu Zhe 			{0, 0, 2.5, 0xFFFF00, 0, 0, 2, "xls"},
1160d8ff32eSLiu Zhe 			{0, 0, 1, 0x00FF00, 0, 0, 0.5, "xls"},
1170d8ff32eSLiu Zhe 			{1, 1, 1, 0x0000FF, 0.5, 1.0, 0.5, "xls"}
1180d8ff32eSLiu Zhe 
1190d8ff32eSLiu Zhe 		});
1200d8ff32eSLiu Zhe 	}
1210d8ff32eSLiu Zhe 
CellBorder(double expInnerLineWidth, double expLineDistance, double expOuterLineWidth, int color, double innerLineWidth, double lineDistance, double outerLineWidth, String fileType)1220d8ff32eSLiu Zhe 	public CellBorder(double expInnerLineWidth, double expLineDistance, double expOuterLineWidth, int color, double innerLineWidth, double lineDistance, double outerLineWidth, String fileType) {
1230d8ff32eSLiu Zhe 		BorderLine eBorderLine = new BorderLine();
1240d8ff32eSLiu Zhe 		BorderLine aBorderLine = new BorderLine();
1250d8ff32eSLiu Zhe 
1260d8ff32eSLiu Zhe 		eBorderLine.Color = color;
1270d8ff32eSLiu Zhe 		eBorderLine.InnerLineWidth = (short) Math.round(2540 / 72.0 * expInnerLineWidth);
1280d8ff32eSLiu Zhe 		eBorderLine.LineDistance = (short) Math.round(2540 / 72.0 * expLineDistance);
1290d8ff32eSLiu Zhe 		eBorderLine.OuterLineWidth = (short) Math.round(2540 / 72.0 * expOuterLineWidth);
1300d8ff32eSLiu Zhe 
1310d8ff32eSLiu Zhe 		aBorderLine.Color = color;
1320d8ff32eSLiu Zhe 		aBorderLine.InnerLineWidth = (short) Math.round(2540 / 72.0 * innerLineWidth);
1330d8ff32eSLiu Zhe 		aBorderLine.LineDistance = (short) Math.round(2540 / 72.0 * lineDistance);
1340d8ff32eSLiu Zhe 		aBorderLine.OuterLineWidth = (short) Math.round(2540 / 72.0 * outerLineWidth);
1350d8ff32eSLiu Zhe 
1360d8ff32eSLiu Zhe 		this.expected = eBorderLine;
1370d8ff32eSLiu Zhe 		this.borderLine = aBorderLine;
1380d8ff32eSLiu Zhe 		this.fileType = fileType;
1390d8ff32eSLiu Zhe 	}
1400d8ff32eSLiu Zhe 
1410d8ff32eSLiu Zhe 
1420d8ff32eSLiu Zhe 	@Before
setUp()1430d8ff32eSLiu Zhe 	public void setUp() throws Exception {
1440d8ff32eSLiu Zhe 		scComponent = unoApp.newDocument("scalc");
1450d8ff32eSLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
1460d8ff32eSLiu Zhe 	}
1470d8ff32eSLiu Zhe 
1480d8ff32eSLiu Zhe 	@After
tearDown()1490d8ff32eSLiu Zhe 	public void tearDown() throws Exception {
1500d8ff32eSLiu Zhe 		unoApp.closeDocument(scComponent);
1510d8ff32eSLiu Zhe 
1520d8ff32eSLiu Zhe 	}
1530d8ff32eSLiu Zhe 
1540d8ff32eSLiu Zhe 	@BeforeClass
setUpConnection()1550d8ff32eSLiu Zhe 	public static void setUpConnection() throws Exception {
1560d8ff32eSLiu Zhe 		unoApp.start();
1570d8ff32eSLiu Zhe 	}
1580d8ff32eSLiu Zhe 
1590d8ff32eSLiu Zhe 	@AfterClass
tearDownConnection()1600d8ff32eSLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
1610d8ff32eSLiu Zhe 		unoApp.close();
1620d8ff32eSLiu Zhe 		SCUtil.clearTempDir();
1630d8ff32eSLiu Zhe 	}
1640d8ff32eSLiu Zhe 
1650d8ff32eSLiu Zhe 	/**
1660d8ff32eSLiu Zhe 	 * Check the cell border settings
1670d8ff32eSLiu Zhe 	 * 1. Create a spreadsheet file.
1680d8ff32eSLiu Zhe 	 * 2. Input number, text, formula into many cell.
1690d8ff32eSLiu Zhe 	 * 3. Set cell border properties.
1700d8ff32eSLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
1710d8ff32eSLiu Zhe 	 * 5. Close and reopen file.  -> Check the border setting.
1720d8ff32eSLiu Zhe 	 * @throws Exception
1730d8ff32eSLiu Zhe 	 */
1740d8ff32eSLiu Zhe 	@Test
testCellBorder()1750d8ff32eSLiu Zhe 	public void testCellBorder() throws Exception {
1760d8ff32eSLiu Zhe 		String fileName = "testCellBorder";
1770d8ff32eSLiu Zhe 		String[] borderType = {"LeftBorder", "RightBorder", "TopBorder", "BottomBorder"};
1780d8ff32eSLiu Zhe 		int borderNum = borderType.length;
1790d8ff32eSLiu Zhe 		int cellNum = 10;
1800d8ff32eSLiu Zhe 		XCell[] cells = new XCell[cellNum];
1810d8ff32eSLiu Zhe 		BorderLine[][] results = new BorderLine[cellNum][borderNum];
1820d8ff32eSLiu Zhe 		CellInfo cInfo = TestUtil.randCell(10, 10);
1830d8ff32eSLiu Zhe 
1840d8ff32eSLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
1850d8ff32eSLiu Zhe 
1860d8ff32eSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
1870d8ff32eSLiu Zhe 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
1880d8ff32eSLiu Zhe 		}
1890d8ff32eSLiu Zhe 
1900d8ff32eSLiu Zhe 		cells[0].setValue(borderLine.Color);
1910d8ff32eSLiu Zhe 		SCUtil. setTextToCell(cells[1], "all border");
1920d8ff32eSLiu Zhe 		cells[2].setFormula("=2^6");
1930d8ff32eSLiu Zhe 		cells[3].setValue(-0.1);
1940d8ff32eSLiu Zhe 
1950d8ff32eSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
1960d8ff32eSLiu Zhe 			for (int j = 0; j < borderNum; j++) {
1970d8ff32eSLiu Zhe 				SCUtil.setCellProperties(cells[i], borderType[j], borderLine);
1980d8ff32eSLiu Zhe 			}
1990d8ff32eSLiu Zhe 		}
2000d8ff32eSLiu Zhe 
2010d8ff32eSLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
2020d8ff32eSLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
2030d8ff32eSLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
2040d8ff32eSLiu Zhe 
2050d8ff32eSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
2060d8ff32eSLiu Zhe 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
2070d8ff32eSLiu Zhe 			for (int j = 0; j < borderNum; j++) {
2080d8ff32eSLiu Zhe 				results[i][j] = (BorderLine) SCUtil.getCellProperties(cells[i], borderType[j]);
2090d8ff32eSLiu Zhe 			}
2100d8ff32eSLiu Zhe 		}
2110d8ff32eSLiu Zhe 
2120d8ff32eSLiu Zhe 		SCUtil.closeFile(scDocument);
2130d8ff32eSLiu Zhe 
2140d8ff32eSLiu Zhe 		for (int i = 0; i< cellNum; i++){
2150d8ff32eSLiu Zhe 			for (int j = 0; j<borderNum; j++) {
2160d8ff32eSLiu Zhe 				assertEquals("Incorrect cell border(" + borderType[j] + ") value got in ." + fileType + " file.",
2170d8ff32eSLiu Zhe 					new BorderLineWithEqualsFunction(expected), new BorderLineWithEqualsFunction(results[i][j]));
2180d8ff32eSLiu Zhe 			}
2190d8ff32eSLiu Zhe 		}
2200d8ff32eSLiu Zhe 
2210d8ff32eSLiu Zhe 	}
2220d8ff32eSLiu Zhe 
2230d8ff32eSLiu Zhe }
224