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 
41 import testlib.uno.SCUtil;
42 import testlib.uno.TestUtil;
43 import testlib.uno.CellInfo;
44 
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.sheet.XSpreadsheet;
47 import com.sun.star.sheet.XSpreadsheetDocument;
48 import com.sun.star.table.CellHoriJustify;
49 import com.sun.star.table.CellVertJustify;
50 import com.sun.star.table.XCell;
51 import com.sun.star.uno.Enum;
52 
53 /**
54  * Check the cell alignment setting can be applied and saved
55  *
56  */
57 @RunWith(value = Parameterized.class)
58 public class CellAlignment {
59 
60 	private Enum expected;
61 	private String inputType;
62 	private Enum inputValue;
63 	private String fileType;
64 
65 	private static final UnoApp unoApp = new UnoApp();
66 
67 	XComponent scComponent = null;
68 	XSpreadsheetDocument scDocument = null;
69 
70 	@Parameters
data()71 	public static Collection<Object[]> data() {
72 		return Arrays.asList(new Object[][] {
73 				{CellHoriJustify.STANDARD, "HoriJustify", CellHoriJustify.STANDARD, "ods"},
74 				{CellHoriJustify.LEFT, "HoriJustify", CellHoriJustify.LEFT, "ods"},
75 				{CellHoriJustify.CENTER, "HoriJustify", CellHoriJustify.CENTER, "ods"},
76 				{CellHoriJustify.RIGHT, "HoriJustify", CellHoriJustify.RIGHT, "ods"},
77 				{CellHoriJustify.BLOCK, "HoriJustify", CellHoriJustify.BLOCK, "ods"},
78 				{CellHoriJustify.REPEAT, "HoriJustify", CellHoriJustify.REPEAT, "ods"},
79 				{CellVertJustify.STANDARD, "VertJustify", CellVertJustify.STANDARD, "ods"},
80 				{CellVertJustify.TOP, "VertJustify", CellVertJustify.TOP, "ods"},
81 				{CellVertJustify.CENTER, "VertJustify", CellVertJustify.CENTER, "ods"},
82 				{CellVertJustify.BOTTOM, "VertJustify", CellVertJustify.BOTTOM, "ods"},
83 
84 				{CellHoriJustify.STANDARD, "HoriJustify", CellHoriJustify.STANDARD, "xls"},
85 				{CellHoriJustify.LEFT, "HoriJustify", CellHoriJustify.LEFT, "xls"},
86 				{CellHoriJustify.CENTER, "HoriJustify", CellHoriJustify.CENTER, "xls"},
87 				{CellHoriJustify.RIGHT, "HoriJustify", CellHoriJustify.RIGHT, "xls"},
88 				{CellHoriJustify.BLOCK, "HoriJustify", CellHoriJustify.BLOCK, "xls"},
89 				{CellHoriJustify.REPEAT, "HoriJustify", CellHoriJustify.REPEAT, "xls"},
90 				{CellVertJustify.STANDARD, "VertJustify", CellVertJustify.STANDARD, "xls"},
91 				{CellVertJustify.TOP, "VertJustify", CellVertJustify.TOP, "xls"},
92 				{CellVertJustify.CENTER, "VertJustify", CellVertJustify.CENTER, "xls"}//,
93 //				{CellVertJustify.BOTTOM, "VertJustify", CellVertJustify.BOTTOM, "xls"}    Bug 120670
94 		});
95 	}
96 
CellAlignment(Enum expected, String inputType, Enum inputValue, String fileType)97 	public CellAlignment(Enum expected, String inputType, Enum inputValue, String fileType) {
98 		this.expected = expected;
99 		this.inputType = inputType;
100 		this.inputValue = inputValue;
101 		this.fileType = fileType;
102 	}
103 
104 	@Before
setUp()105 	public void setUp() throws Exception {
106 		scComponent = unoApp.newDocument("scalc");
107 		scDocument = SCUtil.getSCDocument(scComponent);
108 	}
109 
110 	@After
tearDown()111 	public void tearDown() throws Exception {
112 		unoApp.closeDocument(scComponent);
113 
114 	}
115 
116 	@BeforeClass
setUpConnection()117 	public static void setUpConnection() throws Exception {
118 		unoApp.start();
119 	}
120 
121 	@AfterClass
tearDownConnection()122 	public static void tearDownConnection() throws InterruptedException, Exception {
123 		unoApp.close();
124 		SCUtil.clearTempDir();
125 	}
126 
127 	/**
128 	 * Check the cell alignment setting can be applied and saved
129 	 * 1. Create a spreadsheet file.
130 	 * 2. Input number, text, formula into many cell.
131 	 * 3. Set cell alignment.
132 	 * 4. Save file as ODF/MSBinary format.
133 	 * 5. Close and reopen file.  -> Check the alignment setting.
134 	 * @throws Exception
135 	 */
136 	@Test
testCellAlignment()137 	public void testCellAlignment() throws Exception {
138 		String fileName = "testCellAlignment";
139 		CellInfo cInfo = TestUtil.randCell(20, 50);
140 
141 		int cellNum = 5;
142 		XCell[] cells = new XCell[cellNum];
143 		Enum[] results = new Enum[cellNum];
144 
145 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
146 
147 		//cellNum must be greater than 4
148 		if (cellNum < 5) {
149 			cellNum = 5;
150 		}
151 		for (int i = 0; i < cellNum; i++) {
152 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
153 		}
154 
155 		cells[0].setValue(13.42);
156 		SCUtil. setTextToCell(cells[1], "alignment");
157 		cells[2].setFormula("=SUM(A100:B100)");
158 		cells[3].setValue(-0.2343123);
159 
160 		for (int i = 0; i < cellNum; i++) {
161 			SCUtil.setCellProperties(cells[i], inputType, inputValue);
162 		}
163 
164 		SCUtil.saveFileAs(scComponent, fileName, fileType);
165 
166 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName+"." + fileType);
167 		sheet = SCUtil.getCurrentSheet(scDocument);
168 		for (int i = 0; i < cellNum; i++) {
169 			cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
170 			results[i] = (Enum) SCUtil.getCellProperties(cells[i], inputType);
171 		}
172 		SCUtil.closeFile(scDocument);
173 
174 		for (int i = 0; i < cellNum; i++ ) {
175 
176 			assertEquals("Incorrect cell alignment(" + inputType + ") value got in ." + fileType + " file.", expected, results[i]);
177 
178 		}
179 
180 	}
181 
182 }
183