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.assertEquals; 25*eba4d44aSLiu Zhe import static org.junit.Assert.assertFalse; 26*eba4d44aSLiu Zhe import static org.junit.Assert.assertTrue; 27*eba4d44aSLiu Zhe 28*eba4d44aSLiu Zhe import java.util.Arrays; 29*eba4d44aSLiu Zhe import java.util.Collection; 30*eba4d44aSLiu Zhe 31*eba4d44aSLiu Zhe import org.junit.After; 32*eba4d44aSLiu Zhe import org.junit.AfterClass; 33*eba4d44aSLiu Zhe import org.junit.Before; 34*eba4d44aSLiu Zhe import org.junit.BeforeClass; 35*eba4d44aSLiu Zhe import org.junit.Test; 36*eba4d44aSLiu Zhe import org.junit.runner.RunWith; 37*eba4d44aSLiu Zhe import org.junit.runners.Parameterized; 38*eba4d44aSLiu Zhe import org.junit.runners.Parameterized.Parameters; 39*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp; 40*eba4d44aSLiu Zhe 41*eba4d44aSLiu Zhe import testlib.uno.SCUtil; 42*eba4d44aSLiu Zhe 43*eba4d44aSLiu Zhe import com.sun.star.awt.Point; 44*eba4d44aSLiu Zhe import com.sun.star.awt.Rectangle; 45*eba4d44aSLiu Zhe import com.sun.star.chart.XChartDocument; 46*eba4d44aSLiu Zhe import com.sun.star.drawing.XShape; 47*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent; 48*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheet; 49*eba4d44aSLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument; 50*eba4d44aSLiu Zhe import com.sun.star.table.CellRangeAddress; 51*eba4d44aSLiu Zhe 52*eba4d44aSLiu Zhe /** 53*eba4d44aSLiu Zhe * Check the chart legend and the position can be applied and saved 54*eba4d44aSLiu Zhe * 55*eba4d44aSLiu Zhe */ 56*eba4d44aSLiu Zhe @RunWith(value = Parameterized.class) 57*eba4d44aSLiu Zhe public class ChartLegend { 58*eba4d44aSLiu Zhe 59*eba4d44aSLiu Zhe private String inputType; 60*eba4d44aSLiu Zhe private double[][] numberData; 61*eba4d44aSLiu Zhe private int[] offset; 62*eba4d44aSLiu Zhe private String fileType; 63*eba4d44aSLiu Zhe 64*eba4d44aSLiu Zhe private static final UnoApp unoApp = new UnoApp(); 65*eba4d44aSLiu Zhe 66*eba4d44aSLiu Zhe XComponent scComponent = null; 67*eba4d44aSLiu Zhe XSpreadsheetDocument scDocument = null; 68*eba4d44aSLiu Zhe 69*eba4d44aSLiu Zhe @Parameters 70*eba4d44aSLiu Zhe public static Collection<Object[]> data() throws Exception { 71*eba4d44aSLiu Zhe int[][] offsetList = { 72*eba4d44aSLiu Zhe {-50, -2000}, 73*eba4d44aSLiu Zhe {-1000, 3000}, 74*eba4d44aSLiu Zhe {-4000, -1000} 75*eba4d44aSLiu Zhe }; 76*eba4d44aSLiu Zhe 77*eba4d44aSLiu Zhe double[][] numberData1 = { 78*eba4d44aSLiu Zhe {1, 2, 3, 4}, 79*eba4d44aSLiu Zhe {2, 4.3, 5, 8}, 80*eba4d44aSLiu Zhe {4, 2, 3, 1}, 81*eba4d44aSLiu Zhe {1, -1, 0, 3} 82*eba4d44aSLiu Zhe }; 83*eba4d44aSLiu Zhe return Arrays.asList(new Object[][] { 84*eba4d44aSLiu Zhe {"com.sun.star.chart.BarDiagram", numberData1, offsetList[0], "ods"}, 85*eba4d44aSLiu Zhe {"com.sun.star.chart.BubbleDiagram", numberData1, offsetList[1], "ods"}, 86*eba4d44aSLiu Zhe {"com.sun.star.chart.NetDiagram", numberData1, offsetList[2], "ods"}, 87*eba4d44aSLiu Zhe {"com.sun.star.chart.BarDiagram", numberData1, offsetList[0], "xls"}, 88*eba4d44aSLiu Zhe {"com.sun.star.chart.BubbleDiagram", numberData1, offsetList[1], "xls"}, 89*eba4d44aSLiu Zhe {"com.sun.star.chart.NetDiagram", numberData1, offsetList[2], "xls"} 90*eba4d44aSLiu Zhe }); 91*eba4d44aSLiu Zhe } 92*eba4d44aSLiu Zhe 93*eba4d44aSLiu Zhe public ChartLegend(String inputType, double[][] numberData, int[] offset, String fileType) { 94*eba4d44aSLiu Zhe this.inputType = inputType; 95*eba4d44aSLiu Zhe this.numberData = numberData; 96*eba4d44aSLiu Zhe this.offset = offset; 97*eba4d44aSLiu Zhe this.fileType = fileType; 98*eba4d44aSLiu Zhe } 99*eba4d44aSLiu Zhe 100*eba4d44aSLiu Zhe 101*eba4d44aSLiu Zhe @Before 102*eba4d44aSLiu Zhe public void setUp() throws Exception { 103*eba4d44aSLiu Zhe scComponent = unoApp.newDocument("scalc"); 104*eba4d44aSLiu Zhe scDocument = SCUtil.getSCDocument(scComponent); 105*eba4d44aSLiu Zhe } 106*eba4d44aSLiu Zhe 107*eba4d44aSLiu Zhe @After 108*eba4d44aSLiu Zhe public void tearDown() throws Exception { 109*eba4d44aSLiu Zhe unoApp.closeDocument(scComponent); 110*eba4d44aSLiu Zhe 111*eba4d44aSLiu Zhe } 112*eba4d44aSLiu Zhe 113*eba4d44aSLiu Zhe @BeforeClass 114*eba4d44aSLiu Zhe public static void setUpConnection() throws Exception { 115*eba4d44aSLiu Zhe unoApp.start(); 116*eba4d44aSLiu Zhe } 117*eba4d44aSLiu Zhe 118*eba4d44aSLiu Zhe @AfterClass 119*eba4d44aSLiu Zhe public static void tearDownConnection() throws InterruptedException, Exception { 120*eba4d44aSLiu Zhe unoApp.close(); 121*eba4d44aSLiu Zhe SCUtil.clearTempDir(); 122*eba4d44aSLiu Zhe } 123*eba4d44aSLiu Zhe 124*eba4d44aSLiu Zhe /** 125*eba4d44aSLiu Zhe * Check remove the legend of chart 126*eba4d44aSLiu Zhe * 1. Create a spreadsheet file. 127*eba4d44aSLiu Zhe * 2. Input number in a cell range. 128*eba4d44aSLiu Zhe * 3. Use the data to create a chart, set the chart type. 129*eba4d44aSLiu Zhe * 4. Remove the legend. 130*eba4d44aSLiu Zhe * 5. Save file as ODF/MSBinary format. 131*eba4d44aSLiu Zhe * 6. Close and reopen file. -> Check the legend status. 132*eba4d44aSLiu Zhe * @throws Exception 133*eba4d44aSLiu Zhe */ 134*eba4d44aSLiu Zhe @Test 135*eba4d44aSLiu Zhe public void testDisableLegend() throws Exception { 136*eba4d44aSLiu Zhe String fileName = "testDisableLegend"; 137*eba4d44aSLiu Zhe String chartName = "testChart"; 138*eba4d44aSLiu Zhe String cellRangeName = "A1:D4"; 139*eba4d44aSLiu Zhe Boolean result = true; 140*eba4d44aSLiu Zhe 141*eba4d44aSLiu Zhe if (fileType.equalsIgnoreCase("xls")) { 142*eba4d44aSLiu Zhe chartName = "Object 1"; 143*eba4d44aSLiu Zhe } 144*eba4d44aSLiu Zhe 145*eba4d44aSLiu Zhe XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument); 146*eba4d44aSLiu Zhe 147*eba4d44aSLiu Zhe SCUtil.setValueToCellRange(sheet, 0, 0, numberData); 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 154*eba4d44aSLiu Zhe SCUtil.setChartType(xChartDocument, inputType); 155*eba4d44aSLiu Zhe result = (Boolean) SCUtil.getProperties(xChartDocument, "HasLegend"); 156*eba4d44aSLiu Zhe if (result) { 157*eba4d44aSLiu Zhe SCUtil.setProperties(xChartDocument, "HasLegend", false); 158*eba4d44aSLiu Zhe } 159*eba4d44aSLiu Zhe 160*eba4d44aSLiu Zhe SCUtil.saveFileAs(scComponent, fileName, fileType); 161*eba4d44aSLiu Zhe scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType); 162*eba4d44aSLiu Zhe sheet = SCUtil.getCurrentSheet(scDocument); 163*eba4d44aSLiu Zhe 164*eba4d44aSLiu Zhe xChartDocument = SCUtil.getChartByName(sheet, chartName); 165*eba4d44aSLiu Zhe result = (Boolean) SCUtil.getProperties(xChartDocument, "HasLegend"); 166*eba4d44aSLiu Zhe 167*eba4d44aSLiu Zhe SCUtil.closeFile(scDocument); 168*eba4d44aSLiu Zhe 169*eba4d44aSLiu Zhe assertFalse("Chart legend has not been disabled in " + fileType + " file.", result); 170*eba4d44aSLiu Zhe 171*eba4d44aSLiu Zhe } 172*eba4d44aSLiu Zhe 173*eba4d44aSLiu Zhe /** 174*eba4d44aSLiu Zhe * Check change the position of legend in chart 175*eba4d44aSLiu Zhe * 1. Create a spreadsheet file. 176*eba4d44aSLiu Zhe * 2. Input number in a cell range. 177*eba4d44aSLiu Zhe * 3. Use the data to create a chart, set the chart type. 178*eba4d44aSLiu Zhe * 4. Change the position of legend in chart. 179*eba4d44aSLiu Zhe * 5. Save file as ODF/MSBinary format. 180*eba4d44aSLiu Zhe * 6. Close and reopen file. -> Check the legend position. 181*eba4d44aSLiu Zhe * @throws Exception 182*eba4d44aSLiu Zhe */ 183*eba4d44aSLiu Zhe @Test 184*eba4d44aSLiu Zhe public void testLegendPosition() throws Exception { 185*eba4d44aSLiu Zhe String fileName = "testDisableLegend"; 186*eba4d44aSLiu Zhe String chartName = "testChart"; 187*eba4d44aSLiu Zhe String cellRangeName = "A1:D4"; 188*eba4d44aSLiu Zhe Boolean result = true; 189*eba4d44aSLiu Zhe int delta = 4;//Save as .xls file, the legend position may change a little, set acceptable range. 190*eba4d44aSLiu Zhe 191*eba4d44aSLiu Zhe if (fileType.equalsIgnoreCase("xls")) { 192*eba4d44aSLiu Zhe chartName = "Object 1"; 193*eba4d44aSLiu Zhe } 194*eba4d44aSLiu Zhe 195*eba4d44aSLiu Zhe XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument); 196*eba4d44aSLiu Zhe 197*eba4d44aSLiu Zhe SCUtil.setValueToCellRange(sheet, 0, 0, numberData); 198*eba4d44aSLiu Zhe CellRangeAddress[] cellAddress = new CellRangeAddress[1]; 199*eba4d44aSLiu Zhe cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName); 200*eba4d44aSLiu Zhe Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500); 201*eba4d44aSLiu Zhe XChartDocument xChartDocument = null; 202*eba4d44aSLiu Zhe xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName); 203*eba4d44aSLiu Zhe 204*eba4d44aSLiu Zhe SCUtil.setChartType(xChartDocument, inputType); 205*eba4d44aSLiu Zhe 206*eba4d44aSLiu Zhe XShape legend = xChartDocument.getLegend(); 207*eba4d44aSLiu Zhe Point aPoint = legend.getPosition(); 208*eba4d44aSLiu Zhe aPoint = new Point(aPoint.X + offset[0], aPoint.Y + offset[1]); 209*eba4d44aSLiu Zhe legend.setPosition(aPoint); 210*eba4d44aSLiu Zhe 211*eba4d44aSLiu Zhe SCUtil.saveFileAs(scComponent, fileName, fileType); 212*eba4d44aSLiu Zhe scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType); 213*eba4d44aSLiu Zhe sheet = SCUtil.getCurrentSheet(scDocument); 214*eba4d44aSLiu Zhe 215*eba4d44aSLiu Zhe xChartDocument = SCUtil.getChartByName(sheet, chartName); 216*eba4d44aSLiu Zhe result = (Boolean) SCUtil.getProperties(xChartDocument, "HasLegend"); 217*eba4d44aSLiu Zhe legend = xChartDocument.getLegend(); 218*eba4d44aSLiu Zhe Point resultPoint = legend.getPosition(); 219*eba4d44aSLiu Zhe 220*eba4d44aSLiu Zhe SCUtil.closeFile(scDocument); 221*eba4d44aSLiu Zhe 222*eba4d44aSLiu Zhe assertTrue("Chart legend has not been enabled in ." + fileType + " file.", result); 223*eba4d44aSLiu Zhe 224*eba4d44aSLiu Zhe if (fileType.equalsIgnoreCase("xls")) { 225*eba4d44aSLiu Zhe assertEquals("Incorrect chart legend position X got in ." + fileType + " file.", aPoint.X, resultPoint.X, delta); 226*eba4d44aSLiu Zhe assertEquals("Incorrect chart legend position X got in ." + fileType + " file.", aPoint.Y, resultPoint.Y, delta); 227*eba4d44aSLiu Zhe } 228*eba4d44aSLiu Zhe else { 229*eba4d44aSLiu Zhe assertEquals("Incorrect chart legend position X got in ." + fileType + " file.", aPoint.X, resultPoint.X); 230*eba4d44aSLiu Zhe assertEquals("Incorrect chart legend position X got in ." + fileType + " file.", aPoint.Y, resultPoint.Y); 231*eba4d44aSLiu Zhe } 232*eba4d44aSLiu Zhe 233*eba4d44aSLiu Zhe } 234*eba4d44aSLiu Zhe 235*eba4d44aSLiu Zhe }