xref: /AOO41X/test/testuno/source/fvt/uno/sc/chart/ChartMeanValueLine.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.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 
27 import java.util.Arrays;
28 import java.util.Collection;
29 
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.Parameterized;
37 import org.junit.runners.Parameterized.Parameters;
38 import org.openoffice.test.uno.UnoApp;
39 
40 import testlib.uno.SCUtil;
41 
42 import com.sun.star.awt.Rectangle;
43 import com.sun.star.chart.XChartDocument;
44 import com.sun.star.chart.XDiagram;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.sheet.XSpreadsheet;
47 import com.sun.star.sheet.XSpreadsheetDocument;
48 import com.sun.star.table.CellRangeAddress;
49 
50 /**
51  *  Check mean value line in chart can be applied and saved
52  *
53  */
54 @RunWith(value = Parameterized.class)
55 public class ChartMeanValueLine {
56 
57     private Boolean expected;
58     private Boolean meanValueLine;
59     private String inputType;
60     private double[][] numberData;
61     private String fileType;
62 
63     private static final UnoApp unoApp = new UnoApp();
64 
65     XComponent scComponent = null;
66     XSpreadsheetDocument scDocument = null;
67 
68     @Parameters
data()69     public static Collection<Object[]> data() throws Exception {
70         double[][] numberData1 = {
71                 {10, 20, 30, 40},
72                 {20, 40.3, 50, 80},
73                 {40, 20, 30, 10},
74                 {10, -10, 0, -30}
75         };
76 
77         return Arrays.asList(new Object[][] {
78             {true, true, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
79             {true, true, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
80             {true, true, "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
81             {true, true, "com.sun.star.chart.XYDiagram", numberData1, "ods"},
82 
83             {false, true, "com.sun.star.chart.BarDiagram", numberData1, "xls"}, //Excel does not support this property, save as .xls, the setting will be lost.
84             {false, true, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
85             {false, true, "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
86             {false, true, "com.sun.star.chart.XYDiagram", numberData1, "xls"}
87         });
88     }
89 
ChartMeanValueLine(Boolean expected, Boolean meanValueLine, String inputType, double[][] numberData, String fileType)90     public ChartMeanValueLine(Boolean expected, Boolean meanValueLine, String inputType, double[][] numberData, String fileType) {
91         this.expected = expected;
92         this.meanValueLine = meanValueLine;
93         this.inputType = inputType;
94         this.numberData = numberData;
95         this.fileType = fileType;
96     }
97 
98     @Before
setUp()99     public void setUp() throws Exception {
100         scComponent = unoApp.newDocument("scalc");
101         scDocument = SCUtil.getSCDocument(scComponent);
102     }
103 
104     @After
tearDown()105     public void tearDown() throws Exception {
106         unoApp.closeDocument(scComponent);
107 
108     }
109 
110     @BeforeClass
setUpConnection()111     public static void setUpConnection() throws Exception {
112         unoApp.start();
113     }
114 
115     @AfterClass
tearDownConnection()116     public static void tearDownConnection() throws InterruptedException, Exception {
117         unoApp.close();
118         SCUtil.clearTempDir();
119     }
120 
121     /**
122      * Enable different types of mean value line in chart.
123      * 1. Create a spreadsheet file.
124      * 2. Input number in a cell range and create a 2D chart.
125      * 3. Enable mean value line in chart.
126      * 4. Save file as ODF/MSBinary format.
127      * 5. Close and reopen file.  -> Check the mean value line setting.
128      * @throws Exception
129      */
130     @Test
testCreateTrendline()131     public void testCreateTrendline() throws Exception {
132         String fileName = "testCreateMeanValueline";
133         String chartName = "testChart";
134         String cellRangeName = "A1:D4";
135         Boolean result = null;
136 
137         if (inputType.equals("com.sun.star.chart.StockDiagram")) {
138             cellRangeName = "A1:C4";
139         }
140         if (fileType.equalsIgnoreCase("xls")) {
141             chartName = "Object 1";
142         }
143 
144         XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
145 
146         SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
147 
148         CellRangeAddress[] cellAddress = new CellRangeAddress[1];
149         cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
150         Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
151         XChartDocument xChartDocument = null;
152         xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
153         SCUtil.setChartType(xChartDocument, inputType);
154         XDiagram xDiagram = xChartDocument.getDiagram();
155 
156         SCUtil.setProperties(xDiagram, "MeanValue", meanValueLine);
157 
158         SCUtil.saveFileAs(scComponent, fileName, fileType);
159         scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
160         sheet = SCUtil.getCurrentSheet(scDocument);
161 
162         xChartDocument = SCUtil.getChartByName(sheet, chartName);
163         xDiagram = xChartDocument.getDiagram();
164         result = (Boolean) SCUtil.getProperties(xDiagram, "MeanValue");
165 
166         SCUtil.closeFile(scDocument);
167 
168         if (expected) {
169             assertTrue("Incorrect chart trendline got in ." + fileType + " file.", result);
170         }
171         else {
172             assertFalse("Incorrect chart trendline got in ." + fileType + " file.", result);
173         }
174 
175     }
176 
177 }