15e5a8699SLiu Zhe /**************************************************************
25e5a8699SLiu Zhe  *
35e5a8699SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
45e5a8699SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
55e5a8699SLiu Zhe  * distributed with this work for additional information
65e5a8699SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
75e5a8699SLiu Zhe  * to you under the Apache License, Version 2.0 (the
85e5a8699SLiu Zhe  * "License"); you may not use this file except in compliance
95e5a8699SLiu Zhe  * with the License.  You may obtain a copy of the License at
105e5a8699SLiu Zhe  *
115e5a8699SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
125e5a8699SLiu Zhe  *
135e5a8699SLiu Zhe  * Unless required by applicable law or agreed to in writing,
145e5a8699SLiu Zhe  * software distributed under the License is distributed on an
155e5a8699SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165e5a8699SLiu Zhe  * KIND, either express or implied.  See the License for the
175e5a8699SLiu Zhe  * specific language governing permissions and limitations
185e5a8699SLiu Zhe  * under the License.
195e5a8699SLiu Zhe  *
205e5a8699SLiu Zhe  *************************************************************/
215e5a8699SLiu Zhe 
22*eba4d44aSLiu Zhe package fvt.uno.sc.chart;
235e5a8699SLiu Zhe 
245e5a8699SLiu Zhe import static org.junit.Assert.assertEquals;
255e5a8699SLiu Zhe import static org.junit.Assert.assertFalse;
265e5a8699SLiu Zhe import static org.junit.Assert.assertTrue;
275e5a8699SLiu Zhe 
285e5a8699SLiu Zhe import java.util.Arrays;
295e5a8699SLiu Zhe import java.util.Collection;
305e5a8699SLiu Zhe 
315e5a8699SLiu Zhe import org.junit.After;
325e5a8699SLiu Zhe import org.junit.AfterClass;
335e5a8699SLiu Zhe import org.junit.Before;
345e5a8699SLiu Zhe import org.junit.BeforeClass;
355e5a8699SLiu Zhe import org.junit.Test;
365e5a8699SLiu Zhe import org.junit.runner.RunWith;
375e5a8699SLiu Zhe import org.junit.runners.Parameterized;
385e5a8699SLiu Zhe import org.junit.runners.Parameterized.Parameters;
395e5a8699SLiu Zhe import org.openoffice.test.uno.UnoApp;
405e5a8699SLiu Zhe 
415e5a8699SLiu Zhe import testlib.uno.SCUtil;
425e5a8699SLiu Zhe 
435e5a8699SLiu Zhe import com.sun.star.awt.Rectangle;
445e5a8699SLiu Zhe import com.sun.star.chart.XChartDocument;
455e5a8699SLiu Zhe import com.sun.star.drawing.XShape;
465e5a8699SLiu Zhe import com.sun.star.lang.XComponent;
475e5a8699SLiu Zhe import com.sun.star.sheet.XSpreadsheet;
485e5a8699SLiu Zhe import com.sun.star.sheet.XSpreadsheetDocument;
495e5a8699SLiu Zhe import com.sun.star.table.CellRangeAddress;
505e5a8699SLiu Zhe 
515e5a8699SLiu Zhe /**
525e5a8699SLiu Zhe  *  Check the chart title/subtitle can be created, modified and saved
535e5a8699SLiu Zhe  *
545e5a8699SLiu Zhe  */
555e5a8699SLiu Zhe @RunWith(value = Parameterized.class)
565e5a8699SLiu Zhe public class ChartTitle {
575e5a8699SLiu Zhe 
585e5a8699SLiu Zhe 	private String[] titles;
595e5a8699SLiu Zhe 	private String inputType;
605e5a8699SLiu Zhe 	private double[][] numberData;
615e5a8699SLiu Zhe 	private String fileType;
625e5a8699SLiu Zhe 
635e5a8699SLiu Zhe 	private static final UnoApp unoApp = new UnoApp();
645e5a8699SLiu Zhe 
655e5a8699SLiu Zhe 	XComponent scComponent = null;
665e5a8699SLiu Zhe 	XSpreadsheetDocument scDocument = null;
675e5a8699SLiu Zhe 
685e5a8699SLiu Zhe 	@Parameters
data()695e5a8699SLiu Zhe 	public static Collection<Object[]> data() throws Exception {
705e5a8699SLiu Zhe 		double[][] numberData1 = {
715e5a8699SLiu Zhe 				{1, 2, 3, 4},
725e5a8699SLiu Zhe 				{2, 4.3, 5, 8},
735e5a8699SLiu Zhe 				{4, 2, 3, 1},
745e5a8699SLiu Zhe 				{1, -1, 0, -3}
755e5a8699SLiu Zhe 		};
765e5a8699SLiu Zhe 		String[][] titles = {
775e5a8699SLiu Zhe 				{"MyMainTitle", "MySubTitle"},
785e5a8699SLiu Zhe 				{"A Main Title With Space", "   Sub Title "},
795e5a8699SLiu Zhe 				{"  ", "      "}
805e5a8699SLiu Zhe 		};
815e5a8699SLiu Zhe 
825e5a8699SLiu Zhe 		return Arrays.asList(new Object[][] {
835e5a8699SLiu Zhe 			{titles[0], "com.sun.star.chart.BarDiagram", numberData1, "ods"},
845e5a8699SLiu Zhe 			{titles[1], "com.sun.star.chart.NetDiagram", numberData1, "ods"},
855e5a8699SLiu Zhe 			{titles[2], "com.sun.star.chart.AreaDiagram", numberData1, "ods"},
865e5a8699SLiu Zhe 			{titles[0], "com.sun.star.chart.PieDiagram", numberData1, "xls"},
875e5a8699SLiu Zhe 			{titles[1], "com.sun.star.chart.NetDiagram", numberData1, "xls"},
885e5a8699SLiu Zhe 			{titles[2], "com.sun.star.chart.XYDiagram", numberData1, "xls"}
895e5a8699SLiu Zhe 		});
905e5a8699SLiu Zhe 	}
915e5a8699SLiu Zhe 
ChartTitle(String[] titles, String inputType, double[][] numberData, String fileType)925e5a8699SLiu Zhe 	public ChartTitle(String[] titles, String inputType, double[][] numberData, String fileType) {
935e5a8699SLiu Zhe 		this.titles = titles;
945e5a8699SLiu Zhe 		this.inputType = inputType;
955e5a8699SLiu Zhe 		this.numberData = numberData;
965e5a8699SLiu Zhe 		this.fileType = fileType;
975e5a8699SLiu Zhe 	}
985e5a8699SLiu Zhe 
995e5a8699SLiu Zhe 
1005e5a8699SLiu Zhe 	@Before
setUp()1015e5a8699SLiu Zhe 	public void setUp() throws Exception {
1025e5a8699SLiu Zhe 		scComponent = unoApp.newDocument("scalc");
1035e5a8699SLiu Zhe 		scDocument = SCUtil.getSCDocument(scComponent);
1045e5a8699SLiu Zhe 	}
1055e5a8699SLiu Zhe 
1065e5a8699SLiu Zhe 	@After
tearDown()1075e5a8699SLiu Zhe 	public void tearDown() throws Exception {
1085e5a8699SLiu Zhe 		unoApp.closeDocument(scComponent);
1095e5a8699SLiu Zhe 
1105e5a8699SLiu Zhe 	}
1115e5a8699SLiu Zhe 
1125e5a8699SLiu Zhe 	@BeforeClass
setUpConnection()1135e5a8699SLiu Zhe 	public static void setUpConnection() throws Exception {
1145e5a8699SLiu Zhe 		unoApp.start();
1155e5a8699SLiu Zhe 	}
1165e5a8699SLiu Zhe 
1175e5a8699SLiu Zhe 	@AfterClass
tearDownConnection()1185e5a8699SLiu Zhe 	public static void tearDownConnection() throws InterruptedException, Exception {
1195e5a8699SLiu Zhe 		unoApp.close();
1205e5a8699SLiu Zhe 		SCUtil.clearTempDir();
1215e5a8699SLiu Zhe 	}
1225e5a8699SLiu Zhe 
1235e5a8699SLiu Zhe 	/**
1245e5a8699SLiu Zhe 	 * Create main title in chart.
1255e5a8699SLiu Zhe 	 * 1. Create a spreadsheet file.
1265e5a8699SLiu Zhe 	 * 2. Input number in a cell range and create a chart.
1275e5a8699SLiu Zhe 	 * 3. Create main title in chart.
1285e5a8699SLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
1295e5a8699SLiu Zhe 	 * 5. Close and reopen file.  -> Check the chart main title.
1305e5a8699SLiu Zhe 	 * @throws Exception
1315e5a8699SLiu Zhe 	 */
1325e5a8699SLiu Zhe 	@Test
testCreateMainTitle()1335e5a8699SLiu Zhe 	public void testCreateMainTitle() throws Exception {
1345e5a8699SLiu Zhe 		String fileName = "testCreateMainTitle";
1355e5a8699SLiu Zhe 		String chartName = "testChart";
1365e5a8699SLiu Zhe 		String cellRangeName = "A1:D4";
1375e5a8699SLiu Zhe 		Boolean result = null;
1385e5a8699SLiu Zhe 		String resultTitle = null;
1395e5a8699SLiu Zhe 		String defaultTitle = null;
1405e5a8699SLiu Zhe 
1415e5a8699SLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
1425e5a8699SLiu Zhe 			cellRangeName = "A1:C4";
1435e5a8699SLiu Zhe 		}
1445e5a8699SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
1455e5a8699SLiu Zhe 			chartName = "Object 1";
1465e5a8699SLiu Zhe 		}
1475e5a8699SLiu Zhe 
1485e5a8699SLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
1495e5a8699SLiu Zhe 
1505e5a8699SLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
1515e5a8699SLiu Zhe 
1525e5a8699SLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
1535e5a8699SLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
1545e5a8699SLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
1555e5a8699SLiu Zhe 		XChartDocument xChartDocument = null;
1565e5a8699SLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
1575e5a8699SLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
1585e5a8699SLiu Zhe 
1595e5a8699SLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasMainTitle");
1605e5a8699SLiu Zhe 		if (!result) {
1615e5a8699SLiu Zhe 			SCUtil.setProperties(xChartDocument, "HasMainTitle", true);
1625e5a8699SLiu Zhe 		}
1635e5a8699SLiu Zhe 		XShape aTitle = xChartDocument.getTitle();
1645e5a8699SLiu Zhe 		defaultTitle = (String) SCUtil.getProperties(aTitle, "String");
1655e5a8699SLiu Zhe 
1665e5a8699SLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
1675e5a8699SLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
1685e5a8699SLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
1695e5a8699SLiu Zhe 
1705e5a8699SLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
1715e5a8699SLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasMainTitle");
1725e5a8699SLiu Zhe 		resultTitle = (String) SCUtil.getProperties(xChartDocument.getTitle(), "String");
1735e5a8699SLiu Zhe 		SCUtil.closeFile(scDocument);
1745e5a8699SLiu Zhe 
1755e5a8699SLiu Zhe 		assertTrue("Chart title has not be created in ." + fileType + " file.", result);
1765e5a8699SLiu Zhe 		assertEquals("Incorrect chart title got in ." + fileType + " file.", defaultTitle, resultTitle);
1775e5a8699SLiu Zhe 
1785e5a8699SLiu Zhe 	}
1795e5a8699SLiu Zhe 
1805e5a8699SLiu Zhe 	/**
1815e5a8699SLiu Zhe 	 * Create sub title in chart.
1825e5a8699SLiu Zhe 	 * 1. Create a spreadsheet file.
1835e5a8699SLiu Zhe 	 * 2. Input number in a cell range and create a chart.
1845e5a8699SLiu Zhe 	 * 3. Create subtitle in chart.
1855e5a8699SLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
1865e5a8699SLiu Zhe 	 * 5. Close and reopen file.  -> Check the chart main title.
1875e5a8699SLiu Zhe 	 * @throws Exception
1885e5a8699SLiu Zhe 	 */
1895e5a8699SLiu Zhe 	@Test
testCreateSubTitle()1905e5a8699SLiu Zhe 	public void testCreateSubTitle() throws Exception {
1915e5a8699SLiu Zhe 		String fileName = "testCreateSubTitle";
1925e5a8699SLiu Zhe 		String chartName = "testChart";
1935e5a8699SLiu Zhe 		String cellRangeName = "A1:D4";
1945e5a8699SLiu Zhe 		Boolean result = null;
1955e5a8699SLiu Zhe 		String resultTitle = null;
1965e5a8699SLiu Zhe 		String defaultTitle = null;
1975e5a8699SLiu Zhe 
1985e5a8699SLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
1995e5a8699SLiu Zhe 			cellRangeName = "A1:C4";
2005e5a8699SLiu Zhe 		}
2015e5a8699SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
2025e5a8699SLiu Zhe 			chartName = "Object 1";
2035e5a8699SLiu Zhe 		}
2045e5a8699SLiu Zhe 
2055e5a8699SLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
2065e5a8699SLiu Zhe 
2075e5a8699SLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
2085e5a8699SLiu Zhe 
2095e5a8699SLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
2105e5a8699SLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
2115e5a8699SLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
2125e5a8699SLiu Zhe 		XChartDocument xChartDocument = null;
2135e5a8699SLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
2145e5a8699SLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
2155e5a8699SLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasSubTitle");
2165e5a8699SLiu Zhe 		if (!result) {
2175e5a8699SLiu Zhe 			SCUtil.setProperties(xChartDocument, "HasSubTitle", true);
2185e5a8699SLiu Zhe 		}
2195e5a8699SLiu Zhe 		XShape aSubTitle = xChartDocument.getSubTitle();
2205e5a8699SLiu Zhe 		defaultTitle = (String) SCUtil.getProperties(aSubTitle, "String");
2215e5a8699SLiu Zhe 
2225e5a8699SLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
2235e5a8699SLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
2245e5a8699SLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
2255e5a8699SLiu Zhe 
2265e5a8699SLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
2275e5a8699SLiu Zhe 		result = (Boolean) SCUtil.getProperties(xChartDocument, "HasSubTitle");
2285e5a8699SLiu Zhe 		resultTitle = (String) SCUtil.getProperties(xChartDocument.getSubTitle(), "String");
2295e5a8699SLiu Zhe 		SCUtil.closeFile(scDocument);
2305e5a8699SLiu Zhe 
2315e5a8699SLiu Zhe 
2325e5a8699SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
2335e5a8699SLiu Zhe 			assertFalse("Chart subtitle should not be saved in ." + fileType + " file.", result);
2345e5a8699SLiu Zhe 		}
2355e5a8699SLiu Zhe 		else {
2365e5a8699SLiu Zhe 			assertTrue("Chart subtitle has not be created in ." + fileType + " file.", result);
2375e5a8699SLiu Zhe 			assertEquals("Incorrect chart subtitle got in ." + fileType + " file.", defaultTitle, resultTitle);
2385e5a8699SLiu Zhe 		}
2395e5a8699SLiu Zhe 
2405e5a8699SLiu Zhe 	}
2415e5a8699SLiu Zhe 
2425e5a8699SLiu Zhe 	/**
2435e5a8699SLiu Zhe 	 * Create titles in chart and change title string.
2445e5a8699SLiu Zhe 	 * 1. Create a spreadsheet file.
2455e5a8699SLiu Zhe 	 * 2. Input number in a cell range and create a chart.
2465e5a8699SLiu Zhe 	 * 3. Create main title and subtitle in chart, input customized string in title box.
2475e5a8699SLiu Zhe 	 * 4. Save file as ODF/MSBinary format.
2485e5a8699SLiu Zhe 	 * 5. Close and reopen file.  -> Check the chart titles.
2495e5a8699SLiu Zhe 	 * @throws Exception
2505e5a8699SLiu Zhe 	 */
2515e5a8699SLiu Zhe 	@Test
testInputTitles()2525e5a8699SLiu Zhe 	public void testInputTitles() throws Exception {
2535e5a8699SLiu Zhe 		String fileName = "testInputTitles";
2545e5a8699SLiu Zhe 		String chartName = "testChart";
2555e5a8699SLiu Zhe 		String cellRangeName = "A1:D4";
2565e5a8699SLiu Zhe 		Boolean[] result = new Boolean[2];
2575e5a8699SLiu Zhe 		String[] resultTitle = new String[2];
2585e5a8699SLiu Zhe 
2595e5a8699SLiu Zhe 		if (inputType.equals("com.sun.star.chart.StockDiagram")) {
2605e5a8699SLiu Zhe 			cellRangeName = "A1:C4";
2615e5a8699SLiu Zhe 		}
2625e5a8699SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
2635e5a8699SLiu Zhe 			chartName = "Object 1";
2645e5a8699SLiu Zhe 		}
2655e5a8699SLiu Zhe 
2665e5a8699SLiu Zhe 		XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
2675e5a8699SLiu Zhe 
2685e5a8699SLiu Zhe 		SCUtil.setValueToCellRange(sheet, 0, 0, numberData);
2695e5a8699SLiu Zhe 
2705e5a8699SLiu Zhe 		CellRangeAddress[] cellAddress = new CellRangeAddress[1];
2715e5a8699SLiu Zhe 		cellAddress[0] = SCUtil.getChartDataRangeByName(sheet, cellRangeName);
2725e5a8699SLiu Zhe 		Rectangle rectangle = new Rectangle(1000, 1000, 15000, 9500);
2735e5a8699SLiu Zhe 		XChartDocument xChartDocument = null;
2745e5a8699SLiu Zhe 		xChartDocument = SCUtil.createChart(sheet, rectangle, cellAddress, chartName);
2755e5a8699SLiu Zhe 		SCUtil.setChartType(xChartDocument, inputType);
2765e5a8699SLiu Zhe 		SCUtil.setProperties(xChartDocument, "HasMainTitle", true);
2775e5a8699SLiu Zhe 		SCUtil.setProperties(xChartDocument, "HasSubTitle", true);
2785e5a8699SLiu Zhe 
2795e5a8699SLiu Zhe 		SCUtil.setProperties(xChartDocument.getTitle(), "String", titles[0]);
2805e5a8699SLiu Zhe 		SCUtil.setProperties(xChartDocument.getSubTitle(), "String", titles[1]);
2815e5a8699SLiu Zhe 
2825e5a8699SLiu Zhe 		SCUtil.saveFileAs(scComponent, fileName, fileType);
2835e5a8699SLiu Zhe 		scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName + "." + fileType);
2845e5a8699SLiu Zhe 		sheet = SCUtil.getCurrentSheet(scDocument);
2855e5a8699SLiu Zhe 
2865e5a8699SLiu Zhe 		xChartDocument = SCUtil.getChartByName(sheet, chartName);
2875e5a8699SLiu Zhe 		result[0] = (Boolean) SCUtil.getProperties(xChartDocument, "HasMainTitle");
2885e5a8699SLiu Zhe 		result[1] = (Boolean) SCUtil.getProperties(xChartDocument, "HasSubTitle");
2895e5a8699SLiu Zhe 		resultTitle[0] = (String) SCUtil.getProperties(xChartDocument.getTitle(), "String");
2905e5a8699SLiu Zhe 		resultTitle[1] = (String) SCUtil.getProperties(xChartDocument.getSubTitle(), "String");
2915e5a8699SLiu Zhe 		SCUtil.closeFile(scDocument);
2925e5a8699SLiu Zhe 
2935e5a8699SLiu Zhe 		assertTrue("Chart main title has not be created in ." + fileType + " file.", result[0]);
2945e5a8699SLiu Zhe 		assertEquals("Incorrect chart title got in ." + fileType + " file.", titles[0], resultTitle[0]);
2955e5a8699SLiu Zhe 		if (fileType.equalsIgnoreCase("xls")) {
2965e5a8699SLiu Zhe 			assertFalse("Chart subtitle should not be saved in ." + fileType + " file.", result[1]);
2975e5a8699SLiu Zhe 		}
2985e5a8699SLiu Zhe 		else {
2995e5a8699SLiu Zhe 			assertTrue("Chart subtitle has not be created in ." + fileType + " file.", result[1]);
3005e5a8699SLiu Zhe 			assertEquals("Incorrect chart subtitle got in ." + fileType + " file.", titles[1], resultTitle[1]);
3015e5a8699SLiu Zhe 		}
3025e5a8699SLiu Zhe 
3035e5a8699SLiu Zhe 	}
3045e5a8699SLiu Zhe 
3055e5a8699SLiu Zhe }