1*d48fe848SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*d48fe848SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*d48fe848SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*d48fe848SAndrew Rist * distributed with this work for additional information
6*d48fe848SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*d48fe848SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*d48fe848SAndrew Rist * "License"); you may not use this file except in compliance
9*d48fe848SAndrew Rist * with the License. You may obtain a copy of the License at
10*d48fe848SAndrew Rist *
11*d48fe848SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*d48fe848SAndrew Rist *
13*d48fe848SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*d48fe848SAndrew Rist * software distributed under the License is distributed on an
15*d48fe848SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d48fe848SAndrew Rist * KIND, either express or implied. See the License for the
17*d48fe848SAndrew Rist * specific language governing permissions and limitations
18*d48fe848SAndrew Rist * under the License.
19*d48fe848SAndrew Rist *
20*d48fe848SAndrew Rist *************************************************************/
21*d48fe848SAndrew Rist
22*d48fe848SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_testtools.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "currentcontextchecker.hxx"
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
32cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
33cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
34cdf0e10cSrcweir #include "com/sun/star/uno/XCurrentContext.hpp"
35cdf0e10cSrcweir #include "cppu/unotype.hxx"
36cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
37cdf0e10cSrcweir #include "osl/diagnose.h"
38cdf0e10cSrcweir #include "osl/diagnose.hxx"
39cdf0e10cSrcweir #include "rtl/string.h"
40cdf0e10cSrcweir #include "rtl/ustring.hxx"
41cdf0e10cSrcweir #include "sal/types.h"
42cdf0e10cSrcweir #include "test/testtools/bridgetest/XCurrentContextChecker.hpp"
43cdf0e10cSrcweir #include "uno/current_context.hxx"
44cdf0e10cSrcweir
45cdf0e10cSrcweir namespace {
46cdf0e10cSrcweir
47cdf0e10cSrcweir namespace css = ::com::sun::star;
48cdf0e10cSrcweir
49cdf0e10cSrcweir static char const KEY[] = "testtools.bridgetest.Key";
50cdf0e10cSrcweir static char const VALUE[] = "good";
51cdf0e10cSrcweir
52cdf0e10cSrcweir class CurrentContext:
53cdf0e10cSrcweir public ::osl::DebugBase< CurrentContext >,
54cdf0e10cSrcweir public ::cppu::WeakImplHelper1< css::uno::XCurrentContext >
55cdf0e10cSrcweir {
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir CurrentContext();
58cdf0e10cSrcweir
59cdf0e10cSrcweir virtual ~CurrentContext();
60cdf0e10cSrcweir
61cdf0e10cSrcweir virtual css::uno::Any SAL_CALL getValueByName(::rtl::OUString const & Name)
62cdf0e10cSrcweir throw (css::uno::RuntimeException);
63cdf0e10cSrcweir
64cdf0e10cSrcweir private:
65cdf0e10cSrcweir CurrentContext(CurrentContext &); // not defined
66cdf0e10cSrcweir void operator =(CurrentContext &); // not defined
67cdf0e10cSrcweir };
68cdf0e10cSrcweir
CurrentContext()69cdf0e10cSrcweir CurrentContext::CurrentContext() {}
70cdf0e10cSrcweir
~CurrentContext()71cdf0e10cSrcweir CurrentContext::~CurrentContext() {}
72cdf0e10cSrcweir
getValueByName(::rtl::OUString const & Name)73cdf0e10cSrcweir css::uno::Any CurrentContext::getValueByName(::rtl::OUString const & Name)
74cdf0e10cSrcweir throw (css::uno::RuntimeException)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir return Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(KEY))
77cdf0e10cSrcweir ? css::uno::makeAny(::rtl::OUString::createFromAscii(VALUE))
78cdf0e10cSrcweir : css::uno::Any();
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
CurrentContextChecker()83cdf0e10cSrcweir testtools::bridgetest::CurrentContextChecker::CurrentContextChecker() {}
84cdf0e10cSrcweir
~CurrentContextChecker()85cdf0e10cSrcweir testtools::bridgetest::CurrentContextChecker::~CurrentContextChecker() {}
86cdf0e10cSrcweir
perform(css::uno::Reference<::test::testtools::bridgetest::XCurrentContextChecker> const & other,::sal_Int32 setSteps,::sal_Int32 checkSteps)87cdf0e10cSrcweir ::sal_Bool testtools::bridgetest::CurrentContextChecker::perform(
88cdf0e10cSrcweir css::uno::Reference<
89cdf0e10cSrcweir ::test::testtools::bridgetest::XCurrentContextChecker > const & other,
90cdf0e10cSrcweir ::sal_Int32 setSteps, ::sal_Int32 checkSteps)
91cdf0e10cSrcweir throw (css::uno::RuntimeException)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir if (setSteps == 0) {
94cdf0e10cSrcweir css::uno::ContextLayer layer(new CurrentContext);
95cdf0e10cSrcweir return performCheck(other, setSteps, checkSteps);
96cdf0e10cSrcweir } else {
97cdf0e10cSrcweir return performCheck(other, setSteps, checkSteps);
98cdf0e10cSrcweir }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
performCheck(css::uno::Reference<::test::testtools::bridgetest::XCurrentContextChecker> const & other,::sal_Int32 setSteps,::sal_Int32 checkSteps)101cdf0e10cSrcweir bool testtools::bridgetest::CurrentContextChecker::performCheck(
102cdf0e10cSrcweir css::uno::Reference<
103cdf0e10cSrcweir ::test::testtools::bridgetest::XCurrentContextChecker > const & other,
104cdf0e10cSrcweir ::sal_Int32 setSteps, ::sal_Int32 checkSteps)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir OSL_ASSERT(other.is() && checkSteps >= 0);
107cdf0e10cSrcweir if (checkSteps == 0) {
108cdf0e10cSrcweir css::uno::Reference< css::uno::XCurrentContext > context(
109cdf0e10cSrcweir css::uno::getCurrentContext());
110cdf0e10cSrcweir if (!context.is()) {
111cdf0e10cSrcweir return false;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir css::uno::Any a(
114cdf0e10cSrcweir context->getValueByName(::rtl::OUString::createFromAscii(KEY)));
115cdf0e10cSrcweir if (a.getValueType() != ::cppu::UnoType< ::rtl::OUString >::get()) {
116cdf0e10cSrcweir return false;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir ::rtl::OUString s;
119cdf0e10cSrcweir OSL_VERIFY(a >>= s);
120cdf0e10cSrcweir return s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(VALUE));
121cdf0e10cSrcweir } else {
122cdf0e10cSrcweir return other->perform(
123cdf0e10cSrcweir this, setSteps >= 0 ? setSteps - 1 : -1, checkSteps - 1);
124cdf0e10cSrcweir }
125cdf0e10cSrcweir }
126