15e5a8699SLiu Zhe /**************************************************************
25e5a8699SLiu Zhe  *
35e5a8699SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
45e5a8699SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
55e5a8699SLiu Zhe  * distributed with this work for additional information
65e5a8699SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
75e5a8699SLiu Zhe  * to you under the Apache License, Version 2.0 (the
85e5a8699SLiu Zhe  * "License"); you may not use this file except in compliance
95e5a8699SLiu Zhe  * with the License.  You may obtain a copy of the License at
105e5a8699SLiu Zhe  *
115e5a8699SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
125e5a8699SLiu Zhe  *
135e5a8699SLiu Zhe  * Unless required by applicable law or agreed to in writing,
145e5a8699SLiu Zhe  * software distributed under the License is distributed on an
155e5a8699SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165e5a8699SLiu Zhe  * KIND, either express or implied.  See the License for the
175e5a8699SLiu Zhe  * specific language governing permissions and limitations
185e5a8699SLiu Zhe  * under the License.
195e5a8699SLiu Zhe  *
205e5a8699SLiu Zhe  *************************************************************/
215e5a8699SLiu Zhe 
22*eba4d44aSLiu Zhe package fvt.uno.sc.chart;
235e5a8699SLiu Zhe 
245e5a8699SLiu Zhe import static org.junit.Assert.assertEquals;
255e5a8699SLiu Zhe 
265e5a8699SLiu Zhe import java.util.Arrays;
275e5a8699SLiu Zhe import java.util.Collection;
285e5a8699SLiu Zhe 
295e5a8699SLiu Zhe import org.junit.After;
305e5a8699SLiu Zhe import org.junit.AfterClass;
315e5a8699SLiu Zhe import org.junit.Before;
325e5a8699SLiu Zhe import org.junit.BeforeClass;
335e5a8699SLiu Zhe import org.junit.Test;
345e5a8699SLiu Zhe import org.junit.runner.RunWith;
355e5a8699SLiu Zhe import org.junit.runners.Parameterized;
365e5a8699SLiu Zhe import org.junit.runners.Parameterized.Parameters;
375e5a8699SLiu Zhe import org.openoffice.test.uno.UnoApp;
385e5a8699SLiu Zhe 
395e5a8699SLiu Zhe import testlib.uno.SCUtil;
405e5a8699SLiu Zhe 
415e5a8699SLiu Zhe import com.sun.star.awt.Rectangle;
425e5a8699SLiu Zhe import com.sun.star.chart.ChartRegressionCurveType;
435e5a8699SLiu Zhe import com.sun.star.chart.XChartDocument;
445e5a8699SLiu Zhe import com.sun.star.chart.XDiagram;
455e5a8699SLiu Zhe import com.sun.star.lang.XComponent;
465e5a8699SLiu Zhe import com.sun.star.sheet.XSpreadsheet;
475e5a8699SLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
485e5a8699SLiu Zhe import com.sun.star.table.CellRangeAddress;
495e5a8699SLiu Zhe 
505e5a8699SLiu Zhe /**
515e5a8699SLiu Zhe  *  Check trend line in chart can be applied and saved
525e5a8699SLiu Zhe  *
535e5a8699SLiu Zhe  */
545e5a8699SLiu Zhe @RunWith(value = Parameterized.class)
555e5a8699SLiu Zhe public class ChartTrendline {
565e5a8699SLiu Zhe 
575e5a8699SLiu Zhe 	private ChartRegressionCurveType expected;
585e5a8699SLiu Zhe 	private ChartRegressionCurveType trendlineType;
595e5a8699SLiu Zhe 	private String inputType;
605e5a8699SLiu Zhe 	private double[][] numberData;
615e5a8699SLiu Zhe 	private String fileType;
625e5a8699SLiu Zhe 
635e5a8699SLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
645e5a8699SLiu Zhe 
655e5a8699SLiu Zhe 	XComponent scComponent = null;
665e5a8699SLiu Zhe 	XSpreadsheetDocument scDocument = null;
675e5a8699SLiu Zhe 
685e5a8699SLiu Zhe 	@Parameters
data()695e5a8699SLiu Zhe 	public static Collection<Object[]> data() throws Exception {
705e5a8699SLiu Zhe 		double[][] numberData1 = {
715e5a8699SLiu Zhe 				{10, 20, 30, 40},
725e5a8699SLiu Zhe 				{20, 40.3, 50, 80},
735e5a8699SLiu Zhe 				{40, 20, 30, 10},
745e5a8699SLiu Zhe 				{10, -10, 0, -30}
755e5a8699SLiu Zhe 		};
765e5a8699SLiu Zhe 
775e5a8699SLiu Zhe 		return Arrays.asList(new Object[][] {
785e5a8699SLiu Zhe 			{ChartRegressionCurveType.NONE, ChartRegressionCurveType.NONE, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
795e5a8699SLiu Zhe 			{ChartRegressionCurveType.LINEAR, ChartRegressionCurveType.LINEAR, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
805e5a8699SLiu Zhe 			{ChartRegressionCurveType.LOGARITHM, ChartRegressionCurveType.LOGARITHM, "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
815e5a8699SLiu Zhe 			{ChartRegressionCurveType.EXPONENTIAL, ChartRegressionCurveType.EXPONENTIAL, "com.sun.star.chart.XYDiagram", numberData1, "ods"},
825e5a8699SLiu Zhe 			{ChartRegressionCurveType.POWER, ChartRegressionCurveType.POWER, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
835e5a8699SLiu Zhe 
845e5a8699SLiu Zhe 			{ChartRegressionCurveType.NONE, ChartRegressionCurveType.NONE, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
855e5a8699SLiu Zhe 			{ChartRegressionCurveType.LINEAR, ChartRegressionCurveType.LINEAR, "com.sun.star.chart.XYDiagram", numberData1, "xls"},
865e5a8699SLiu Zhe 			{ChartRegressionCurveType.LOGARITHM, ChartRegressionCurveType.LOGARITHM, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
875e5a8699SLiu Zhe 			{ChartRegressionCurveType.EXPONENTIAL, ChartRegressionCurveType.EXPONENTIAL, "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
885e5a8699SLiu Zhe 			{ChartRegressionCurveType.POWER, ChartRegressionCurveType.POWER, "com.sun.star.chart.BarDiagram", numberData1, "xls"}
895e5a8699SLiu Zhe 
905e5a8699SLiu Zhe 		});
915e5a8699SLiu Zhe 	}
925e5a8699SLiu Zhe 
ChartTrendline(ChartRegressionCurveType expected, ChartRegressionCurveType trendlineType, String inputType, double[][] numberData, String fileType)935e5a8699SLiu Zhe 	public ChartTrendline(ChartRegressionCurveType expected, ChartRegressionCurveType trendlineType, String inputType, double[][] numberData, String fileType) {
945e5a8699SLiu Zhe 		this.expected = expected;
955e5a8699SLiu Zhe 		this.trendlineType = trendlineType;
965e5a8699SLiu Zhe 		this.inputType = inputType;
975e5a8699SLiu Zhe 		this.numberData = numberData;
985e5a8699SLiu Zhe 		this.fileType = fileType;
995e5a8699SLiu Zhe 	}
1005e5a8699SLiu Zhe 
1015e5a8699SLiu Zhe 	@Before
setUp()1025e5a8699SLiu Zhe 	public void setUp() throws Exception {
1035e5a8699SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
1045e5a8699SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
1055e5a8699SLiu Zhe 	}
1065e5a8699SLiu Zhe 
1075e5a8699SLiu Zhe 	@After
tearDown()1085e5a8699SLiu Zhe 	public void tearDown() throws Exception {
1095e5a8699SLiu Zhe 		unoApp.closeDocument(scComponent);
1105e5a8699SLiu Zhe 
1115e5a8699SLiu Zhe 	}
1125e5a8699SLiu Zhe 
1135e5a8699SLiu Zhe 	@BeforeClass
setUpConnection()1145e5a8699SLiu Zhe 	public static void setUpConnection() throws Exception {
1155e5a8699SLiu Zhe 		unoApp.start();
1165e5a8699SLiu Zhe 	}
1175e5a8699SLiu Zhe 
1185e5a8699SLiu Zhe 	@AfterClass
tearDownConnection()1195e5a8699SLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
1205e5a8699SLiu Zhe 		unoApp.close();
1215e5a8699SLiu Zhe 		SCUtil.clearTempDir();
1225e5a8699SLiu Zhe 	}
1235e5a8699SLiu Zhe 
1245e5a8699SLiu Zhe 	/**
1255e5a8699SLiu Zhe 	 * Enable different types of trend line in chart.
1265e5a8699SLiu Zhe 	 * 1. Create a spreadsheet file.
1275e5a8699SLiu Zhe 	 * 2. Input number in a cell range and create a 2D chart.
1285e5a8699SLiu Zhe 	 * 3. Enable trend line in chart.
1295e5a8699SLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
1305e5a8699SLiu Zhe 	 * 5. Close and reopen file.  -> Check the trend line setting.
1315e5a8699SLiu Zhe 	 * @throws Exception
1325e5a8699SLiu Zhe 	 */
1335e5a8699SLiu Zhe 	@Test
testCreateTrendline()1345e5a8699SLiu Zhe 	public void testCreateTrendline() throws Exception {
1355e5a8699SLiu Zhe 		String fileName = "testCreateTrendline";
1365e5a8699SLiu Zhe 		String chartName = "testChart";
1375e5a8699SLiu Zhe 		String cellRangeName = "A1:D4";
1385e5a8699SLiu Zhe 		ChartRegressionCurveType result = null;
1395e5a8699SLiu Zhe 
1405e5a8699SLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
1415e5a8699SLiu Zhe 			cellRangeName = "A1:C4";
1425e5a8699SLiu Zhe 		}
1435e5a8699SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
1445e5a8699SLiu Zhe 			chartName = "Object 1";
1455e5a8699SLiu Zhe 		}
1465e5a8699SLiu Zhe 
1475e5a8699SLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
1485e5a8699SLiu Zhe 
1495e5a8699SLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
1505e5a8699SLiu Zhe 
1515e5a8699SLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
1525e5a8699SLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
1535e5a8699SLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
1545e5a8699SLiu Zhe 		XChartDocument xChartDocument = null;
1555e5a8699SLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
1565e5a8699SLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
1575e5a8699SLiu Zhe 		XDiagram xDiagram = xChartDocument.getDiagram();
1585e5a8699SLiu Zhe 
1595e5a8699SLiu Zhe 		SCUtil.setProperties(xDiagram, "RegressionCurves", trendlineType);
1605e5a8699SLiu Zhe 
1615e5a8699SLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
1625e5a8699SLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
1635e5a8699SLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
1645e5a8699SLiu Zhe 
1655e5a8699SLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
1665e5a8699SLiu Zhe 		xDiagram = xChartDocument.getDiagram();
1675e5a8699SLiu Zhe 		result = (ChartRegressionCurveType) SCUtil.getProperties(xDiagram, "RegressionCurves");
1685e5a8699SLiu Zhe 
1695e5a8699SLiu Zhe 		SCUtil.closeFile(scDocument);
1705e5a8699SLiu Zhe 
1715e5a8699SLiu Zhe 		assertEquals("Incorrect chart trendline got in ." + fileType + " file.", expected, result);
1725e5a8699SLiu Zhe 
1735e5a8699SLiu Zhe 	}
1745e5a8699SLiu Zhe 
1755e5a8699SLiu Zhe }