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