Kaydet (Commit) 78a2ef14 authored tarafından Liu Zhe's avatar Liu Zhe

#120671 - [testUNO patch] Add methods in SCUtil and TestUtil, create test case…

#120671 - [testUNO patch] Add methods in SCUtil and TestUtil, create test case to check cell alignment.

Patch by: Zhu Shan <shanzhu33@gmail.com>
Review by: Liu Zhe <aliuzhe@gmail.com>
üst 52580050
/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
package testcase.uno.sc;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openoffice.test.uno.UnoApp;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.XComponent;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheetView;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.table.XCell;
import com.sun.star.uno.UnoRuntime;
/**
* Check the addition operator works in formula
* @author test
*
*/
@RunWith(value=Parameterized.class)
public class AddtionOperatorInFormula {
private double[] inputData;
private double expected;
@Parameters
public static Collection<Object[]> data(){
double[] input1 = new double[] {1.34, 2004.1234};
double[] input2 = new double[] {-0.4, -0.73};
double[] input3 = new double[] {5.25, -0.35, 11.23, 45, -123.111};
double[] input4 = new double[] {-0, 0, 0, -0.0000};
return Arrays.asList(new Object[][]{
{addtionExpectedData(input1), input1},
{addtionExpectedData(input2), input2},
{addtionExpectedData(input3), input3},
{addtionExpectedData(input4), input4}
});
}
public AddtionOperatorInFormula(double expected, double[] inputData) {
this.inputData = inputData;
this.expected = expected;
}
UnoApp unoApp = new UnoApp();
XSpreadsheetDocument scDocument = null;
XComponent scComponent = null;
@Before
public void setUp() throws Exception {
unoApp.start();
}
@After
public void tearDown() throws Exception {
unoApp.closeDocument(scComponent);
unoApp.close();
}
@Test
public void testAddtion() throws Exception {
String sheetname = "AddTest";
String inputformula = null;
double cellvalue = 0;
//Create Spreadsheet file.
scComponent = unoApp.newDocument("scalc");
scDocument = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, scComponent);
//Create a sheet at the first place.
XSpreadsheets spreadsheets = scDocument.getSheets();
spreadsheets.insertNewByName(sheetname, (short) 0);
Object sheetObj = spreadsheets.getByName(sheetname);
XSpreadsheet sheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheetObj);
//Active the new sheet.
XModel scModel = (XModel) UnoRuntime.queryInterface(XModel.class, scDocument);
XController scController = scModel.getCurrentController();
XSpreadsheetView sheetview = (XSpreadsheetView) UnoRuntime.queryInterface(XSpreadsheetView.class, scController);
sheetview.setActiveSheet(sheet);
//Input formula string in cell A1.
XCell cell = sheet.getCellByPosition(0, 0);
inputformula = toFormula(createFormulaString(inputData, "+"));
cell.setFormula(inputformula);
//Get the formula calculation result.
cellvalue = cell.getValue();
//Verify whether the actual result equal to the expected.
assertEquals("Unexpected calculate result.", expected, cellvalue, 0);
}
//Create formula string
private static String toFormula(String expression) {
return "=" + expression;
}
private static String createFormulaString(double[] inputData, String operator) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < inputData.length; i++) {
buffer.append(inputData[i]);
if (i < inputData.length - 1) {
buffer.append(operator);
}
}
return buffer.toString();
}
//Calculate the expected result
private static double addtionExpectedData(double[] inputData){
double data = 0;
for (double input : inputData) {
data += input;
}
return data;
}
}
/**************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*************************************************************/
package testcase.uno.sc.cell;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openoffice.test.uno.UnoApp;
import testlib.uno.SCUtil;
import testlib.uno.TestUtil;
import testlib.uno.CellInfo;
import com.sun.star.lang.XComponent;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.table.CellHoriJustify;
import com.sun.star.table.CellVertJustify;
import com.sun.star.table.XCell;
import com.sun.star.uno.Enum;
/**
* Check the cell alignment setting can be applied and saved
*
*/
@RunWith(value = Parameterized.class)
public class CellAlignment {
private Enum expected;
private String inputType;
private Enum inputValue;
private String fileType;
private static final UnoApp unoApp = new UnoApp();
XComponent scComponent = null;
XSpreadsheetDocument scDocument = null;
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{CellHoriJustify.STANDARD, "HoriJustify", CellHoriJustify.STANDARD, "ods"},
{CellHoriJustify.LEFT, "HoriJustify", CellHoriJustify.LEFT, "ods"},
{CellHoriJustify.CENTER, "HoriJustify", CellHoriJustify.CENTER, "ods"},
{CellHoriJustify.RIGHT, "HoriJustify", CellHoriJustify.RIGHT, "ods"},
{CellHoriJustify.BLOCK, "HoriJustify", CellHoriJustify.BLOCK, "ods"},
{CellHoriJustify.REPEAT, "HoriJustify", CellHoriJustify.REPEAT, "ods"},
{CellVertJustify.STANDARD, "VertJustify", CellVertJustify.STANDARD, "ods"},
{CellVertJustify.TOP, "VertJustify", CellVertJustify.TOP, "ods"},
{CellVertJustify.CENTER, "VertJustify", CellVertJustify.CENTER, "ods"},
{CellVertJustify.BOTTOM, "VertJustify", CellVertJustify.BOTTOM, "ods"},
{CellHoriJustify.STANDARD, "HoriJustify", CellHoriJustify.STANDARD, "xls"},
{CellHoriJustify.LEFT, "HoriJustify", CellHoriJustify.LEFT, "xls"},
{CellHoriJustify.CENTER, "HoriJustify", CellHoriJustify.CENTER, "xls"},
{CellHoriJustify.RIGHT, "HoriJustify", CellHoriJustify.RIGHT, "xls"},
{CellHoriJustify.BLOCK, "HoriJustify", CellHoriJustify.BLOCK, "xls"},
{CellHoriJustify.REPEAT, "HoriJustify", CellHoriJustify.REPEAT, "xls"},
{CellVertJustify.STANDARD, "VertJustify", CellVertJustify.STANDARD, "xls"},
{CellVertJustify.TOP, "VertJustify", CellVertJustify.TOP, "xls"},
{CellVertJustify.CENTER, "VertJustify", CellVertJustify.CENTER, "xls"}//,
// {CellVertJustify.BOTTOM, "VertJustify", CellVertJustify.BOTTOM, "xls"} Bug 120670
});
}
public CellAlignment(Enum expected, String inputType, Enum inputValue, String fileType) {
this.expected = expected;
this.inputType = inputType;
this.inputValue = inputValue;
this.fileType = fileType;
}
@Before
public void setUp() throws Exception {
scComponent = unoApp.newDocument("scalc");
scDocument = SCUtil.getSCDocument(scComponent);
}
@After
public void tearDown() throws Exception {
unoApp.closeDocument(scComponent);
}
@BeforeClass
public static void setUpConnection() throws Exception {
unoApp.start();
}
@AfterClass
public static void tearDownConnection() throws InterruptedException, Exception {
unoApp.close();
SCUtil.clearTempDir();
}
/**
* Check the cell alignment setting can be applied and saved
* 1. Create a spreadsheet file.
* 2. Input number, text, formula into many cell.
* 3. Set cell alignment.
* 4. Save file as ODF/MSBinary format.
* 5. Close and reopen file. -> Check the alignment setting.
* @throws Exception
*/
@Test
public void testCellAlignment() throws Exception {
String fileName = "testCellAlignment";
CellInfo cInfo = TestUtil.randCell(20, 50);
int cellNum = 5;
XCell[] cells = new XCell[cellNum];
Enum[] results = new Enum[cellNum];
XSpreadsheet sheet = SCUtil.getCurrentSheet(scDocument);
//cellNum must be greater than 4
if (cellNum < 5) {
cellNum = 5;
}
for (int i = 0; i < cellNum; i++) {
cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
}
cells[0].setValue(13.42);
SCUtil. setTextToCell(cells[1], "alignment");
cells[2].setFormula("=SUM(A100:B100)");
cells[3].setValue(-0.2343123);
for (int i = 0; i < cellNum; i++) {
SCUtil.setCellProperties(cells[i], inputType, inputValue);
}
SCUtil.saveFileAs(scComponent, fileName, fileType);
scDocument = SCUtil.reloadFile(unoApp, scDocument, fileName+"." + fileType);
sheet = SCUtil.getCurrentSheet(scDocument);
for (int i = 0; i < cellNum; i++) {
cells[i] = sheet.getCellByPosition(cInfo.getCol(), cInfo.getRow() + i);
results[i] = (Enum) SCUtil.getCellProperties(cells[i], inputType);
}
SCUtil.closeFile(scDocument);
for (int i = 0; i < cellNum; i++ ) {
assertEquals("Incorrect cell alignment(" + inputType + ") value got in ." + fileType + " file.", expected, results[i]);
}
}
}
......@@ -24,10 +24,12 @@ package testlib.uno;
import java.util.HashMap;
import org.openoffice.test.common.FileUtil;
import org.openoffice.test.common.Testspace;
import org.openoffice.test.uno.UnoApp;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XIndexAccess;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
......@@ -49,12 +51,12 @@ import com.sun.star.util.XCloseable;
/**
* Utilities of Spreadsheet
* @author test
*
*/
public class SCUtil {
private static final String scTempDir = "output/sc/"; //Spreadsheet temp file directory
private static HashMap filterName = new HashMap();
private SCUtil() {
......@@ -161,6 +163,17 @@ public class SCUtil {
xText.setString(text);
}
/**
* Set text into specific cell
* @param xCell
* @param text
* @throws Exception
*/
public static void setTextToCell(XCell xCell, String text) throws Exception {
XText xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
xText.setString(text);
}
/**
* Set formula into specific cell
* @param xSpreadsheet
......@@ -306,7 +319,7 @@ public class SCUtil {
String[][] cellTexts = new String[end_row - start_row+1][end_col - start_col +1];
for (int i = 0; i <= (end_row - start_row); i++ ) {
for(int j = 0; j <= (end_col - start_col); j++) {
for (int j = 0; j <= (end_col - start_col); j++) {
xCell = xCellRange.getCellByPosition(j, i);
xText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
cellTexts[i][j] = xText.getString();
......@@ -345,7 +358,41 @@ public class SCUtil {
}
/**
* Save file as specific file format into testspace/output folder.
* Set value of specific property from a cell
* @param xCell
* @param propName
* @param value
* @throws Exception
*/
public static void setCellProperties(XCell xCell, String propName, Object value) throws Exception {
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
xPropertySet.setPropertyValue(propName, value);
}
/**
* Get value of specific property from a cell
* @param xCell
* @param propName
* @return
* @throws Exception
*/
public static Object getCellProperties(XCell xCell, String propName) throws Exception {
XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCell);
Object value = xPropertySet.getPropertyValue(propName);
return value;
}
/**
* Clear temp file directory
*/
public static void clearTempDir() {
FileUtil.deleteFile(Testspace.getFile(Testspace.getPath(scTempDir)));
}
/**
* Save file as specific file format into spreadsheet temp file folder.
* @param scComponent
* @param fileName File name string without extension name (e.g. "sampleFile")
* @param extName ("ods", "ots", "xls", "xlt", "csv")
......@@ -355,7 +402,7 @@ public class SCUtil {
initFilterName();
String storeUrl = Testspace.getUrl("output/" + fileName + "." + extName);
String storeUrl = Testspace.getUrl(scTempDir + fileName + "." + extName);
PropertyValue[] storeProps = new PropertyValue[2];
storeProps[0] = new PropertyValue();
......@@ -381,7 +428,7 @@ public class SCUtil {
}
/**
* Close a opening file saved in testspace/output direction and reopen it in Spreadsheet. For save&reload test scenario only.
* Close a opening file saved in spreadsheet temp file direction and reopen it in Spreadsheet. For save&reload test scenario only.
* @param unoApp
* @param xSpreadsheetDocument
* @param fullFileName File name with the extension name. (e.g. "sc.ods")
......@@ -391,7 +438,7 @@ public class SCUtil {
public static XSpreadsheetDocument reloadFile(UnoApp unoApp, XSpreadsheetDocument xSpreadsheetDocument, String fullFileName) throws Exception {
closeFile(xSpreadsheetDocument);
String filePath = Testspace.getPath("output/" + fullFileName);
String filePath = Testspace.getPath(scTempDir + fullFileName);
XSpreadsheetDocument xScDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class, unoApp.loadDocument(filePath));
return xScDocument;
......
......@@ -93,20 +93,35 @@ public class TestUtil {
/**
* Generate a random decimal RGB color number in limited color space
* @param rMax The R value limit, [0, rMax)
* @param gMax The G value limit, [0, gMax)
* @param bMax The B value limit, [0, bMax)
* @param rMax The R value limit, get a value in [0, rMax]
* @param gMax The G value limit, get a value in [0, gMax]
* @param bMax The B value limit, get a value in [0, bMax]
* @return
* @throws Exception
*/
public static int randColor(int rMax, int gMax, int bMax) throws Exception {
int r = random.nextInt(rMax) % 256;
int g = random.nextInt(gMax) % 256;
int b = random.nextInt(bMax) % 256;
int r = random.nextInt(rMax + 1) % 256;
int g = random.nextInt(gMax + 1) % 256;
int b = random.nextInt(bMax + 1) % 256;
return r * 65536 + g * 256 + b;
}
/**
* Generate a series of decimal RGB color number
* @param size Set the quantity of random color value generated into the array
* @return
* @throws Exception
*/
public static int[] randColorList(int size) throws Exception {
int[] colorList = new int[size];
for (int i = 0; i < size; i++) {
colorList[i] = randColor();
}
return colorList;
}
/**
* Add "=" before a string
* @param expression
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment