xref: /AOO41X/test/testuno/source/fvt/uno/sc/chart/ChartDataLabel.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
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.XChartDocument;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.sheet.XSpreadsheet;
45 import com.sun.star.sheet.XSpreadsheetDocument;
46 import com.sun.star.table.CellRangeAddress;
47 
48 /**
49  *  Check different types of chart data labels can be applied and saved
50  *
51  */
52 @RunWith(value = Parameterized.class)
53 public class ChartDataLabel {
54 
55     private int labelType;
56     private String inputType;
57     private double[][] numberData;
58     private String fileType;
59 
60     private static final UnoApp unoApp = new UnoApp();
61 
62     XComponent scComponent = null;
63     XSpreadsheetDocument scDocument = null;
64 
65     @Parameters
data()66     public static Collection<Object[]> data() throws Exception {
67         double[][] numberData1 = {
68                 {1, 2, 3, 4},
69                 {2, 4.3, 5, 8},
70                 {4, 2, 3, 1},
71                 {1, -1, 0, -3}
72         };
73 
74         return Arrays.asList(new Object[][] {
75             {0, "com.sun.star.chart.XYDiagram", numberData1, "ods"}, // no label
76             {1, "com.sun.star.chart.PieDiagram", numberData1, "ods"}, // show number
77             {2, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show percentage
78             {4, "com.sun.star.chart.LineDiagram", numberData1, "ods"}, // show category name
79             {16, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // check legend symbol (won't shown on UI)
80             {3, "com.sun.star.chart.DonutDiagram", numberData1, "ods"}, // show number & percentage
81             {5, "com.sun.star.chart.BubbleDiagram", numberData1, "ods"}, // show number & category name
82             {6, "com.sun.star.chart.NetDiagram", numberData1, "ods"}, // show percentage & category name
83             {7, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show number & percentage & category name
84             {17, "com.sun.star.chart.LineDiagram", numberData1, "ods"}, // show number & legend symbol
85             {18, "com.sun.star.chart.XYDiagram", numberData1, "ods"}, // show percentage & legend symbol
86             {19, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show number & percentage & legend symbol
87             {20, "com.sun.star.chart.NetDiagram", numberData1, "ods"}, // show category name & legend symbol
88             {21, "com.sun.star.chart.AreaDiagram", numberData1, "ods"}, // show number & Category name & legend symbol
89             {22, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show percentage & Category name & legend symbol
90             {23, "com.sun.star.chart.BarDiagram", numberData1, "ods"}, // show number & percentage & & Category name & legend symbol
91             {0, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
92             {1, "com.sun.star.chart.PieDiagram", numberData1, "xls"},
93             {4, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
94             {5, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
95             {17, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
96             {20, "com.sun.star.chart.BubbleDiagram", numberData1, "xls"},
97             {21, "com.sun.star.chart.BarDiagram", numberData1, "xls"}
98         });
99     }
100 
ChartDataLabel(int labelType, String inputType, double[][] numberData, String fileType)101     public ChartDataLabel(int labelType, String inputType, double[][] numberData, String fileType) {
102         this.labelType = labelType;
103         this.inputType = inputType;
104         this.numberData = numberData;
105         this.fileType = fileType;
106     }
107 
108 
109     @Before
setUp()110     public void setUp() throws Exception {
111         scComponent = unoApp.newDocument("scalc");
112         scDocument = SCUtil.getSCDocument(scComponent);
113     }
114 
115     @After
tearDown()116     public void tearDown() throws Exception {
117         unoApp.closeDocument(scComponent);
118 
119     }
120 
121     @BeforeClass
setUpConnection()122     public static void setUpConnection() throws Exception {
123         unoApp.start();
124     }
125 
126     @AfterClass
tearDownConnection()127     public static void tearDownConnection() throws InterruptedException, Exception {
128         unoApp.close();
129         SCUtil.clearTempDir();
130     }
131 
132     /**
133      * Enable different types of data labels in chart.
134      * 1. Create a spreadsheet file.
135      * 2. Input number in a cell range and create a chart.
136      * 3. Enable different types of data labels for all data series in chart.
137      * 4. Save file as ODF/MSBinary format.
138      * 5. Close and reopen file.  -> Check the chart data label setting.
139      * @throws Exception
140      */
141     @Test
testCreateDataLabel()142     public void testCreateDataLabel() throws Exception {
143         String fileName = "testCreateDataLabel";
144         String chartName = "testChart";
145         String cellRangeName = "A1:D4";
146         int result = 0;
147 
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         SCUtil.setChartType(xChartDocument, inputType);
165 
166         SCUtil.setProperties(xChartDocument.getDiagram(), "DataCaption", labelType);
167 
168         SCUtil.saveFileAs(scComponent, fileName, fileType);
169         scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
170         sheet = SCUtil.getCurrentSheet(scDocument);
171 
172         xChartDocument = SCUtil.getChartByName(sheet, chartName);
173         result = ((Integer) SCUtil.getProperties(xChartDocument.getDiagram(), "DataCaption")).intValue();
174         SCUtil.closeFile(scDocument);
175 
176         assertEquals("Incorrect chart data label types got in ." + fileType + " file.", labelType, result, 0);
177 
178     }
179 
180 }