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 package fvt.uno.sc.chart;
23 
24 import static org.junit.Assert.assertArrayEquals;
25 
26 import java.util.Arrays;
27 import java.util.Collection;
28 
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized;
36 import org.junit.runners.Parameterized.Parameters;
37 import org.openoffice.test.uno.UnoApp;
38 
39 import testlib.uno.SCUtil;
40 
41 import com.sun.star.awt.Rectangle;
42 import com.sun.star.chart.XChartDocument;
43 import com.sun.star.chart.XDiagram;
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.CellRangeAddress;
48 
49 /**
50  *  Check Grids in chart can be applied and saved
51  *
52  */
53 @RunWith(value = Parameterized.class)
54 public class ChartGrid {
55 
56 	private Boolean[] majorGrids;
57 	private Boolean[] minorGrids;
58 	private String inputType;
59 	private double[][] numberData;
60 	private String fileType;
61 
62 	private static final UnoApp unoApp = new UnoApp();
63 
64 	XComponent scComponent = null;
65 	XSpreadsheetDocument scDocument = null;
66 
67 	@Parameters
data()68 	public static Collection<Object[]> data() throws Exception {
69 		double[][] numberData1 = {
70 				{10, 20, 30, 40},
71 				{20, 40.3, 50, 80},
72 				{40, 20, 30, 10},
73 				{10, -10, 0, -30}
74 		};
75 		Boolean[][] gridsList = {
76 				{false, false, false}, //[0] no grid
77 				{true, false, false}, // [1] X
78 				{false, true, false}, // [2] Y
79 				{true, true, false}, // [3] X & Y
80 				{true, true, true}, // [4] X & Y & Z
81 				{false, false, true}, // [5] Z
82 				{false, true, true}, // [6] Y & Z
83 				{true, false, true} // [7] X & Z
84 		};
85 
86 		return Arrays.asList(new Object[][] {
87 			{gridsList[0], null, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
88 			{gridsList[2], gridsList[3], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
89 			{gridsList[2], gridsList[4], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
90 			{gridsList[6], gridsList[7], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
91 			{gridsList[1], gridsList[5], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
92 			{gridsList[4], gridsList[4], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
93 
94 			{gridsList[0], null, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
95 			{gridsList[2], gridsList[3], "com.sun.star.chart.BarDiagram", numberData1, "xls"}
96 //			{null, gridsList[4], "com.sun.star.chart.BarDiagram", numberData1, "xls"},
97 //			{gridsList[6], gridsList[7], "com.sun.star.chart.BarDiagram", numberData1, "xls"},
98 //			{gridsList[1], gridsList[5], "com.sun.star.chart.BarDiagram", numberData1, "xls"},
99 //			{gridsList[4], gridsList[4], "com.sun.star.chart.BarDiagram", numberData1, "xls"}
100 
101 		});
102 	}
103 
ChartGrid(Boolean[] majorGrids, Boolean[] minorGrids, String inputType, double[][] numberData, String fileType)104 	public ChartGrid(Boolean[] majorGrids, Boolean[] minorGrids, String inputType, double[][] numberData, String fileType) {
105 		this.majorGrids = majorGrids;
106 		this.minorGrids = minorGrids;
107 		this.inputType = inputType;
108 		this.numberData = numberData;
109 		this.fileType = fileType;
110 	}
111 
112 	@Before
setUp()113 	public void setUp() throws Exception {
114 		scComponent = unoApp.newDocument("scalc");
115 		scDocument = SCUtil.getSCDocument(scComponent);
116 	}
117 
118 	@After
tearDown()119 	public void tearDown() throws Exception {
120 		unoApp.closeDocument(scComponent);
121 
122 	}
123 
124 	@BeforeClass
setUpConnection()125 	public static void setUpConnection() throws Exception {
126 		unoApp.start();
127 	}
128 
129 	@AfterClass
tearDownConnection()130 	public static void tearDownConnection() throws InterruptedException, Exception {
131 		unoApp.close();
132 		SCUtil.clearTempDir();
133 	}
134 
135 	/**
136 	 * Enable different types of grids in chart.
137 	 * 1. Create a spreadsheet file.
138 	 * 2. Input number in a cell range and create a 2D chart.
139 	 * 3. Enable grids of X/Y axis in chart.
140 	 * 4. Save file as ODF/MSBinary format.
141 	 * 5. Close and reopen file.  -> Check the grid setting.
142 	 * @throws Exception
143 	 */
144 	@Test
testCreateXYGrid()145 	public void testCreateXYGrid() throws Exception {
146 		String fileName = "testCreateXYGrid";
147 		String chartName = "testChart";
148 		String cellRangeName = "A1:D4";
149 		Boolean[][] expected = {
150 				{false, true, false},
151 				{false, false, false}
152 		};
153 		Boolean[][] results = {
154 				{false, false, false},
155 				{false, false, false}
156 		};
157 
158 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
159 			cellRangeName = "A1:C4";
160 		}
161 		if (fileType.equalsIgnoreCase("xls")) {
162 			chartName = "Object 1";
163 		}
164 
165 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
166 
167 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
168 
169 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
170 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
171 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
172 		XChartDocument xChartDocument = null;
173 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
174 		SCUtil.setChartType(xChartDocument, inputType);
175 		XDiagram xDiagram = xChartDocument.getDiagram();
176 
177 		if (majorGrids != null) {
178 			SCUtil.setProperties(xDiagram, "HasXAxisGrid", majorGrids[0]);
179 			SCUtil.setProperties(xDiagram, "HasYAxisGrid", majorGrids[1]);
180 			expected[0][0] = majorGrids[0];
181 			expected[0][1] = majorGrids[1];
182 		}
183 		if (minorGrids != null) {
184 			SCUtil.setProperties(xDiagram, "HasXAxisHelpGrid", minorGrids[0]);
185 			SCUtil.setProperties(xDiagram, "HasYAxisHelpGrid", minorGrids[1]);
186 			expected[1][0] = minorGrids[0];
187 			expected[1][1] = minorGrids[1];
188 		}
189 
190 		SCUtil.saveFileAs(scComponent, fileName, fileType);
191 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
192 		sheet = SCUtil.getCurrentSheet(scDocument);
193 
194 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
195 		xDiagram = xChartDocument.getDiagram();
196 		results[0][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasXAxisGrid");
197 		results[0][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasYAxisGrid");
198 		results[1][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasXAxisHelpGrid");
199 		results[1][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasYAxisHelpGrid");
200 
201 		SCUtil.closeFile(scDocument);
202 
203 		assertArrayEquals("Incorrect chart grids got in ." + fileType + " file.", expected, results);
204 
205 	}
206 
207 	/**
208 	 * Enable different types of grids in chart.
209 	 * 1. Create a spreadsheet file.
210 	 * 2. Input number in a cell range and create a 3D chart.
211 	 * 3. Enable grids of X/Y/Z axes in chart.
212 	 * 4. Save file as ODF/MSBinary format.
213 	 * 5. Close and reopen file.  -> Check the grid setting.
214 	 * @throws Exception
215 	 */
216 	@Test
testCreateXYZGrid()217 	public void testCreateXYZGrid() throws Exception {
218 		String fileName = "testCreateXYZGrid";
219 		String chartName = "testChart";
220 		String cellRangeName = "A1:D4";
221 		Boolean[][] expected = {
222 				{false, true, false},
223 				{false, false, false}
224 		};
225 		Boolean[][] results = {
226 				{false, false, false},
227 				{false, false, false}
228 		};
229 
230 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
231 			cellRangeName = "A1:C4";
232 		}
233 		if (fileType.equalsIgnoreCase("xls")) {
234 			chartName = "Object 1";
235 		}
236 
237 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
238 
239 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
240 
241 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
242 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
243 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
244 		XChartDocument xChartDocument = null;
245 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
246 		SCUtil.setChartType(xChartDocument, inputType);
247 		SCUtil.setProperties(xChartDocument.getDiagram(), "Dim3D", true);
248 		XDiagram xDiagram = xChartDocument.getDiagram();
249 
250 		if (majorGrids != null) {
251 			SCUtil.setProperties(xDiagram, "HasXAxisGrid", majorGrids[0]);
252 			SCUtil.setProperties(xDiagram, "HasYAxisGrid", majorGrids[1]);
253 			SCUtil.setProperties(xDiagram, "HasZAxisGrid", majorGrids[2]);
254 			expected[0][0] = majorGrids[0];
255 			expected[0][1] = majorGrids[1];
256 			expected[0][2] = majorGrids[2];
257 		}
258 		if (minorGrids != null) {
259 			SCUtil.setProperties(xDiagram, "HasXAxisHelpGrid", minorGrids[0]);
260 			SCUtil.setProperties(xDiagram, "HasYAxisHelpGrid", minorGrids[1]);
261 			SCUtil.setProperties(xDiagram, "HasZAxisHelpGrid", minorGrids[2]);
262 			expected[1][0] = minorGrids[0];
263 			expected[1][1] = minorGrids[1];
264 			expected[1][2] = minorGrids[2];
265 		}
266 
267 		SCUtil.saveFileAs(scComponent, fileName, fileType);
268 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
269 		sheet = SCUtil.getCurrentSheet(scDocument);
270 
271 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
272 		xDiagram = xChartDocument.getDiagram();
273 		results[0][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasXAxisGrid");
274 		results[0][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasYAxisGrid");
275 		results[0][2] = (Boolean) SCUtil.getProperties(xDiagram, "HasZAxisGrid");
276 		results[1][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasXAxisHelpGrid");
277 		results[1][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasYAxisHelpGrid");
278 		results[1][2] = (Boolean) SCUtil.getProperties(xDiagram, "HasZAxisHelpGrid");
279 
280 		SCUtil.closeFile(scDocument);
281 
282 		assertArrayEquals("Incorrect chart grids got in ." + fileType + " file.", expected, results);
283 
284 	}
285 
286 }