1*5c44d1b3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5c44d1b3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5c44d1b3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5c44d1b3SAndrew Rist  * distributed with this work for additional information
6*5c44d1b3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5c44d1b3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5c44d1b3SAndrew Rist  * "License"); you may not use this file except in compliance
9*5c44d1b3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5c44d1b3SAndrew Rist  *
11*5c44d1b3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5c44d1b3SAndrew Rist  *
13*5c44d1b3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5c44d1b3SAndrew Rist  * software distributed under the License is distributed on an
15*5c44d1b3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5c44d1b3SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5c44d1b3SAndrew Rist  * specific language governing permissions and limitations
18*5c44d1b3SAndrew Rist  * under the License.
19*5c44d1b3SAndrew Rist  *
20*5c44d1b3SAndrew Rist  *************************************************************/
21*5c44d1b3SAndrew Rist 
22*5c44d1b3SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.comp.bridge;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.uno.Any;
27cdf0e10cSrcweir import com.sun.star.uno.Type;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import com.sun.star.uno.XCurrentContext;
30cdf0e10cSrcweir import test.testtools.bridgetest.XCurrentContextChecker;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir final class CurrentContextChecker implements XCurrentContextChecker {
CurrentContextChecker()33cdf0e10cSrcweir     public CurrentContextChecker() {}
34cdf0e10cSrcweir 
perform( XCurrentContextChecker other, int setSteps, int checkSteps)35cdf0e10cSrcweir     public boolean perform(
36cdf0e10cSrcweir         XCurrentContextChecker other, int setSteps, int checkSteps)
37cdf0e10cSrcweir     {
38cdf0e10cSrcweir         if (setSteps == 0) {
39cdf0e10cSrcweir             XCurrentContext old = UnoRuntime.getCurrentContext();
40cdf0e10cSrcweir             UnoRuntime.setCurrentContext(
41cdf0e10cSrcweir                 new XCurrentContext() {
42cdf0e10cSrcweir                     public Object getValueByName(String Name) {
43cdf0e10cSrcweir                         return Name.equals(KEY)
44cdf0e10cSrcweir                             ? (Object) VALUE : (Object) Any.VOID;
45cdf0e10cSrcweir                     }
46cdf0e10cSrcweir                 });
47cdf0e10cSrcweir             try {
48cdf0e10cSrcweir                 return performCheck(other, setSteps, checkSteps);
49cdf0e10cSrcweir             } finally {
50cdf0e10cSrcweir                 UnoRuntime.setCurrentContext(old);
51cdf0e10cSrcweir             }
52cdf0e10cSrcweir         } else {
53cdf0e10cSrcweir             return performCheck(other, setSteps, checkSteps);
54cdf0e10cSrcweir         }
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir 
performCheck( XCurrentContextChecker other, int setSteps, int checkSteps)57cdf0e10cSrcweir     private boolean performCheck(
58cdf0e10cSrcweir         XCurrentContextChecker other, int setSteps, int checkSteps)
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         // assert other != null && checkSteps >= 0;
61cdf0e10cSrcweir         if (checkSteps == 0) {
62cdf0e10cSrcweir             XCurrentContext context = UnoRuntime.getCurrentContext();
63cdf0e10cSrcweir             if (context == null) {
64cdf0e10cSrcweir                 return false;
65cdf0e10cSrcweir             }
66cdf0e10cSrcweir             Any a = Any.complete(context.getValueByName(KEY));
67cdf0e10cSrcweir             return
68cdf0e10cSrcweir                 a.getType().equals(Type.STRING) && a.getObject().equals(VALUE);
69cdf0e10cSrcweir         } else {
70cdf0e10cSrcweir             return other.perform(
71cdf0e10cSrcweir                 this, setSteps >= 0 ? setSteps - 1 : -1, checkSteps - 1);
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     private static final String KEY = "testtools.bridgetest.Key";
76cdf0e10cSrcweir     private static final String VALUE = "good";
77cdf0e10cSrcweir }
78