xref: /aoo41x/main/configmgr/qa/unit/test.cxx (revision 3a7cf181)
1*3a7cf181SAndrew Rist /**************************************************************
2*3a7cf181SAndrew Rist  *
3*3a7cf181SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*3a7cf181SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*3a7cf181SAndrew Rist  * distributed with this work for additional information
6*3a7cf181SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*3a7cf181SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*3a7cf181SAndrew Rist  * "License"); you may not use this file except in compliance
9*3a7cf181SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*3a7cf181SAndrew Rist  *
11*3a7cf181SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*3a7cf181SAndrew Rist  *
13*3a7cf181SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*3a7cf181SAndrew Rist  * software distributed under the License is distributed on an
15*3a7cf181SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3a7cf181SAndrew Rist  * KIND, either express or implied.  See the License for the
17*3a7cf181SAndrew Rist  * specific language governing permissions and limitations
18*3a7cf181SAndrew Rist  * under the License.
19*3a7cf181SAndrew Rist  *
20*3a7cf181SAndrew Rist  *************************************************************/
21*3a7cf181SAndrew Rist 
22*3a7cf181SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_configmgr.hxx"
25cdf0e10cSrcweir #include "sal/config.h"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <cstddef>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "com/sun/star/beans/NamedValue.hpp"
30cdf0e10cSrcweir #include "com/sun/star/beans/PropertyChangeEvent.hpp"
31cdf0e10cSrcweir #include "com/sun/star/beans/XPropertyChangeListener.hpp"
32cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
33cdf0e10cSrcweir #include "com/sun/star/beans/XPropertyState.hpp"
34cdf0e10cSrcweir #include "com/sun/star/container/XHierarchicalNameAccess.hpp"
35cdf0e10cSrcweir #include "com/sun/star/container/XNameReplace.hpp"
36cdf0e10cSrcweir #include "com/sun/star/container/XNamed.hpp"
37cdf0e10cSrcweir #include "com/sun/star/lang/EventObject.hpp"
38cdf0e10cSrcweir #include "com/sun/star/lang/XComponent.hpp"
39cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
40cdf0e10cSrcweir #include "com/sun/star/uno/Any.hxx"
41cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
42cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
43cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
44cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
45cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
46cdf0e10cSrcweir #include "com/sun/star/util/XChangesBatch.hpp"
47cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
48cdf0e10cSrcweir #include "cppuhelper/servicefactory.hxx"
49cdf0e10cSrcweir #include "osl/conditn.hxx"
50cdf0e10cSrcweir #include "osl/thread.h"
51cdf0e10cSrcweir #include "osl/thread.hxx"
52cdf0e10cSrcweir #include "osl/time.h"
53cdf0e10cSrcweir #include "rtl/ref.hxx"
54cdf0e10cSrcweir #include "rtl/string.h"
55cdf0e10cSrcweir #include "rtl/textcvt.h"
56cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
57cdf0e10cSrcweir #include "rtl/ustring.h"
58cdf0e10cSrcweir #include "rtl/ustring.hxx"
59cdf0e10cSrcweir #include "sal/types.h"
60cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
61cdf0e10cSrcweir 
62cdf0e10cSrcweir namespace {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir namespace css = com::sun::star;
65cdf0e10cSrcweir 
normalize(rtl::OUString const & path,rtl::OUString const & relative,rtl::OUString * normalizedPath,rtl::OUString * name)66cdf0e10cSrcweir void normalize(
67cdf0e10cSrcweir     rtl::OUString const & path, rtl::OUString const & relative,
68cdf0e10cSrcweir     rtl::OUString * normalizedPath, rtl::OUString * name)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     sal_Int32 i = relative.lastIndexOf('/');
71cdf0e10cSrcweir     if (i == -1) {
72cdf0e10cSrcweir         *normalizedPath = path;
73cdf0e10cSrcweir         *name = relative;
74cdf0e10cSrcweir     } else {
75cdf0e10cSrcweir         rtl::OUStringBuffer buf(path);
76cdf0e10cSrcweir         buf.append(sal_Unicode('/'));
77cdf0e10cSrcweir         buf.append(relative.copy(0, i));
78cdf0e10cSrcweir         *normalizedPath = buf.makeStringAndClear();
79cdf0e10cSrcweir         *name = relative.copy(i + 1);
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir class Test: public CppUnit::TestFixture {
84cdf0e10cSrcweir public:
85cdf0e10cSrcweir     virtual void setUp();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     virtual void tearDown();
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     void testKeyFetch();
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     void testKeySet();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     void testKeyReset();
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     void testSetSetMemberName();
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     void testReadCommands();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     void testThreads();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     void testRecursive();
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     void testCrossThreads();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     css::uno::Any getKey(
106cdf0e10cSrcweir         rtl::OUString const & path, rtl::OUString const & relative) const;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     void setKey(
109cdf0e10cSrcweir         rtl::OUString const & path, rtl::OUString const & name,
110cdf0e10cSrcweir         css::uno::Any const & value) const;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     bool resetKey(rtl::OUString const & path, rtl::OUString const & name) const;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > createViewAccess(
115cdf0e10cSrcweir         rtl::OUString const & path) const;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     css::uno::Reference< css::uno::XInterface > createUpdateAccess(
118cdf0e10cSrcweir         rtl::OUString const & path) const;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
121cdf0e10cSrcweir     CPPUNIT_TEST(testKeyFetch);
122cdf0e10cSrcweir     CPPUNIT_TEST(testKeySet);
123cdf0e10cSrcweir     CPPUNIT_TEST(testKeyReset);
124cdf0e10cSrcweir     CPPUNIT_TEST(testSetSetMemberName);
125cdf0e10cSrcweir     CPPUNIT_TEST(testReadCommands);
126cdf0e10cSrcweir     CPPUNIT_TEST(testThreads);
127cdf0e10cSrcweir     CPPUNIT_TEST(testRecursive);
128cdf0e10cSrcweir     CPPUNIT_TEST(testCrossThreads);
129cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
130cdf0e10cSrcweir 
131cdf0e10cSrcweir private:
132cdf0e10cSrcweir     css::uno::Reference< css::uno::XComponentContext > context_;
133cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > provider_;
134cdf0e10cSrcweir };
135cdf0e10cSrcweir 
136cdf0e10cSrcweir class TestThread: public osl::Thread {
137cdf0e10cSrcweir public:
138cdf0e10cSrcweir     TestThread(osl::Condition & stop);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     bool getSuccess() const;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir protected:
143cdf0e10cSrcweir     virtual bool iteration() = 0;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir private:
146cdf0e10cSrcweir     virtual void SAL_CALL run();
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     osl::Condition & stop_;
149cdf0e10cSrcweir     bool success_;
150cdf0e10cSrcweir };
151cdf0e10cSrcweir 
TestThread(osl::Condition & stop)152cdf0e10cSrcweir TestThread::TestThread(
153cdf0e10cSrcweir     osl::Condition & stop):
154cdf0e10cSrcweir     stop_(stop), success_(true)
155cdf0e10cSrcweir {}
156cdf0e10cSrcweir 
getSuccess() const157cdf0e10cSrcweir bool TestThread::getSuccess() const {
158cdf0e10cSrcweir     return success_;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
run()161cdf0e10cSrcweir void TestThread::run() {
162cdf0e10cSrcweir     try {
163cdf0e10cSrcweir         while (!stop_.check()) {
164cdf0e10cSrcweir             if (!iteration()) {
165cdf0e10cSrcweir                 success_ = false;
166cdf0e10cSrcweir             }
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir     } catch (...) {
169cdf0e10cSrcweir         success_ = false;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir class ReaderThread: public TestThread {
174cdf0e10cSrcweir public:
175cdf0e10cSrcweir     ReaderThread(
176cdf0e10cSrcweir         osl::Condition & stop, Test const & test, rtl::OUString const & path,
177cdf0e10cSrcweir         rtl::OUString const & relative);
178cdf0e10cSrcweir 
179cdf0e10cSrcweir private:
180cdf0e10cSrcweir     virtual bool iteration();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     Test const & test_;
183cdf0e10cSrcweir     rtl::OUString path_;
184cdf0e10cSrcweir     rtl::OUString relative_;
185cdf0e10cSrcweir };
186cdf0e10cSrcweir 
ReaderThread(osl::Condition & stop,Test const & test,rtl::OUString const & path,rtl::OUString const & relative)187cdf0e10cSrcweir ReaderThread::ReaderThread(
188cdf0e10cSrcweir     osl::Condition & stop, Test const & test, rtl::OUString const & path,
189cdf0e10cSrcweir     rtl::OUString const & relative):
190cdf0e10cSrcweir     TestThread(stop), test_(test), path_(path), relative_(relative)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     create();
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
iteration()195cdf0e10cSrcweir bool ReaderThread::iteration() {
196cdf0e10cSrcweir     return test_.getKey(path_, relative_).hasValue();
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir class WriterThread: public TestThread {
200cdf0e10cSrcweir public:
201cdf0e10cSrcweir     WriterThread(
202cdf0e10cSrcweir         osl::Condition & stop, Test const & test, rtl::OUString const & path,
203cdf0e10cSrcweir         rtl::OUString const & relative);
204cdf0e10cSrcweir 
205cdf0e10cSrcweir private:
206cdf0e10cSrcweir     virtual bool iteration();
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     Test const & test_;
209cdf0e10cSrcweir     rtl::OUString path_;
210cdf0e10cSrcweir     rtl::OUString name_;
211cdf0e10cSrcweir     std::size_t index_;
212cdf0e10cSrcweir };
213cdf0e10cSrcweir 
WriterThread(osl::Condition & stop,Test const & test,rtl::OUString const & path,rtl::OUString const & relative)214cdf0e10cSrcweir WriterThread::WriterThread(
215cdf0e10cSrcweir     osl::Condition & stop, Test const & test, rtl::OUString const & path,
216cdf0e10cSrcweir     rtl::OUString const & relative):
217cdf0e10cSrcweir     TestThread(stop), test_(test), index_(0)
218cdf0e10cSrcweir {
219cdf0e10cSrcweir     normalize(path, relative, &path_, &name_);
220cdf0e10cSrcweir     create();
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
iteration()223cdf0e10cSrcweir bool WriterThread::iteration() {
224cdf0e10cSrcweir     rtl::OUString options[] = {
225cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("fish")),
226cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("chips")),
227cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("kippers")),
228cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bloaters")) };
229cdf0e10cSrcweir     test_.setKey(path_, name_, css::uno::makeAny(options[index_]));
230cdf0e10cSrcweir     index_ = (index_ + 1) % (sizeof options / sizeof (rtl::OUString));
231cdf0e10cSrcweir     return true;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir class RecursiveTest:
235cdf0e10cSrcweir     public cppu::WeakImplHelper1< css::beans::XPropertyChangeListener >
236cdf0e10cSrcweir {
237cdf0e10cSrcweir public:
238cdf0e10cSrcweir     RecursiveTest(Test const & theTest, int count, bool * destroyed);
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     void test();
241cdf0e10cSrcweir 
242cdf0e10cSrcweir protected:
243cdf0e10cSrcweir     virtual ~RecursiveTest();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     virtual void step() const = 0;
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     Test const & test_;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir private:
250cdf0e10cSrcweir     virtual void SAL_CALL disposing(css::lang::EventObject const &)
251cdf0e10cSrcweir         throw (css::uno::RuntimeException);
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     virtual void SAL_CALL propertyChange(
254cdf0e10cSrcweir         css::beans::PropertyChangeEvent const &)
255cdf0e10cSrcweir         throw (css::uno::RuntimeException);
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     int count_;
258cdf0e10cSrcweir     bool * destroyed_;
259cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > properties_;
260cdf0e10cSrcweir };
261cdf0e10cSrcweir 
RecursiveTest(Test const & theTest,int count,bool * destroyed)262cdf0e10cSrcweir RecursiveTest::RecursiveTest(
263cdf0e10cSrcweir     Test const & theTest, int count, bool * destroyed):
264cdf0e10cSrcweir     test_(theTest), count_(count), destroyed_(destroyed)
265cdf0e10cSrcweir {}
266cdf0e10cSrcweir 
test()267cdf0e10cSrcweir void RecursiveTest::test() {
268cdf0e10cSrcweir     properties_ = css::uno::Reference< css::beans::XPropertySet >(
269cdf0e10cSrcweir         test_.createUpdateAccess(
270cdf0e10cSrcweir             rtl::OUString(
271cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
272cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
273cdf0e10cSrcweir                     "dotuno:WebHtml"))),
274cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
275cdf0e10cSrcweir     properties_->addPropertyChangeListener(
276cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")), this);
277cdf0e10cSrcweir     step();
278cdf0e10cSrcweir     CPPUNIT_ASSERT(count_ == 0);
279cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
280cdf0e10cSrcweir         properties_, css::uno::UNO_QUERY_THROW)->dispose();
281cdf0e10cSrcweir }
282cdf0e10cSrcweir 
~RecursiveTest()283cdf0e10cSrcweir RecursiveTest::~RecursiveTest() {
284cdf0e10cSrcweir     *destroyed_ = true;
285cdf0e10cSrcweir }
286cdf0e10cSrcweir 
disposing(css::lang::EventObject const & Source)287cdf0e10cSrcweir void RecursiveTest::disposing(css::lang::EventObject const & Source)
288cdf0e10cSrcweir     throw (css::uno::RuntimeException)
289cdf0e10cSrcweir {
290cdf0e10cSrcweir     CPPUNIT_ASSERT(properties_.is() && Source.Source == properties_);
291cdf0e10cSrcweir     properties_.clear();
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
propertyChange(css::beans::PropertyChangeEvent const & evt)294cdf0e10cSrcweir void RecursiveTest::propertyChange(css::beans::PropertyChangeEvent const & evt)
295cdf0e10cSrcweir     throw (css::uno::RuntimeException)
296cdf0e10cSrcweir {
297cdf0e10cSrcweir     CPPUNIT_ASSERT(
298cdf0e10cSrcweir         evt.Source == properties_ &&
299cdf0e10cSrcweir         evt.PropertyName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Label")));
300cdf0e10cSrcweir     if (count_ > 0) {
301cdf0e10cSrcweir         --count_;
302cdf0e10cSrcweir         step();
303cdf0e10cSrcweir     }
304cdf0e10cSrcweir }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir class SimpleRecursiveTest: public RecursiveTest {
307cdf0e10cSrcweir public:
308cdf0e10cSrcweir     SimpleRecursiveTest(Test const & theTest, int count, bool * destroyed);
309cdf0e10cSrcweir 
310cdf0e10cSrcweir private:
311cdf0e10cSrcweir     virtual void step() const;
312cdf0e10cSrcweir };
313cdf0e10cSrcweir 
SimpleRecursiveTest(Test const & theTest,int count,bool * destroyed)314cdf0e10cSrcweir SimpleRecursiveTest::SimpleRecursiveTest(
315cdf0e10cSrcweir     Test const & theTest, int count, bool * destroyed):
316cdf0e10cSrcweir     RecursiveTest(theTest, count, destroyed)
317cdf0e10cSrcweir {}
318cdf0e10cSrcweir 
step() const319cdf0e10cSrcweir void SimpleRecursiveTest::step() const {
320cdf0e10cSrcweir     test_.setKey(
321cdf0e10cSrcweir         rtl::OUString(
322cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
323cdf0e10cSrcweir                 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
324cdf0e10cSrcweir                 "dotuno:WebHtml")),
325cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")),
326cdf0e10cSrcweir         css::uno::makeAny(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("step"))));
327cdf0e10cSrcweir }
328cdf0e10cSrcweir 
329cdf0e10cSrcweir class CrossThreadTest: public RecursiveTest {
330cdf0e10cSrcweir public:
331cdf0e10cSrcweir     CrossThreadTest(Test const & theTest, int count, bool * destroyed);
332cdf0e10cSrcweir 
333cdf0e10cSrcweir private:
334cdf0e10cSrcweir     virtual void step() const;
335cdf0e10cSrcweir };
336cdf0e10cSrcweir 
CrossThreadTest(Test const & theTest,int count,bool * destroyed)337cdf0e10cSrcweir CrossThreadTest::CrossThreadTest(
338cdf0e10cSrcweir     Test const & theTest, int count, bool * destroyed):
339cdf0e10cSrcweir     RecursiveTest(theTest, count, destroyed)
340cdf0e10cSrcweir {}
341cdf0e10cSrcweir 
step() const342cdf0e10cSrcweir void CrossThreadTest::step() const {
343cdf0e10cSrcweir     osl::Condition stop;
344cdf0e10cSrcweir     stop.set();
345cdf0e10cSrcweir     WriterThread(
346cdf0e10cSrcweir         stop, test_,
347cdf0e10cSrcweir         rtl::OUString(
348cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
349cdf0e10cSrcweir                 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
350cdf0e10cSrcweir                 "dotuno:WebHtml")),
351cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label"))).join();
352cdf0e10cSrcweir     test_.resetKey(
353cdf0e10cSrcweir         rtl::OUString(
354cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
355cdf0e10cSrcweir                 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
356cdf0e10cSrcweir                 "dotuno:WebHtml")),
357cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")));
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
setUp()360cdf0e10cSrcweir void Test::setUp() {
361cdf0e10cSrcweir     char const * forward = getForwardString();
362cdf0e10cSrcweir     rtl_uString * registry = 0;
363cdf0e10cSrcweir     CPPUNIT_ASSERT(
364cdf0e10cSrcweir         rtl_convertStringToUString(
365cdf0e10cSrcweir             &registry, forward, rtl_str_getLength(forward),
366cdf0e10cSrcweir             osl_getThreadTextEncoding(),
367cdf0e10cSrcweir             (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
368cdf0e10cSrcweir              RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
369cdf0e10cSrcweir              RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
370cdf0e10cSrcweir     context_ = css::uno::Reference< css::uno::XComponentContext >(
371cdf0e10cSrcweir         css::uno::Reference< css::beans::XPropertySet >(
372cdf0e10cSrcweir             cppu::createRegistryServiceFactory(
373cdf0e10cSrcweir                 rtl::OUString(registry, SAL_NO_ACQUIRE)),
374cdf0e10cSrcweir             css::uno::UNO_QUERY_THROW)->getPropertyValue(
375cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext"))),
376cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
377cdf0e10cSrcweir     CPPUNIT_ASSERT(
378cdf0e10cSrcweir         context_->getValueByName(
379cdf0e10cSrcweir             rtl::OUString(
380cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
381cdf0e10cSrcweir                     "/singletons/"
382cdf0e10cSrcweir                     "com.sun.star.configuration.theDefaultProvider"))) >>=
383cdf0e10cSrcweir         provider_);
384cdf0e10cSrcweir }
385cdf0e10cSrcweir 
tearDown()386cdf0e10cSrcweir void Test::tearDown() {
387cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
388cdf0e10cSrcweir         context_, css::uno::UNO_QUERY_THROW)->dispose();
389cdf0e10cSrcweir }
390cdf0e10cSrcweir 
testKeyFetch()391cdf0e10cSrcweir void Test::testKeyFetch() {
392cdf0e10cSrcweir     rtl::OUString s;
393cdf0e10cSrcweir     CPPUNIT_ASSERT(
394cdf0e10cSrcweir         getKey(
395cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
396cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("L10N/ooLocale"))) >>=
397cdf0e10cSrcweir         s);
398cdf0e10cSrcweir     CPPUNIT_ASSERT(
399cdf0e10cSrcweir         getKey(
400cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
401cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/AString"))) >>=
402cdf0e10cSrcweir         s);
403cdf0e10cSrcweir }
404cdf0e10cSrcweir 
testKeySet()405cdf0e10cSrcweir void Test::testKeySet() {
406cdf0e10cSrcweir     setKey(
407cdf0e10cSrcweir         rtl::OUString(
408cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
409cdf0e10cSrcweir         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString")),
410cdf0e10cSrcweir         css::uno::makeAny(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("baa"))));
411cdf0e10cSrcweir     rtl::OUString s;
412cdf0e10cSrcweir     CPPUNIT_ASSERT(
413cdf0e10cSrcweir         getKey(
414cdf0e10cSrcweir             rtl::OUString(
415cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
416cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString"))) >>=
417cdf0e10cSrcweir         s);
418cdf0e10cSrcweir     CPPUNIT_ASSERT(s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("baa")));
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
testKeyReset()421cdf0e10cSrcweir void Test::testKeyReset() {
422cdf0e10cSrcweir     if (resetKey(
423cdf0e10cSrcweir             rtl::OUString(
424cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
425cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString"))))
426cdf0e10cSrcweir     {
427cdf0e10cSrcweir         rtl::OUString s;
428cdf0e10cSrcweir         CPPUNIT_ASSERT(
429cdf0e10cSrcweir             getKey(
430cdf0e10cSrcweir                 rtl::OUString(
431cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup/Test")),
432cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AString"))) >>=
433cdf0e10cSrcweir             s);
434cdf0e10cSrcweir         CPPUNIT_ASSERT(s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Foo")));
435cdf0e10cSrcweir     }
436cdf0e10cSrcweir }
437cdf0e10cSrcweir 
testSetSetMemberName()438cdf0e10cSrcweir void Test::testSetSetMemberName() {
439cdf0e10cSrcweir     rtl::OUString s;
440cdf0e10cSrcweir     CPPUNIT_ASSERT(
441cdf0e10cSrcweir         getKey(
442cdf0e10cSrcweir             rtl::OUString(
443cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
444cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
445cdf0e10cSrcweir                     ".uno:FontworkShapeType")),
446cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label"))) >>=
447cdf0e10cSrcweir         s);
448cdf0e10cSrcweir     CPPUNIT_ASSERT(
449cdf0e10cSrcweir         s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Fontwork Shape")));
450cdf0e10cSrcweir 
451cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > access(
452cdf0e10cSrcweir         createUpdateAccess(
453cdf0e10cSrcweir             rtl::OUString(
454cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
455cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/"
456cdf0e10cSrcweir                     "Commands"))),
457cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
458cdf0e10cSrcweir     css::uno::Reference< css::container::XNamed > member;
459cdf0e10cSrcweir     access->getByName(
460cdf0e10cSrcweir         rtl::OUString(
461cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(".uno:FontworkGalleryFloater"))) >>=
462cdf0e10cSrcweir         member;
463cdf0e10cSrcweir     CPPUNIT_ASSERT(member.is());
464cdf0e10cSrcweir     member->setName(
465cdf0e10cSrcweir         rtl::OUString(
466cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(".uno:FontworkShapeType")));
467cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch >(
468cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->commitChanges();
469cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
470cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
471cdf0e10cSrcweir 
472cdf0e10cSrcweir     CPPUNIT_ASSERT(
473cdf0e10cSrcweir         getKey(
474cdf0e10cSrcweir             rtl::OUString(
475cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
476cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/Commands/"
477cdf0e10cSrcweir                     ".uno:FontworkShapeType")),
478cdf0e10cSrcweir             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label"))) >>=
479cdf0e10cSrcweir         s);
480cdf0e10cSrcweir     CPPUNIT_ASSERT(
481cdf0e10cSrcweir         s.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Fontwork Gallery")));
482cdf0e10cSrcweir }
483cdf0e10cSrcweir 
testReadCommands()484cdf0e10cSrcweir void Test::testReadCommands() {
485cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > access(
486cdf0e10cSrcweir         createViewAccess(
487cdf0e10cSrcweir             rtl::OUString(
488cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM(
489cdf0e10cSrcweir                     "/org.openoffice.UI.GenericCommands/UserInterface/"
490cdf0e10cSrcweir                     "Commands"))),
491cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
492cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > names(access->getElementNames());
493cdf0e10cSrcweir     CPPUNIT_ASSERT(names.getLength() == 695);
494cdf0e10cSrcweir         // testSetSetMemberName() already removed ".uno:FontworkGalleryFloater"
495cdf0e10cSrcweir     sal_uInt32 n = osl_getGlobalTimer();
496cdf0e10cSrcweir     for (int i = 0; i < 8; ++i) {
497cdf0e10cSrcweir         for (sal_Int32 j = 0; j < names.getLength(); ++j) {
498cdf0e10cSrcweir             css::uno::Reference< css::container::XNameAccess > child;
499cdf0e10cSrcweir             if (access->getByName(names[j]) >>= child) {
500cdf0e10cSrcweir                 CPPUNIT_ASSERT(child.is());
501cdf0e10cSrcweir                 child->getByName(
502cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Label")));
503cdf0e10cSrcweir                 child->getByName(
504cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ContextLabel")));
505cdf0e10cSrcweir                 child->getByName(
506cdf0e10cSrcweir                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Properties")));
507cdf0e10cSrcweir             }
508cdf0e10cSrcweir         }
509cdf0e10cSrcweir     }
510cdf0e10cSrcweir     n = osl_getGlobalTimer() - n;
511cdf0e10cSrcweir     t_print("Reading elements took %" SAL_PRIuUINT32 " ms\n", n);
512cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
513cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
514cdf0e10cSrcweir }
515cdf0e10cSrcweir 
testThreads()516cdf0e10cSrcweir void Test::testThreads() {
517cdf0e10cSrcweir     struct Entry { rtl::OUString path; rtl::OUString relative; };
518cdf0e10cSrcweir     Entry list[] = {
519cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
520cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/AString")) },
521cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
522cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/AString")) },
523cdf0e10cSrcweir         { rtl::OUString(
524cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
525cdf0e10cSrcweir                   "/org.openoffice.UI.GenericCommands")),
526cdf0e10cSrcweir           rtl::OUString(
527cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
528cdf0e10cSrcweir                   "UserInterface/Commands/dotuno:WebHtml/Label")) },
529cdf0e10cSrcweir         { rtl::OUString(
530cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
531cdf0e10cSrcweir                   "/org.openoffice.UI.GenericCommands")),
532cdf0e10cSrcweir           rtl::OUString(
533cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
534cdf0e10cSrcweir                   "UserInterface/Commands/dotuno:NewPresentation/Label")) },
535cdf0e10cSrcweir         { rtl::OUString(
536cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
537cdf0e10cSrcweir                   "/org.openoffice.UI.GenericCommands")),
538cdf0e10cSrcweir           rtl::OUString(
539cdf0e10cSrcweir               RTL_CONSTASCII_USTRINGPARAM(
540cdf0e10cSrcweir                   "UserInterface/Commands/dotuno:RecentFileList/Label")) },
541cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
542cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("L10N/ooLocale")) },
543cdf0e10cSrcweir         { rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Setup")),
544cdf0e10cSrcweir           rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Test/ABoolean")) }
545cdf0e10cSrcweir     };
546cdf0e10cSrcweir     std::size_t const numReaders = sizeof list / sizeof (Entry);
547cdf0e10cSrcweir     std::size_t const numWriters = numReaders - 2;
548cdf0e10cSrcweir     ReaderThread * readers[numReaders];
549cdf0e10cSrcweir     WriterThread * writers[numWriters];
550cdf0e10cSrcweir     osl::Condition stop;
551cdf0e10cSrcweir     for (std::size_t i = 0; i < numReaders; ++i) {
552cdf0e10cSrcweir         CPPUNIT_ASSERT(getKey(list[i].path, list[i].relative).hasValue());
553cdf0e10cSrcweir         readers[i] = new ReaderThread(
554cdf0e10cSrcweir             stop, *this, list[i].path, list[i].relative);
555cdf0e10cSrcweir     }
556cdf0e10cSrcweir     for (std::size_t i = 0; i < numWriters; ++i) {
557cdf0e10cSrcweir         writers[i] = new WriterThread(
558cdf0e10cSrcweir             stop, *this, list[i].path, list[i].relative);
559cdf0e10cSrcweir     }
560cdf0e10cSrcweir     for (int i = 0; i < 5; ++i) {
561cdf0e10cSrcweir         for (std::size_t j = 0; j < numReaders; ++j) {
562cdf0e10cSrcweir             rtl::OUString path;
563cdf0e10cSrcweir             rtl::OUString name;
564cdf0e10cSrcweir             normalize(list[j].path, list[j].relative, &path, &name);
565cdf0e10cSrcweir             resetKey(path, name);
566cdf0e10cSrcweir             osl::Thread::yield();
567cdf0e10cSrcweir         }
568cdf0e10cSrcweir     }
569cdf0e10cSrcweir     stop.set();
570cdf0e10cSrcweir     bool success = true;
571cdf0e10cSrcweir     for (std::size_t i = 0; i < numReaders; ++i) {
572cdf0e10cSrcweir         readers[i]->join();
573cdf0e10cSrcweir         success = success && readers[i]->getSuccess();
574cdf0e10cSrcweir         delete readers[i];
575cdf0e10cSrcweir     }
576cdf0e10cSrcweir     for (std::size_t i = 0; i < numWriters; ++i) {
577cdf0e10cSrcweir         writers[i]->join();
578cdf0e10cSrcweir         success = success && writers[i]->getSuccess();
579cdf0e10cSrcweir         delete writers[i];
580cdf0e10cSrcweir     }
581cdf0e10cSrcweir     CPPUNIT_ASSERT(success);
582cdf0e10cSrcweir }
583cdf0e10cSrcweir 
testRecursive()584cdf0e10cSrcweir void Test::testRecursive() {
585cdf0e10cSrcweir     bool destroyed = false;
586cdf0e10cSrcweir     rtl::Reference< RecursiveTest >(
587cdf0e10cSrcweir         new SimpleRecursiveTest(*this, 100, &destroyed))->test();
588cdf0e10cSrcweir     CPPUNIT_ASSERT(destroyed);
589cdf0e10cSrcweir }
590cdf0e10cSrcweir 
testCrossThreads()591cdf0e10cSrcweir void Test::testCrossThreads() {
592cdf0e10cSrcweir     bool destroyed = false;
593cdf0e10cSrcweir     rtl::Reference< RecursiveTest >(
594cdf0e10cSrcweir         new SimpleRecursiveTest(*this, 10, &destroyed))->test();
595cdf0e10cSrcweir     CPPUNIT_ASSERT(destroyed);
596cdf0e10cSrcweir }
597cdf0e10cSrcweir 
getKey(rtl::OUString const & path,rtl::OUString const & relative) const598cdf0e10cSrcweir css::uno::Any Test::getKey(
599cdf0e10cSrcweir     rtl::OUString const & path, rtl::OUString const & relative) const
600cdf0e10cSrcweir {
601cdf0e10cSrcweir     css::uno::Reference< css::container::XHierarchicalNameAccess > access(
602cdf0e10cSrcweir         createViewAccess(path), css::uno::UNO_QUERY_THROW);
603cdf0e10cSrcweir     css::uno::Any value(access->getByHierarchicalName(relative));
604cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
605cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
606cdf0e10cSrcweir     return value;
607cdf0e10cSrcweir }
608cdf0e10cSrcweir 
setKey(rtl::OUString const & path,rtl::OUString const & name,css::uno::Any const & value) const609cdf0e10cSrcweir void Test::setKey(
610cdf0e10cSrcweir     rtl::OUString const & path, rtl::OUString const & name,
611cdf0e10cSrcweir     css::uno::Any const & value) const
612cdf0e10cSrcweir {
613cdf0e10cSrcweir     css::uno::Reference< css::container::XNameReplace > access(
614cdf0e10cSrcweir         createUpdateAccess(path), css::uno::UNO_QUERY_THROW);
615cdf0e10cSrcweir     access->replaceByName(name, value);
616cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch >(
617cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->commitChanges();
618cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
619cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
620cdf0e10cSrcweir }
621cdf0e10cSrcweir 
resetKey(rtl::OUString const & path,rtl::OUString const & name) const622cdf0e10cSrcweir bool Test::resetKey(rtl::OUString const & path, rtl::OUString const & name)
623cdf0e10cSrcweir     const
624cdf0e10cSrcweir {
625cdf0e10cSrcweir     //TODO: support setPropertyToDefault
626cdf0e10cSrcweir     css::uno::Reference< css::util::XChangesBatch > access(
627cdf0e10cSrcweir         createUpdateAccess(path), css::uno::UNO_QUERY_THROW);
628cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertyState > state(
629cdf0e10cSrcweir         access, css::uno::UNO_QUERY);
630cdf0e10cSrcweir     if (!state.is()) {
631cdf0e10cSrcweir         return false;
632cdf0e10cSrcweir     }
633cdf0e10cSrcweir     state->setPropertyToDefault(name);
634cdf0e10cSrcweir     access->commitChanges();
635cdf0e10cSrcweir     css::uno::Reference< css::lang::XComponent >(
636cdf0e10cSrcweir         access, css::uno::UNO_QUERY_THROW)->dispose();
637cdf0e10cSrcweir     return true;
638cdf0e10cSrcweir }
639cdf0e10cSrcweir 
createViewAccess(rtl::OUString const & path) const640cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > Test::createViewAccess(
641cdf0e10cSrcweir     rtl::OUString const & path) const
642cdf0e10cSrcweir {
643cdf0e10cSrcweir     css::uno::Any arg(
644cdf0e10cSrcweir         css::uno::makeAny(
645cdf0e10cSrcweir             css::beans::NamedValue(
646cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")),
647cdf0e10cSrcweir                 css::uno::makeAny(path))));
648cdf0e10cSrcweir     return provider_->createInstanceWithArguments(
649cdf0e10cSrcweir         rtl::OUString(
650cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
651cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationAccess")),
652cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any >(&arg, 1));
653cdf0e10cSrcweir }
654cdf0e10cSrcweir 
createUpdateAccess(rtl::OUString const & path) const655cdf0e10cSrcweir css::uno::Reference< css::uno::XInterface > Test::createUpdateAccess(
656cdf0e10cSrcweir     rtl::OUString const & path) const
657cdf0e10cSrcweir {
658cdf0e10cSrcweir     css::uno::Any arg(
659cdf0e10cSrcweir         css::uno::makeAny(
660cdf0e10cSrcweir             css::beans::NamedValue(
661cdf0e10cSrcweir                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")),
662cdf0e10cSrcweir                 css::uno::makeAny(path))));
663cdf0e10cSrcweir     return provider_->createInstanceWithArguments(
664cdf0e10cSrcweir         rtl::OUString(
665cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM(
666cdf0e10cSrcweir                 "com.sun.star.configuration.ConfigurationUpdateAccess")),
667cdf0e10cSrcweir         css::uno::Sequence< css::uno::Any >(&arg, 1));
668cdf0e10cSrcweir }
669cdf0e10cSrcweir 
670cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltest");
671cdf0e10cSrcweir 
672cdf0e10cSrcweir }
673cdf0e10cSrcweir 
674cdf0e10cSrcweir NOADDITIONAL;
675