xref: /AOO41X/test/testuno/source/fvt/uno/sc/chart/ChartDataLabel.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
1*eba4d44aSLiu Zhe /**************************************************************
2*eba4d44aSLiu Zhe  *
3*eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*eba4d44aSLiu Zhe  * distributed with this work for additional information
6*eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9*eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*eba4d44aSLiu Zhe  *
11*eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*eba4d44aSLiu Zhe  *
13*eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15*eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17*eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18*eba4d44aSLiu Zhe  * under the License.
19*eba4d44aSLiu Zhe  *
20*eba4d44aSLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe 
22*eba4d44aSLiu Zhe package fvt.uno.sc.chart;
23*eba4d44aSLiu Zhe 
24*eba4d44aSLiu Zhe import static org.junit.Assert.assertEquals;
25*eba4d44aSLiu Zhe 
26*eba4d44aSLiu Zhe import java.util.Arrays;
27*eba4d44aSLiu Zhe import java.util.Collection;
28*eba4d44aSLiu Zhe 
29*eba4d44aSLiu Zhe import org.junit.After;
30*eba4d44aSLiu Zhe import org.junit.AfterClass;
31*eba4d44aSLiu Zhe import org.junit.Before;
32*eba4d44aSLiu Zhe import org.junit.BeforeClass;
33*eba4d44aSLiu Zhe import org.junit.Test;
34*eba4d44aSLiu Zhe import org.junit.runner.RunWith;
35*eba4d44aSLiu Zhe import org.junit.runners.Parameterized;
36*eba4d44aSLiu Zhe import org.junit.runners.Parameterized.Parameters;
37*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
38*eba4d44aSLiu Zhe 
39*eba4d44aSLiu Zhe import testlib.uno.SCUtil;
40*eba4d44aSLiu Zhe 
41*eba4d44aSLiu Zhe import com.sun.star.awt.Rectangle;
42*eba4d44aSLiu Zhe import com.sun.star.chart.XChartDocument;
43*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
44*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
45*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
46*eba4d44aSLiu Zhe import com.sun.star.table.CellRangeAddress;
47*eba4d44aSLiu Zhe 
48*eba4d44aSLiu Zhe /**
49*eba4d44aSLiu Zhe  *  Check different types of chart data labels can be applied and saved
50*eba4d44aSLiu Zhe  *
51*eba4d44aSLiu Zhe  */
52*eba4d44aSLiu Zhe @RunWith(value = Parameterized.class)
53*eba4d44aSLiu Zhe public class ChartDataLabel {
54*eba4d44aSLiu Zhe 
55*eba4d44aSLiu Zhe 	private int labelType;
56*eba4d44aSLiu Zhe 	private String inputType;
57*eba4d44aSLiu Zhe 	private double[][] numberData;
58*eba4d44aSLiu Zhe 	private String fileType;
59*eba4d44aSLiu Zhe 
60*eba4d44aSLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
61*eba4d44aSLiu Zhe 
62*eba4d44aSLiu Zhe 	XComponent scComponent = null;
63*eba4d44aSLiu Zhe 	XSpreadsheetDocument scDocument = null;
64*eba4d44aSLiu Zhe 
65*eba4d44aSLiu Zhe 	@Parameters
data()66*eba4d44aSLiu Zhe 	public static Collection<Object[]> data() throws Exception {
67*eba4d44aSLiu Zhe 		double[][] numberData1 = {
68*eba4d44aSLiu Zhe 				{1, 2, 3, 4},
69*eba4d44aSLiu Zhe 				{2, 4.3, 5, 8},
70*eba4d44aSLiu Zhe 				{4, 2, 3, 1},
71*eba4d44aSLiu Zhe 				{1, -1, 0, -3}
72*eba4d44aSLiu Zhe 		};
73*eba4d44aSLiu Zhe 
74*eba4d44aSLiu Zhe 		return Arrays.asList(new Object[][] {
75*eba4d44aSLiu Zhe 			{0, "com.sun.star.chart.XYDiagram", numberData1, "ods"}, // no label
76*eba4d44aSLiu Zhe 			{1, "com.sun.star.chart.PieDiagram", numberData1, "ods"}, // show number
77*eba4d44aSLiu Zhe 			{2, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show percentage
78*eba4d44aSLiu Zhe 			{4, "com.sun.star.chart.LineDiagram", numberData1, "ods"}, // show category name
79*eba4d44aSLiu Zhe 			{16, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // check legend symbol (won't shown on UI)
80*eba4d44aSLiu Zhe 			{3, "com.sun.star.chart.DonutDiagram", numberData1, "ods"}, // show number & percentage
81*eba4d44aSLiu Zhe 			{5, "com.sun.star.chart.BubbleDiagram", numberData1, "ods"}, // show number & category name
82*eba4d44aSLiu Zhe 			{6, "com.sun.star.chart.NetDiagram", numberData1, "ods"}, // show percentage & category name
83*eba4d44aSLiu Zhe 			{7, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show number & percentage & category name
84*eba4d44aSLiu Zhe 			{17, "com.sun.star.chart.LineDiagram", numberData1, "ods"}, // show number & legend symbol
85*eba4d44aSLiu Zhe 			{18, "com.sun.star.chart.XYDiagram", numberData1, "ods"}, // show percentage & legend symbol
86*eba4d44aSLiu Zhe 			{19, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show number & percentage & legend symbol
87*eba4d44aSLiu Zhe 			{20, "com.sun.star.chart.NetDiagram", numberData1, "ods"}, // show category name & legend symbol
88*eba4d44aSLiu Zhe 			{21, "com.sun.star.chart.AreaDiagram", numberData1, "ods"}, // show number & Category name & legend symbol
89*eba4d44aSLiu Zhe 			{22, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show percentage & Category name & legend symbol
90*eba4d44aSLiu Zhe 			{23, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show number & percentage & & Category name & legend symbol
91*eba4d44aSLiu Zhe 			{0, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
92*eba4d44aSLiu Zhe 			{1, "com.sun.star.chart.PieDiagram", numberData1, "xls"},
93*eba4d44aSLiu Zhe 			{4, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
94*eba4d44aSLiu Zhe 			{5, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
95*eba4d44aSLiu Zhe 			{17, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
96*eba4d44aSLiu Zhe 			{20, "com.sun.star.chart.BubbleDiagram", numberData1, "xls"},
97*eba4d44aSLiu Zhe 			{21, "com.sun.star.chart.BarDiagram", numberData1, "xls"}
98*eba4d44aSLiu Zhe 		});
99*eba4d44aSLiu Zhe 	}
100*eba4d44aSLiu Zhe 
ChartDataLabel(int labelType, String inputType, double[][] numberData, String fileType)101*eba4d44aSLiu Zhe 	public ChartDataLabel(int labelType, String inputType, double[][] numberData, String fileType) {
102*eba4d44aSLiu Zhe 		this.labelType = labelType;
103*eba4d44aSLiu Zhe 		this.inputType = inputType;
104*eba4d44aSLiu Zhe 		this.numberData = numberData;
105*eba4d44aSLiu Zhe 		this.fileType = fileType;
106*eba4d44aSLiu Zhe 	}
107*eba4d44aSLiu Zhe 
108*eba4d44aSLiu Zhe 
109*eba4d44aSLiu Zhe 	@Before
setUp()110*eba4d44aSLiu Zhe 	public void setUp() throws Exception {
111*eba4d44aSLiu Zhe 		scComponent = unoApp.newDocument("scalc");
112*eba4d44aSLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
113*eba4d44aSLiu Zhe 	}
114*eba4d44aSLiu Zhe 
115*eba4d44aSLiu Zhe 	@After
tearDown()116*eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
117*eba4d44aSLiu Zhe 		unoApp.closeDocument(scComponent);
118*eba4d44aSLiu Zhe 
119*eba4d44aSLiu Zhe 	}
120*eba4d44aSLiu Zhe 
121*eba4d44aSLiu Zhe 	@BeforeClass
setUpConnection()122*eba4d44aSLiu Zhe 	public static void setUpConnection() throws Exception {
123*eba4d44aSLiu Zhe 		unoApp.start();
124*eba4d44aSLiu Zhe 	}
125*eba4d44aSLiu Zhe 
126*eba4d44aSLiu Zhe 	@AfterClass
tearDownConnection()127*eba4d44aSLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
128*eba4d44aSLiu Zhe 		unoApp.close();
129*eba4d44aSLiu Zhe 		SCUtil.clearTempDir();
130*eba4d44aSLiu Zhe 	}
131*eba4d44aSLiu Zhe 
132*eba4d44aSLiu Zhe 	/**
133*eba4d44aSLiu Zhe 	 * Enable different types of data labels in chart.
134*eba4d44aSLiu Zhe 	 * 1. Create a spreadsheet file.
135*eba4d44aSLiu Zhe 	 * 2. Input number in a cell range and create a chart.
136*eba4d44aSLiu Zhe 	 * 3. Enable different types of data labels for all data series in chart.
137*eba4d44aSLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
138*eba4d44aSLiu Zhe 	 * 5. Close and reopen file.  -> Check the chart data label setting.
139*eba4d44aSLiu Zhe 	 * @throws Exception
140*eba4d44aSLiu Zhe 	 */
141*eba4d44aSLiu Zhe 	@Test
testCreateDataLabel()142*eba4d44aSLiu Zhe 	public void testCreateDataLabel() throws Exception {
143*eba4d44aSLiu Zhe 		String fileName = "testCreateDataLabel";
144*eba4d44aSLiu Zhe 		String chartName = "testChart";
145*eba4d44aSLiu Zhe 		String cellRangeName = "A1:D4";
146*eba4d44aSLiu Zhe 		int result = 0;
147*eba4d44aSLiu Zhe 
148*eba4d44aSLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
149*eba4d44aSLiu Zhe 			cellRangeName = "A1:C4";
150*eba4d44aSLiu Zhe 		}
151*eba4d44aSLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
152*eba4d44aSLiu Zhe 			chartName = "Object 1";
153*eba4d44aSLiu Zhe 		}
154*eba4d44aSLiu Zhe 
155*eba4d44aSLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
156*eba4d44aSLiu Zhe 
157*eba4d44aSLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
158*eba4d44aSLiu Zhe 
159*eba4d44aSLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
160*eba4d44aSLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
161*eba4d44aSLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
162*eba4d44aSLiu Zhe 		XChartDocument xChartDocument = null;
163*eba4d44aSLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
164*eba4d44aSLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
165*eba4d44aSLiu Zhe 
166*eba4d44aSLiu Zhe 		SCUtil.setProperties(xChartDocument.getDiagram(), "DataCaption", labelType);
167*eba4d44aSLiu Zhe 
168*eba4d44aSLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
169*eba4d44aSLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
170*eba4d44aSLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
171*eba4d44aSLiu Zhe 
172*eba4d44aSLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
173*eba4d44aSLiu Zhe 		result = ((Integer) SCUtil.getProperties(xChartDocument.getDiagram(), "DataCaption")).intValue();
174*eba4d44aSLiu Zhe 		SCUtil.closeFile(scDocument);
175*eba4d44aSLiu Zhe 
176*eba4d44aSLiu Zhe 		assertEquals("Incorrect chart data label types got in ." + fileType + " file.", labelType, result, 0);
177*eba4d44aSLiu Zhe 
178*eba4d44aSLiu Zhe 	}
179*eba4d44aSLiu Zhe 
180*eba4d44aSLiu Zhe }