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 import util.utils;
32 
33 import com.sun.star.frame.DispatchDescriptor;
34 import com.sun.star.frame.FrameSearchFlag;
35 import com.sun.star.frame.XDispatch;
36 import com.sun.star.frame.XDispatchProvider;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.util.URL;
40 import com.sun.star.util.XURLTransformer;
41 
42 /**
43 * Testing <code>com.sun.star.frame.XDispatchProvider</code>
44 * interface methods:
45 * <ul>
46 *  <li><code> queryDispatch() </code></li>
47 *  <li><code> queryDispatches() </code></li>
48 * </ul><p>
49 * This test needs the following object relations :
50 * <ul>
51 *  <li> <code>'XDispatchProvider.URL'</code> (of type <code>String</code>):
52 *  used to obtain unparsed url of DispatchProvider </li>
53 * </ul> <p>
54 * Test is <b> NOT </b> multithread compilant. <p>
55 * @see com.sun.star.frame.XDispatchProvider
56 */
57 public class _XDispatchProvider extends MultiMethodTest {
58     public static XDispatchProvider oObj = null;
59     private String dispatchUrl = null ;
60 
61     /**
62     * Retrieves object relation.
63     */
64     public void before() {
65         dispatchUrl = (String) tEnv.getObjRelation("XDispatchProvider.URL") ;
66         if (dispatchUrl == null) {
67             dispatchUrl = utils.getFullTestDocName("index.html");
68         }
69         log.println("Using URL: '" + dispatchUrl + "'");
70     }
71 
72     /**
73     * Test calls the method. <p>
74     * Has <b> OK </b> status if the method does not return null.
75     */
76     public void _queryDispatch() {
77         URL url = new URL();
78         String frameName = "_top";
79 
80         url.Complete = dispatchUrl;
81         try {
82             XURLTransformer xParser=(XURLTransformer)
83                 UnoRuntime.queryInterface(XURLTransformer.class,
84                     ((XMultiServiceFactory)tParam.getMSF()).createInstance
85                         ("com.sun.star.util.URLTransformer"));
86             // Because it's an in/out parameter we must use an array of
87             // URL objects.
88             URL[] aParseURL = new URL[1];
89             aParseURL[0] = new URL();
90             aParseURL[0].Complete = dispatchUrl;
91             xParser.parseStrict(aParseURL);
92             url = aParseURL[0];
93         } catch (com.sun.star.uno.Exception e) {
94             log.println("Couldn't parse URL");
95         }
96         XDispatch xDispatch = oObj.queryDispatch(url,
97                 frameName, FrameSearchFlag.ALL);
98         tRes.tested("queryDispatch()", xDispatch != null);
99     }
100 
101     /**
102     * Before test calls the method, DispatchDescriptor sequence is defined.<p>
103     * Has <b> OK </b> status if the method does not return null, returned
104     * sequence length is equal to a number of DispatchDescriptors
105     * and returned sequence consists of non-null elements.
106     */
107     public void _queryDispatches() {
108         String name1 = "_self";
109         String name2 = "_top";
110         URL url1 = new URL();
111         URL url2 = new URL();
112 
113         url1.Complete = dispatchUrl;
114         url2.Complete = dispatchUrl;
115         try {
116             log.println("Parsing URL");
117             XURLTransformer xParser = (XURLTransformer)
118                 UnoRuntime.queryInterface(XURLTransformer.class,
119                     ((XMultiServiceFactory)tParam.getMSF()).createInstance
120                         ("com.sun.star.util.URLTransformer"));
121             // Because it's an in/out parameter we must use an array of
122             // URL objects.
123             URL[] aParseURL = new URL[1];
124             aParseURL[0] = new URL();
125             aParseURL[0].Complete = dispatchUrl;
126             xParser.parseStrict(aParseURL);
127             url1 = aParseURL[0];
128             url2 = aParseURL[0];
129         } catch (com.sun.star.uno.Exception e) {
130             log.println("Couldn't parse URL");
131         }
132         DispatchDescriptor descs[] = new DispatchDescriptor[] {
133             new DispatchDescriptor(url1, name1, FrameSearchFlag.ALL),
134             new DispatchDescriptor(url2, name2, FrameSearchFlag.ALL)
135         };
136         XDispatch[] xDispatches = oObj.queryDispatches(descs);
137 
138         if (xDispatches == null) {
139             log.println("queryDispatches() returned null");
140             tRes.tested("queryDispatches()", false);
141             return;
142         }
143 
144         if (xDispatches.length != descs.length) {
145             log.println("queryDispatches() returned "
146                     + xDispatches.length
147                     + " amount of XDispatch instead of "
148                     + descs.length);
149             tRes.tested("queryDispatches()", false);
150             return;
151         }
152 
153         for (int i = 0; i < xDispatches.length; i++) {
154             if (xDispatches[i] == null) {
155                 log.println("queryDispatches() result contains"
156                         + " null object");
157                 tRes.tested("queryDispatches()", false);
158                 return;
159             }
160         }
161 
162         tRes.tested("queryDispatches()", true);
163         return;
164     }
165 
166 }
167 
168