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 
24 package com.sun.star.comp.bridge;
25 
26 import com.sun.star.uno.Any;
27 import com.sun.star.uno.Type;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.XCurrentContext;
30 import test.testtools.bridgetest.XCurrentContextChecker;
31 
32 final class CurrentContextChecker implements XCurrentContextChecker {
CurrentContextChecker()33     public CurrentContextChecker() {}
34 
perform( XCurrentContextChecker other, int setSteps, int checkSteps)35     public boolean perform(
36         XCurrentContextChecker other, int setSteps, int checkSteps)
37     {
38         if (setSteps == 0) {
39             XCurrentContext old = UnoRuntime.getCurrentContext();
40             UnoRuntime.setCurrentContext(
41                 new XCurrentContext() {
42                     public Object getValueByName(String Name) {
43                         return Name.equals(KEY)
44                             ? (Object) VALUE : (Object) Any.VOID;
45                     }
46                 });
47             try {
48                 return performCheck(other, setSteps, checkSteps);
49             } finally {
50                 UnoRuntime.setCurrentContext(old);
51             }
52         } else {
53             return performCheck(other, setSteps, checkSteps);
54         }
55     }
56 
performCheck( XCurrentContextChecker other, int setSteps, int checkSteps)57     private boolean performCheck(
58         XCurrentContextChecker other, int setSteps, int checkSteps)
59     {
60         // assert other != null && checkSteps >= 0;
61         if (checkSteps == 0) {
62             XCurrentContext context = UnoRuntime.getCurrentContext();
63             if (context == null) {
64                 return false;
65             }
66             Any a = Any.complete(context.getValueByName(KEY));
67             return
68                 a.getType().equals(Type.STRING) && a.getObject().equals(VALUE);
69         } else {
70             return other.perform(
71                 this, setSteps >= 0 ? setSteps - 1 : -1, checkSteps - 1);
72         }
73     }
74 
75     private static final String KEY = "testtools.bridgetest.Key";
76     private static final String VALUE = "good";
77 }
78