11dfd36f5SLiu Zhe /**************************************************************
21dfd36f5SLiu Zhe  *
31dfd36f5SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
41dfd36f5SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
51dfd36f5SLiu Zhe  * distributed with this work for additional information
61dfd36f5SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
71dfd36f5SLiu Zhe  * to you under the Apache License, Version 2.0 (the
81dfd36f5SLiu Zhe  * "License"); you may not use this file except in compliance
91dfd36f5SLiu Zhe  * with the License.  You may obtain a copy of the License at
101dfd36f5SLiu Zhe  *
111dfd36f5SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
121dfd36f5SLiu Zhe  *
131dfd36f5SLiu Zhe  * Unless required by applicable law or agreed to in writing,
141dfd36f5SLiu Zhe  * software distributed under the License is distributed on an
151dfd36f5SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
161dfd36f5SLiu Zhe  * KIND, either express or implied.  See the License for the
171dfd36f5SLiu Zhe  * specific language governing permissions and limitations
181dfd36f5SLiu Zhe  * under the License.
191dfd36f5SLiu Zhe  *
201dfd36f5SLiu Zhe  *************************************************************/
211dfd36f5SLiu Zhe 
22*eba4d44aSLiu Zhe package fvt.uno.sc.chart;
231dfd36f5SLiu Zhe 
241dfd36f5SLiu Zhe import static org.junit.Assert.assertEquals;
251dfd36f5SLiu Zhe 
261dfd36f5SLiu Zhe import java.util.Arrays;
271dfd36f5SLiu Zhe import java.util.Collection;
281dfd36f5SLiu Zhe 
291dfd36f5SLiu Zhe import org.junit.After;
301dfd36f5SLiu Zhe import org.junit.AfterClass;
311dfd36f5SLiu Zhe import org.junit.Before;
321dfd36f5SLiu Zhe import org.junit.BeforeClass;
331dfd36f5SLiu Zhe import org.junit.Test;
341dfd36f5SLiu Zhe import org.junit.runner.RunWith;
351dfd36f5SLiu Zhe import org.junit.runners.Parameterized;
361dfd36f5SLiu Zhe import org.junit.runners.Parameterized.Parameters;
371dfd36f5SLiu Zhe import org.openoffice.test.uno.UnoApp;
381dfd36f5SLiu Zhe 
391dfd36f5SLiu Zhe import testlib.uno.SCUtil;
401dfd36f5SLiu Zhe import com.sun.star.awt.Rectangle;
411dfd36f5SLiu Zhe import com.sun.star.chart.XChartDocument;
421dfd36f5SLiu Zhe import com.sun.star.lang.XComponent;
431dfd36f5SLiu Zhe import com.sun.star.sheet.XSpreadsheet;
441dfd36f5SLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
451dfd36f5SLiu Zhe import com.sun.star.table.CellRangeAddress;
461dfd36f5SLiu Zhe 
471dfd36f5SLiu Zhe /**
481dfd36f5SLiu Zhe  *  Check the chart type setting can be applied and saved
491dfd36f5SLiu Zhe  *
501dfd36f5SLiu Zhe  */
511dfd36f5SLiu Zhe @RunWith(value = Parameterized.class)
521dfd36f5SLiu Zhe public class ChartType {
531dfd36f5SLiu Zhe 
541dfd36f5SLiu Zhe 	private String expected;
551dfd36f5SLiu Zhe 	private String inputType;
561dfd36f5SLiu Zhe 	private double[][] numberData;
571dfd36f5SLiu Zhe 	private String fileType;
581dfd36f5SLiu Zhe 
591dfd36f5SLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
601dfd36f5SLiu Zhe 
611dfd36f5SLiu Zhe 	XComponent scComponent = null;
621dfd36f5SLiu Zhe 	XSpreadsheetDocument scDocument = null;
631dfd36f5SLiu Zhe 
641dfd36f5SLiu Zhe 	@Parameters
data()651dfd36f5SLiu Zhe 	public static Collection<Object[]> data() throws Exception {
661dfd36f5SLiu Zhe 		double[][] numberData1 = {
671dfd36f5SLiu Zhe 				{1, 2, 3, 4},
681dfd36f5SLiu Zhe 				{2, 4.3, 5, 8},
691dfd36f5SLiu Zhe 				{4, 2, 3, 1},
701dfd36f5SLiu Zhe 				{1, -1, 0, 3}
711dfd36f5SLiu Zhe 		};
721dfd36f5SLiu Zhe 		double[][] numberData2 = {
731dfd36f5SLiu Zhe 				{22, 12, 15},
741dfd36f5SLiu Zhe 				{20, 11, 20},
751dfd36f5SLiu Zhe 				{40, 37, 38},
761dfd36f5SLiu Zhe 				{3.01, 2.2, 2.2}
771dfd36f5SLiu Zhe 		};
781dfd36f5SLiu Zhe 
791dfd36f5SLiu Zhe 		return Arrays.asList(new Object[][] {
801dfd36f5SLiu Zhe 			{"com.sun.star.chart.BarDiagram", "com.sun.star.chart.BarDiagram", numberData1, "ods"},
811dfd36f5SLiu Zhe 			{"com.sun.star.chart.AreaDiagram", "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
821dfd36f5SLiu Zhe 			{"com.sun.star.chart.LineDiagram", "com.sun.star.chart.LineDiagram", numberData1, "ods"},
831dfd36f5SLiu Zhe 			{"com.sun.star.chart.PieDiagram", "com.sun.star.chart.PieDiagram", numberData1, "ods"},
841dfd36f5SLiu Zhe 			{"com.sun.star.chart.DonutDiagram", "com.sun.star.chart.DonutDiagram", numberData1, "ods"},
851dfd36f5SLiu Zhe 			{"com.sun.star.chart.NetDiagram", "com.sun.star.chart.NetDiagram", numberData1, "ods"},
861dfd36f5SLiu Zhe 			{"com.sun.star.chart.XYDiagram", "com.sun.star.chart.XYDiagram", numberData1, "ods"},
871dfd36f5SLiu Zhe 			{"com.sun.star.chart.StockDiagram", "com.sun.star.chart.StockDiagram", numberData2, "ods"},
881dfd36f5SLiu Zhe 			{"com.sun.star.chart.BubbleDiagram", "com.sun.star.chart.BubbleDiagram", numberData1, "ods"},
891dfd36f5SLiu Zhe 
901dfd36f5SLiu Zhe 			{"com.sun.star.chart.BarDiagram", "com.sun.star.chart.BarDiagram", numberData1, "xls"},
911dfd36f5SLiu Zhe 			{"com.sun.star.chart.AreaDiagram", "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
921dfd36f5SLiu Zhe 			{"com.sun.star.chart.LineDiagram", "com.sun.star.chart.LineDiagram", numberData1, "xls"},
931dfd36f5SLiu Zhe 			{"com.sun.star.chart.PieDiagram", "com.sun.star.chart.PieDiagram", numberData1, "xls"},
941dfd36f5SLiu Zhe 			{"com.sun.star.chart.DonutDiagram", "com.sun.star.chart.DonutDiagram", numberData1, "xls"},
951dfd36f5SLiu Zhe 			{"com.sun.star.chart.NetDiagram", "com.sun.star.chart.NetDiagram", numberData1, "xls"},
961dfd36f5SLiu Zhe 			{"com.sun.star.chart.XYDiagram", "com.sun.star.chart.XYDiagram", numberData1, "xls"},
971dfd36f5SLiu Zhe 			{"com.sun.star.chart.StockDiagram", "com.sun.star.chart.StockDiagram", numberData2, "xls"},
981dfd36f5SLiu Zhe 			{"com.sun.star.chart.BubbleDiagram", "com.sun.star.chart.BubbleDiagram", numberData1, "xls"}
991dfd36f5SLiu Zhe 		});
1001dfd36f5SLiu Zhe 	}
1011dfd36f5SLiu Zhe 
ChartType(String expected, String inputType, double[][] numberData, String fileType)1021dfd36f5SLiu Zhe 	public ChartType(String expected, String inputType, double[][] numberData, String fileType) {
1031dfd36f5SLiu Zhe 		this.expected = expected;
1041dfd36f5SLiu Zhe 		this.inputType = inputType;
1051dfd36f5SLiu Zhe 		this.numberData = numberData;
1061dfd36f5SLiu Zhe 		this.fileType = fileType;
1071dfd36f5SLiu Zhe 	}
1081dfd36f5SLiu Zhe 
1091dfd36f5SLiu Zhe 
1101dfd36f5SLiu Zhe 	@Before
setUp()1111dfd36f5SLiu Zhe 	public void setUp() throws Exception {
1121dfd36f5SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
1131dfd36f5SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
1141dfd36f5SLiu Zhe 	}
1151dfd36f5SLiu Zhe 
1161dfd36f5SLiu Zhe 	@After
tearDown()1171dfd36f5SLiu Zhe 	public void tearDown() throws Exception {
1181dfd36f5SLiu Zhe 		unoApp.closeDocument(scComponent);
1191dfd36f5SLiu Zhe 
1201dfd36f5SLiu Zhe 	}
1211dfd36f5SLiu Zhe 
1221dfd36f5SLiu Zhe 	@BeforeClass
setUpConnection()1231dfd36f5SLiu Zhe 	public static void setUpConnection() throws Exception {
1241dfd36f5SLiu Zhe 		unoApp.start();
1251dfd36f5SLiu Zhe 	}
1261dfd36f5SLiu Zhe 
1271dfd36f5SLiu Zhe 	@AfterClass
tearDownConnection()1281dfd36f5SLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
1291dfd36f5SLiu Zhe 		unoApp.close();
1301dfd36f5SLiu Zhe 		SCUtil.clearTempDir();
1311dfd36f5SLiu Zhe 	}
1321dfd36f5SLiu Zhe 
1331dfd36f5SLiu Zhe 	/**
1341dfd36f5SLiu Zhe 	 * Check the basic types of chart
1351dfd36f5SLiu Zhe 	 * 1. Create a spreadsheet file.
1361dfd36f5SLiu Zhe 	 * 2. Input number in a cell range.
1371dfd36f5SLiu Zhe 	 * 3. Use the data to create a chart, change the chart type.
1381dfd36f5SLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
1391dfd36f5SLiu Zhe 	 * 5. Close and reopen file.  -> Check the chart type setting.
1401dfd36f5SLiu Zhe 	 * @throws Exception
1411dfd36f5SLiu Zhe 	 */
1421dfd36f5SLiu Zhe 	@Test
testCreateChart()1431dfd36f5SLiu Zhe 	public void testCreateChart() throws Exception {
1441dfd36f5SLiu Zhe 		String fileName = "testCreateChart";
1451dfd36f5SLiu Zhe 		String chartName = "testChart";
1461dfd36f5SLiu Zhe 		String cellRangeName = "A1:D4";
1471dfd36f5SLiu Zhe 		String result = null;
1481dfd36f5SLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
1491dfd36f5SLiu Zhe 			cellRangeName = "A1:C4";
1501dfd36f5SLiu Zhe 		}
1511dfd36f5SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
1521dfd36f5SLiu Zhe 			chartName = "Object 1";
1531dfd36f5SLiu Zhe 		}
1541dfd36f5SLiu Zhe 
1551dfd36f5SLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
1561dfd36f5SLiu Zhe 
1571dfd36f5SLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
1581dfd36f5SLiu Zhe 
1591dfd36f5SLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
1601dfd36f5SLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
1611dfd36f5SLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
1621dfd36f5SLiu Zhe 		XChartDocument xChartDocument = null;
1631dfd36f5SLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
1641dfd36f5SLiu Zhe 
1651dfd36f5SLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
1661dfd36f5SLiu Zhe 
1671dfd36f5SLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
1681dfd36f5SLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
1691dfd36f5SLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
1701dfd36f5SLiu Zhe 
1711dfd36f5SLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
1721dfd36f5SLiu Zhe 		result = xChartDocument.getDiagram().getDiagramType();
1731dfd36f5SLiu Zhe 
1741dfd36f5SLiu Zhe 		SCUtil.closeFile(scDocument);
1751dfd36f5SLiu Zhe 
1761dfd36f5SLiu Zhe 		assertEquals("Incorrect chart type string got in ." + fileType + " file.", expected, result);
1771dfd36f5SLiu Zhe 
1781dfd36f5SLiu Zhe 	}
1791dfd36f5SLiu Zhe 
1801dfd36f5SLiu Zhe }