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 package fvt.uno.sc.data;
22 
23 import static org.junit.Assert.*;
24 
25 import java.util.Arrays;
26 import java.util.Collection;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Parameterized;
34 import org.junit.runners.Parameterized.Parameters;
35 import org.openoffice.test.common.Testspace;
36 import org.openoffice.test.uno.UnoApp;
37 import testlib.uno.SCUtil;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.sheet.FilterOperator;
41 import com.sun.star.sheet.TableFilterField;
42 import com.sun.star.sheet.XSheetFilterDescriptor;
43 import com.sun.star.sheet.XSheetFilterable;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.table.XCellRange;
46 import com.sun.star.table.XColumnRowRange;
47 import com.sun.star.table.XTableRows;
48 import com.sun.star.uno.Enum;
49 import com.sun.star.uno.UnoRuntime;
50 
51 @RunWith(value = Parameterized.class)
52 public class StardarFilterTest {
53 	UnoApp unoApp = new UnoApp();
54 	XSpreadsheetDocument scDocument = null;
55 	XComponent scComponent = null;
56 	private String filename = "FilterTest.xls";
57 	private Enum operator;
58 	private int column;
59 	private boolean[] rowshow;
60 	private int value;
61 
62 	@Parameters
data()63 	public static Collection<Object[]> data() throws Exception {
64 		boolean[] rowshow1 = { true, true, false, true, false, false };
65 		boolean[] rowshow2 = { true, true, true, true, true, false };
66 		boolean[] rowshow3 = { true, false, false, true, false, false };
67 		boolean[] rowshow4 = { true, false, true, true, false, false };
68 		boolean[] rowshow5 = { true, false, false, false, false, true };
69 		boolean[] rowshow6 = { true, false, true, true, false, true };
70 		boolean[] rowshow7 = { true, false, true, false, false, false };
71 		boolean[] rowshow8 = { true, false, true, true, false, true };
72 		boolean[] rowshow9 = { true, true, true, false, true, true };
73 		boolean[] rowshow10 = { true, true, false, false, true, true };
74 		boolean[] rowshow11 = { true, true, false, false, false, false };
75 		boolean[] rowshow12 = { true, true, true, true, false, false };
76 
77 		return Arrays.asList(new Object[][] { { FilterOperator.BOTTOM_PERCENT, 1, 30, rowshow1 }, { FilterOperator.BOTTOM_VALUES, 2, 4, rowshow2 },
78 				{ FilterOperator.EMPTY, 5, 1, rowshow3 }, { FilterOperator.EQUAL, 2, 44, rowshow4 }, { FilterOperator.GREATER, 2, 44, rowshow5 },
79 				{ FilterOperator.GREATER_EQUAL, 2, 44, rowshow6 }, { FilterOperator.LESS, 3, 155, rowshow7 }, { FilterOperator.LESS_EQUAL, 3, 155, rowshow8 },
80 				{ FilterOperator.NOT_EMPTY, 5, 44, rowshow9 }, { FilterOperator.NOT_EQUAL, 4, 85, rowshow10 }, { FilterOperator.TOP_PERCENT, 4, 30, rowshow11 },
81 				{ FilterOperator.TOP_VALUES, 4, 3, rowshow12 }, });
82 	}
83 
84 	@Before
setUpDocument()85 	public void setUpDocument() throws Exception {
86 		unoApp.start();
87 	}
88 
89 	@After
tearDownDocument()90 	public void tearDownDocument() {
91 		unoApp.close();
92 		unoApp.closeDocument(scComponent);
93 
94 	}
95 
96 	@BeforeClass
setUpConnection()97 	public static void setUpConnection() throws Exception {
98 
99 	}
100 
101 	@AfterClass
tearDownConnection()102 	public static void tearDownConnection() throws InterruptedException, Exception {
103 
104 	}
105 
StardarFilterTest(Enum operator, int column, int value, boolean[] rowshow)106 	public StardarFilterTest(Enum operator, int column, int value, boolean[] rowshow) {
107 		this.operator = operator;
108 		this.column = column;
109 		this.rowshow = rowshow;
110 		this.value = value;
111 	}
112 
113 	/**
114 	 * test standard filter with very criteria
115 	 */
116 	@Test
testStandardFilter()117 	public void testStandardFilter() throws Exception {
118 		String sample = Testspace.prepareData(filename);
119 
120 		// Open document
121 		scDocument = SCUtil.openFile(sample, unoApp);
122 
123 		// Get cell range
124 		XCellRange xdataRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, SCUtil.getCurrentSheet(scDocument));
125 
126 		// Set filter property and filter the cell range
127 		XSheetFilterable xFilter = (XSheetFilterable) UnoRuntime.queryInterface(XSheetFilterable.class, xdataRange.getCellRangeByName("A1:F6"));
128 		XSheetFilterDescriptor xFilterDesc = xFilter.createFilterDescriptor(true);
129 		TableFilterField[] aFilterFields = new TableFilterField[1];
130 		aFilterFields[0] = new TableFilterField();
131 		aFilterFields[0].Field = column;
132 		aFilterFields[0].IsNumeric = true;
133 		aFilterFields[0].Operator = (FilterOperator) operator;
134 		aFilterFields[0].NumericValue = value;
135 		xFilterDesc.setFilterFields(aFilterFields);
136 		XPropertySet xFilterProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xFilterDesc);
137 		xFilterProp.setPropertyValue("ContainsHeader", new Boolean(true));
138 		xFilter.filter(xFilterDesc);
139 
140 		// Verify filter result
141 		XColumnRowRange ColRowRange = (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xdataRange.getCellRangeByName("A1:F6"));
142 		XTableRows Rows = ColRowRange.getRows();
143 		for (int i = 0; i < Rows.getCount() - 1; i++) {
144 			Object aRowObj = Rows.getByIndex(i);
145 			XPropertySet PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aRowObj);
146 			assertEquals(rowshow[i], (Boolean) PropSet.getPropertyValue("IsVisible"));
147 		}
148 
149 		// Save and reload the document
150 		SCUtil.save(scDocument);
151 		SCUtil.closeFile(scDocument);
152 		scDocument = SCUtil.openFile(sample, unoApp);
153 
154 		// Verify the result agains
155 		xdataRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, SCUtil.getCurrentSheet(scDocument));
156 		ColRowRange = (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xdataRange.getCellRangeByName("A1:F6"));
157 		Rows = ColRowRange.getRows();
158 		for (int i = 0; i < Rows.getCount() - 1; i++) {
159 			Object aRowObj = Rows.getByIndex(i);
160 			XPropertySet PropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, aRowObj);
161 			assertEquals(rowshow[i], (Boolean) PropSet.getPropertyValue("IsVisible"));
162 		}
163 		SCUtil.save(scDocument);
164 	}
165 }
166