nss: upgrade to release 3.73
[LibreOffice.git] / configmgr / qa / unit / test.cxx
blobdaa2070086f74f26496dd9a5c19fe1f32aab3b60
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
42 #include <osl/time.h>
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>
55 namespace {
57 class Test: public CppUnit::TestFixture {
58 public:
59 virtual void setUp() override;
61 void testKeyFetch();
62 void testKeySet();
63 void testKeyReset();
64 void testSetSetMemberName();
65 void testInsertSetMember();
66 void testReadCommands();
67 void testListener();
68 void testRecursive();
69 void testCrossThreads();
71 css::uno::Any getKey(
72 OUString const & path, OUString const & relative) const;
74 void setKey(
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();
98 private:
99 css::uno::Reference< css::lang::XMultiServiceFactory > provider_;
102 class RecursiveTest:
103 public cppu::WeakImplHelper< css::beans::XPropertyChangeListener >
105 public:
106 RecursiveTest(Test const & theTest, int count, bool * destroyed);
108 void test();
110 protected:
111 virtual ~RecursiveTest() override;
113 virtual void step() const = 0;
115 Test const & test_;
117 private:
118 virtual void SAL_CALL disposing(css::lang::EventObject const &) override;
120 virtual void SAL_CALL propertyChange(
121 css::beans::PropertyChangeEvent const &) override;
123 int count_;
124 bool * destroyed_;
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()
135 properties_.set(
136 test_.createUpdateAccess(
137 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
138 ".uno:WebHtml"),
139 css::uno::UNO_QUERY_THROW);
140 properties_->addPropertyChangeListener("Label", this);
141 step();
142 CPPUNIT_ASSERT_EQUAL(0, count_);
143 css::uno::Reference< css::lang::XComponent >(
144 properties_, css::uno::UNO_QUERY_THROW)->dispose();
147 RecursiveTest::~RecursiveTest()
149 *destroyed_ = true;
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),
158 Source.Source);
159 properties_.clear();
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),
167 evt.Source);
168 CPPUNIT_ASSERT_EQUAL( OUString("Label"), evt.PropertyName );
169 if (count_ > 0) {
170 --count_;
171 step();
175 class SimpleRecursiveTest: public RecursiveTest {
176 public:
177 SimpleRecursiveTest(Test const & theTest, int count, bool * destroyed);
179 private:
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
190 test_.setKey(
191 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
192 ".uno:WebHtml",
193 "Label",
194 css::uno::Any(OUString("step")));
197 void Test::setUp()
199 provider_ = css::configuration::theDefaultProvider::get(
200 comphelper::getProcessComponentContext() );
203 void Test::testKeyFetch()
205 OUString s;
206 CPPUNIT_ASSERT(
207 getKey(
208 "/org.openoffice.System",
209 "L10N/Locale") >>=
213 void Test::testKeySet()
215 setKey(
216 "/org.openoffice.System/L10N",
217 "Locale",
218 css::uno::Any(OUString("com.sun.star.configuration.backend.LocaleBackend UILocale")));
219 OUString s;
220 CPPUNIT_ASSERT(
221 getKey(
222 "/org.openoffice.System/L10N",
223 "Locale") >>=
225 CPPUNIT_ASSERT_EQUAL( OUString("com.sun.star.configuration.backend.LocaleBackend UILocale"), s );
228 void Test::testKeyReset()
230 if (resetKey(
231 "/org.openoffice.System/L10N",
232 "Locale"))
234 OUString s;
235 CPPUNIT_ASSERT(
236 getKey(
237 "/org.openoffice.System/L10N",
238 "Locale") >>=
240 CPPUNIT_ASSERT_EQUAL( OUString("com.sun.star.configuration.backend.LocaleBackend Locale"), s );
244 void Test::testSetSetMemberName()
246 OUString s;
247 CPPUNIT_ASSERT(
248 getKey(
249 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
250 ".uno:FontworkShapeType",
251 "Label") >>=
253 CPPUNIT_ASSERT_EQUAL( OUString("Fontwork Shape"), s );
255 css::uno::Reference< css::container::XNameAccess > access(
256 createUpdateAccess(
257 "/org.openoffice.Office.UI.GenericCommands/UserInterface/"
258 "Commands"),
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();
269 CPPUNIT_ASSERT(
270 getKey(
271 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands/"
272 ".uno:FontworkShapeType",
273 "Label") >>=
275 CPPUNIT_ASSERT_EQUAL( OUString("Insert Fontwork"), s );
278 void Test::testInsertSetMember() {
279 css::uno::Reference<css::container::XNameContainer> access(
280 createUpdateAccess(
281 "/org.openoffice.Office.UI.GenericCommands/UserInterface/Commands"),
282 css::uno::UNO_QUERY_THROW);
283 css::uno::Reference<css::uno::XInterface> member;
284 member.set(
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));
289 member.set(
290 css::uno::Reference<css::lang::XSingleServiceFactory>(
291 access, css::uno::UNO_QUERY_THROW)->createInstance());
292 CPPUNIT_ASSERT(member.is());
293 try {
294 access->insertByName("", css::uno::Any(member));
295 CPPUNIT_FAIL("expected IllegalArgumentException");
296 } catch (css::lang::IllegalArgumentException &) {}
297 try {
298 access->insertByName("\x01", css::uno::Any(member));
299 CPPUNIT_FAIL("expected IllegalArgumentException");
300 } catch (css::lang::IllegalArgumentException &) {}
301 try {
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(
315 createViewAccess(
316 "/org.openoffice.Office.UI.GenericCommands/UserInterface/"
317 "Commands"),
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();
352 // test some changes
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());
360 // set to false
362 std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
363 comphelper::ConfigurationChanges::create());
364 officecfg::Office::Math::View::AutoRedraw::set(false, xChanges);
365 xChanges->commit();
367 CPPUNIT_ASSERT_MESSAGE("listener failed to trigger", !aSetting.get());
369 // set to true
371 std::shared_ptr< comphelper::ConfigurationChanges > xChanges(
372 comphelper::ConfigurationChanges::create());
373 officecfg::Office::Math::View::AutoRedraw::set(true, xChanges);
374 xChanges->commit();
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();
406 return value;
409 void Test::setKey(
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)
423 const
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);
430 if (!state.is()) {
431 return false;
433 state->setPropertyToDefault(name);
434 access->commitChanges();
435 css::uno::Reference< css::lang::XComponent >(
436 access, css::uno::UNO_QUERY_THROW)->dispose();
437 return true;
440 css::uno::Reference< css::uno::XInterface > Test::createViewAccess(
441 OUString const & path) const
443 css::uno::Any arg(
444 css::beans::NamedValue(
445 "nodepath",
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
455 css::uno::Any arg(
456 css::beans::NamedValue(
457 "nodepath",
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: */