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 import com.sun.star.awt.Rectangle; 41 import com.sun.star.chart.XChartDocument; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.sheet.XSpreadsheet; 44 import com.sun.star.sheet.XSpreadsheetDocument; 45 import com.sun.star.table.CellRangeAddress; 46 47 /** 48 * Check the chart type setting can be applied and saved 49 * 50 */ 51 @RunWith(value = Parameterized.class) 52 public class ChartType { 53 54 private String expected; 55 private String inputType; 56 private double[][] numberData; 57 private String fileType; 58 59 private static final UnoApp unoApp = new UnoApp(); 60 61 XComponent scComponent = null; 62 XSpreadsheetDocument scDocument = null; 63 64 @Parameters data()65 public static Collection<Object[]> data() throws Exception { 66 double[][] numberData1 = { 67 {1, 2, 3, 4}, 68 {2, 4.3, 5, 8}, 69 {4, 2, 3, 1}, 70 {1, -1, 0, 3} 71 }; 72 double[][] numberData2 = { 73 {22, 12, 15}, 74 {20, 11, 20}, 75 {40, 37, 38}, 76 {3.01, 2.2, 2.2} 77 }; 78 79 return Arrays.asList(new Object[][] { 80 {"com.sun.star.chart.BarDiagram", "com.sun.star.chart.BarDiagram", numberData1, "ods"}, 81 {"com.sun.star.chart.AreaDiagram", "com.sun.star.chart.AreaDiagram", numberData1, "ods"}, 82 {"com.sun.star.chart.LineDiagram", "com.sun.star.chart.LineDiagram", numberData1, "ods"}, 83 {"com.sun.star.chart.PieDiagram", "com.sun.star.chart.PieDiagram", numberData1, "ods"}, 84 {"com.sun.star.chart.DonutDiagram", "com.sun.star.chart.DonutDiagram", numberData1, "ods"}, 85 {"com.sun.star.chart.NetDiagram", "com.sun.star.chart.NetDiagram", numberData1, "ods"}, 86 {"com.sun.star.chart.XYDiagram", "com.sun.star.chart.XYDiagram", numberData1, "ods"}, 87 {"com.sun.star.chart.StockDiagram", "com.sun.star.chart.StockDiagram", numberData2, "ods"}, 88 {"com.sun.star.chart.BubbleDiagram", "com.sun.star.chart.BubbleDiagram", numberData1, "ods"}, 89 90 {"com.sun.star.chart.BarDiagram", "com.sun.star.chart.BarDiagram", numberData1, "xls"}, 91 {"com.sun.star.chart.AreaDiagram", "com.sun.star.chart.AreaDiagram", numberData1, "xls"}, 92 {"com.sun.star.chart.LineDiagram", "com.sun.star.chart.LineDiagram", numberData1, "xls"}, 93 {"com.sun.star.chart.PieDiagram", "com.sun.star.chart.PieDiagram", numberData1, "xls"}, 94 {"com.sun.star.chart.DonutDiagram", "com.sun.star.chart.DonutDiagram", numberData1, "xls"}, 95 {"com.sun.star.chart.NetDiagram", "com.sun.star.chart.NetDiagram", numberData1, "xls"}, 96 {"com.sun.star.chart.XYDiagram", "com.sun.star.chart.XYDiagram", numberData1, "xls"}, 97 {"com.sun.star.chart.StockDiagram", "com.sun.star.chart.StockDiagram", numberData2, "xls"}, 98 {"com.sun.star.chart.BubbleDiagram", "com.sun.star.chart.BubbleDiagram", numberData1, "xls"} 99 }); 100 } 101 ChartType(String expected, String inputType, double[][] numberData, String fileType)102 public ChartType(String expected, String inputType, double[][] numberData, String fileType) { 103 this.expected = expected; 104 this.inputType = inputType; 105 this.numberData = numberData; 106 this.fileType = fileType; 107 } 108 109 110 @Before setUp()111 public void setUp() throws Exception { 112 scComponent = unoApp.newDocument("scalc"); 113 scDocument = SCUtil.getSCDocument(scComponent); 114 } 115 116 @After tearDown()117 public void tearDown() throws Exception { 118 unoApp.closeDocument(scComponent); 119 120 } 121 122 @BeforeClass setUpConnection()123 public static void setUpConnection() throws Exception { 124 unoApp.start(); 125 } 126 127 @AfterClass tearDownConnection()128 public static void tearDownConnection() throws InterruptedException, Exception { 129 unoApp.close(); 130 SCUtil.clearTempDir(); 131 } 132 133 /** 134 * Check the basic types of chart 135 * 1. Create a spreadsheet file. 136 * 2. Input number in a cell range. 137 * 3. Use the data to create a chart, change the chart type. 138 * 4. Save file as ODF/MSBinary format. 139 * 5. Close and reopen file. -> Check the chart type setting. 140 * @throws Exception 141 */ 142 @Test testCreateChart()143 public void testCreateChart() throws Exception { 144 String fileName = "testCreateChart"; 145 String chartName = "testChart"; 146 String cellRangeName = "A1:D4"; 147 String result = null; 148 if (inputType.equals("com.sun.star.chart.StockDiagram")) { 149 cellRangeName = "A1:C4"; 150 } 151 if (fileType.equalsIgnoreCase("xls")) { 152 chartName = "Object 1"; 153 } 154 155 XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument); 156 157 SCUtil.setValueToCellRange(sheet, 0, 0, numberData); 158 159 CellRangeAddress[] cellAddress = new CellRangeAddress[1]; 160 cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName); 161 Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500); 162 XChartDocument xChartDocument = null; 163 xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName); 164 165 SCUtil.setChartType(xChartDocument, inputType); 166 167 SCUtil.saveFileAs(scComponent, fileName, fileType); 168 scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType); 169 sheet = SCUtil.getCurrentSheet(scDocument); 170 171 xChartDocument = SCUtil.getChartByName(sheet, chartName); 172 result = xChartDocument.getDiagram().getDiagramType(); 173 174 SCUtil.closeFile(scDocument); 175 176 assertEquals("Incorrect chart type string got in ." + fileType + " file.", expected, result); 177 178 } 179 180 }