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 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 
29 import java.util.Arrays;
30 import java.util.Collection;
31 
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.Parameterized;
39 import org.junit.runners.Parameterized.Parameters;
40 import org.openoffice.test.uno.UnoApp;
41 
42 import testlib.uno.SCUtil;
43 import testlib.uno.TestUtil;
44 import testlib.uno.CellInfo;
45 
46 import com.sun.star.lang.XComponent;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.table.XCell;
50 
51 
52 /**
53  *  Check the cell background color and font color setting can be applied and saved
54  *
55  */
56 @RunWith(value = Parameterized.class)
57 public class CellEffectUnderline {
58 
59 	private int expectedLine;
60 	private int expectedColor;
61 	private String[] inputType;
62 	private int inputStyle;
63 	private int inputColor;
64 	private String fileType;
65 
66 	private static final UnoApp unoApp = new UnoApp();
67 
68 	XComponent scComponent = null;
69 	XSpreadsheetDocument scDocument = null;
70 
71 	@Parameters
data()72 	public static Collection<Object[]> data() throws Exception {
73 		String[] typeList = {"CharUnderline", "CharUnderlineHasColor", "CharUnderlineColor"};
74 		int[] list = TestUtil.randColorList(19);
75 		return Arrays.asList(new Object[][] {
76 			{0, list[0], typeList, 0, list[0], "ods"}, //NONE 0
77 			{1, list[1], typeList, 1, list[1], "ods"}, //SIGNLE 1
78 			{2, list[2], typeList, 2, list[2], "ods"}, //DOUBLE 2
79 			{3, list[3], typeList, 3, list[3], "ods"}, //DOTTED 3
80 			{0, list[4], typeList, 4, list[4], "ods"}, //DONTKNOW 4  can not set this setting via UI
81 			{5, list[5], typeList, 5, list[5], "ods"}, //DASH 5
82 			{6, list[6], typeList, 6, list[6], "ods"}, //LONGDASH 6
83 			{7, list[7], typeList, 7, list[7], "ods"}, //DASHDOT 7
84 			{8, list[8], typeList, 8, list[8], "ods"}, //DASHDOTDOT 8
85 			{9, list[9], typeList, 9, list[9], "ods"}, //SMALLWAVE 9  can not set this setting via UI
86 			{10, list[10], typeList, 10, list[10], "ods"}, //WAVE 10
87 			{11, list[11], typeList, 11, list[11], "ods"}, //DOUBLEWAVE 11
88 			{12, list[12], typeList, 12, list[12], "ods"}, //BOLD 12
89 			{13, list[13], typeList, 13, list[13], "ods"}, //BOLDDOTTED 13
90 			{14, list[14], typeList, 14, list[14], "ods"}, //BOLDDASH 14
91 			{15, list[15], typeList, 15, list[15], "ods"}, //BOLDLONGDASH 15
92 			{16, list[16], typeList, 16, list[16], "ods"}, //BOLDDASHDOT 16
93 			{17, list[17], typeList, 17, list[17], "ods"}, //BOLDDASHDOTDOT 17
94 			{18, list[18], typeList, 18, list[18], "ods"}, //BOLDWAVE = 18
95 
96 			{0, list[0], typeList, 0, list[0], "xls"}, //NONE 0
97 			{1, list[1], typeList, 1, list[1], "xls"}, //SIGNLE 1
98 			{2, list[2], typeList, 2, list[2], "xls"}, //DOUBLE 2
99 			{1, list[3], typeList, 3, list[3], "xls"}, //DOTTED 3
100 			{0, list[4], typeList, 4, list[4], "xls"}, //DONTKNOW 4  can not set this setting via UI
101 			{1, list[5], typeList, 5, list[5], "xls"}, //DASH 5
102 			{1, list[6], typeList, 6, list[6], "xls"}, //LONGDASH 6
103 			{1, list[7], typeList, 7, list[7], "xls"}, //DASHDOT 7
104 			{1, list[8], typeList, 8, list[8], "xls"}, //DASHDOTDOT 8
105 			{1, list[9], typeList, 9, list[9], "xls"}, //SMALLWAVE 9  can not set this setting via UI
106 			{1, list[10], typeList, 10, list[10], "xls"}, //WAVE 10
107 			{2, list[11], typeList, 11, list[11], "xls"}, //DOUBLEWAVE 11
108 			{1, list[12], typeList, 12, list[12], "xls"}, //BOLD 12
109 			{1, list[13], typeList, 13, list[13], "xls"}, //BOLDDOTTED 13
110 			{1, list[14], typeList, 14, list[14], "xls"}, //BOLDDASH 14
111 			{1, list[15], typeList, 15, list[15], "xls"}, //BOLDLONGDASH 15
112 			{1, list[16], typeList, 16, list[16], "xls"}, //BOLDDASHDOT 16
113 			{1, list[17], typeList, 17, list[17], "xls"}, //BOLDDASHDOTDOT 17
114 			{1, list[18], typeList, 18, list[18], "xls"} //BOLDWAVE = 18
115 		});
116 	}
117 
CellEffectUnderline(int expectedStyle, int expectedColor, String[] inputType, int inputStyle, int inputColor, String fileType)118 	public CellEffectUnderline(int expectedStyle, int expectedColor, String[] inputType, int inputStyle, int inputColor, String fileType) {
119 		this.expectedLine = expectedStyle;
120 		this.expectedColor = expectedColor;
121 		this.inputType = inputType;
122 		this.inputStyle = inputStyle;
123 		this.inputColor = inputColor;
124 		this.fileType = fileType;
125 	}
126 
127 
128 	@Before
setUp()129 	public void setUp() throws Exception {
130 		scComponent = unoApp.newDocument("scalc");
131 		scDocument = SCUtil.getSCDocument(scComponent);
132 	}
133 
134 	@After
tearDown()135 	public void tearDown() throws Exception {
136 		unoApp.closeDocument(scComponent);
137 
138 	}
139 
140 	@BeforeClass
setUpConnection()141 	public static void setUpConnection() throws Exception {
142 		unoApp.start();
143 	}
144 
145 	@AfterClass
tearDownConnection()146 	public static void tearDownConnection() throws InterruptedException, Exception {
147 		unoApp.close();
148 		SCUtil.clearTempDir();
149 	}
150 
151 	/**
152 	 * Check the cell underline style and underline color
153 	 * 1. Create a spreadsheet file.
154 	 * 2. Input number, text, formula into many cell.
155 	 * 3. Set cell underline style.
156 	 * 4. Set cell underline color, if underline style is not NONE.
157 	 * 4. Save file as ODF/MSBinary format.
158 	 * 5. Close and reopen file.  -> Check the underline style and underline color setting.
159 	 * @throws Exception
160 	 */
161 	@Test
testCharUnderline()162 	public void testCharUnderline() throws Exception {
163 		String fileName = "testCharUnderline";
164 		int cellNum = 5;
165 		XCell[] cells = new XCell[cellNum];
166 		int[] styleResults = new int[cellNum];
167 		boolean[] hasColor = new boolean[cellNum];
168 		int[] colorResults = new int[cellNum];
169 		CellInfo cInfo = TestUtil.randCell(100, 32768);
170 
171 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
172 
173 		for (int i = 0; i < cellNum; i++) {
174 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
175 		}
176 
177 		cells[0].setValue(inputColor);
178 		SCUtil.setTextToCell(cells[1], inputType[0]);
179 		cells[2].setFormula("=\"ABC\"");
180 		cells[3].setValue(-0.90000001);
181 
182 		for (int i = 0; i < cellNum; i++) {
183 			SCUtil.setCellProperties(cells[i], inputType[0], inputStyle);
184 			if (inputStyle > 0) {
185 				SCUtil.setCellProperties(cells[i], inputType[1], true);
186 				SCUtil.setCellProperties(cells[i], inputType[2], inputColor);
187 			}
188 		}
189 
190 		SCUtil.saveFileAs(scComponent, fileName, fileType);
191 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
192 		sheet = SCUtil.getCurrentSheet(scDocument);
193 
194 		for (int i = 0; i < cellNum; i++) {
195 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
196 			styleResults[i] = ((Short) SCUtil.getCellProperties(cells[i], inputType[0])).shortValue();
197 			if (inputStyle > 0) {
198 				hasColor[i] = ((Boolean) SCUtil.getCellProperties(cells[i], inputType[1])).booleanValue();
199 				colorResults[i] = ((Integer) SCUtil.getCellProperties(cells[i], inputType[2])).intValue();
200 			}
201 		}
202 		SCUtil.closeFile(scDocument);
203 
204 		for (int i = 0; i < cellNum; i++) {
205 			assertEquals("Incorrect cell underline style(" + inputType[0] + ") value got in ." + fileType + " file.", expectedLine, styleResults[i], 0);
206 			if (inputStyle > 0) {
207 				if( fileType.equalsIgnoreCase("xls") || fileType.equalsIgnoreCase("xlt")) {
208 					assertFalse("Incorrect cell underline has color setting(" + inputType[1] + ") value got in ." + fileType + " file.", hasColor[i]);
209 				}
210 				else {
211 					assertTrue("Incorrect cell underline has color setting(" + inputType[1] + ") value got in ." + fileType + " file.", hasColor[i]);
212 					assertEquals("Incorrect cell underline color(" + inputType[2] + ") value got in ." + fileType + " file.", expectedColor, colorResults[i], 0);
213 				}
214 
215 			}
216 		}
217 
218 	}
219 
220 }
221