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 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 
28 import java.util.Arrays;
29 import java.util.Collection;
30 
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.junit.runners.Parameterized.Parameters;
39 import org.openoffice.test.uno.UnoApp;
40 
41 import testlib.uno.SCUtil;
42 
43 import com.sun.star.awt.Rectangle;
44 import com.sun.star.chart.XChartDocument;
45 import com.sun.star.drawing.XShape;
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 the chart title/subtitle can be created, modified and saved
53  *
54  */
55 @RunWith(value = Parameterized.class)
56 public class ChartTitle {
57 
58 	private String[] titles;
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 				{1, 2, 3, 4},
72 				{2, 4.3, 5, 8},
73 				{4, 2, 3, 1},
74 				{1, -1, 0, -3}
75 		};
76 		String[][] titles = {
77 				{"MyMainTitle", "MySubTitle"},
78 				{"A Main Title With Space", "   Sub Title "},
79 				{"  ", "      "}
80 		};
81 
82 		return Arrays.asList(new Object[][] {
83 			{titles[0], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
84 			{titles[1], "com.sun.star.chart.NetDiagram", numberData1, "ods"},
85 			{titles[2], "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
86 			{titles[0], "com.sun.star.chart.PieDiagram", numberData1, "xls"},
87 			{titles[1], "com.sun.star.chart.NetDiagram", numberData1, "xls"},
88 			{titles[2], "com.sun.star.chart.XYDiagram", numberData1, "xls"}
89 		});
90 	}
91 
ChartTitle(String[] titles, String inputType, double[][] numberData, String fileType)92 	public ChartTitle(String[] titles, String inputType, double[][] numberData, String fileType) {
93 		this.titles = titles;
94 		this.inputType = inputType;
95 		this.numberData = numberData;
96 		this.fileType = fileType;
97 	}
98 
99 
100 	@Before
setUp()101 	public void setUp() throws Exception {
102 		scComponent = unoApp.newDocument("scalc");
103 		scDocument = SCUtil.getSCDocument(scComponent);
104 	}
105 
106 	@After
tearDown()107 	public void tearDown() throws Exception {
108 		unoApp.closeDocument(scComponent);
109 
110 	}
111 
112 	@BeforeClass
setUpConnection()113 	public static void setUpConnection() throws Exception {
114 		unoApp.start();
115 	}
116 
117 	@AfterClass
tearDownConnection()118 	public static void tearDownConnection() throws InterruptedException, Exception {
119 		unoApp.close();
120 		SCUtil.clearTempDir();
121 	}
122 
123 	/**
124 	 * Create main title in chart.
125 	 * 1. Create a spreadsheet file.
126 	 * 2. Input number in a cell range and create a chart.
127 	 * 3. Create main title in chart.
128 	 * 4. Save file as ODF/MSBinary format.
129 	 * 5. Close and reopen file.  -> Check the chart main title.
130 	 * @throws Exception
131 	 */
132 	@Test
testCreateMainTitle()133 	public void testCreateMainTitle() throws Exception {
134 		String fileName = "testCreateMainTitle";
135 		String chartName = "testChart";
136 		String cellRangeName = "A1:D4";
137 		Boolean result = null;
138 		String resultTitle = null;
139 		String defaultTitle = null;
140 
141 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
142 			cellRangeName = "A1:C4";
143 		}
144 		if (fileType.equalsIgnoreCase("xls")) {
145 			chartName = "Object 1";
146 		}
147 
148 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
149 
150 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
151 
152 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
153 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
154 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
155 		XChartDocument xChartDocument = null;
156 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
157 		SCUtil.setChartType(xChartDocument, inputType);
158 
159 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasMainTitle");
160 		if (!result) {
161 			SCUtil.setProperties(xChartDocument, "HasMainTitle", true);
162 		}
163 		XShape aTitle = xChartDocument.getTitle();
164 		defaultTitle = (String) SCUtil.getProperties(aTitle, "String");
165 
166 		SCUtil.saveFileAs(scComponent, fileName, fileType);
167 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
168 		sheet = SCUtil.getCurrentSheet(scDocument);
169 
170 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
171 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasMainTitle");
172 		resultTitle = (String) SCUtil.getProperties(xChartDocument.getTitle(), "String");
173 		SCUtil.closeFile(scDocument);
174 
175 		assertTrue("Chart title has not be created in ." + fileType + " file.", result);
176 		assertEquals("Incorrect chart title got in ." + fileType + " file.", defaultTitle, resultTitle);
177 
178 	}
179 
180 	/**
181 	 * Create sub title in chart.
182 	 * 1. Create a spreadsheet file.
183 	 * 2. Input number in a cell range and create a chart.
184 	 * 3. Create subtitle in chart.
185 	 * 4. Save file as ODF/MSBinary format.
186 	 * 5. Close and reopen file.  -> Check the chart main title.
187 	 * @throws Exception
188 	 */
189 	@Test
testCreateSubTitle()190 	public void testCreateSubTitle() throws Exception {
191 		String fileName = "testCreateSubTitle";
192 		String chartName = "testChart";
193 		String cellRangeName = "A1:D4";
194 		Boolean result = null;
195 		String resultTitle = null;
196 		String defaultTitle = null;
197 
198 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
199 			cellRangeName = "A1:C4";
200 		}
201 		if (fileType.equalsIgnoreCase("xls")) {
202 			chartName = "Object 1";
203 		}
204 
205 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
206 
207 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
208 
209 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
210 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
211 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
212 		XChartDocument xChartDocument = null;
213 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
214 		SCUtil.setChartType(xChartDocument, inputType);
215 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasSubTitle");
216 		if (!result) {
217 			SCUtil.setProperties(xChartDocument, "HasSubTitle", true);
218 		}
219 		XShape aSubTitle = xChartDocument.getSubTitle();
220 		defaultTitle = (String) SCUtil.getProperties(aSubTitle, "String");
221 
222 		SCUtil.saveFileAs(scComponent, fileName, fileType);
223 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
224 		sheet = SCUtil.getCurrentSheet(scDocument);
225 
226 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
227 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasSubTitle");
228 		resultTitle = (String) SCUtil.getProperties(xChartDocument.getSubTitle(), "String");
229 		SCUtil.closeFile(scDocument);
230 
231 
232 		if (fileType.equalsIgnoreCase("xls")) {
233 			assertFalse("Chart subtitle should not be saved in ." + fileType + " file.", result);
234 		}
235 		else {
236 			assertTrue("Chart subtitle has not be created in ." + fileType + " file.", result);
237 			assertEquals("Incorrect chart subtitle got in ." + fileType + " file.", defaultTitle, resultTitle);
238 		}
239 
240 	}
241 
242 	/**
243 	 * Create titles in chart and change title string.
244 	 * 1. Create a spreadsheet file.
245 	 * 2. Input number in a cell range and create a chart.
246 	 * 3. Create main title and subtitle in chart, input customized string in title box.
247 	 * 4. Save file as ODF/MSBinary format.
248 	 * 5. Close and reopen file.  -> Check the chart titles.
249 	 * @throws Exception
250 	 */
251 	@Test
testInputTitles()252 	public void testInputTitles() throws Exception {
253 		String fileName = "testInputTitles";
254 		String chartName = "testChart";
255 		String cellRangeName = "A1:D4";
256 		Boolean[] result = new Boolean[2];
257 		String[] resultTitle = new String[2];
258 
259 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
260 			cellRangeName = "A1:C4";
261 		}
262 		if (fileType.equalsIgnoreCase("xls")) {
263 			chartName = "Object 1";
264 		}
265 
266 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
267 
268 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
269 
270 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
271 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
272 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
273 		XChartDocument xChartDocument = null;
274 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
275 		SCUtil.setChartType(xChartDocument, inputType);
276 		SCUtil.setProperties(xChartDocument, "HasMainTitle", true);
277 		SCUtil.setProperties(xChartDocument, "HasSubTitle", true);
278 
279 		SCUtil.setProperties(xChartDocument.getTitle(), "String", titles[0]);
280 		SCUtil.setProperties(xChartDocument.getSubTitle(), "String", titles[1]);
281 
282 		SCUtil.saveFileAs(scComponent, fileName, fileType);
283 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
284 		sheet = SCUtil.getCurrentSheet(scDocument);
285 
286 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
287 		result[0] = (Boolean) SCUtil.getProperties(xChartDocument, "HasMainTitle");
288 		result[1] = (Boolean) SCUtil.getProperties(xChartDocument, "HasSubTitle");
289 		resultTitle[0] = (String) SCUtil.getProperties(xChartDocument.getTitle(), "String");
290 		resultTitle[1] = (String) SCUtil.getProperties(xChartDocument.getSubTitle(), "String");
291 		SCUtil.closeFile(scDocument);
292 
293 		assertTrue("Chart main title has not be created in ." + fileType + " file.", result[0]);
294 		assertEquals("Incorrect chart title got in ." + fileType + " file.", titles[0], resultTitle[0]);
295 		if (fileType.equalsIgnoreCase("xls")) {
296 			assertFalse("Chart subtitle should not be saved in ." + fileType + " file.", result[1]);
297 		}
298 		else {
299 			assertTrue("Chart subtitle has not be created in ." + fileType + " file.", result[1]);
300 			assertEquals("Incorrect chart subtitle got in ." + fileType + " file.", titles[1], resultTitle[1]);
301 		}
302 
303 	}
304 
305 }