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 63 private: 64 CurrentContext(CurrentContext &); // not defined 65 void operator =(CurrentContext &); // not defined 66 }; 67 68 CurrentContext::CurrentContext() {} 69 70 CurrentContext::~CurrentContext() {} 71 72 css::uno::Any CurrentContext::getValueByName(::rtl::OUString const & Name) 73 { 74 return Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(KEY)) 75 ? css::uno::makeAny(::rtl::OUString::createFromAscii(VALUE)) 76 : css::uno::Any(); 77 } 78 79 } 80 81 testtools::bridgetest::CurrentContextChecker::CurrentContextChecker() {} 82 83 testtools::bridgetest::CurrentContextChecker::~CurrentContextChecker() {} 84 85 ::sal_Bool testtools::bridgetest::CurrentContextChecker::perform( 86 css::uno::Reference< 87 ::test::testtools::bridgetest::XCurrentContextChecker > const & other, 88 ::sal_Int32 setSteps, ::sal_Int32 checkSteps) 89 { 90 if (setSteps == 0) { 91 css::uno::ContextLayer layer(new CurrentContext); 92 return performCheck(other, setSteps, checkSteps); 93 } else { 94 return performCheck(other, setSteps, checkSteps); 95 } 96 } 97 98 bool testtools::bridgetest::CurrentContextChecker::performCheck( 99 css::uno::Reference< 100 ::test::testtools::bridgetest::XCurrentContextChecker > const & other, 101 ::sal_Int32 setSteps, ::sal_Int32 checkSteps) 102 { 103 OSL_ASSERT(other.is() && checkSteps >= 0); 104 if (checkSteps == 0) { 105 css::uno::Reference< css::uno::XCurrentContext > context( 106 css::uno::getCurrentContext()); 107 if (!context.is()) { 108 return false; 109 } 110 css::uno::Any a( 111 context->getValueByName(::rtl::OUString::createFromAscii(KEY))); 112 if (a.getValueType() != ::cppu::UnoType< ::rtl::OUString >::get()) { 113 return false; 114 } 115 ::rtl::OUString s; 116 OSL_VERIFY(a >>= s); 117 return s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(VALUE)); 118 } else { 119 return other->perform( 120 this, setSteps >= 0 ? setSteps - 1 : -1, checkSteps - 1); 121 } 122 } 123