merge the formfield patch from ooo-build
[ooovba.git] / configmgr / qa / unit / threading.cxx
blobb04f7c35509dc94bef4c1235a0f7385d67611955
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: threading.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "common.hxx"
32 #include <osl/thread.hxx>
33 #include <osl/conditn.hxx>
34 #include <cppuhelper/implbase1.hxx>
35 #include "com/sun/star/beans/XPropertyChangeListener.hpp"
37 class KeyTester : public osl::Thread
39 osl::Condition *m_pCond;
40 protected:
41 rtl::OString m_aPath;
42 rtl::OString m_aKey;
43 Test *m_pTest;
44 public:
45 KeyTester (osl::Condition *pCond, Test *pTest,
46 const char *pPath, const char *pKey)
47 : m_pCond (pCond)
48 , m_aPath (pPath)
49 , m_aKey (pKey)
50 , m_pTest (pTest)
53 virtual ~KeyTester ()
56 virtual void run ()
58 testIteration();
59 while (!m_pCond->check())
60 testIteration();
62 virtual void testIteration()
64 css::uno::Any a = m_pTest->getKey (m_aPath, m_aKey);
65 CPPUNIT_ASSERT_MESSAGE ("no value", a.hasValue());
69 class KeyReader : public KeyTester
71 public:
72 KeyReader (osl::Condition *pCond, Test *pTest,
73 const char *pPath, const char *pKey)
74 : KeyTester (pCond, pTest, pPath, pKey)
76 // to ensure we have the right vtable when we hit 'run'
77 create();
81 class KeyWriter : public KeyTester
83 int curOpt;
84 public:
85 KeyWriter (osl::Condition *pCond, Test *pTest,
86 const char *pPath, const char *pKey)
87 : KeyTester (pCond, pTest, pPath, pKey)
88 , curOpt(0)
90 m_pTest->normalizePathKey (m_aPath, m_aKey);
91 create();
93 virtual void testIteration ()
95 try {
96 static const char *options[] = { "fish", "chips", "kippers", "bloaters" };
97 // fprintf (stderr, "set key %d\n",
98 // (int) osl_getThreadIdentifier(NULL));
99 m_pTest->setKey (m_aPath, rtl::OUString::createFromAscii (m_aKey),
100 css::uno::makeAny (rtl::OUString::createFromAscii(options[(curOpt++ & 3)])));
101 } CATCH_FAIL ("setting keys")
105 void Test::threadTests()
107 osl::Condition stop;
108 stop.reset();
110 struct {
111 const char *pPath;
112 const char *pKey;
113 } keyList[] = {
114 { "/org.openoffice.Setup", "Test/AString" },
115 { "/org.openoffice.Setup", "Test/AString" },
116 { "/org.openoffice.UI.GenericCommands", "UserInterface/Commands/dotuno:WebHtml/Label" },
117 { "/org.openoffice.UI.GenericCommands", "UserInterface/Commands/dotuno:NewPresentation/Label" },
118 { "/org.openoffice.UI.GenericCommands", "UserInterface/Commands/dotuno:RecentFileList/Label" },
120 { "/org.openoffice.Setup", "L10N/ooLocale" },
121 { "/org.openoffice.Setup", "Test/ABoolean" }
123 const int numReaders = sizeof (keyList) / sizeof (keyList[0]);
124 const int numWriters = (sizeof (keyList) / sizeof (keyList[0])) - 2;
125 KeyReader *pReaders[numReaders];
126 KeyWriter *pWriters[numReaders];
128 int i;
129 try {
130 for (i = 0; i < numReaders; i++) {
131 css::uno::Any a = getKey (keyList[i].pPath, keyList[i].pKey);
132 CPPUNIT_ASSERT_MESSAGE ("check key", a.hasValue());
135 // a few readers
136 for (i = 0; i < numReaders; i++)
137 pReaders[i] = new KeyReader (&stop, this, keyList[i].pPath,
138 keyList[i].pKey);
139 // a few writers
140 for (i = 0; i < numWriters; i++)
141 pWriters[i] = new KeyWriter (&stop, this, keyList[i].pPath,
142 keyList[i].pKey);
144 // Threads are running ...
145 const int numIters = 5;
146 for (int j = 0; j < numIters; j++) {
147 for (i = 0; i < numReaders; i++)
149 try {
150 rtl::OString aPath (keyList[i].pPath);
151 rtl::OString aKey (keyList[i].pKey);
152 normalizePathKey (aPath, aKey);
153 resetKey (aPath, rtl::OUString::createFromAscii (aKey));
154 osl::Thread::yield();
155 } CATCH_FAIL ("resetting keys");
158 stop.set();
160 for (i = 0; i < numReaders; i++) {
161 pReaders[i]->join();
162 delete pReaders[i];
164 for (i = 0; i < numWriters; i++) {
165 pWriters[i]->join();
166 delete pWriters[i];
169 } CATCH_FAIL ("checking keys exist")
172 class RecursiveListener : public cppu::WeakImplHelper1< css::beans::XPropertyChangeListener >
174 public:
175 sal_Int32 m_nRecurse;
176 css::uno::Reference< css::beans::XPropertySet > mxPropSet;
177 protected:
178 Test *m_pTest;
179 rtl::OString m_pPath;
180 rtl::OString m_pKey;
181 public:
182 RecursiveListener (Test *pTest, int nCount,
183 const char *pPath, const char *pKey)
184 : m_nRecurse (nCount)
185 , m_pTest (pTest)
186 , m_pPath (pPath)
187 , m_pKey (pKey)
189 mxPropSet = css::uno::Reference< css::beans::XPropertySet > (
190 pTest->createView (pPath, true), css::uno::UNO_QUERY_THROW );
192 CPPUNIT_ASSERT_MESSAGE ("is prop set", mxPropSet.is());
193 mxPropSet->addPropertyChangeListener (rtl::OUString::createFromAscii (m_pKey),
194 css::uno::Reference<css::beans::XPropertyChangeListener>(this));
196 virtual ~RecursiveListener()
198 disposeComponent (mxPropSet);
201 virtual void SAL_CALL acquire() throw() { cppu::WeakImplHelper1< css::beans::XPropertyChangeListener >::acquire(); }
202 virtual void SAL_CALL release() throw() { cppu::WeakImplHelper1< css::beans::XPropertyChangeListener >::acquire(); }
203 // XPropertyChangeListener
204 virtual void SAL_CALL propertyChange( const ::css::beans::PropertyChangeEvent& ) throw (::css::uno::RuntimeException)
206 if (m_nRecurse-- > 0)
207 runTest();
209 // XEventListener
210 virtual void SAL_CALL disposing( const ::css::lang::EventObject& ) throw (::css::uno::RuntimeException)
213 virtual void runTest()
215 m_pTest->setKey (m_pPath, rtl::OUString::createFromAscii (m_pKey),
216 css::uno::makeAny(
217 rtl::OUString::valueOf (m_nRecurse) ) );
221 class CrossThreadListener : public RecursiveListener
223 public:
224 CrossThreadListener (Test *pTest, int nCount,
225 const char *pPath, const char *pKey)
226 : RecursiveListener (pTest, nCount, pPath, pKey)
229 virtual ~CrossThreadListener()
232 virtual void runTest()
234 osl::Condition stopAfterOne;
235 stopAfterOne.set();
236 KeyWriter aWriter (&stopAfterOne, m_pTest, m_pPath, m_pKey);
237 aWriter.join();
239 rtl::OString aPath (m_pPath), aKey (m_pKey);
240 m_pTest->normalizePathKey (aPath, aKey);
241 m_pTest->resetKey (aPath, rtl::OUString::createFromAscii (aKey));
245 void Test::recursiveTests()
247 RecursiveListener *pList = new RecursiveListener(this, 100,
248 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/dotuno:WebHtml",
249 "Label");
250 css::uno::Reference< css::beans::XPropertyChangeListener > xListener(pList);
251 pList->runTest();
254 void Test::eventTests()
256 CrossThreadListener *pList = new CrossThreadListener(this, 10,
257 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/dotuno:WebHtml",
258 "Label");
259 css::uno::Reference< css::beans::XPropertyChangeListener > xListener(pList);
260 pList->runTest();