1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/beans/NamedValue.hpp>
23 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
24 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/beans/XPropertyState.hpp>
27 #include <com/sun/star/configuration/theDefaultProvider.hpp>
28 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/container/XNameReplace.hpp>
31 #include <com/sun/star/container/XNamed.hpp>
32 #include <com/sun/star/lang/EventObject.hpp>
33 #include <com/sun/star/lang/XComponent.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Reference.hxx>
38 #include <com/sun/star/uno/Sequence.hxx>
39 #include <com/sun/star/uno/XInterface.hpp>
40 #include <com/sun/star/util/XChangesBatch.hpp>
41 #include <cppuhelper/implbase.hxx>
43 #include <rtl/ref.hxx>
44 #include <rtl/ustring.hxx>
45 #include <sal/types.h>
46 #include <comphelper/processfactory.hxx>
47 #include <comphelper/configuration.hxx>
48 #include <comphelper/configurationlistener.hxx>
49 #include <cppunit/TestAssert.h>
50 #include <cppunit/TestFixture.h>
51 #include <cppunit/extensions/HelperMacros.h>
52 #include <cppunit/plugin/TestPlugIn.h>
53 #include <officecfg/Office/Math.hxx>
57 class Test
: public CppUnit::TestFixture
{
59 virtual void setUp() override
;
64 void testSetSetMemberName();
65 void testInsertSetMember();
66 void testReadCommands();
69 void testCrossThreads();
72 OUString
const & path
, OUString
const & relative
) const;
75 OUString
const & path
, OUString
const & name
,
76 css::uno::Any
const & value
) const;
78 bool resetKey(OUString
const & path
, OUString
const & name
) const;
80 css::uno::Reference
< css::uno::XInterface
> createViewAccess(
81 OUString
const & path
) const;
83 css::uno::Reference
< css::uno::XInterface
> createUpdateAccess(
84 OUString
const & path
) const;
86 CPPUNIT_TEST_SUITE(Test
);
87 CPPUNIT_TEST(testKeyFetch
);
88 CPPUNIT_TEST(testKeySet
);
89 CPPUNIT_TEST(testKeyReset
);
90 CPPUNIT_TEST(testSetSetMemberName
);
91 CPPUNIT_TEST(testInsertSetMember
);
92 CPPUNIT_TEST(testReadCommands
);
93 CPPUNIT_TEST(testListener
);
94 CPPUNIT_TEST(testRecursive
);
95 CPPUNIT_TEST(testCrossThreads
);
96 CPPUNIT_TEST_SUITE_END();
99 css::uno::Reference
< css::lang::XMultiServiceFactory
> provider_
;
103 public cppu::WeakImplHelper
< css::beans::XPropertyChangeListener
>
106 RecursiveTest(Test
const & theTest
, int count
, bool * destroyed
);
111 virtual ~RecursiveTest() override
;
113 virtual void step() const = 0;
118 virtual void SAL_CALL
disposing(css::lang::EventObject
const &) override
;
120 virtual void SAL_CALL
propertyChange(
121 css::beans::PropertyChangeEvent
const &) override
;
125 css::uno::Reference
< css::beans::XPropertySet
> properties_
;
128 RecursiveTest::RecursiveTest(
129 Test
const & theTest
, int count
, bool * destroyed
):
130 test_(theTest
), count_(count
), destroyed_(destroyed
)
133 void RecursiveTest::test()
136 test_
.createUpdateAccess(
137 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
139 css::uno::UNO_QUERY_THROW
);
140 properties_
->addPropertyChangeListener("Label", this);
142 CPPUNIT_ASSERT_EQUAL(0, count_
);
143 css::uno::Reference
< css::lang::XComponent
>(
144 properties_
, css::uno::UNO_QUERY_THROW
)->dispose();
147 RecursiveTest::~RecursiveTest()
152 void RecursiveTest::disposing(css::lang::EventObject
const & Source
)
154 CPPUNIT_ASSERT(properties_
.is());
155 CPPUNIT_ASSERT_EQUAL(
156 css::uno::Reference
<css::uno::XInterface
>(
157 properties_
, css::uno::UNO_QUERY_THROW
),
162 void RecursiveTest::propertyChange(css::beans::PropertyChangeEvent
const & evt
)
164 CPPUNIT_ASSERT_EQUAL(
165 css::uno::Reference
<css::uno::XInterface
>(
166 properties_
, css::uno::UNO_QUERY_THROW
),
168 CPPUNIT_ASSERT_EQUAL( OUString("Label"), evt
.PropertyName
);
175 class SimpleRecursiveTest
: public RecursiveTest
{
177 SimpleRecursiveTest(Test
const & theTest
, int count
, bool * destroyed
);
180 virtual void step() const override
;
183 SimpleRecursiveTest::SimpleRecursiveTest(
184 Test
const & theTest
, int count
, bool * destroyed
):
185 RecursiveTest(theTest
, count
, destroyed
)
188 void SimpleRecursiveTest::step() const
191 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
194 css::uno::Any(OUString("step")));
199 provider_
= css::configuration::theDefaultProvider::get(
200 comphelper::getProcessComponentContext() );
203 void Test::testKeyFetch()
208 "/org.openoffice.System",
213 void Test::testKeySet()
216 "/org.openoffice.System/L10N",
218 css::uno::Any(OUString("com.sun.star.configuration.backend.LocaleBackend UILocale")));
222 "/org.openoffice.System/L10N",
225 CPPUNIT_ASSERT_EQUAL( OUString("com.sun.star.configuration.backend.LocaleBackend UILocale"), s
);
228 void Test::testKeyReset()
231 "/org.openoffice.System/L10N",
237 "/org.openoffice.System/L10N",
240 CPPUNIT_ASSERT_EQUAL( OUString("com.sun.star.configuration.backend.LocaleBackend Locale"), s
);
244 void Test::testSetSetMemberName()
249 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
250 ".uno:FontworkShapeType",
253 CPPUNIT_ASSERT_EQUAL( OUString("Fontwork Shape"), s
);
255 css::uno::Reference
< css::container::XNameAccess
> access(
257 "/org.openoffice.Office.UI.GenericCommands/UserInterface/"
259 css::uno::UNO_QUERY_THROW
);
260 css::uno::Reference
< css::container::XNamed
> member
;
261 access
->getByName(".uno:FontworkGalleryFloater") >>= member
;
262 CPPUNIT_ASSERT(member
.is());
263 member
->setName(".uno:FontworkShapeType");
264 css::uno::Reference
< css::util::XChangesBatch
>(
265 access
, css::uno::UNO_QUERY_THROW
)->commitChanges();
266 css::uno::Reference
< css::lang::XComponent
>(
267 access
, css::uno::UNO_QUERY_THROW
)->dispose();
271 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
272 ".uno:FontworkShapeType",
275 CPPUNIT_ASSERT_EQUAL( OUString("Insert Fontwork"), s
);
278 void Test::testInsertSetMember() {
279 css::uno::Reference
<css::container::XNameContainer
> access(
281 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands"),
282 css::uno::UNO_QUERY_THROW
);
283 css::uno::Reference
<css::uno::XInterface
> member
;
285 css::uno::Reference
<css::lang::XSingleServiceFactory
>(
286 access
, css::uno::UNO_QUERY_THROW
)->createInstance());
287 CPPUNIT_ASSERT(member
.is());
288 access
->insertByName("A", css::uno::Any(member
));
290 css::uno::Reference
<css::lang::XSingleServiceFactory
>(
291 access
, css::uno::UNO_QUERY_THROW
)->createInstance());
292 CPPUNIT_ASSERT(member
.is());
294 access
->insertByName("", css::uno::Any(member
));
295 CPPUNIT_FAIL("expected IllegalArgumentException");
296 } catch (css::lang::IllegalArgumentException
&) {}
298 access
->insertByName("\x01", css::uno::Any(member
));
299 CPPUNIT_FAIL("expected IllegalArgumentException");
300 } catch (css::lang::IllegalArgumentException
&) {}
302 access
->insertByName("a/b", css::uno::Any(member
));
303 } catch (css::lang::IllegalArgumentException
&) {
304 CPPUNIT_FAIL("unexpected IllegalArgumentException");
306 css::uno::Reference
<css::util::XChangesBatch
>(
307 access
, css::uno::UNO_QUERY_THROW
)->commitChanges();
308 css::uno::Reference
<css::lang::XComponent
>(
309 access
, css::uno::UNO_QUERY_THROW
)->dispose();
312 void Test::testReadCommands()
314 css::uno::Reference
< css::container::XNameAccess
> access(
316 "/org.openoffice.Office.UI.GenericCommands/UserInterface/"
318 css::uno::UNO_QUERY_THROW
);
319 const css::uno::Sequence
< OUString
> names(access
->getElementNames());
321 /*CPPUNIT_ASSERT_EQUAL(749, names.getLength());*/
322 // testSetSetMemberName() already removed ".uno:FontworkGalleryFloater"
323 sal_uInt32 n
= osl_getGlobalTimer();
324 for (int i
= 0; i
< 8; ++i
) {
325 for (OUString
const & childName
: names
) {
326 css::uno::Reference
< css::container::XNameAccess
> child
;
327 if (access
->getByName(childName
) >>= child
) {
328 CPPUNIT_ASSERT(child
.is());
329 child
->getByName("Label");
330 child
->getByName("ContextLabel");
331 child
->getByName("Properties");
335 n
= osl_getGlobalTimer() - n
;
336 printf("Reading elements took %" SAL_PRIuUINT32
" ms\n", n
);
337 css::uno::Reference
< css::lang::XComponent
>(
338 access
, css::uno::UNO_QUERY_THROW
)->dispose();
341 void Test::testListener()
343 OUString aRandomPath
= "/org.openoffice.Office.Math/View";
345 // test with no props.
347 rtl::Reference
xListener(
348 new comphelper::ConfigurationListener(aRandomPath
));
349 xListener
->dispose();
354 rtl::Reference
xListener(
355 new comphelper::ConfigurationListener(aRandomPath
));
357 comphelper::ConfigurationListenerProperty
<bool> aSetting(xListener
, "AutoRedraw");
358 CPPUNIT_ASSERT_MESSAGE("check AutoRedraw defaults to true", aSetting
.get());
362 std::shared_ptr
< comphelper::ConfigurationChanges
> xChanges(
363 comphelper::ConfigurationChanges::create());
364 officecfg::Office::Math::View::AutoRedraw::set(false, xChanges
);
367 CPPUNIT_ASSERT_MESSAGE("listener failed to trigger", !aSetting
.get());
371 std::shared_ptr
< comphelper::ConfigurationChanges
> xChanges(
372 comphelper::ConfigurationChanges::create());
373 officecfg::Office::Math::View::AutoRedraw::set(true, xChanges
);
376 CPPUNIT_ASSERT_MESSAGE("listener failed to trigger", aSetting
.get());
378 xListener
->dispose();
382 void Test::testRecursive()
384 bool destroyed
= false;
385 rtl::Reference
< RecursiveTest
>(
386 new SimpleRecursiveTest(*this, 100, &destroyed
))->test();
387 CPPUNIT_ASSERT(destroyed
);
390 void Test::testCrossThreads()
392 bool destroyed
= false;
393 rtl::Reference
< RecursiveTest
>(
394 new SimpleRecursiveTest(*this, 10, &destroyed
))->test();
395 CPPUNIT_ASSERT(destroyed
);
398 css::uno::Any
Test::getKey(
399 OUString
const & path
, OUString
const & relative
) const
401 css::uno::Reference
< css::container::XHierarchicalNameAccess
> access(
402 createViewAccess(path
), css::uno::UNO_QUERY_THROW
);
403 css::uno::Any
value(access
->getByHierarchicalName(relative
));
404 css::uno::Reference
< css::lang::XComponent
>(
405 access
, css::uno::UNO_QUERY_THROW
)->dispose();
410 OUString
const & path
, OUString
const & name
,
411 css::uno::Any
const & value
) const
413 css::uno::Reference
< css::container::XNameReplace
> access(
414 createUpdateAccess(path
), css::uno::UNO_QUERY_THROW
);
415 access
->replaceByName(name
, value
);
416 css::uno::Reference
< css::util::XChangesBatch
>(
417 access
, css::uno::UNO_QUERY_THROW
)->commitChanges();
418 css::uno::Reference
< css::lang::XComponent
>(
419 access
, css::uno::UNO_QUERY_THROW
)->dispose();
422 bool Test::resetKey(OUString
const & path
, OUString
const & name
)
425 //TODO: support setPropertyToDefault
426 css::uno::Reference
< css::util::XChangesBatch
> access(
427 createUpdateAccess(path
), css::uno::UNO_QUERY_THROW
);
428 css::uno::Reference
< css::beans::XPropertyState
> state(
429 access
, css::uno::UNO_QUERY
);
433 state
->setPropertyToDefault(name
);
434 access
->commitChanges();
435 css::uno::Reference
< css::lang::XComponent
>(
436 access
, css::uno::UNO_QUERY_THROW
)->dispose();
440 css::uno::Reference
< css::uno::XInterface
> Test::createViewAccess(
441 OUString
const & path
) const
444 css::beans::NamedValue(
446 css::uno::Any(path
)));
447 return provider_
->createInstanceWithArguments(
448 "com.sun.star.configuration.ConfigurationAccess",
449 css::uno::Sequence
< css::uno::Any
>(&arg
, 1));
452 css::uno::Reference
< css::uno::XInterface
> Test::createUpdateAccess(
453 OUString
const & path
) const
456 css::beans::NamedValue(
458 css::uno::Any(path
)));
459 return provider_
->createInstanceWithArguments(
460 "com.sun.star.configuration.ConfigurationUpdateAccess",
461 css::uno::Sequence
< css::uno::Any
>(&arg
, 1));
464 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
468 CPPUNIT_PLUGIN_IMPLEMENT();
470 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */