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.ChartErrorCategory;
43 import com.sun.star.chart.ChartErrorIndicatorType;
44 import com.sun.star.chart.XChartDocument;
45 import com.sun.star.chart.XDiagram;
46 import com.sun.star.lang.XComponent;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.table.CellRangeAddress;
50 
51 /**
52  *  Check Y error bar in chart can be applied and saved
53  *
54  */
55 @RunWith(value = Parameterized.class)
56 public class ChartYErrorBar {
57 
58 	private ChartErrorCategory expCategory;
59 	private ChartErrorIndicatorType expIndicator;
60 	private ChartErrorCategory inputCategory;
61 	private ChartErrorIndicatorType inputIndicator;
62 	private String inputType;
63 	private double[][] numberData;
64 	private String fileType;
65 
66 	private static final UnoApp unoApp = new UnoApp();
67 
68 	XComponent scComponent = null;
69 	XSpreadsheetDocument scDocument = null;
70 
71 	@Parameters
data()72 	public static Collection<Object[]> data() throws Exception {
73 		double[][] numberData1 = {
74 				{10, 20, 30, 40},
75 				{20, 40.3, 50, 80},
76 				{40, 20, 30, 10},
77 				{10, -10, 0, -30}
78 		};
79 
80 		return Arrays.asList(new Object[][] {
81 			{ChartErrorCategory.NONE, ChartErrorIndicatorType.NONE, ChartErrorCategory.NONE, ChartErrorIndicatorType.NONE, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
82 			{ChartErrorCategory.VARIANCE, ChartErrorIndicatorType.TOP_AND_BOTTOM, ChartErrorCategory.VARIANCE, ChartErrorIndicatorType.TOP_AND_BOTTOM, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
83 			{ChartErrorCategory.STANDARD_DEVIATION, ChartErrorIndicatorType.UPPER, ChartErrorCategory.STANDARD_DEVIATION, ChartErrorIndicatorType.UPPER, "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
84 			{ChartErrorCategory.PERCENT, ChartErrorIndicatorType.LOWER, ChartErrorCategory.PERCENT, ChartErrorIndicatorType.LOWER, "com.sun.star.chart.BarDiagram", numberData1, "ods"},
85 			{ChartErrorCategory.ERROR_MARGIN, ChartErrorIndicatorType.TOP_AND_BOTTOM, ChartErrorCategory.ERROR_MARGIN, ChartErrorIndicatorType.TOP_AND_BOTTOM, "com.sun.star.chart.LineDiagram", numberData1, "ods"},
86 			{ChartErrorCategory.CONSTANT_VALUE, ChartErrorIndicatorType.UPPER, ChartErrorCategory.CONSTANT_VALUE, ChartErrorIndicatorType.UPPER, "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
87 
88 			{ChartErrorCategory.NONE, ChartErrorIndicatorType.NONE, ChartErrorCategory.NONE, ChartErrorIndicatorType.NONE, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
89 //			{ChartErrorCategory.VARIANCE, ChartErrorIndicatorType.TOP_AND_BOTTOM, ChartErrorCategory.VARIANCE, ChartErrorIndicatorType.TOP_AND_BOTTOM, "com.sun.star.chart.LineDiagram", numberData1, "xls"},
90 			{ChartErrorCategory.STANDARD_DEVIATION, ChartErrorIndicatorType.UPPER, ChartErrorCategory.STANDARD_DEVIATION, ChartErrorIndicatorType.UPPER, "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
91 			{ChartErrorCategory.PERCENT, ChartErrorIndicatorType.LOWER, ChartErrorCategory.PERCENT, ChartErrorIndicatorType.LOWER, "com.sun.star.chart.BarDiagram", numberData1, "xls"},
92 //			{ChartErrorCategory.ERROR_MARGIN, ChartErrorIndicatorType.TOP_AND_BOTTOM, ChartErrorCategory.ERROR_MARGIN, ChartErrorIndicatorType.TOP_AND_BOTTOM, "com.sun.star.chart.AreaDiagram", numberData1, "xls"},
93 			{ChartErrorCategory.CONSTANT_VALUE, ChartErrorIndicatorType.UPPER, ChartErrorCategory.CONSTANT_VALUE, ChartErrorIndicatorType.UPPER, "com.sun.star.chart.LineDiagram", numberData1, "xls"}
94 
95 		});
96 	}
97 
ChartYErrorBar(ChartErrorCategory expCategory, ChartErrorIndicatorType expIndicator, ChartErrorCategory inputCategory, ChartErrorIndicatorType inputIndicator, String inputType, double[][] numberData, String fileType)98 	public ChartYErrorBar(ChartErrorCategory expCategory, ChartErrorIndicatorType expIndicator, ChartErrorCategory inputCategory, ChartErrorIndicatorType inputIndicator, String inputType, double[][] numberData, String fileType) {
99 		this.expCategory = expCategory;
100 		this.expIndicator = expIndicator;
101 		this.inputCategory = inputCategory;
102 		this.inputIndicator = inputIndicator;
103 		this.inputType = inputType;
104 		this.numberData = numberData;
105 		this.fileType = fileType;
106 	}
107 
108 	@Before
setUp()109 	public void setUp() throws Exception {
110 		scComponent = unoApp.newDocument("scalc");
111 		scDocument = SCUtil.getSCDocument(scComponent);
112 	}
113 
114 	@After
tearDown()115 	public void tearDown() throws Exception {
116 		unoApp.closeDocument(scComponent);
117 
118 	}
119 
120 	@BeforeClass
setUpConnection()121 	public static void setUpConnection() throws Exception {
122 		unoApp.start();
123 	}
124 
125 	@AfterClass
tearDownConnection()126 	public static void tearDownConnection() throws InterruptedException, Exception {
127 		unoApp.close();
128 		SCUtil.clearTempDir();
129 	}
130 
131 	/**
132 	 * Enable different types of Y error bar in chart.
133 	 * 1. Create a spreadsheet file.
134 	 * 2. Input number in a cell range and create a chart.
135 	 * 3. Enable Y error bar in chart.
136 	 * 4. Save file as ODF/MSBinary format.
137 	 * 5. Close and reopen file.  -> Check the Y error bar setting.
138 	 * @throws Exception
139 	 */
140 	@Test
testCreateYErrorBar()141 	public void testCreateYErrorBar() throws Exception {
142 		String fileName = "testCreateYErrorBar";
143 		String chartName = "testChart";
144 		String cellRangeName = "A1:D4";
145 		ChartErrorCategory result1 = null;
146 		ChartErrorIndicatorType result2 = null;
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 		XDiagram xDiagram = xChartDocument.getDiagram();
166 
167 		SCUtil.setProperties(xDiagram, "ErrorCategory", inputCategory);
168 		SCUtil.setProperties(xDiagram, "ErrorIndicator", inputIndicator);
169 
170 		SCUtil.saveFileAs(scComponent, fileName, fileType);
171 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
172 		sheet = SCUtil.getCurrentSheet(scDocument);
173 
174 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
175 		xDiagram = xChartDocument.getDiagram();
176 		result1 = (ChartErrorCategory) SCUtil.getProperties(xDiagram, "ErrorCategory");
177 		result2 = (ChartErrorIndicatorType) SCUtil.getProperties(xDiagram, "ErrorIndicator");
178 
179 		SCUtil.closeFile(scDocument);
180 
181 		assertEquals("Incorrect chart Y error bar category got in ." + fileType + " file.", expCategory, result1);
182 		assertEquals("Incorrect chart Y error bar indicator got in ." + fileType + " file.", expIndicator, result2);
183 
184 	}
185 
186 }