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