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.XCell;
48 import com.sun.star.util.CellProtection;
49 
50 
51 /**
52  *  Check the cell protection setting can be applied and saved
53  *
54  */
55 @RunWith(value = Parameterized.class)
56 public class CellProtected {
57 
58 	private Boolean[] expected;
59 	private String inputType;
60 	private CellProtection inputProtectProps;
61 	private String fileType;
62 
63 	private static final UnoApp unoApp = new UnoApp();
64 
65 	XComponent scComponent = null;
66 	XSpreadsheetDocument scDocument = null;
67 
68 	@Parameters
data()69 	public static Collection<Object[]> data() throws Exception {
70 		Boolean[][] list = {
71 				{true, false, false, false}, //lock cell
72 				{false, true, false, false}, //hide formula
73 				{false, false, true, false}, //hide cell
74 				{false, false, false, true}, //hide cell from print
75 
76 				{true, true, true, false}
77 		};
78 
79 		return Arrays.asList(new Object[][] {
80 			{list[0], "CellProtection", list[0], "ods"},
81 			{list[1], "CellProtection", list[1], "ods"},
82 			{list[4], "CellProtection", list[2], "ods"},
83 			{list[3], "CellProtection", list[3], "ods"}
84 		});
85 	}
86 
CellProtected(Boolean[] expected, String inputType, Boolean[] inputData, String fileType)87 	public CellProtected(Boolean[] expected, String inputType, Boolean[] inputData, String fileType) {
88 
89 		CellProtection protection = new CellProtection();
90 
91 		protection.IsLocked = inputData[0];
92 		protection.IsFormulaHidden = inputData[1];
93 		protection.IsHidden = inputData[2];
94 		protection.IsPrintHidden = inputData[3];
95 
96 		this.expected = expected;
97 		this.inputType = inputType;
98 		this.inputProtectProps = protection;
99 		this.fileType = fileType;
100 	}
101 
102 
103 	@Before
setUp()104 	public void setUp() throws Exception {
105 		scComponent = unoApp.newDocument("scalc");
106 		scDocument = SCUtil.getSCDocument(scComponent);
107 	}
108 
109 	@After
tearDown()110 	public void tearDown() throws Exception {
111 		unoApp.closeDocument(scComponent);
112 
113 	}
114 
115 	@BeforeClass
setUpConnection()116 	public static void setUpConnection() throws Exception {
117 		unoApp.start();
118 	}
119 
120 	@AfterClass
tearDownConnection()121 	public static void tearDownConnection() throws InterruptedException, Exception {
122 		unoApp.close();
123 		SCUtil.clearTempDir();
124 	}
125 
126 	/**
127 	 * Check the cell protection settings
128 	 * 1. Create a spreadsheet file.
129 	 * 2. Input number, text, formula into many cell.
130 	 * 3. Set cell protection properties. (Clock, HiddenFormula, Hidden Cell, Hidden Cell from Printing)
131 	 * 4. Save file as ODF/MSBinary format.
132 	 * 5. Close and reopen file.  -> Check the cell protection setting.
133 	 * @throws Exception
134 	 */
135 	@Test
testCellProtected()136 	public void testCellProtected() throws Exception {
137 		String fileName = "testCellProtected";
138 
139 		int cellNum = 5;
140 		XCell[] cells = new XCell[cellNum];
141 		CellProtection[] results = new CellProtection[cellNum];
142 		CellInfo cInfo = TestUtil.randCell(10, 10);
143 
144 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
145 
146 		for (int i = 0; i < cellNum; i++) {
147 			cells[i] = sheet.getCellByPosition(cInfo.getCol() + i, cInfo.getRow());
148 		}
149 
150 		cells[0].setValue(2134359.343223);
151 		SCUtil. setTextToCell(cells[1], inputType);
152 		cells[2].setFormula("=Average(A1:A10)");
153 		cells[3].setValue(-0.0003424);
154 
155 		for (int i = 0; i < cellNum; i++) {
156 			SCUtil.setCellProperties(cells[i], inputType, inputProtectProps);
157 		}
158 
159 		SCUtil.saveFileAs(scComponent, fileName, fileType);
160 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
161 		sheet = SCUtil.getCurrentSheet(scDocument);
162 
163 		for (int i = 0; i < cellNum; i++) {
164 			cells[i] = sheet.getCellByPosition(cInfo.getCol() + i, cInfo.getRow());
165 			results[i] = (CellProtection) SCUtil.getCellProperties(cells[i], inputType);
166 		}
167 
168 		SCUtil.closeFile(scDocument);
169 
170 		for (int i = 0; i < cellNum; i++) {
171 			assertEquals("Incorrect cell protection (IsLocked) value got in ." + fileType + " file.", expected[0], results[i].IsLocked);
172 			assertEquals("Incorrect cell protection(IsFormulaHidden) value got in ." + fileType + " file.", expected[1], results[i].IsFormulaHidden);
173 			assertEquals("Incorrect cell protection(IsHidden) value got in ." + fileType + " file.", expected[2], results[i].IsHidden);
174 			assertEquals("Incorrect cell protection(IsPrintHidden) value got in ." + fileType + " file.", expected[3], results[i].IsPrintHidden);
175 		}
176 	}
177 
178 }
179