11dfd36f5SLiu Zhe /**************************************************************
21dfd36f5SLiu Zhe  *
31dfd36f5SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
41dfd36f5SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
51dfd36f5SLiu Zhe  * distributed with this work for additional information
61dfd36f5SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
71dfd36f5SLiu Zhe  * to you under the Apache License, Version 2.0 (the
81dfd36f5SLiu Zhe  * "License"); you may not use this file except in compliance
91dfd36f5SLiu Zhe  * with the License.  You may obtain a copy of the License at
101dfd36f5SLiu Zhe  *
111dfd36f5SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
121dfd36f5SLiu Zhe  *
131dfd36f5SLiu Zhe  * Unless required by applicable law or agreed to in writing,
141dfd36f5SLiu Zhe  * software distributed under the License is distributed on an
151dfd36f5SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
161dfd36f5SLiu Zhe  * KIND, either express or implied.  See the License for the
171dfd36f5SLiu Zhe  * specific language governing permissions and limitations
181dfd36f5SLiu Zhe  * under the License.
191dfd36f5SLiu Zhe  *
201dfd36f5SLiu Zhe  *************************************************************/
211dfd36f5SLiu Zhe 
221dfd36f5SLiu Zhe 
23*eba4d44aSLiu Zhe package fvt.uno.sc.cell;
241dfd36f5SLiu Zhe 
251dfd36f5SLiu Zhe import static org.junit.Assert.assertEquals;
261dfd36f5SLiu Zhe 
271dfd36f5SLiu Zhe import java.util.Arrays;
281dfd36f5SLiu Zhe import java.util.Collection;
291dfd36f5SLiu Zhe 
301dfd36f5SLiu Zhe import org.junit.After;
311dfd36f5SLiu Zhe import org.junit.AfterClass;
321dfd36f5SLiu Zhe import org.junit.Before;
331dfd36f5SLiu Zhe import org.junit.BeforeClass;
341dfd36f5SLiu Zhe import org.junit.Test;
351dfd36f5SLiu Zhe import org.junit.runner.RunWith;
361dfd36f5SLiu Zhe import org.junit.runners.Parameterized;
371dfd36f5SLiu Zhe import org.junit.runners.Parameterized.Parameters;
381dfd36f5SLiu Zhe import org.openoffice.test.uno.UnoApp;
391dfd36f5SLiu Zhe 
401dfd36f5SLiu Zhe import testlib.uno.SCUtil;
411dfd36f5SLiu Zhe import testlib.uno.TestUtil;
421dfd36f5SLiu Zhe import testlib.uno.CellInfo;
431dfd36f5SLiu Zhe 
441dfd36f5SLiu Zhe import com.sun.star.lang.XComponent;
451dfd36f5SLiu Zhe import com.sun.star.sheet.XSpreadsheet;
461dfd36f5SLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
471dfd36f5SLiu Zhe import com.sun.star.table.XCell;
481dfd36f5SLiu Zhe import com.sun.star.util.CellProtection;
491dfd36f5SLiu Zhe 
501dfd36f5SLiu Zhe 
511dfd36f5SLiu Zhe /**
521dfd36f5SLiu Zhe  *  Check the cell protection setting can be applied and saved
531dfd36f5SLiu Zhe  *
541dfd36f5SLiu Zhe  */
551dfd36f5SLiu Zhe @RunWith(value = Parameterized.class)
561dfd36f5SLiu Zhe public class CellProtected {
571dfd36f5SLiu Zhe 
581dfd36f5SLiu Zhe 	private Boolean[] expected;
591dfd36f5SLiu Zhe 	private String inputType;
601dfd36f5SLiu Zhe 	private CellProtection inputProtectProps;
611dfd36f5SLiu Zhe 	private String fileType;
621dfd36f5SLiu Zhe 
631dfd36f5SLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
641dfd36f5SLiu Zhe 
651dfd36f5SLiu Zhe 	XComponent scComponent = null;
661dfd36f5SLiu Zhe 	XSpreadsheetDocument scDocument = null;
671dfd36f5SLiu Zhe 
681dfd36f5SLiu Zhe 	@Parameters
data()691dfd36f5SLiu Zhe 	public static Collection<Object[]> data() throws Exception {
701dfd36f5SLiu Zhe 		Boolean[][] list = {
711dfd36f5SLiu Zhe 				{true, false, false, false}, //lock cell
721dfd36f5SLiu Zhe 				{false, true, false, false}, //hide formula
731dfd36f5SLiu Zhe 				{false, false, true, false}, //hide cell
741dfd36f5SLiu Zhe 				{false, false, false, true}, //hide cell from print
751dfd36f5SLiu Zhe 
761dfd36f5SLiu Zhe 				{true, true, true, false}
771dfd36f5SLiu Zhe 		};
781dfd36f5SLiu Zhe 
791dfd36f5SLiu Zhe 		return Arrays.asList(new Object[][] {
801dfd36f5SLiu Zhe 			{list[0], "CellProtection", list[0], "ods"},
811dfd36f5SLiu Zhe 			{list[1], "CellProtection", list[1], "ods"},
821dfd36f5SLiu Zhe 			{list[4], "CellProtection", list[2], "ods"},
831dfd36f5SLiu Zhe 			{list[3], "CellProtection", list[3], "ods"}
841dfd36f5SLiu Zhe 		});
851dfd36f5SLiu Zhe 	}
861dfd36f5SLiu Zhe 
CellProtected(Boolean[] expected, String inputType, Boolean[] inputData, String fileType)871dfd36f5SLiu Zhe 	public CellProtected(Boolean[] expected, String inputType, Boolean[] inputData, String fileType) {
881dfd36f5SLiu Zhe 
891dfd36f5SLiu Zhe 		CellProtection protection = new CellProtection();
901dfd36f5SLiu Zhe 
911dfd36f5SLiu Zhe 		protection.IsLocked = inputData[0];
921dfd36f5SLiu Zhe 		protection.IsFormulaHidden = inputData[1];
931dfd36f5SLiu Zhe 		protection.IsHidden = inputData[2];
941dfd36f5SLiu Zhe 		protection.IsPrintHidden = inputData[3];
951dfd36f5SLiu Zhe 
961dfd36f5SLiu Zhe 		this.expected = expected;
971dfd36f5SLiu Zhe 		this.inputType = inputType;
981dfd36f5SLiu Zhe 		this.inputProtectProps = protection;
991dfd36f5SLiu Zhe 		this.fileType = fileType;
1001dfd36f5SLiu Zhe 	}
1011dfd36f5SLiu Zhe 
1021dfd36f5SLiu Zhe 
1031dfd36f5SLiu Zhe 	@Before
setUp()1041dfd36f5SLiu Zhe 	public void setUp() throws Exception {
1051dfd36f5SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
1061dfd36f5SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
1071dfd36f5SLiu Zhe 	}
1081dfd36f5SLiu Zhe 
1091dfd36f5SLiu Zhe 	@After
tearDown()1101dfd36f5SLiu Zhe 	public void tearDown() throws Exception {
1111dfd36f5SLiu Zhe 		unoApp.closeDocument(scComponent);
1121dfd36f5SLiu Zhe 
1131dfd36f5SLiu Zhe 	}
1141dfd36f5SLiu Zhe 
1151dfd36f5SLiu Zhe 	@BeforeClass
setUpConnection()1161dfd36f5SLiu Zhe 	public static void setUpConnection() throws Exception {
1171dfd36f5SLiu Zhe 		unoApp.start();
1181dfd36f5SLiu Zhe 	}
1191dfd36f5SLiu Zhe 
1201dfd36f5SLiu Zhe 	@AfterClass
tearDownConnection()1211dfd36f5SLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
1221dfd36f5SLiu Zhe 		unoApp.close();
1231dfd36f5SLiu Zhe 		SCUtil.clearTempDir();
1241dfd36f5SLiu Zhe 	}
1251dfd36f5SLiu Zhe 
1261dfd36f5SLiu Zhe 	/**
1271dfd36f5SLiu Zhe 	 * Check the cell protection settings
1281dfd36f5SLiu Zhe 	 * 1. Create a spreadsheet file.
1291dfd36f5SLiu Zhe 	 * 2. Input number, text, formula into many cell.
1301dfd36f5SLiu Zhe 	 * 3. Set cell protection properties. (Clock, HiddenFormula, Hidden Cell, Hidden Cell from Printing)
1311dfd36f5SLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
1321dfd36f5SLiu Zhe 	 * 5. Close and reopen file.  -> Check the cell protection setting.
1331dfd36f5SLiu Zhe 	 * @throws Exception
1341dfd36f5SLiu Zhe 	 */
1351dfd36f5SLiu Zhe 	@Test
testCellProtected()1361dfd36f5SLiu Zhe 	public void testCellProtected() throws Exception {
1371dfd36f5SLiu Zhe 		String fileName = "testCellProtected";
1381dfd36f5SLiu Zhe 
1391dfd36f5SLiu Zhe 		int cellNum = 5;
1401dfd36f5SLiu Zhe 		XCell[] cells = new XCell[cellNum];
1411dfd36f5SLiu Zhe 		CellProtection[] results = new CellProtection[cellNum];
1421dfd36f5SLiu Zhe 		CellInfo cInfo = TestUtil.randCell(10, 10);
1431dfd36f5SLiu Zhe 
1441dfd36f5SLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
1451dfd36f5SLiu Zhe 
1461dfd36f5SLiu Zhe 		for (int i = 0; i < cellNum; i++) {
1471dfd36f5SLiu Zhe 			cells[i] = sheet.getCellByPosition(cInfo.getCol() + i, cInfo.getRow());
1481dfd36f5SLiu Zhe 		}
1491dfd36f5SLiu Zhe 
1501dfd36f5SLiu Zhe 		cells[0].setValue(2134359.343223);
1511dfd36f5SLiu Zhe 		SCUtil. setTextToCell(cells[1], inputType);
1521dfd36f5SLiu Zhe 		cells[2].setFormula("=Average(A1:A10)");
1531dfd36f5SLiu Zhe 		cells[3].setValue(-0.0003424);
1541dfd36f5SLiu Zhe 
1551dfd36f5SLiu Zhe 		for (int i = 0; i < cellNum; i++) {
1561dfd36f5SLiu Zhe 			SCUtil.setCellProperties(cells[i], inputType, inputProtectProps);
1571dfd36f5SLiu Zhe 		}
1581dfd36f5SLiu Zhe 
1591dfd36f5SLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
1601dfd36f5SLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
1611dfd36f5SLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
1621dfd36f5SLiu Zhe 
1631dfd36f5SLiu Zhe 		for (int i = 0; i < cellNum; i++) {
1641dfd36f5SLiu Zhe 			cells[i] = sheet.getCellByPosition(cInfo.getCol() + i, cInfo.getRow());
1651dfd36f5SLiu Zhe 			results[i] = (CellProtection) SCUtil.getCellProperties(cells[i], inputType);
1661dfd36f5SLiu Zhe 		}
1671dfd36f5SLiu Zhe 
1681dfd36f5SLiu Zhe 		SCUtil.closeFile(scDocument);
1691dfd36f5SLiu Zhe 
1701dfd36f5SLiu Zhe 		for (int i = 0; i < cellNum; i++) {
1711dfd36f5SLiu Zhe 			assertEquals("Incorrect cell protection (IsLocked) value got in ." + fileType + " file.", expected[0], results[i].IsLocked);
1721dfd36f5SLiu Zhe 			assertEquals("Incorrect cell protection(IsFormulaHidden) value got in ." + fileType + " file.", expected[1], results[i].IsFormulaHidden);
1731dfd36f5SLiu Zhe 			assertEquals("Incorrect cell protection(IsHidden) value got in ." + fileType + " file.", expected[2], results[i].IsHidden);
1741dfd36f5SLiu Zhe 			assertEquals("Incorrect cell protection(IsPrintHidden) value got in ." + fileType + " file.", expected[3], results[i].IsPrintHidden);
1751dfd36f5SLiu Zhe 		}
1761dfd36f5SLiu Zhe 	}
1771dfd36f5SLiu Zhe 
1781dfd36f5SLiu Zhe }
179