xref: /AOO41X/test/testuno/source/fvt/uno/sc/chart/ChartMeanValueLine.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.assertFalse;
25*eba4d44aSLiu Zhe import static org.junit.Assert.assertTrue;
26*eba4d44aSLiu Zhe 
27*eba4d44aSLiu Zhe import java.util.Arrays;
28*eba4d44aSLiu Zhe import java.util.Collection;
29*eba4d44aSLiu Zhe 
30*eba4d44aSLiu Zhe import org.junit.After;
31*eba4d44aSLiu Zhe import org.junit.AfterClass;
32*eba4d44aSLiu Zhe import org.junit.Before;
33*eba4d44aSLiu Zhe import org.junit.BeforeClass;
34*eba4d44aSLiu Zhe import org.junit.Test;
35*eba4d44aSLiu Zhe import org.junit.runner.RunWith;
36*eba4d44aSLiu Zhe import org.junit.runners.Parameterized;
37*eba4d44aSLiu Zhe import org.junit.runners.Parameterized.Parameters;
38*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
39*eba4d44aSLiu Zhe 
40*eba4d44aSLiu Zhe import testlib.uno.SCUtil;
41*eba4d44aSLiu Zhe 
42*eba4d44aSLiu Zhe import com.sun.star.awt.Rectangle;
43*eba4d44aSLiu Zhe import com.sun.star.chart.XChartDocument;
44*eba4d44aSLiu Zhe import com.sun.star.chart.XDiagram;
45*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
46*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheet;
47*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
48*eba4d44aSLiu Zhe import com.sun.star.table.CellRangeAddress;
49*eba4d44aSLiu Zhe 
50*eba4d44aSLiu Zhe /**
51*eba4d44aSLiu Zhe  *  Check mean value line in chart can be applied and saved
52*eba4d44aSLiu Zhe  *
53*eba4d44aSLiu Zhe  */
54*eba4d44aSLiu Zhe @RunWith(value = Parameterized.class)
55*eba4d44aSLiu Zhe public class ChartMeanValueLine {
56*eba4d44aSLiu Zhe 
57*eba4d44aSLiu Zhe 	private Boolean expected;
58*eba4d44aSLiu Zhe 	private Boolean meanValueLine;
59*eba4d44aSLiu Zhe 	private String inputType;
60*eba4d44aSLiu Zhe 	private double[][] numberData;
61*eba4d44aSLiu Zhe 	private String fileType;
62*eba4d44aSLiu Zhe 
63*eba4d44aSLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
64*eba4d44aSLiu Zhe 
65*eba4d44aSLiu Zhe 	XComponent scComponent = null;
66*eba4d44aSLiu Zhe 	XSpreadsheetDocument scDocument = null;
67*eba4d44aSLiu Zhe 
68*eba4d44aSLiu Zhe 	@Parameters
data()69*eba4d44aSLiu Zhe 	public static Collection<Object[]> data() throws Exception {
70*eba4d44aSLiu Zhe 		double[][] numberData1 = {
71*eba4d44aSLiu Zhe 				{10, 20, 30, 40},
72*eba4d44aSLiu Zhe 				{20, 40.3, 50, 80},
73*eba4d44aSLiu Zhe 				{40, 20, 30, 10},
74*eba4d44aSLiu Zhe 				{10, -10, 0, -30}
75*eba4d44aSLiu Zhe 		};
76*eba4d44aSLiu Zhe 
77*eba4d44aSLiu Zhe 		return Arrays.asList(new Object[][] {
78*eba4d44aSLiu Zhe 			{true, true, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
79*eba4d44aSLiu Zhe 			{true, true, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
80*eba4d44aSLiu Zhe 			{true, true, "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
81*eba4d44aSLiu Zhe 			{true, true, "com.sun.star.chart.XYDiagram", numberData1, "ods"},
82*eba4d44aSLiu Zhe 
83*eba4d44aSLiu Zhe 			{false, true, "com.sun.star.chart.BarDiagram", numberData1, "xls"}, //Excel does not support this property, save as .xls, the setting will be lost.
84*eba4d44aSLiu Zhe 			{false, true, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
85*eba4d44aSLiu Zhe 			{false, true, "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
86*eba4d44aSLiu Zhe 			{false, true, "com.sun.star.chart.XYDiagram", numberData1, "xls"}
87*eba4d44aSLiu Zhe 		});
88*eba4d44aSLiu Zhe 	}
89*eba4d44aSLiu Zhe 
ChartMeanValueLine(Boolean expected, Boolean meanValueLine, String inputType, double[][] numberData, String fileType)90*eba4d44aSLiu Zhe 	public ChartMeanValueLine(Boolean expected, Boolean meanValueLine, String inputType, double[][] numberData, String fileType) {
91*eba4d44aSLiu Zhe 		this.expected = expected;
92*eba4d44aSLiu Zhe 		this.meanValueLine = meanValueLine;
93*eba4d44aSLiu Zhe 		this.inputType = inputType;
94*eba4d44aSLiu Zhe 		this.numberData = numberData;
95*eba4d44aSLiu Zhe 		this.fileType = fileType;
96*eba4d44aSLiu Zhe 	}
97*eba4d44aSLiu Zhe 
98*eba4d44aSLiu Zhe 	@Before
setUp()99*eba4d44aSLiu Zhe 	public void setUp() throws Exception {
100*eba4d44aSLiu Zhe 		scComponent = unoApp.newDocument("scalc");
101*eba4d44aSLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
102*eba4d44aSLiu Zhe 	}
103*eba4d44aSLiu Zhe 
104*eba4d44aSLiu Zhe 	@After
tearDown()105*eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
106*eba4d44aSLiu Zhe 		unoApp.closeDocument(scComponent);
107*eba4d44aSLiu Zhe 
108*eba4d44aSLiu Zhe 	}
109*eba4d44aSLiu Zhe 
110*eba4d44aSLiu Zhe 	@BeforeClass
setUpConnection()111*eba4d44aSLiu Zhe 	public static void setUpConnection() throws Exception {
112*eba4d44aSLiu Zhe 		unoApp.start();
113*eba4d44aSLiu Zhe 	}
114*eba4d44aSLiu Zhe 
115*eba4d44aSLiu Zhe 	@AfterClass
tearDownConnection()116*eba4d44aSLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
117*eba4d44aSLiu Zhe 		unoApp.close();
118*eba4d44aSLiu Zhe 		SCUtil.clearTempDir();
119*eba4d44aSLiu Zhe 	}
120*eba4d44aSLiu Zhe 
121*eba4d44aSLiu Zhe 	/**
122*eba4d44aSLiu Zhe 	 * Enable different types of mean value line in chart.
123*eba4d44aSLiu Zhe 	 * 1. Create a spreadsheet file.
124*eba4d44aSLiu Zhe 	 * 2. Input number in a cell range and create a 2D chart.
125*eba4d44aSLiu Zhe 	 * 3. Enable mean value line in chart.
126*eba4d44aSLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
127*eba4d44aSLiu Zhe 	 * 5. Close and reopen file.  -> Check the mean value line setting.
128*eba4d44aSLiu Zhe 	 * @throws Exception
129*eba4d44aSLiu Zhe 	 */
130*eba4d44aSLiu Zhe 	@Test
testCreateTrendline()131*eba4d44aSLiu Zhe 	public void testCreateTrendline() throws Exception {
132*eba4d44aSLiu Zhe 		String fileName = "testCreateMeanValueline";
133*eba4d44aSLiu Zhe 		String chartName = "testChart";
134*eba4d44aSLiu Zhe 		String cellRangeName = "A1:D4";
135*eba4d44aSLiu Zhe 		Boolean result = null;
136*eba4d44aSLiu Zhe 
137*eba4d44aSLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
138*eba4d44aSLiu Zhe 			cellRangeName = "A1:C4";
139*eba4d44aSLiu Zhe 		}
140*eba4d44aSLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
141*eba4d44aSLiu Zhe 			chartName = "Object 1";
142*eba4d44aSLiu Zhe 		}
143*eba4d44aSLiu Zhe 
144*eba4d44aSLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
145*eba4d44aSLiu Zhe 
146*eba4d44aSLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
147*eba4d44aSLiu Zhe 
148*eba4d44aSLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
149*eba4d44aSLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
150*eba4d44aSLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
151*eba4d44aSLiu Zhe 		XChartDocument xChartDocument = null;
152*eba4d44aSLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
153*eba4d44aSLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
154*eba4d44aSLiu Zhe 		XDiagram xDiagram = xChartDocument.getDiagram();
155*eba4d44aSLiu Zhe 
156*eba4d44aSLiu Zhe 		SCUtil.setProperties(xDiagram, "MeanValue", meanValueLine);
157*eba4d44aSLiu Zhe 
158*eba4d44aSLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
159*eba4d44aSLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
160*eba4d44aSLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
161*eba4d44aSLiu Zhe 
162*eba4d44aSLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
163*eba4d44aSLiu Zhe 		xDiagram = xChartDocument.getDiagram();
164*eba4d44aSLiu Zhe 		result = (Boolean) SCUtil.getProperties(xDiagram, "MeanValue");
165*eba4d44aSLiu Zhe 
166*eba4d44aSLiu Zhe 		SCUtil.closeFile(scDocument);
167*eba4d44aSLiu Zhe 
168*eba4d44aSLiu Zhe 		if (expected) {
169*eba4d44aSLiu Zhe 			assertTrue("Incorrect chart trendline got in ." + fileType + " file.", result);
170*eba4d44aSLiu Zhe 		}
171*eba4d44aSLiu Zhe 		else {
172*eba4d44aSLiu Zhe 			assertFalse("Incorrect chart trendline got in ." + fileType + " file.", result);
173*eba4d44aSLiu Zhe 		}
174*eba4d44aSLiu Zhe 
175*eba4d44aSLiu Zhe 	}
176*eba4d44aSLiu Zhe 
177*eba4d44aSLiu Zhe }