xref: /AOO41X/test/testuno/source/fvt/uno/sc/cell/CellProtected.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
1*eba4d44aSLiu Zhe /**************************************************************
2*eba4d44aSLiu Zhe  *
3*eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*eba4d44aSLiu Zhe  * distributed with this work for additional information
6*eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9*eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*eba4d44aSLiu Zhe  *
11*eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*eba4d44aSLiu Zhe  *
13*eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15*eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17*eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18*eba4d44aSLiu Zhe  * under the License.
19*eba4d44aSLiu Zhe  *
20*eba4d44aSLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe 
22*eba4d44aSLiu Zhe 
23*eba4d44aSLiu Zhe package fvt.uno.sc.cell;
24*eba4d44aSLiu Zhe 
25*eba4d44aSLiu Zhe import static org.junit.Assert.assertEquals;
26*eba4d44aSLiu Zhe 
27*eba4d44aSLiu Zhe import java.util.Arrays;
28*eba4d44aSLiu Zhe import java.util.Collection;
29*eba4d44aSLiu Zhe 
30*eba4d44aSLiu Zhe import org.junit.After;
31*eba4d44aSLiu Zhe import org.junit.AfterClass;
32*eba4d44aSLiu Zhe import org.junit.Before;
33*eba4d44aSLiu Zhe import org.junit.BeforeClass;
34*eba4d44aSLiu Zhe import org.junit.Test;
35*eba4d44aSLiu Zhe import org.junit.runner.RunWith;
36*eba4d44aSLiu Zhe import org.junit.runners.Parameterized;
37*eba4d44aSLiu Zhe import org.junit.runners.Parameterized.Parameters;
38*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
39*eba4d44aSLiu Zhe 
40*eba4d44aSLiu Zhe import testlib.uno.SCUtil;
41*eba4d44aSLiu Zhe import testlib.uno.TestUtil;
42*eba4d44aSLiu Zhe import testlib.uno.CellInfo;
43*eba4d44aSLiu Zhe 
44*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
45*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
46*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
47*eba4d44aSLiu Zhe import com.sun.star.table.XCell;
48*eba4d44aSLiu Zhe import com.sun.star.util.CellProtection;
49*eba4d44aSLiu Zhe 
50*eba4d44aSLiu Zhe 
51*eba4d44aSLiu Zhe /**
52*eba4d44aSLiu Zhe  *  Check the cell protection setting can be applied and saved
53*eba4d44aSLiu Zhe  *
54*eba4d44aSLiu Zhe  */
55*eba4d44aSLiu Zhe @RunWith(value = Parameterized.class)
56*eba4d44aSLiu Zhe public class CellProtected {
57*eba4d44aSLiu Zhe 
58*eba4d44aSLiu Zhe 	private Boolean[] expected;
59*eba4d44aSLiu Zhe 	private String inputType;
60*eba4d44aSLiu Zhe 	private CellProtection inputProtectProps;
61*eba4d44aSLiu Zhe 	private String fileType;
62*eba4d44aSLiu Zhe 
63*eba4d44aSLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
64*eba4d44aSLiu Zhe 
65*eba4d44aSLiu Zhe 	XComponent scComponent = null;
66*eba4d44aSLiu Zhe 	XSpreadsheetDocument scDocument = null;
67*eba4d44aSLiu Zhe 
68*eba4d44aSLiu Zhe 	@Parameters
data()69*eba4d44aSLiu Zhe 	public static Collection<Object[]> data() throws Exception {
70*eba4d44aSLiu Zhe 		Boolean[][] list = {
71*eba4d44aSLiu Zhe 				{true, false, false, false}, //lock cell
72*eba4d44aSLiu Zhe 				{false, true, false, false}, //hide formula
73*eba4d44aSLiu Zhe 				{false, false, true, false}, //hide cell
74*eba4d44aSLiu Zhe 				{false, false, false, true}, //hide cell from print
75*eba4d44aSLiu Zhe 
76*eba4d44aSLiu Zhe 				{true, true, true, false}
77*eba4d44aSLiu Zhe 		};
78*eba4d44aSLiu Zhe 
79*eba4d44aSLiu Zhe 		return Arrays.asList(new Object[][] {
80*eba4d44aSLiu Zhe 			{list[0], "CellProtection", list[0], "ods"},
81*eba4d44aSLiu Zhe 			{list[1], "CellProtection", list[1], "ods"},
82*eba4d44aSLiu Zhe 			{list[4], "CellProtection", list[2], "ods"},
83*eba4d44aSLiu Zhe 			{list[3], "CellProtection", list[3], "ods"}
84*eba4d44aSLiu Zhe 		});
85*eba4d44aSLiu Zhe 	}
86*eba4d44aSLiu Zhe 
CellProtected(Boolean[] expected, String inputType, Boolean[] inputData, String fileType)87*eba4d44aSLiu Zhe 	public CellProtected(Boolean[] expected, String inputType, Boolean[] inputData, String fileType) {
88*eba4d44aSLiu Zhe 
89*eba4d44aSLiu Zhe 		CellProtection protection = new CellProtection();
90*eba4d44aSLiu Zhe 
91*eba4d44aSLiu Zhe 		protection.IsLocked = inputData[0];
92*eba4d44aSLiu Zhe 		protection.IsFormulaHidden = inputData[1];
93*eba4d44aSLiu Zhe 		protection.IsHidden = inputData[2];
94*eba4d44aSLiu Zhe 		protection.IsPrintHidden = inputData[3];
95*eba4d44aSLiu Zhe 
96*eba4d44aSLiu Zhe 		this.expected = expected;
97*eba4d44aSLiu Zhe 		this.inputType = inputType;
98*eba4d44aSLiu Zhe 		this.inputProtectProps = protection;
99*eba4d44aSLiu Zhe 		this.fileType = fileType;
100*eba4d44aSLiu Zhe 	}
101*eba4d44aSLiu Zhe 
102*eba4d44aSLiu Zhe 
103*eba4d44aSLiu Zhe 	@Before
setUp()104*eba4d44aSLiu Zhe 	public void setUp() throws Exception {
105*eba4d44aSLiu Zhe 		scComponent = unoApp.newDocument("scalc");
106*eba4d44aSLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
107*eba4d44aSLiu Zhe 	}
108*eba4d44aSLiu Zhe 
109*eba4d44aSLiu Zhe 	@After
tearDown()110*eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
111*eba4d44aSLiu Zhe 		unoApp.closeDocument(scComponent);
112*eba4d44aSLiu Zhe 
113*eba4d44aSLiu Zhe 	}
114*eba4d44aSLiu Zhe 
115*eba4d44aSLiu Zhe 	@BeforeClass
setUpConnection()116*eba4d44aSLiu Zhe 	public static void setUpConnection() throws Exception {
117*eba4d44aSLiu Zhe 		unoApp.start();
118*eba4d44aSLiu Zhe 	}
119*eba4d44aSLiu Zhe 
120*eba4d44aSLiu Zhe 	@AfterClass
tearDownConnection()121*eba4d44aSLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
122*eba4d44aSLiu Zhe 		unoApp.close();
123*eba4d44aSLiu Zhe 		SCUtil.clearTempDir();
124*eba4d44aSLiu Zhe 	}
125*eba4d44aSLiu Zhe 
126*eba4d44aSLiu Zhe 	/**
127*eba4d44aSLiu Zhe 	 * Check the cell protection settings
128*eba4d44aSLiu Zhe 	 * 1. Create a spreadsheet file.
129*eba4d44aSLiu Zhe 	 * 2. Input number, text, formula into many cell.
130*eba4d44aSLiu Zhe 	 * 3. Set cell protection properties. (Clock, HiddenFormula, Hidden Cell, Hidden Cell from Printing)
131*eba4d44aSLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
132*eba4d44aSLiu Zhe 	 * 5. Close and reopen file.  -> Check the cell protection setting.
133*eba4d44aSLiu Zhe 	 * @throws Exception
134*eba4d44aSLiu Zhe 	 */
135*eba4d44aSLiu Zhe 	@Test
testCellProtected()136*eba4d44aSLiu Zhe 	public void testCellProtected() throws Exception {
137*eba4d44aSLiu Zhe 		String fileName = "testCellProtected";
138*eba4d44aSLiu Zhe 
139*eba4d44aSLiu Zhe 		int cellNum = 5;
140*eba4d44aSLiu Zhe 		XCell[] cells = new XCell[cellNum];
141*eba4d44aSLiu Zhe 		CellProtection[] results = new CellProtection[cellNum];
142*eba4d44aSLiu Zhe 		CellInfo cInfo = TestUtil.randCell(10, 10);
143*eba4d44aSLiu Zhe 
144*eba4d44aSLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
145*eba4d44aSLiu Zhe 
146*eba4d44aSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
147*eba4d44aSLiu Zhe 			cells[i] = sheet.getCellByPosition(cInfo.getCol() + i, cInfo.getRow());
148*eba4d44aSLiu Zhe 		}
149*eba4d44aSLiu Zhe 
150*eba4d44aSLiu Zhe 		cells[0].setValue(2134359.343223);
151*eba4d44aSLiu Zhe 		SCUtil. setTextToCell(cells[1], inputType);
152*eba4d44aSLiu Zhe 		cells[2].setFormula("=Average(A1:A10)");
153*eba4d44aSLiu Zhe 		cells[3].setValue(-0.0003424);
154*eba4d44aSLiu Zhe 
155*eba4d44aSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
156*eba4d44aSLiu Zhe 			SCUtil.setCellProperties(cells[i], inputType, inputProtectProps);
157*eba4d44aSLiu Zhe 		}
158*eba4d44aSLiu Zhe 
159*eba4d44aSLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
160*eba4d44aSLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
161*eba4d44aSLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
162*eba4d44aSLiu Zhe 
163*eba4d44aSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
164*eba4d44aSLiu Zhe 			cells[i] = sheet.getCellByPosition(cInfo.getCol() + i, cInfo.getRow());
165*eba4d44aSLiu Zhe 			results[i] = (CellProtection) SCUtil.getCellProperties(cells[i], inputType);
166*eba4d44aSLiu Zhe 		}
167*eba4d44aSLiu Zhe 
168*eba4d44aSLiu Zhe 		SCUtil.closeFile(scDocument);
169*eba4d44aSLiu Zhe 
170*eba4d44aSLiu Zhe 		for (int i = 0; i < cellNum; i++) {
171*eba4d44aSLiu Zhe 			assertEquals("Incorrect cell protection (IsLocked) value got in ." + fileType + " file.", expected[0], results[i].IsLocked);
172*eba4d44aSLiu Zhe 			assertEquals("Incorrect cell protection(IsFormulaHidden) value got in ." + fileType + " file.", expected[1], results[i].IsFormulaHidden);
173*eba4d44aSLiu Zhe 			assertEquals("Incorrect cell protection(IsHidden) value got in ." + fileType + " file.", expected[2], results[i].IsHidden);
174*eba4d44aSLiu Zhe 			assertEquals("Incorrect cell protection(IsPrintHidden) value got in ." + fileType + " file.", expected[3], results[i].IsPrintHidden);
175*eba4d44aSLiu Zhe 		}
176*eba4d44aSLiu Zhe 	}
177*eba4d44aSLiu Zhe 
178*eba4d44aSLiu Zhe }
179