1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_testtools.hxx"
30 
31 #include "sal/config.h"
32 
33 #include "currentcontextchecker.hxx"
34 
35 #include "com/sun/star/uno/Any.hxx"
36 #include "com/sun/star/uno/Reference.hxx"
37 #include "com/sun/star/uno/RuntimeException.hpp"
38 #include "com/sun/star/uno/XCurrentContext.hpp"
39 #include "cppu/unotype.hxx"
40 #include "cppuhelper/implbase1.hxx"
41 #include "osl/diagnose.h"
42 #include "osl/diagnose.hxx"
43 #include "rtl/string.h"
44 #include "rtl/ustring.hxx"
45 #include "sal/types.h"
46 #include "test/testtools/bridgetest/XCurrentContextChecker.hpp"
47 #include "uno/current_context.hxx"
48 
49 namespace {
50 
51 namespace css = ::com::sun::star;
52 
53 static char const KEY[] = "testtools.bridgetest.Key";
54 static char const VALUE[] = "good";
55 
56 class CurrentContext:
57     public ::osl::DebugBase< CurrentContext >,
58     public ::cppu::WeakImplHelper1< css::uno::XCurrentContext >
59 {
60 public:
61     CurrentContext();
62 
63     virtual ~CurrentContext();
64 
65     virtual css::uno::Any SAL_CALL getValueByName(::rtl::OUString const & Name)
66         throw (css::uno::RuntimeException);
67 
68 private:
69     CurrentContext(CurrentContext &); // not defined
70     void operator =(CurrentContext &); // not defined
71 };
72 
73 CurrentContext::CurrentContext() {}
74 
75 CurrentContext::~CurrentContext() {}
76 
77 css::uno::Any CurrentContext::getValueByName(::rtl::OUString const & Name)
78     throw (css::uno::RuntimeException)
79 {
80     return Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(KEY))
81         ? css::uno::makeAny(::rtl::OUString::createFromAscii(VALUE))
82         : css::uno::Any();
83 }
84 
85 }
86 
87 testtools::bridgetest::CurrentContextChecker::CurrentContextChecker() {}
88 
89 testtools::bridgetest::CurrentContextChecker::~CurrentContextChecker() {}
90 
91 ::sal_Bool testtools::bridgetest::CurrentContextChecker::perform(
92     css::uno::Reference<
93         ::test::testtools::bridgetest::XCurrentContextChecker > const & other,
94     ::sal_Int32 setSteps, ::sal_Int32 checkSteps)
95     throw (css::uno::RuntimeException)
96 {
97     if (setSteps == 0) {
98         css::uno::ContextLayer layer(new CurrentContext);
99         return performCheck(other, setSteps, checkSteps);
100     } else {
101         return performCheck(other, setSteps, checkSteps);
102     }
103 }
104 
105 bool testtools::bridgetest::CurrentContextChecker::performCheck(
106     css::uno::Reference<
107         ::test::testtools::bridgetest::XCurrentContextChecker > const & other,
108     ::sal_Int32 setSteps, ::sal_Int32 checkSteps)
109 {
110     OSL_ASSERT(other.is() && checkSteps >= 0);
111     if (checkSteps == 0) {
112         css::uno::Reference< css::uno::XCurrentContext > context(
113             css::uno::getCurrentContext());
114         if (!context.is()) {
115             return false;
116         }
117         css::uno::Any a(
118             context->getValueByName(::rtl::OUString::createFromAscii(KEY)));
119         if (a.getValueType() != ::cppu::UnoType< ::rtl::OUString >::get()) {
120             return false;
121         }
122         ::rtl::OUString s;
123         OSL_VERIFY(a >>= s);
124         return s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM(VALUE));
125     } else {
126         return other->perform(
127             this, setSteps >= 0 ? setSteps - 1 : -1, checkSteps - 1);
128     }
129 }
130