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