1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: threading.cxx,v $
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 ************************************************************************/
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
;
45 KeyTester (osl::Condition
*pCond
, Test
*pTest
,
46 const char *pPath
, const char *pKey
)
59 while (!m_pCond
->check())
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
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'
81 class KeyWriter
: public KeyTester
85 KeyWriter (osl::Condition
*pCond
, Test
*pTest
,
86 const char *pPath
, const char *pKey
)
87 : KeyTester (pCond
, pTest
, pPath
, pKey
)
90 m_pTest
->normalizePathKey (m_aPath
, m_aKey
);
93 virtual void testIteration ()
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()
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
];
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());
136 for (i
= 0; i
< numReaders
; i
++)
137 pReaders
[i
] = new KeyReader (&stop
, this, keyList
[i
].pPath
,
140 for (i
= 0; i
< numWriters
; i
++)
141 pWriters
[i
] = new KeyWriter (&stop
, this, keyList
[i
].pPath
,
144 // Threads are running ...
145 const int numIters
= 5;
146 for (int j
= 0; j
< numIters
; j
++) {
147 for (i
= 0; i
< numReaders
; i
++)
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");
160 for (i
= 0; i
< numReaders
; i
++) {
164 for (i
= 0; i
< numWriters
; i
++) {
169 } CATCH_FAIL ("checking keys exist")
172 class RecursiveListener
: public cppu::WeakImplHelper1
< css::beans::XPropertyChangeListener
>
175 sal_Int32 m_nRecurse
;
176 css::uno::Reference
< css::beans::XPropertySet
> mxPropSet
;
179 rtl::OString m_pPath
;
182 RecursiveListener (Test
*pTest
, int nCount
,
183 const char *pPath
, const char *pKey
)
184 : m_nRecurse (nCount
)
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)
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
),
217 rtl::OUString::valueOf (m_nRecurse
) ) );
221 class CrossThreadListener
: public RecursiveListener
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
;
236 KeyWriter
aWriter (&stopAfterOne
, m_pTest
, m_pPath
, m_pKey
);
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",
250 css::uno::Reference
< css::beans::XPropertyChangeListener
> xListener(pList
);
254 void Test::eventTests()
256 CrossThreadListener
*pList
= new CrossThreadListener(this, 10,
257 "/org.openoffice.UI.GenericCommands/UserInterface/Commands/dotuno:WebHtml",
259 css::uno::Reference
< css::beans::XPropertyChangeListener
> xListener(pList
);