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 fvt.uno.sc.data;
22 
23 import static org.junit.Assert.*;
24 
25 import java.util.Arrays;
26 import java.util.Collection;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Parameterized;
34 import org.junit.runners.Parameterized.Parameters;
35 import org.openoffice.test.uno.UnoApp;
36 import testlib.uno.SCUtil;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.sheet.ConditionOperator;
40 import com.sun.star.sheet.ValidationAlertStyle;
41 import com.sun.star.sheet.ValidationType;
42 import com.sun.star.sheet.XSheetCondition;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.table.XCellRange;
46 import com.sun.star.uno.Enum;
47 import com.sun.star.uno.UnoRuntime;
48 
49 @RunWith(value = Parameterized.class)
50 public class ValidityTypeTest {
51 	UnoApp unoApp = new UnoApp();
52 	XSpreadsheetDocument scDocument = null;
53 	XComponent scComponent = null;
54 
55 	private Enum validationtype;
56 
57 	private boolean ignoreBlankCells;
58 
59 	private Enum validationAlertStyle;
60 
61 	private boolean showInputMessage;
62 
63 	private boolean showErrorMessage;
64 
65 	private Enum conditionOperator;
66 
67 	private String formula1;
68 
69 	private String formula2;
70 
71 	private String extName;
72 
73 	@Parameters
data()74 	public static Collection<Object[]> data() throws Exception {
75 		return Arrays
76 				.asList(new Object[][] {
77 						{ ValidationType.ANY, false, ValidationAlertStyle.STOP,
78 								false, false, ConditionOperator.EQUAL, "0",
79 								"5", "xls" },
80 						//Custom type is not exist in GUI so remove this
81 						// { ValidationType.CUSTOM, true,
82 						// ValidationAlertStyle.INFO, false, true,
83 						// ConditionOperator.EQUAL, "0", "5", "ods"},
84 						{ ValidationType.DATE, false,
85 								ValidationAlertStyle.MACRO, false, false,
86 								ConditionOperator.EQUAL, "0", "0", "ods" },
87 						{ ValidationType.DECIMAL, true,
88 								ValidationAlertStyle.WARNING, false, true,
89 								ConditionOperator.BETWEEN, "0", "5", "ods" },
90 						{ ValidationType.LIST, false,
91 								ValidationAlertStyle.STOP, true, false,
92 								ConditionOperator.EQUAL, "\"Joe\";\"Jane\"",
93 								"0", "ods" },
94 						{ ValidationType.LIST, false,
95 								ValidationAlertStyle.STOP, true, false,
96 								ConditionOperator.EQUAL, "a;b;c", "0", "ods" },
97 						{ ValidationType.TEXT_LEN, true,
98 								ValidationAlertStyle.INFO, true, true,
99 								ConditionOperator.GREATER, "0", "5", "xls" },
100 						{ ValidationType.TIME, false,
101 								ValidationAlertStyle.STOP, true, false,
102 								ConditionOperator.GREATER_EQUAL, "0", "5",
103 								"xls" },
104 						{ ValidationType.WHOLE, true,
105 								ValidationAlertStyle.WARNING, true, true,
106 								ConditionOperator.LESS, "0", "5", "xls" },
107 						{ ValidationType.DECIMAL, false,
108 								ValidationAlertStyle.STOP, true, false,
109 								ConditionOperator.LESS_EQUAL, "0", "5", "xls" },
110 						{ ValidationType.WHOLE, true,
111 								ValidationAlertStyle.INFO, false, true,
112 								ConditionOperator.NOT_BETWEEN, "0", "5", "ods" },
113 						{ ValidationType.TEXT_LEN, false,
114 								ValidationAlertStyle.MACRO, true, false,
115 								ConditionOperator.NOT_EQUAL, "5", "0", "ods" }, });
116 	}
117 
118 	@Before
setUpDocument()119 	public void setUpDocument() throws Exception {
120 		unoApp.start();
121 	}
122 
123 	@After
tearDownDocument()124 	public void tearDownDocument() {
125 		unoApp.close();
126 		unoApp.closeDocument(scComponent);
127 
128 	}
129 
130 	@BeforeClass
setUpConnection()131 	public static void setUpConnection() throws Exception {
132 
133 	}
134 
135 	@AfterClass
tearDownConnection()136 	public static void tearDownConnection() throws InterruptedException,
137 			Exception {
138 
139 	}
140 
ValidityTypeTest(Enum validationtype, boolean ignoreBlankCells, Enum validationAlertStyle, boolean showInputMessage, boolean showErrorMessage, Enum conditionOperator, String formula1, String formula2, String extName)141 	public ValidityTypeTest(Enum validationtype, boolean ignoreBlankCells,
142 			Enum validationAlertStyle, boolean showInputMessage,
143 			boolean showErrorMessage, Enum conditionOperator, String formula1,
144 			String formula2, String extName) {
145 		this.validationtype = validationtype;
146 		this.ignoreBlankCells = ignoreBlankCells;
147 		this.validationAlertStyle = validationAlertStyle;
148 		this.showInputMessage = showInputMessage;
149 		this.showErrorMessage = showErrorMessage;
150 		this.conditionOperator = conditionOperator;
151 		this.formula1 = formula1;
152 		this.formula2 = formula2;
153 		this.extName = extName;
154 	}
155 
156 	/**
157 	 * test filter with every type
158 	 */
159 	@Test
test()160 	public void test() throws Exception {
161 		// New a SC document
162 		scComponent = unoApp.newDocument("scalc");
163 		scDocument = SCUtil.getSCDocument(scComponent);
164 		XSpreadsheet currentsheet = SCUtil.getCurrentSheet(scDocument);
165 		// --- Data validation ---
166 		XCellRange xCellRange = currentsheet.getCellRangeByName("A7:C7");
167 		XPropertySet xCellPropSet = (XPropertySet) UnoRuntime.queryInterface(
168 				XPropertySet.class, xCellRange);
169 
170 		// validation properties
171 		XPropertySet xValidPropSet = (XPropertySet) UnoRuntime
172 				.queryInterface(XPropertySet.class,
173 						xCellPropSet.getPropertyValue("Validation"));
174 
175 		String errorTitle = "This is an Error title";
176 		String inputHelp = "This is an input help";
177 		String inputTitle = "This is an input title";
178 		String errorMessage = "This is an invalid value!";
179 		xValidPropSet.setPropertyValue("IgnoreBlankCells", ignoreBlankCells);
180 		xValidPropSet.setPropertyValue("Type", validationtype);
181 		xValidPropSet.setPropertyValue("ErrorTitle", errorTitle);
182 		xValidPropSet.setPropertyValue("ShowErrorMessage", showErrorMessage);
183 		xValidPropSet.setPropertyValue("ErrorMessage", errorMessage);
184 		xValidPropSet.setPropertyValue("ErrorAlertStyle", validationAlertStyle);
185 		xValidPropSet.setPropertyValue("ShowInputMessage", showInputMessage);
186 		xValidPropSet.setPropertyValue("InputMessage", inputHelp);
187 		xValidPropSet.setPropertyValue("InputTitle", inputTitle);
188 
189 		// condition
190 		XSheetCondition xCondition = (XSheetCondition) UnoRuntime
191 				.queryInterface(XSheetCondition.class, xValidPropSet);
192 		xCondition.setOperator((ConditionOperator) conditionOperator);
193 		xCondition.setFormula1(formula1);
194 		xCondition.setFormula2(formula2);
195 
196 		// apply on cell range
197 		xCellPropSet.setPropertyValue("Validation", xValidPropSet);
198 
199 		// Save and reload the file
200 		SCUtil.saveFileAs(scComponent, "Validationtest", extName);
201 		XSpreadsheetDocument scDocumentTemp = SCUtil.reloadFile(unoApp,
202 				scDocument, "Validationtest." + extName);
203 		scDocument = scDocumentTemp;
204 		currentsheet = SCUtil.getCurrentSheet(scDocument);
205 
206 		// --- Data validation ---
207 		xCellRange = currentsheet.getCellRangeByName("A7:C7");
208 		xCellPropSet = (XPropertySet) UnoRuntime.queryInterface(
209 				XPropertySet.class, xCellRange);
210 		// validation properties
211 		xValidPropSet = (XPropertySet) UnoRuntime
212 				.queryInterface(XPropertySet.class,
213 						xCellPropSet.getPropertyValue("Validation"));
214 
215 		// Verify the validation property
216 		//If validation Alert Style is Macro, after save the error message will be clean
217 		if (validationAlertStyle == ValidationAlertStyle.MACRO) {
218 			assertEquals("", xValidPropSet.getPropertyValue("ErrorMessage"));
219 		} else {
220 			assertEquals(errorMessage,
221 					xValidPropSet.getPropertyValue("ErrorMessage"));
222 		}
223 		assertEquals(showErrorMessage,
224 				xValidPropSet.getPropertyValue("ShowErrorMessage"));
225 		assertEquals(showInputMessage,
226 				xValidPropSet.getPropertyValue("ShowInputMessage"));
227 		assertEquals(inputHelp, xValidPropSet.getPropertyValue("InputMessage"));
228 		assertEquals(ignoreBlankCells,
229 				xValidPropSet.getPropertyValue("IgnoreBlankCells"));
230 		assertEquals(validationtype, xValidPropSet.getPropertyValue("Type"));
231 		assertEquals(errorTitle, xValidPropSet.getPropertyValue("ErrorTitle"));
232 		assertEquals(validationAlertStyle,
233 				xValidPropSet.getPropertyValue("ErrorAlertStyle"));
234 		assertEquals(inputTitle, xValidPropSet.getPropertyValue("InputTitle"));
235 
236 		xCondition = (XSheetCondition) UnoRuntime.queryInterface(
237 				XSheetCondition.class, xValidPropSet);
238 		assertEquals(formula1, xCondition.getFormula1());
239 		assertEquals(formula2, xCondition.getFormula2());
240 		assertEquals(conditionOperator, xCondition.getOperator());
241 
242 	}
243 }
244