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 
28 package ifc.frame;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.container.XIndexAccess;
33 import com.sun.star.frame.XFrame;
34 import com.sun.star.frame.XFramesSupplier;
35 import com.sun.star.uno.AnyConverter;
36 import com.sun.star.uno.Type;
37 
38 /**
39 * Testing <code>com.sun.star.frame.XFramesSupplier</code>
40 * interface methods:
41 * <ul>
42 *  <li><code> getActiveFrame() </code></li>
43 *  <li><code> getFrames() </code></li>
44 *  <li><code> setActiveFrame() </code></li>
45 * </ul><p>
46 * Test is <b> NOT </b> multithread compilant. <p>
47 * @see com.sun.star.frame.XFramesSupplier
48 */
49 public class _XFramesSupplier extends MultiMethodTest {
50     public static XFramesSupplier oObj = null;
51     protected XIndexAccess frames = null ;
52     protected XFrame active = null ;
53     protected int activeIdx = -1 ;
54 
55     /**
56     * Test calls the method, then result is checked. Also active frame index
57     * is saved in activeIdx variable.<p>
58     *
59     * Has <b> OK </b> status if the method does not return null and the object
60     * contains returned frame. Or if no frames available and the method
61     * returns null.<p>
62     *
63     * The following method tests are to be completed successfully before :
64     * <ul>
65     *  <li> <code> getFrames() </code> : obtains frames from the object </li>
66     * </ul>
67     */
68     public void _getActiveFrame() {
69         boolean result = true ;
70 
71         requiredMethod("getFrames()") ;
72         active = oObj.getActiveFrame() ;
73         if (active == null) {
74             // if no child frames then no active frame could be
75             result =  oObj.getFrames().getCount() == 0;
76             log.println("getActiveFrame() returned null") ;
77         }
78         else {
79             boolean hasActiveFrame = false ;
80             for (int i = 0; i < frames.getCount(); i++) {
81                 XFrame fr = null ;
82                 try {
83                    fr = null;
84                    try {
85                        fr = (XFrame) AnyConverter.toObject(
86                                 new Type(XFrame.class),frames.getByIndex(i));
87                    } catch (com.sun.star.lang.IllegalArgumentException iae) {
88                        log.println("Can't convert any");
89                    }
90                 } catch (com.sun.star.lang.WrappedTargetException e) {
91                    log.println("Exception occured while calling getByIndex() method :") ;
92                    e.printStackTrace(log) ;
93                    return;
94                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
95                    log.println("Exception occured while calling getByIndex() method :") ;
96                    e.printStackTrace(log) ;
97                    return;
98                 }
99                 if (active.equals(fr)) {
100                     hasActiveFrame = true ;
101                     activeIdx = i ;
102                 }
103             }
104             if (!hasActiveFrame) {
105                 log.println("getActiveFrame() isn't contained " +
106                     "in getFrames() collection") ;
107                 result = false ;
108             }
109         }
110 
111         tRes.tested("getActiveFrame()", result) ;
112     }
113 
114     /**
115     * Test calls the method, then result is checked. <p>
116     * Has <b> OK </b> status if the method does not return null,
117     * number of returned frames is not zero and each of them is not null too.
118     */
119     public void _getFrames() {
120         boolean result = true ;
121         int cnt = 0;
122 
123         frames = oObj.getFrames() ;
124         if (frames != null) {
125             cnt = frames.getCount() ;
126 //            if (cnt == 0) result = false ;
127             log.println("There are " + cnt + " frames.") ;
128         } else {
129             log.println("getFrames() returned null !!!") ;
130             result = false ;
131         }
132         for (int i = 0; i < cnt; i++) {
133             try {
134                 if (frames.getByIndex(i) == null) {
135                     log.println("Frame(" + i + ") == null") ;
136                     result = false ;
137                 }
138             } catch (com.sun.star.lang.WrappedTargetException e) {
139                     log.println("Exception occured while calling getByIndex() method :") ;
140                     e.printStackTrace(log) ;
141                     return;
142             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143                     log.println("Exception occured while calling getByIndex() method :") ;
144                     e.printStackTrace(log) ;
145                     return;
146             }
147         }
148 
149         tRes.tested("getFrames()", result) ;
150     }
151 
152     /**
153     * After selecting frame to be activated, test calls the method. <p>
154     *
155     * Has <b> OK </b> status if set and gotten active frames are equal.<p>
156     *
157     * The following method tests are to be completed successfully before :
158     * <ul>
159     *  <li> <code> getActiveFrame() </code> : gets active frame </li>
160     * </ul>
161     */
162     public void _setActiveFrame() {
163         boolean result = true ;
164         XFrame sFrame = null ;
165 
166         requiredMethod("getActiveFrame()") ;
167         if (frames.getCount() > 1) {
168             try {
169                 if (activeIdx != 0)
170                    try {
171                        sFrame = (XFrame) AnyConverter.toObject(
172                                 new Type(XFrame.class),frames.getByIndex(0));
173                    } catch (com.sun.star.lang.IllegalArgumentException iae) {
174                        log.println("Can't convert any");
175                    }
176                 else
177                    try {
178                        sFrame = (XFrame) AnyConverter.toObject(
179                                 new Type(XFrame.class),frames.getByIndex(1));
180                    } catch (com.sun.star.lang.IllegalArgumentException iae) {
181                        log.println("Can't convert any");
182                    }
183             } catch (com.sun.star.lang.WrappedTargetException e) {
184                     log.println("Exception occured while calling getByIndex() method :") ;
185                     e.printStackTrace(log) ;
186                     return;
187             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
188                     log.println("Exception occured while calling getByIndex() method :") ;
189                     e.printStackTrace(log) ;
190                     return;
191             }
192         } else if (frames.getCount() > 0) {
193             sFrame = active ;
194         } else {
195             sFrame = null;
196         }
197 
198         oObj.setActiveFrame(sFrame) ;
199         XFrame gFrame = oObj.getActiveFrame() ;
200         if (!(gFrame == null && sFrame == null
201               || sFrame.equals(gFrame))) {
202 
203             log.println("Active frame set is not equal frame get: FAILED");
204             result = false ;
205         }
206 
207         tRes.tested("setActiveFrame()", result) ;
208     }
209 
210 } // finished class _XFramesSupplier
211 
212