xref: /AOO41X/test/testuno/source/fvt/uno/sc/chart/ChartLegend.java (revision d3e0dd8eb215533c15e891ee35bd141abe9397ee)
1eba4d44aSLiu Zhe /**************************************************************
2eba4d44aSLiu Zhe  *
3eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5eba4d44aSLiu Zhe  * distributed with this work for additional information
6eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10eba4d44aSLiu Zhe  *
11eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12eba4d44aSLiu Zhe  *
13eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18eba4d44aSLiu Zhe  * under the License.
19eba4d44aSLiu Zhe  *
20eba4d44aSLiu Zhe  *************************************************************/
21eba4d44aSLiu Zhe 
22eba4d44aSLiu Zhe package fvt.uno.sc.chart;
23eba4d44aSLiu Zhe 
24eba4d44aSLiu Zhe import static org.junit.Assert.assertEquals;
25eba4d44aSLiu Zhe import static org.junit.Assert.assertFalse;
26eba4d44aSLiu Zhe import static org.junit.Assert.assertTrue;
27eba4d44aSLiu Zhe 
28eba4d44aSLiu Zhe import java.util.Arrays;
29eba4d44aSLiu Zhe import java.util.Collection;
30eba4d44aSLiu Zhe 
31eba4d44aSLiu Zhe import org.junit.After;
32eba4d44aSLiu Zhe import org.junit.AfterClass;
33eba4d44aSLiu Zhe import org.junit.Before;
34eba4d44aSLiu Zhe import org.junit.BeforeClass;
35eba4d44aSLiu Zhe import org.junit.Test;
36eba4d44aSLiu Zhe import org.junit.runner.RunWith;
37eba4d44aSLiu Zhe import org.junit.runners.Parameterized;
38eba4d44aSLiu Zhe import org.junit.runners.Parameterized.Parameters;
39eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
40eba4d44aSLiu Zhe 
41eba4d44aSLiu Zhe import testlib.uno.SCUtil;
42eba4d44aSLiu Zhe 
43eba4d44aSLiu Zhe import com.sun.star.awt.Point;
44eba4d44aSLiu Zhe import com.sun.star.awt.Rectangle;
45eba4d44aSLiu Zhe import com.sun.star.chart.XChartDocument;
46eba4d44aSLiu Zhe import com.sun.star.drawing.XShape;
47eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
48eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
49eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
50eba4d44aSLiu Zhe import com.sun.star.table.CellRangeAddress;
51eba4d44aSLiu Zhe 
52eba4d44aSLiu Zhe /**
53eba4d44aSLiu Zhe  *  Check the chart legend and the position can be applied and saved
54eba4d44aSLiu Zhe  *
55eba4d44aSLiu Zhe  */
56eba4d44aSLiu Zhe @RunWith(value = Parameterized.class)
57eba4d44aSLiu Zhe public class ChartLegend {
58eba4d44aSLiu Zhe 
59eba4d44aSLiu Zhe 	private String inputType;
60eba4d44aSLiu Zhe 	private double[][] numberData;
61eba4d44aSLiu Zhe 	private int[] offset;
62eba4d44aSLiu Zhe 	private String fileType;
63eba4d44aSLiu Zhe 
64eba4d44aSLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
65eba4d44aSLiu Zhe 
66eba4d44aSLiu Zhe 	XComponent scComponent = null;
67eba4d44aSLiu Zhe 	XSpreadsheetDocument scDocument = null;
68eba4d44aSLiu Zhe 
69eba4d44aSLiu Zhe 	@Parameters
data()70eba4d44aSLiu Zhe 	public static Collection<Object[]> data() throws Exception {
71eba4d44aSLiu Zhe 		int[][] offsetList = {
72eba4d44aSLiu Zhe 				{-50, -2000},
73eba4d44aSLiu Zhe 				{-1000, 3000},
74eba4d44aSLiu Zhe 				{-4000, -1000}
75eba4d44aSLiu Zhe 		};
76eba4d44aSLiu Zhe 
77eba4d44aSLiu Zhe 		double[][] numberData1 = {
78eba4d44aSLiu Zhe 				{1, 2, 3, 4},
79eba4d44aSLiu Zhe 				{2, 4.3, 5, 8},
80eba4d44aSLiu Zhe 				{4, 2, 3, 1},
81eba4d44aSLiu Zhe 				{1, -1, 0, 3}
82eba4d44aSLiu Zhe 		};
83eba4d44aSLiu Zhe 		return Arrays.asList(new Object[][] {
84eba4d44aSLiu Zhe 			{"com.sun.star.chart.BarDiagram", numberData1, offsetList[0], "ods"},
85eba4d44aSLiu Zhe 			{"com.sun.star.chart.BubbleDiagram", numberData1, offsetList[1], "ods"},
86eba4d44aSLiu Zhe 			{"com.sun.star.chart.NetDiagram", numberData1, offsetList[2], "ods"},
87eba4d44aSLiu Zhe 			{"com.sun.star.chart.BarDiagram", numberData1, offsetList[0], "xls"},
88eba4d44aSLiu Zhe 			{"com.sun.star.chart.BubbleDiagram", numberData1, offsetList[1], "xls"},
89eba4d44aSLiu Zhe 			{"com.sun.star.chart.NetDiagram", numberData1, offsetList[2], "xls"}
90eba4d44aSLiu Zhe 		});
91eba4d44aSLiu Zhe 	}
92eba4d44aSLiu Zhe 
ChartLegend(String inputType, double[][] numberData, int[] offset, String fileType)93eba4d44aSLiu Zhe 	public ChartLegend(String inputType, double[][] numberData, int[] offset, String fileType) {
94eba4d44aSLiu Zhe 		this.inputType = inputType;
95eba4d44aSLiu Zhe 		this.numberData = numberData;
96eba4d44aSLiu Zhe 		this.offset = offset;
97eba4d44aSLiu Zhe 		this.fileType = fileType;
98eba4d44aSLiu Zhe 	}
99eba4d44aSLiu Zhe 
100eba4d44aSLiu Zhe 
101eba4d44aSLiu Zhe 	@Before
setUp()102eba4d44aSLiu Zhe 	public void setUp() throws Exception {
103eba4d44aSLiu Zhe 		scComponent = unoApp.newDocument("scalc");
104eba4d44aSLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
105eba4d44aSLiu Zhe 	}
106eba4d44aSLiu Zhe 
107eba4d44aSLiu Zhe 	@After
tearDown()108eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
109eba4d44aSLiu Zhe 		unoApp.closeDocument(scComponent);
110eba4d44aSLiu Zhe 
111eba4d44aSLiu Zhe 	}
112eba4d44aSLiu Zhe 
113eba4d44aSLiu Zhe 	@BeforeClass
setUpConnection()114eba4d44aSLiu Zhe 	public static void setUpConnection() throws Exception {
115eba4d44aSLiu Zhe 		unoApp.start();
116eba4d44aSLiu Zhe 	}
117eba4d44aSLiu Zhe 
118eba4d44aSLiu Zhe 	@AfterClass
tearDownConnection()119eba4d44aSLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
120eba4d44aSLiu Zhe 		unoApp.close();
121eba4d44aSLiu Zhe 		SCUtil.clearTempDir();
122eba4d44aSLiu Zhe 	}
123eba4d44aSLiu Zhe 
124eba4d44aSLiu Zhe 	/**
125eba4d44aSLiu Zhe 	 * Check remove the legend of chart
126eba4d44aSLiu Zhe 	 * 1. Create a spreadsheet file.
127eba4d44aSLiu Zhe 	 * 2. Input number in a cell range.
128eba4d44aSLiu Zhe 	 * 3. Use the data to create a chart, set the chart type.
129eba4d44aSLiu Zhe 	 * 4. Remove the legend.
130eba4d44aSLiu Zhe 	 * 5. Save file as ODF/MSBinary format.
131eba4d44aSLiu Zhe 	 * 6. Close and reopen file.  -> Check the legend status.
132eba4d44aSLiu Zhe 	 * @throws Exception
133eba4d44aSLiu Zhe 	 */
134eba4d44aSLiu Zhe 	@Test
testDisableLegend()135eba4d44aSLiu Zhe 	public void testDisableLegend() throws Exception {
136eba4d44aSLiu Zhe 		String fileName = "testDisableLegend";
137eba4d44aSLiu Zhe 		String chartName = "testChart";
138eba4d44aSLiu Zhe 		String cellRangeName = "A1:D4";
139eba4d44aSLiu Zhe 		Boolean result = true;
140eba4d44aSLiu Zhe 
141eba4d44aSLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
142eba4d44aSLiu Zhe 			chartName = "Object 1";
143eba4d44aSLiu Zhe 		}
144eba4d44aSLiu Zhe 
145eba4d44aSLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
146eba4d44aSLiu Zhe 
147eba4d44aSLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
148eba4d44aSLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
149eba4d44aSLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
150eba4d44aSLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
151eba4d44aSLiu Zhe 		XChartDocument xChartDocument = null;
152eba4d44aSLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
153eba4d44aSLiu Zhe 
154eba4d44aSLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
155eba4d44aSLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasLegend");
156eba4d44aSLiu Zhe 		if (result) {
157eba4d44aSLiu Zhe 			SCUtil.setProperties(xChartDocument, "HasLegend", false);
158eba4d44aSLiu Zhe 		}
159eba4d44aSLiu Zhe 
160eba4d44aSLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
161eba4d44aSLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
162eba4d44aSLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
163eba4d44aSLiu Zhe 
164eba4d44aSLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
165eba4d44aSLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasLegend");
166eba4d44aSLiu Zhe 
167eba4d44aSLiu Zhe 		SCUtil.closeFile(scDocument);
168eba4d44aSLiu Zhe 
169eba4d44aSLiu Zhe 		assertFalse("Chart legend has not been disabled in " + fileType + " file.", result);
170eba4d44aSLiu Zhe 
171eba4d44aSLiu Zhe 	}
172eba4d44aSLiu Zhe 
173eba4d44aSLiu Zhe 	/**
174eba4d44aSLiu Zhe 	 * Check change the position of legend in chart
175eba4d44aSLiu Zhe 	 * 1. Create a spreadsheet file.
176eba4d44aSLiu Zhe 	 * 2. Input number in a cell range.
177eba4d44aSLiu Zhe 	 * 3. Use the data to create a chart, set the chart type.
178eba4d44aSLiu Zhe 	 * 4. Change the position of legend in chart.
179eba4d44aSLiu Zhe 	 * 5. Save file as ODF/MSBinary format.
180eba4d44aSLiu Zhe 	 * 6. Close and reopen file.  -> Check the legend position.
181eba4d44aSLiu Zhe 	 * @throws Exception
182eba4d44aSLiu Zhe 	 */
183eba4d44aSLiu Zhe 	@Test
testLegendPosition()184eba4d44aSLiu Zhe 	public void testLegendPosition() throws Exception {
185eba4d44aSLiu Zhe 		String fileName = "testDisableLegend";
186eba4d44aSLiu Zhe 		String chartName = "testChart";
187eba4d44aSLiu Zhe 		String cellRangeName = "A1:D4";
188eba4d44aSLiu Zhe 		Boolean result = true;
189*ca86e9e5SHerbert Dürr 		int delta = 1; // tolerate legend position changes from integer rounding
190eba4d44aSLiu Zhe 
191eba4d44aSLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
192*ca86e9e5SHerbert Dürr 			delta = 4; // increase tolerance for legend position changes in the XLS roundtrip
193eba4d44aSLiu Zhe 			chartName = "Object 1";
194eba4d44aSLiu Zhe 		}
195eba4d44aSLiu Zhe 
196eba4d44aSLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
197eba4d44aSLiu Zhe 
198eba4d44aSLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
199eba4d44aSLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
200eba4d44aSLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
201eba4d44aSLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
202eba4d44aSLiu Zhe 		XChartDocument xChartDocument = null;
203eba4d44aSLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
204eba4d44aSLiu Zhe 
205eba4d44aSLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
206eba4d44aSLiu Zhe 
207eba4d44aSLiu Zhe 		XShape legend = xChartDocument.getLegend();
208eba4d44aSLiu Zhe 		Point aPoint = legend.getPosition();
209eba4d44aSLiu Zhe 		aPoint = new Point(aPoint.X + offset[0], aPoint.Y + offset[1]);
210eba4d44aSLiu Zhe 		legend.setPosition(aPoint);
211eba4d44aSLiu Zhe 
212eba4d44aSLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
213eba4d44aSLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
214eba4d44aSLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
215eba4d44aSLiu Zhe 
216eba4d44aSLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
217eba4d44aSLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasLegend");
218eba4d44aSLiu Zhe 		legend = xChartDocument.getLegend();
219eba4d44aSLiu Zhe 		Point resultPoint = legend.getPosition();
220eba4d44aSLiu Zhe 
221eba4d44aSLiu Zhe 		SCUtil.closeFile(scDocument);
222eba4d44aSLiu Zhe 
223eba4d44aSLiu Zhe 		assertTrue("Chart legend has not been enabled in ." + fileType + " file.", result);
224eba4d44aSLiu Zhe 
225eba4d44aSLiu Zhe 		assertEquals("Incorrect chart legend position X got in ." + fileType + " file.", aPoint.X, resultPoint.X, delta);
226*ca86e9e5SHerbert Dürr 		assertEquals("Incorrect chart legend position Y got in ." + fileType + " file.", aPoint.Y, resultPoint.Y, delta);
227eba4d44aSLiu Zhe 	}
228eba4d44aSLiu Zhe 
229eba4d44aSLiu Zhe }
230