1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package ifc.table;
28 
29 import java.util.Random;
30 
31 import lib.MultiMethodTest;
32 
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.table.XAutoFormattable;
37 import com.sun.star.table.XCell;
38 import com.sun.star.table.XCellRange;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 
42 
43 /**
44 * Testing <code>com.sun.star.table.XAutoFormattable</code>
45 * interface methods :
46 * <ul>
47 *  <li><code> autoFormat()</code></li>
48 * </ul> <p>
49 * The component tested <b>must implement</b> interface
50 * <code>com.sun.star.table.XCellRange</code>. <p>
51 * Test is <b> NOT </b> multithread compilant. <p>
52 * @see com.sun.star.table.XAutoFormattable
53 */
54 public class _XAutoFormattable extends MultiMethodTest {
55     public XAutoFormattable oObj = null;
56 
57     /**
58     * First 'Default' autoformat is set and a background of a cell
59     * is obtained. Then any other autoformat is set and background
60     * of a cell is obtained again.<p>
61     * Has <b> OK </b> status if backgrounds with different autoformat
62     * settings are differ. <p>
63     */
64     public void _autoFormat() {
65         boolean bResult = true;
66         XMultiServiceFactory oMSF = (XMultiServiceFactory) tParam.getMSF();
67         String name = "Default";
68 
69         try {
70             oObj.autoFormat(name); // applying default format
71 
72             // getting current background of the cell
73             XCellRange cellRange = (XCellRange) UnoRuntime.queryInterface(
74                                            XCellRange.class, oObj);
75             XCell oCell = cellRange.getCellByPosition(0, 0);
76             XPropertySet PS = (XPropertySet) UnoRuntime.queryInterface(
77                                       XPropertySet.class, oCell);
78 
79             Integer bkgrnd1;
80             try {
81                 bkgrnd1 = (Integer) PS.getPropertyValue("CellBackColor");
82             } catch (com.sun.star.beans.UnknownPropertyException e) {
83                 bkgrnd1 = (Integer) PS.getPropertyValue("BackColor");
84             }
85 
86             // getting formats names.
87             XInterface iFormats = (XInterface) oMSF.createInstance(
88                                           "com.sun.star.sheet.TableAutoFormats");
89             XNameAccess formats = (XNameAccess) UnoRuntime.queryInterface(
90                                           XNameAccess.class, iFormats);
91             String[] names = formats.getElementNames();
92 
93             // getting one random not default style name
94             Random rnd = new Random();
95 
96             if (names.length > 1) {
97                 while (name.equals("Default")) {
98                     name = names[rnd.nextInt(names.length)];
99                 }
100             } else {
101                 name = names[0];
102             }
103 
104             log.println("Applying style " + name);
105 
106 
107             // applying style
108             oObj.autoFormat(name);
109 
110             // getting new cell's backround.
111             Integer bkgrnd2;
112             try {
113                 bkgrnd2 = (Integer) PS.getPropertyValue("CellBackColor");
114             } catch (com.sun.star.beans.UnknownPropertyException e) {
115                 bkgrnd2 = (Integer) PS.getPropertyValue("BackColor");
116             }
117 
118             bResult &= !bkgrnd1.equals(bkgrnd2);
119         } catch (com.sun.star.uno.Exception e) {
120             log.println("Exception occured :");
121             e.printStackTrace(log);
122             bResult = false;
123         }
124 
125         tRes.tested("autoFormat()", bResult);
126     }
127 
128     /**
129     * Forces environment recreation.
130     */
131     protected void after() {
132         disposeEnvironment();
133     }
134 }