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