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 Axis in chart can be applied and saved
51  *
52  */
53 @RunWith(value = Parameterized.class)
54 public class ChartAxis {
55 
56 	private Boolean[] axes;
57 	private Boolean[] secondaryAxes;
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[][] axesList = {
76 				{false, false, false}, //[0] no Axis
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 //			{axesList[0], null, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, //Bug #121046
88 //			{axesList[2], axesList[3], "com.sun.star.chart.BarDiagram", numberData1, "ods"}, //Bug #121046
89 			{null, axesList[4], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
90 //			{axesList[6], axesList[7], "com.sun.star.chart.BarDiagram", numberData1, "ods"}, //Bug #121046
91 			{axesList[1], axesList[5], "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
92 			{axesList[4], axesList[4], "com.sun.star.chart.LineDiagram", numberData1, "ods"},
93 
94 //			{axesList[0], axesList[0], "com.sun.star.chart.BarDiagram", numberData1, "xls"}, //Bug #121043
95 //			{axesList[2], axesList[3], "com.sun.star.chart.BarDiagram", numberData1, "xls"}, //Bug #121043
96 			{null, axesList[4], "com.sun.star.chart.BarDiagram", numberData1, "xls"},
97 			{axesList[6], axesList[7], "com.sun.star.chart.BarDiagram", numberData1, "xls"},
98 			{axesList[1], axesList[5], "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
99 			{axesList[4], axesList[4], "com.sun.star.chart.LineDiagram", numberData1, "xls"}
100 
101 		});
102 	}
103 
ChartAxis(Boolean[] axes, Boolean[] secondaryAxes, String inputType, double[][] numberData, String fileType)104 	public ChartAxis(Boolean[] axes, Boolean[] secondaryAxes, String inputType, double[][] numberData, String fileType) {
105 		this.axes = axes;
106 		this.secondaryAxes = secondaryAxes;
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 axes in chart.
137 	 * 1. Create a spreadsheet file.
138 	 * 2. Input number in a cell range and create a 2D chart.
139 	 * 3. Enable X/Y axis/secondaryAxis in chart.
140 	 * 4. Save file as ODF/MSBinary format.
141 	 * 5. Close and reopen file.  -> Check the axes setting.
142 	 * @throws Exception
143 	 */
144 	@Test
testCreateXYAxes()145 	public void testCreateXYAxes() throws Exception {
146 		String fileName = "testCreateXYAxes";
147 		String chartName = "testChart";
148 		String cellRangeName = "A1:D4";
149 		Boolean[][] expected = {
150 				{true, 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 (axes != null) {
178 			SCUtil.setProperties(xDiagram, "HasXAxis", axes[0]);
179 			SCUtil.setProperties(xDiagram, "HasYAxis", axes[1]);
180 			expected[0][0] = axes[0];
181 			expected[0][1] = axes[1];
182 		}
183 		if (secondaryAxes != null) {
184 			SCUtil.setProperties(xDiagram, "HasSecondaryXAxis", secondaryAxes[0]);
185 			SCUtil.setProperties(xDiagram, "HasSecondaryYAxis", secondaryAxes[1]);
186 			expected[1][0] = secondaryAxes[0];
187 			expected[1][1] = secondaryAxes[1];
188 		}
189 
190 		//Excel does not has secondary X axis, the value depends on the secondary Y axis setting
191 		if (fileType.equalsIgnoreCase("xls")) {
192 			expected[1][0] = false;
193 			if (expected[1][1]) {
194 				expected[1][0] = true;
195 				SCUtil.setProperties(xDiagram.getDataRowProperties(1), "Axis", 4);
196 			}
197 		}
198 
199 		SCUtil.saveFileAs(scComponent, fileName, fileType);
200 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
201 		sheet = SCUtil.getCurrentSheet(scDocument);
202 
203 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
204 		xDiagram = xChartDocument.getDiagram();
205 		results[0][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasXAxis");
206 		results[0][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasYAxis");
207 		results[1][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasSecondaryXAxis");
208 		results[1][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasSecondaryYAxis");
209 
210 		SCUtil.closeFile(scDocument);
211 
212 		assertArrayEquals("Incorrect chart grids got in ." + fileType + " file.", expected, results);
213 
214 	}
215 
216 	/**
217 	 * Enable different types of axes in 3D chart.
218 	 * 1. Create a spreadsheet file.
219 	 * 2. Input number in a cell range and create a 2D chart.
220 	 * 3. Enable X/Y/Z axis in chart.
221 	 * 4. Save file as ODF/MSBinary format.
222 	 * 5. Close and reopen file.  -> Check the axes setting.
223 	 * @throws Exception
224 	 */
225 	@Test
testCreateXYZAxes()226 	public void testCreateXYZAxes() throws Exception {
227 		String fileName = "testCreateXYZAxes";
228 		String chartName = "testChart";
229 		String cellRangeName = "A1:D4";
230 		Boolean[][] expected = {
231 				{true, true, true},
232 				{false, false, false}
233 		};
234 		Boolean[][] results = {
235 				{false, false, false},
236 				{false, false, false}
237 		};
238 
239 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
240 			cellRangeName = "A1:C4";
241 		}
242 		if (fileType.equalsIgnoreCase("xls")) {
243 			chartName = "Object 1";
244 		}
245 
246 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
247 
248 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
249 
250 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
251 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
252 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
253 		XChartDocument xChartDocument = null;
254 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
255 		SCUtil.setChartType(xChartDocument, inputType);
256 		SCUtil.setProperties(xChartDocument.getDiagram(), "Dim3D", true);
257 		XDiagram xDiagram = xChartDocument.getDiagram();
258 
259 		if (axes != null) {
260 			SCUtil.setProperties(xDiagram, "HasXAxis", axes[0]);
261 			SCUtil.setProperties(xDiagram, "HasYAxis", axes[1]);
262 			SCUtil.setProperties(xDiagram, "HasZAxis", axes[2]);
263 			expected[0][0] = axes[0];
264 			expected[0][1] = axes[1];
265 			expected[0][2] = axes[2];
266 		}
267 
268 
269 		SCUtil.saveFileAs(scComponent, fileName, fileType);
270 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
271 		sheet = SCUtil.getCurrentSheet(scDocument);
272 
273 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
274 		xDiagram = xChartDocument.getDiagram();
275 		results[0][0] = (Boolean) SCUtil.getProperties(xDiagram, "HasXAxis");
276 		results[0][1] = (Boolean) SCUtil.getProperties(xDiagram, "HasYAxis");
277 		results[0][2] = (Boolean) SCUtil.getProperties(xDiagram, "HasZAxis");
278 
279 		SCUtil.closeFile(scDocument);
280 
281 		assertArrayEquals("Incorrect chart Axes got in ." + fileType + " file.", expected, results);
282 
283 	}
284 
285 }