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 .
23 #include <com/sun/star/test/XSimpleTest.hpp>
25 #include <com/sun/star/io/XActiveDataSource.hpp>
26 #include <com/sun/star/io/XActiveDataSink.hpp>
27 #include <com/sun/star/io/XActiveDataControl.hpp>
28 #include <com/sun/star/io/XConnectable.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/registry/XRegistryKey.hpp>
34 #include <uno/dispatcher.h>
35 #include <uno/mapping.hxx>
36 #include <cppuhelper/implbase.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <osl/thread.hxx>
42 using namespace ::osl
;
43 using namespace ::cppu
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::io
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::test
;
49 #include "testfactreg.hxx"
53 osl::Thread::wait(std::chrono::microseconds(10));
58 class OPumpTest
: public WeakImplHelper
< XSimpleTest
>
61 explicit OPumpTest( const Reference
< XMultiServiceFactory
> & rFactory
);
64 public: // implementation names
65 static Sequence
< OUString
> getSupportedServiceNames_Static() throw();
66 static OUString
getImplementationName_Static() throw();
69 virtual void SAL_CALL
testInvariant(const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
70 throw ( IllegalArgumentException
, RuntimeException
) ;
72 virtual sal_Int32 SAL_CALL
test( const OUString
& TestName
,
73 const Reference
< XInterface
>& TestObject
,
74 sal_Int32 hTestHandle
)
75 throw ( IllegalArgumentException
,
78 virtual sal_Bool SAL_CALL
testPassed() throw ( RuntimeException
) ;
79 virtual Sequence
< OUString
> SAL_CALL
getErrors() throw (RuntimeException
) ;
80 virtual Sequence
< Any
> SAL_CALL
getErrorExceptions() throw (RuntimeException
);
81 virtual Sequence
< OUString
> SAL_CALL
getWarnings() throw (RuntimeException
);
84 void testSimple( const Reference
< XInterface
> & );
85 void testWrongUsage( const Reference
< XInterface
> & );
86 void testClose( const Reference
< XInterface
>& );
87 void testTerminate( const Reference
< XInterface
>& );
88 void testFunction( const Reference
< XInterface
>& );
90 Sequence
<Any
> m_seqExceptions
;
91 Sequence
<OUString
> m_seqErrors
;
92 Sequence
<OUString
> m_seqWarnings
;
93 Reference
< XMultiServiceFactory
> m_rSmgr
;
97 OPumpTest::OPumpTest( const Reference
< XMultiServiceFactory
> &rFactory
) :
103 OPumpTest::~OPumpTest()
109 void OPumpTest::testInvariant( const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
110 throw ( IllegalArgumentException
,
113 Reference
< XServiceInfo
> info( TestObject
, UNO_QUERY
);
114 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
117 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
118 ERROR_ASSERT( ! info
->supportsService(
119 OUString( "bla bluzb" ) ), "XServiceInfo test failed" );
122 Reference
< XActiveDataSource
> xActiveDataSource( TestObject
, UNO_QUERY
);
123 Reference
< XActiveDataSink
> xActiveDataSink( TestObject
, UNO_QUERY
);
124 Reference
< XActiveDataControl
> xActiveDataControl( TestObject
, UNO_QUERY
);
125 Reference
< XConnectable
> xConnectable( TestObject
, UNO_QUERY
);
127 ERROR_ASSERT( xActiveDataSource
.is() && xActiveDataSink
.is() && xActiveDataControl
.is () &&
128 xConnectable
.is(), "specified interface not supported" );
132 sal_Int32
OPumpTest::test(
133 const OUString
& TestName
,
134 const Reference
< XInterface
>& TestObject
,
135 sal_Int32 hTestHandle
)
136 throw ( IllegalArgumentException
, RuntimeException
)
138 if( OUString( "com.sun.star.io.Pump" ) == TestName
) {
141 if( 0 == hTestHandle
) {
142 testInvariant( TestName
, TestObject
);
144 else if ( 1 == hTestHandle
)
146 testWrongUsage( TestObject
);
148 else if ( 2 == hTestHandle
)
150 testClose( TestObject
);
152 else if ( 3 == hTestHandle
)
154 testTerminate( TestObject
);
156 else if ( 4 == hTestHandle
)
158 testFunction( TestObject
);
161 catch( const Exception
& e
)
163 OString s
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
164 BUILD_ERROR( 0 , s
.getStr() );
168 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
173 if( 5 == hTestHandle
)
175 // all tests finished.
180 throw IllegalArgumentException();
186 sal_Bool
OPumpTest::testPassed() throw (RuntimeException
)
188 return m_seqErrors
.getLength() == 0;
192 Sequence
< OUString
> OPumpTest::getErrors() throw (RuntimeException
)
198 Sequence
< Any
> OPumpTest::getErrorExceptions() throw (RuntimeException
)
200 return m_seqExceptions
;
204 Sequence
< OUString
> OPumpTest::getWarnings() throw (RuntimeException
)
206 return m_seqWarnings
;
216 void OPumpTest::testSimple( const Reference
< XInterface
> &r
)
218 // jbu todo: add sensible test
222 class TestListener
: public WeakImplHelper
< XStreamListener
>
227 sal_Bool m_bTerminated
;
229 sal_Bool m_bDisposed
;
230 TestListener() : m_bStarted (sal_False
),
231 m_bClosed (sal_False
),
232 m_bTerminated ( sal_False
),
233 m_bError( sal_False
),
234 m_bDisposed( sal_False
)
237 virtual void SAL_CALL
disposing( const EventObject
&obj
) throw (css::uno::RuntimeException
)
239 m_bDisposed
= sal_True
;
240 // printf( "disposing called\n");
243 virtual void SAL_CALL
started( ) throw (css::uno::RuntimeException
)
245 m_bStarted
= sal_True
;
246 // printf( "started called\n");
248 virtual void SAL_CALL
closed( ) throw (css::uno::RuntimeException
)
250 m_bClosed
= sal_True
;
251 // printf( "closed called\n");
253 virtual void SAL_CALL
terminated( ) throw (css::uno::RuntimeException
)
255 m_bTerminated
= sal_True
;
256 // printf( "terminated called\n");
258 virtual void SAL_CALL
error( const css::uno::Any
& aException
)
259 throw (css::uno::RuntimeException
)
264 // printf( "error called %s\n", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
271 TestCase( const Reference
< XMultiServiceFactory
> & rSMgr
,
272 const Reference
< XInterface
> &r
) : m_rSmgr( rSMgr
), m_pTestListener( 0 )
274 m_rControl
.set( r
, UNO_QUERY
);
276 Reference
< XActiveDataSource
> rSource ( r
, UNO_QUERY
);
277 Reference
< XActiveDataSink
> rSink( r
, UNO_QUERY
);
279 m_rOutSource
.set( createPipe() );
280 rSink
->setInputStream(Reference
< XInputStream
> (m_rOutSource
,UNO_QUERY
));
282 Reference
< XOutputStream
> rOutSink( createPipe() );
283 m_rInSink
.set( rOutSink
, UNO_QUERY
);
284 rSource
->setOutputStream( rOutSink
);
286 m_pTestListener
= new TestListener();
287 m_pTestListener
->acquire();
288 m_rControl
->addListener( m_pTestListener
);
293 if( m_pTestListener
)
294 m_pTestListener
->release();
297 TestListener
*m_pTestListener
;
298 Reference
< XActiveDataControl
> m_rControl
;
299 Reference
< XOutputStream
> m_rOutSource
;
300 Reference
< XInputStream
> m_rInSink
;
301 Reference
< XMultiServiceFactory
> m_rSmgr
;
304 Reference
< XOutputStream
> createPipe()
306 Reference
< XOutputStream
> rOut( m_rSmgr
->createInstance(
307 "com.sun.star.io.Pipe"),UNO_QUERY
);
313 void OPumpTest::testClose( const Reference
< XInterface
> &r
)
315 TestCase
t( m_rSmgr
, r
);
317 ERROR_ASSERT( ! t
.m_pTestListener
->m_bStarted
, "started too early" );
318 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "termination unexpected" );
319 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
320 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected close" );
322 t
.m_rControl
->start();
325 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
326 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "termination unexpected" );
327 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
328 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected close" );
330 Reference
< XStreamListener
> rListener( new TestListener() );
331 t
.m_rControl
->addListener( rListener
);
332 t
.m_rControl
->removeListener( rListener
);
334 t
.m_rOutSource
->closeOutput();
336 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
337 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "should be terminated already" );
338 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
339 ERROR_ASSERT( t
.m_pTestListener
->m_bClosed
, "should be closed already" );
342 void OPumpTest::testTerminate( const Reference
< XInterface
> &r
)
344 TestCase
t( m_rSmgr
, r
);
346 ERROR_ASSERT( ! t
.m_pTestListener
->m_bStarted
, "started too early" );
347 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "termination unexpected" );
348 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
349 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected closed" );
351 t
.m_rControl
->start();
354 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
355 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "termination unexpected" );
356 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
357 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected closed" );
359 t
.m_rControl
->terminate();
362 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
363 ERROR_ASSERT( t
.m_pTestListener
->m_bTerminated
, "should be terminated already" );
364 // terminate leads to an error, that is no surprise, in fact
365 // one can't tell whether the error occurs because of the terminate
366 // call or for some other reason!
367 // ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
368 ERROR_ASSERT( t
.m_pTestListener
->m_bClosed
, "should be closed already" );
371 void OPumpTest::testFunction( const Reference
< XInterface
> &r
)
373 TestCase
t( m_rSmgr
, r
);
375 t
.m_rControl
->start();
377 t
.m_rOutSource
->writeBytes( Sequence
< sal_Int8
> ( 5 ) );
379 Sequence
< sal_Int8
> dummy
;
380 ERROR_ASSERT( 5 == t
.m_rInSink
->readBytes( dummy
, 5 ), "couldn't read the expected number of bytes" );
382 t
.m_rOutSource
->closeOutput();
385 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
386 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "should be terminated already" );
387 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
388 ERROR_ASSERT( t
.m_pTestListener
->m_bClosed
, "should be closed already" );
391 void OPumpTest::testWrongUsage( const Reference
< XInterface
> &r
)
393 Reference
< XActiveDataSource
> rSource ( r
, UNO_QUERY
);
394 Reference
< XActiveDataSink
> rSink( r
, UNO_QUERY
);
395 Reference
< XActiveDataControl
> rControl( r
, UNO_QUERY
);
397 Reference
< XInputStream
> rIn( m_rSmgr
->createInstance(
398 "com.sun.star.io.DataInputStream"),UNO_QUERY
);
399 Reference
< XOutputStream
> rOut( m_rSmgr
->createInstance(
400 "com.sun.star.io.DataOutputStream"),UNO_QUERY
);
402 rSink
->setInputStream( rIn
);
403 rSource
->setOutputStream( rOut
);
410 Reference
< XInterface
> SAL_CALL
OPumpTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw( Exception
)
412 return *new OPumpTest( rSMgr
);
415 Sequence
<OUString
> OPumpTest_getSupportedServiceNames() throw()
417 return { OPumpTest_getServiceName() };
420 OUString
OPumpTest_getServiceName() throw()
422 return OUString( "test.com.sun.star.io.Pump" );
425 OUString
OPumpTest_getImplementationName() throw()
427 return OUString( "test.com.sun.star.comp.io.Pump" );
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */