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 
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.ChartRegressionCurveType;
43 import com.sun.star.chart.XChartDocument;
44 import com.sun.star.chart.XDiagram;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.sheet.XSpreadsheet;
47 import com.sun.star.sheet.XSpreadsheetDocument;
48 import com.sun.star.table.CellRangeAddress;
49 
50 /**
51  *  Check trend line in chart can be applied and saved
52  *
53  */
54 @RunWith(value = Parameterized.class)
55 public class ChartTrendline {
56 
57 	private ChartRegressionCurveType expected;
58 	private ChartRegressionCurveType trendlineType;
59 	private String inputType;
60 	private double[][] numberData;
61 	private String fileType;
62 
63 	private static final UnoApp unoApp = new UnoApp();
64 
65 	XComponent scComponent = null;
66 	XSpreadsheetDocument scDocument = null;
67 
68 	@Parameters
data()69 	public static Collection<Object[]> data() throws Exception {
70 		double[][] numberData1 = {
71 				{10, 20, 30, 40},
72 				{20, 40.3, 50, 80},
73 				{40, 20, 30, 10},
74 				{10, -10, 0, -30}
75 		};
76 
77 		return Arrays.asList(new Object[][] {
78 			{ChartRegressionCurveType.NONE, ChartRegressionCurveType.NONE, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
79 			{ChartRegressionCurveType.LINEAR, ChartRegressionCurveType.LINEAR, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
80 			{ChartRegressionCurveType.LOGARITHM, ChartRegressionCurveType.LOGARITHM, "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
81 			{ChartRegressionCurveType.EXPONENTIAL, ChartRegressionCurveType.EXPONENTIAL, "com.sun.star.chart.XYDiagram", numberData1, "ods"},
82 			{ChartRegressionCurveType.POWER, ChartRegressionCurveType.POWER, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
83 
84 			{ChartRegressionCurveType.NONE, ChartRegressionCurveType.NONE, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
85 			{ChartRegressionCurveType.LINEAR, ChartRegressionCurveType.LINEAR, "com.sun.star.chart.XYDiagram", numberData1, "xls"},
86 			{ChartRegressionCurveType.LOGARITHM, ChartRegressionCurveType.LOGARITHM, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
87 			{ChartRegressionCurveType.EXPONENTIAL, ChartRegressionCurveType.EXPONENTIAL, "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
88 			{ChartRegressionCurveType.POWER, ChartRegressionCurveType.POWER, "com.sun.star.chart.BarDiagram", numberData1, "xls"}
89 
90 		});
91 	}
92 
ChartTrendline(ChartRegressionCurveType expected, ChartRegressionCurveType trendlineType, String inputType, double[][] numberData, String fileType)93 	public ChartTrendline(ChartRegressionCurveType expected, ChartRegressionCurveType trendlineType, String inputType, double[][] numberData, String fileType) {
94 		this.expected = expected;
95 		this.trendlineType = trendlineType;
96 		this.inputType = inputType;
97 		this.numberData = numberData;
98 		this.fileType = fileType;
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 	 * Enable different types of trend line in chart.
126 	 * 1. Create a spreadsheet file.
127 	 * 2. Input number in a cell range and create a 2D chart.
128 	 * 3. Enable trend line in chart.
129 	 * 4. Save file as ODF/MSBinary format.
130 	 * 5. Close and reopen file.  -> Check the trend line setting.
131 	 * @throws Exception
132 	 */
133 	@Test
testCreateTrendline()134 	public void testCreateTrendline() throws Exception {
135 		String fileName = "testCreateTrendline";
136 		String chartName = "testChart";
137 		String cellRangeName = "A1:D4";
138 		ChartRegressionCurveType result = null;
139 
140 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
141 			cellRangeName = "A1:C4";
142 		}
143 		if (fileType.equalsIgnoreCase("xls")) {
144 			chartName = "Object 1";
145 		}
146 
147 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
148 
149 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
150 
151 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
152 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
153 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
154 		XChartDocument xChartDocument = null;
155 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
156 		SCUtil.setChartType(xChartDocument, inputType);
157 		XDiagram xDiagram = xChartDocument.getDiagram();
158 
159 		SCUtil.setProperties(xDiagram, "RegressionCurves", trendlineType);
160 
161 		SCUtil.saveFileAs(scComponent, fileName, fileType);
162 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
163 		sheet = SCUtil.getCurrentSheet(scDocument);
164 
165 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
166 		xDiagram = xChartDocument.getDiagram();
167 		result = (ChartRegressionCurveType) SCUtil.getProperties(xDiagram, "RegressionCurves");
168 
169 		SCUtil.closeFile(scDocument);
170 
171 		assertEquals("Incorrect chart trendline got in ." + fileType + " file.", expected, result);
172 
173 	}
174 
175 }