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 
23 package fvt.uno.sc.cell;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 import java.util.Arrays;
28 import java.util.Collection;
29 
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.Parameterized;
37 import org.junit.runners.Parameterized.Parameters;
38 import org.openoffice.test.uno.UnoApp;
39 
40 import testlib.uno.SCUtil;
41 import testlib.uno.TestUtil;
42 import testlib.uno.CellInfo;
43 
44 import com.sun.star.lang.XComponent;
45 import com.sun.star.sheet.XSpreadsheet;
46 import com.sun.star.sheet.XSpreadsheetDocument;
47 import com.sun.star.table.BorderLine;
48 import com.sun.star.table.XCell;
49 
50 
51 /**
52  *  Check the cell border setting can be applied and saved
53  *
54  */
55 @RunWith(value = Parameterized.class)
56 public class CellBorder {
57 	//create a class to implement Equals method for BorderLine
58 	private class BorderLineWithEqualsFunction{
59 		private int Color;
60 		private short InnerLineWidth;
61 		private short LineDistance;
62 		private short OuterLineWidth;
63 
BorderLineWithEqualsFunction(BorderLine borderLine)64 		public BorderLineWithEqualsFunction(BorderLine borderLine) {
65 			this.Color = borderLine.Color;
66 			this.InnerLineWidth = borderLine.InnerLineWidth;
67 			this.LineDistance = borderLine.LineDistance;
68 			this.OuterLineWidth = borderLine.OuterLineWidth;
69 		}
70 
equals(Object obj)71 		public boolean equals(Object obj) {
72 			if (!(obj instanceof BorderLineWithEqualsFunction)) {
73 				return false;
74 			}
75 			BorderLineWithEqualsFunction borderLine = (BorderLineWithEqualsFunction) obj;
76 			return this.Color == borderLine.Color
77 					&& this.InnerLineWidth == borderLine.InnerLineWidth
78 					&& this.LineDistance == borderLine.LineDistance
79 					&& this.OuterLineWidth == borderLine.OuterLineWidth;
80 		}
81 
hashCode()82 		public int hashCode() {
83 	       int result = 17;
84 	       result = 37 * result + (int) this.Color;
85 	       result = 37 * result + (short) this.InnerLineWidth;
86 	       result = 37 * result + (short) this.LineDistance;
87 	       result = 37 * result + (short) this.OuterLineWidth;
88 	       return result;
89 	    }
90 	}
91 
92 	private BorderLine expected;
93 	private BorderLine borderLine;
94 	private String fileType;
95 
96 	private static final UnoApp unoApp = new UnoApp();
97 
98 	XComponent scComponent = null;
99 	XSpreadsheetDocument scDocument = null;
100 
101 	@Parameters
data()102 	public static Collection<Object[]> data() throws Exception {
103 		int[] colorList = TestUtil.randColorList(3);
104 
105 		return Arrays.asList(new Object[][] {
106 			//{inner line (pt), distance (pt), outer line (pt), color number, inner line (pt), distance (pt), outer line (pt), file type}
107 			{0, 0, 1, 0xFF0000, 0, 0, 1, "ods"},
108 			{0, 0, 0.5, 0x00FF00, 0, 0, 0.5, "ods"},
109 			{0, 0, 2.5, 0x0000FF, 0, 0, 2.5, "ods"},
110 			{0, 0, 5, 0x0000FF, 0, 0, 5, "ods"},
111 			{0.05, 0.05, 0.05, colorList[0], 0.05, 0.05, 0.05, "ods"},
112 			{1.0, 0.5, 1.0, colorList[1], 1.0, 0.5, 1.0, "ods"},
113 			{5, 2, 5, colorList[2], 5, 2, 5, "ods"},
114 			{0, 0, 4, 0xFF0000, 0, 0, 5, "xls"},
115 			{0, 0, 2.5, 0xFFFF00, 0, 0, 2, "xls"},
116 			{0, 0, 1, 0x00FF00, 0, 0, 0.5, "xls"},
117 			{1, 1, 1, 0x0000FF, 0.5, 1.0, 0.5, "xls"}
118 
119 		});
120 	}
121 
CellBorder(double expInnerLineWidth, double expLineDistance, double expOuterLineWidth, int color, double innerLineWidth, double lineDistance, double outerLineWidth, String fileType)122 	public CellBorder(double expInnerLineWidth, double expLineDistance, double expOuterLineWidth, int color, double innerLineWidth, double lineDistance, double outerLineWidth, String fileType) {
123 		BorderLine eBorderLine = new BorderLine();
124 		BorderLine aBorderLine = new BorderLine();
125 
126 		eBorderLine.Color = color;
127 		eBorderLine.InnerLineWidth = (short) Math.round(2540 / 72.0 * expInnerLineWidth);
128 		eBorderLine.LineDistance = (short) Math.round(2540 / 72.0 * expLineDistance);
129 		eBorderLine.OuterLineWidth = (short) Math.round(2540 / 72.0 * expOuterLineWidth);
130 
131 		aBorderLine.Color = color;
132 		aBorderLine.InnerLineWidth = (short) Math.round(2540 / 72.0 * innerLineWidth);
133 		aBorderLine.LineDistance = (short) Math.round(2540 / 72.0 * lineDistance);
134 		aBorderLine.OuterLineWidth = (short) Math.round(2540 / 72.0 * outerLineWidth);
135 
136 		this.expected = eBorderLine;
137 		this.borderLine = aBorderLine;
138 		this.fileType = fileType;
139 	}
140 
141 
142 	@Before
setUp()143 	public void setUp() throws Exception {
144 		scComponent = unoApp.newDocument("scalc");
145 		scDocument = SCUtil.getSCDocument(scComponent);
146 	}
147 
148 	@After
tearDown()149 	public void tearDown() throws Exception {
150 		unoApp.closeDocument(scComponent);
151 
152 	}
153 
154 	@BeforeClass
setUpConnection()155 	public static void setUpConnection() throws Exception {
156 		unoApp.start();
157 	}
158 
159 	@AfterClass
tearDownConnection()160 	public static void tearDownConnection() throws InterruptedException, Exception {
161 		unoApp.close();
162 		SCUtil.clearTempDir();
163 	}
164 
165 	/**
166 	 * Check the cell border settings
167 	 * 1. Create a spreadsheet file.
168 	 * 2. Input number, text, formula into many cell.
169 	 * 3. Set cell border properties.
170 	 * 4. Save file as ODF/MSBinary format.
171 	 * 5. Close and reopen file.  -> Check the border setting.
172 	 * @throws Exception
173 	 */
174 	@Test
testCellBorder()175 	public void testCellBorder() throws Exception {
176 		String fileName = "testCellBorder";
177 		String[] borderType = {"LeftBorder", "RightBorder", "TopBorder", "BottomBorder"};
178 		int borderNum = borderType.length;
179 		int cellNum = 10;
180 		XCell[] cells = new XCell[cellNum];
181 		BorderLine[][] results = new BorderLine[cellNum][borderNum];
182 		CellInfo cInfo = TestUtil.randCell(10, 10);
183 
184 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
185 
186 		for (int i = 0; i < cellNum; i++) {
187 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
188 		}
189 
190 		cells[0].setValue(borderLine.Color);
191 		SCUtil. setTextToCell(cells[1], "all border");
192 		cells[2].setFormula("=2^6");
193 		cells[3].setValue(-0.1);
194 
195 		for (int i = 0; i < cellNum; i++) {
196 			for (int j = 0; j < borderNum; j++) {
197 				SCUtil.setCellProperties(cells[i], borderType[j], borderLine);
198 			}
199 		}
200 
201 		SCUtil.saveFileAs(scComponent, fileName, fileType);
202 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
203 		sheet = SCUtil.getCurrentSheet(scDocument);
204 
205 		for (int i = 0; i < cellNum; i++) {
206 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
207 			for (int j = 0; j < borderNum; j++) {
208 				results[i][j] = (BorderLine) SCUtil.getCellProperties(cells[i], borderType[j]);
209 			}
210 		}
211 
212 		SCUtil.closeFile(scDocument);
213 
214 		for (int i = 0; i< cellNum; i++){
215 			for (int j = 0; j<borderNum; j++) {
216 				assertEquals("Incorrect cell border(" + borderType[j] + ") value got in ." + fileType + " file.",
217 					new BorderLineWithEqualsFunction(expected), new BorderLineWithEqualsFunction(results[i][j]));
218 			}
219 		}
220 
221 	}
222 
223 }
224