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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_testtools.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "currentcontextchecker.hxx"
30 
31 #include "com/sun/star/uno/Any.hxx"
32 #include "com/sun/star/uno/Reference.hxx"
33 #include "com/sun/star/uno/RuntimeException.hpp"
34 #include "com/sun/star/uno/XCurrentContext.hpp"
35 #include "cppu/unotype.hxx"
36 #include "cppuhelper/implbase1.hxx"
37 #include "osl/diagnose.h"
38 #include "osl/diagnose.hxx"
39 #include "rtl/string.h"
40 #include "rtl/ustring.hxx"
41 #include "sal/types.h"
42 #include "test/testtools/bridgetest/XCurrentContextChecker.hpp"
43 #include "uno/current_context.hxx"
44 
45 namespace {
46 
47 namespace css = ::com::sun::star;
48 
49 static char const KEY[] = "testtools.bridgetest.Key";
50 static char const VALUE[] = "good";
51 
52 class CurrentContext:
53     public ::osl::DebugBase< CurrentContext >,
54     public ::cppu::WeakImplHelper1< css::uno::XCurrentContext >
55 {
56 public:
57     CurrentContext();
58 
59     virtual ~CurrentContext();
60 
61     virtual css::uno::Any SAL_CALL getValueByName(::rtl::OUString const & Name)
62         throw (css::uno::RuntimeException);
63 
64 private:
65     CurrentContext(CurrentContext &); // not defined
66     void operator =(CurrentContext &); // not defined
67 };
68 
CurrentContext()69 CurrentContext::CurrentContext() {}
70 
~CurrentContext()71 CurrentContext::~CurrentContext() {}
72 
getValueByName(::rtl::OUString const & Name)73 css::uno::Any CurrentContext::getValueByName(::rtl::OUString const & Name)
74     throw (css::uno::RuntimeException)
75 {
76     return Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(KEY))
77         ? css::uno::makeAny(::rtl::OUString::createFromAscii(VALUE))
78         : css::uno::Any();
79 }
80 
81 }
82 
CurrentContextChecker()83 testtools::bridgetest::CurrentContextChecker::CurrentContextChecker() {}
84 
~CurrentContextChecker()85 testtools::bridgetest::CurrentContextChecker::~CurrentContextChecker() {}
86 
perform(css::uno::Reference<::test::testtools::bridgetest::XCurrentContextChecker> const & other,::sal_Int32 setSteps,::sal_Int32 checkSteps)87 ::sal_Bool testtools::bridgetest::CurrentContextChecker::perform(
88     css::uno::Reference<
89         ::test::testtools::bridgetest::XCurrentContextChecker > const & other,
90     ::sal_Int32 setSteps, ::sal_Int32 checkSteps)
91     throw (css::uno::RuntimeException)
92 {
93     if (setSteps == 0) {
94         css::uno::ContextLayer layer(new CurrentContext);
95         return performCheck(other, setSteps, checkSteps);
96     } else {
97         return performCheck(other, setSteps, checkSteps);
98     }
99 }
100 
performCheck(css::uno::Reference<::test::testtools::bridgetest::XCurrentContextChecker> const & other,::sal_Int32 setSteps,::sal_Int32 checkSteps)101 bool testtools::bridgetest::CurrentContextChecker::performCheck(
102     css::uno::Reference<
103         ::test::testtools::bridgetest::XCurrentContextChecker > const & other,
104     ::sal_Int32 setSteps, ::sal_Int32 checkSteps)
105 {
106     OSL_ASSERT(other.is() && checkSteps >= 0);
107     if (checkSteps == 0) {
108         css::uno::Reference< css::uno::XCurrentContext > context(
109             css::uno::getCurrentContext());
110         if (!context.is()) {
111             return false;
112         }
113         css::uno::Any a(
114             context->getValueByName(::rtl::OUString::createFromAscii(KEY)));
115         if (a.getValueType() != ::cppu::UnoType< ::rtl::OUString >::get()) {
116             return false;
117         }
118         ::rtl::OUString s;
119         OSL_VERIFY(a >>= s);
120         return s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(VALUE));
121     } else {
122         return other->perform(
123             this, setSteps >= 0 ? setSteps - 1 : -1, checkSteps - 1);
124     }
125 }
126