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