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 
23 package ifc.table;
24 
25 import java.util.Random;
26 
27 import lib.MultiMethodTest;
28 
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.table.XAutoFormattable;
33 import com.sun.star.table.XCell;
34 import com.sun.star.table.XCellRange;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 
38 
39 /**
40 * Testing <code>com.sun.star.table.XAutoFormattable</code>
41 * interface methods :
42 * <ul>
43 *  <li><code> autoFormat()</code></li>
44 * </ul> <p>
45 * The component tested <b>must implement</b> interface
46 * <code>com.sun.star.table.XCellRange</code>. <p>
47 * Test is <b> NOT </b> multithread compilant. <p>
48 * @see com.sun.star.table.XAutoFormattable
49 */
50 public class _XAutoFormattable extends MultiMethodTest {
51     public XAutoFormattable oObj = null;
52 
53     /**
54     * First 'Default' autoformat is set and a background of a cell
55     * is obtained. Then any other autoformat is set and background
56     * of a cell is obtained again.<p>
57     * Has <b> OK </b> status if backgrounds with different autoformat
58     * settings are differ. <p>
59     */
_autoFormat()60     public void _autoFormat() {
61         boolean bResult = true;
62         XMultiServiceFactory oMSF = (XMultiServiceFactory) tParam.getMSF();
63         String name = "Default";
64 
65         try {
66             oObj.autoFormat(name); // applying default format
67 
68             // getting current background of the cell
69             XCellRange cellRange = (XCellRange) UnoRuntime.queryInterface(
70                                            XCellRange.class, oObj);
71             XCell oCell = cellRange.getCellByPosition(0, 0);
72             XPropertySet PS = (XPropertySet) UnoRuntime.queryInterface(
73                                       XPropertySet.class, oCell);
74 
75             Integer bkgrnd1;
76             try {
77                 bkgrnd1 = (Integer) PS.getPropertyValue("CellBackColor");
78             } catch (com.sun.star.beans.UnknownPropertyException e) {
79                 bkgrnd1 = (Integer) PS.getPropertyValue("BackColor");
80             }
81 
82             // getting formats names.
83             XInterface iFormats = (XInterface) oMSF.createInstance(
84                                           "com.sun.star.sheet.TableAutoFormats");
85             XNameAccess formats = (XNameAccess) UnoRuntime.queryInterface(
86                                           XNameAccess.class, iFormats);
87             String[] names = formats.getElementNames();
88 
89             // getting one random not default style name
90             Random rnd = new Random();
91 
92             if (names.length > 1) {
93                 while (name.equals("Default")) {
94                     name = names[rnd.nextInt(names.length)];
95                 }
96             } else {
97                 name = names[0];
98             }
99 
100             log.println("Applying style " + name);
101 
102 
103             // applying style
104             oObj.autoFormat(name);
105 
106             // getting new cell's backround.
107             Integer bkgrnd2;
108             try {
109                 bkgrnd2 = (Integer) PS.getPropertyValue("CellBackColor");
110             } catch (com.sun.star.beans.UnknownPropertyException e) {
111                 bkgrnd2 = (Integer) PS.getPropertyValue("BackColor");
112             }
113 
114             bResult &= !bkgrnd1.equals(bkgrnd2);
115         } catch (com.sun.star.uno.Exception e) {
116             log.println("Exception occured :");
117             e.printStackTrace(log);
118             bResult = false;
119         }
120 
121         tRes.tested("autoFormat()", bResult);
122     }
123 
124     /**
125     * Forces environment recreation.
126     */
after()127     protected void after() {
128         disposeEnvironment();
129     }
130 }