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 <osl/diagnose.h>
24 #include <com/sun/star/test/XSimpleTest.hpp>
26 #include <com/sun/star/io/XActiveDataSource.hpp>
27 #include <com/sun/star/io/XActiveDataSink.hpp>
28 #include <com/sun/star/io/XActiveDataControl.hpp>
29 #include <com/sun/star/io/XConnectable.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/registry/XRegistryKey.hpp>
35 #include <uno/dispatcher.h>
36 #include <uno/mapping.hxx>
37 #include <cppuhelper/implbase1.hxx>
38 #include <cppuhelper/factory.hxx>
39 #include <osl/mutex.hxx>
40 #include <osl/thread.h>
46 using namespace ::rtl
;
47 using namespace ::osl
;
48 using namespace ::cppu
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::io
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::test
;
54 #include "testfactreg.hxx"
58 TimeValue a
= { 0, 10000 };
64 class OPumpTest
: public WeakImplHelper1
< XSimpleTest
>
67 OPumpTest( const Reference
< XMultiServiceFactory
> & rFactory
);
70 public: // implementation names
71 static Sequence
< OUString
> getSupportedServiceNames_Static(void) throw();
72 static OUString
getImplementationName_Static() throw();
75 virtual void SAL_CALL
testInvariant(const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
76 throw ( IllegalArgumentException
, RuntimeException
) ;
78 virtual sal_Int32 SAL_CALL
test( const OUString
& TestName
,
79 const Reference
< XInterface
>& TestObject
,
80 sal_Int32 hTestHandle
)
81 throw ( IllegalArgumentException
,
84 virtual sal_Bool SAL_CALL
testPassed(void) throw ( RuntimeException
) ;
85 virtual Sequence
< OUString
> SAL_CALL
getErrors(void) throw (RuntimeException
) ;
86 virtual Sequence
< Any
> SAL_CALL
getErrorExceptions(void) throw (RuntimeException
);
87 virtual Sequence
< OUString
> SAL_CALL
getWarnings(void) throw (RuntimeException
);
90 void testSimple( const Reference
< XInterface
> & );
91 void testWrongUsage( const Reference
< XInterface
> & );
92 void testClose( const Reference
< XInterface
>& );
93 void testTerminate( const Reference
< XInterface
>& );
94 void testFunction( const Reference
< XInterface
>& );
96 Sequence
<Any
> m_seqExceptions
;
97 Sequence
<OUString
> m_seqErrors
;
98 Sequence
<OUString
> m_seqWarnings
;
99 Reference
< XMultiServiceFactory
> m_rSmgr
;
103 OPumpTest::OPumpTest( const Reference
< XMultiServiceFactory
> &rFactory
) :
109 OPumpTest::~OPumpTest()
116 void OPumpTest::testInvariant( const OUString
& TestName
, const Reference
< XInterface
>& TestObject
)
117 throw ( IllegalArgumentException
,
120 Reference
< XServiceInfo
> info( TestObject
, UNO_QUERY
);
121 ERROR_ASSERT( info
.is() , "XServiceInfo not supported !" );
124 ERROR_ASSERT( info
->supportsService( TestName
), "XServiceInfo test failed" );
125 ERROR_ASSERT( ! info
->supportsService(
126 OUString( "bla bluzb" ) ), "XServiceInfo test failed" );
129 Reference
< XActiveDataSource
> xActiveDataSource( TestObject
, UNO_QUERY
);
130 Reference
< XActiveDataSink
> xActiveDataSink( TestObject
, UNO_QUERY
);
131 Reference
< XActiveDataControl
> xActiveDataControl( TestObject
, UNO_QUERY
);
132 Reference
< XConnectable
> xConnectable( TestObject
, UNO_QUERY
);
134 ERROR_ASSERT( xActiveDataSource
.is() && xActiveDataSink
.is() && xActiveDataControl
.is () &&
135 xConnectable
.is(), "specified interface not supported" );
139 sal_Int32
OPumpTest::test(
140 const OUString
& TestName
,
141 const Reference
< XInterface
>& TestObject
,
142 sal_Int32 hTestHandle
)
143 throw ( IllegalArgumentException
, RuntimeException
)
145 if( OUString( "com.sun.star.io.Pump" ) == TestName
) {
148 if( 0 == hTestHandle
) {
149 testInvariant( TestName
, TestObject
);
151 else if ( 1 == hTestHandle
)
153 testWrongUsage( TestObject
);
155 else if ( 2 == hTestHandle
)
157 testClose( TestObject
);
159 else if ( 3 == hTestHandle
)
161 testTerminate( TestObject
);
163 else if ( 4 == hTestHandle
)
165 testFunction( TestObject
);
168 catch( const Exception
& e
)
170 OString s
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
171 BUILD_ERROR( 0 , s
.getStr() );
175 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
180 if( 5 == hTestHandle
)
182 // all tests finished.
187 throw IllegalArgumentException();
194 sal_Bool
OPumpTest::testPassed(void) throw (RuntimeException
)
196 return m_seqErrors
.getLength() == 0;
200 Sequence
< OUString
> OPumpTest::getErrors(void) throw (RuntimeException
)
206 Sequence
< Any
> OPumpTest::getErrorExceptions(void) throw (RuntimeException
)
208 return m_seqExceptions
;
212 Sequence
< OUString
> OPumpTest::getWarnings(void) throw (RuntimeException
)
214 return m_seqWarnings
;
224 void OPumpTest::testSimple( const Reference
< XInterface
> &r
)
226 // jbu todo: add sensible test
230 class TestListener
: public WeakImplHelper1
< XStreamListener
>
235 sal_Bool m_bTerminated
;
237 sal_Bool m_bDisposed
;
238 TestListener() : m_bStarted (sal_False
),
239 m_bClosed (sal_False
),
240 m_bTerminated ( sal_False
),
241 m_bError( sal_False
),
242 m_bDisposed( sal_False
)
245 virtual void SAL_CALL
disposing( const EventObject
&obj
) throw (::com::sun::star::uno::RuntimeException
)
247 m_bDisposed
= sal_True
;
248 // printf( "disposing called\n");
251 virtual void SAL_CALL
started( ) throw (::com::sun::star::uno::RuntimeException
)
253 m_bStarted
= sal_True
;
254 // printf( "started called\n");
256 virtual void SAL_CALL
closed( ) throw (::com::sun::star::uno::RuntimeException
)
258 m_bClosed
= sal_True
;
259 // printf( "closed called\n");
261 virtual void SAL_CALL
terminated( ) throw (::com::sun::star::uno::RuntimeException
)
263 m_bTerminated
= sal_True
;
264 // printf( "terminated called\n");
266 virtual void SAL_CALL
error( const ::com::sun::star::uno::Any
& aException
)
267 throw (::com::sun::star::uno::RuntimeException
)
272 // printf( "error called %s\n", OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US).getStr() );
279 TestCase( const Reference
< XMultiServiceFactory
> & rSMgr
,
280 const Reference
< XInterface
> &r
) : m_rSmgr( rSMgr
), m_pTestListener( 0 )
282 m_rControl
= Reference
<XActiveDataControl
>( r
, UNO_QUERY
);
284 Reference
< XActiveDataSource
> rSource ( r
, UNO_QUERY
);
285 Reference
< XActiveDataSink
> rSink( r
, UNO_QUERY
);
287 m_rOutSource
= Reference
< XOutputStream
> ( createPipe() );
288 rSink
->setInputStream(Reference
< XInputStream
> (m_rOutSource
,UNO_QUERY
));
290 Reference
< XOutputStream
> rOutSink( createPipe() );
291 m_rInSink
= Reference
< XInputStream
> ( rOutSink
, UNO_QUERY
);
292 rSource
->setOutputStream( rOutSink
);
294 m_pTestListener
= new TestListener();
295 m_pTestListener
->acquire();
296 m_rControl
->addListener( m_pTestListener
);
301 if( m_pTestListener
)
302 m_pTestListener
->release();
305 TestListener
*m_pTestListener
;
306 Reference
< XActiveDataControl
> m_rControl
;
307 Reference
< XOutputStream
> m_rOutSource
;
308 Reference
< XInputStream
> m_rInSink
;
309 Reference
< XMultiServiceFactory
> m_rSmgr
;
312 Reference
< XOutputStream
> createPipe()
314 Reference
< XOutputStream
> rOut( m_rSmgr
->createInstance(
315 OUString("com.sun.star.io.Pipe")),UNO_QUERY
);
322 void OPumpTest::testClose( const Reference
< XInterface
> &r
)
324 TestCase
t( m_rSmgr
, r
);
326 ERROR_ASSERT( ! t
.m_pTestListener
->m_bStarted
, "started too early" );
327 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "terminiation unexpected" );
328 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
329 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected clase" );
331 t
.m_rControl
->start();
334 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
335 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "terminiation unexpected" );
336 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
337 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected clase" );
339 Reference
< XStreamListener
> rListener( new TestListener() );
340 t
.m_rControl
->addListener( rListener
);
341 t
.m_rControl
->removeListener( rListener
);
343 t
.m_rOutSource
->closeOutput();
345 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
346 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "should be terminiated already" );
347 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
348 ERROR_ASSERT( t
.m_pTestListener
->m_bClosed
, "should be closed already" );
351 void OPumpTest::testTerminate( const Reference
< XInterface
> &r
)
353 TestCase
t( m_rSmgr
, r
);
355 ERROR_ASSERT( ! t
.m_pTestListener
->m_bStarted
, "started too early" );
356 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "terminiation unexpected" );
357 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
358 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected clase" );
360 t
.m_rControl
->start();
363 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
364 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "terminiation unexpected" );
365 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
366 ERROR_ASSERT( ! t
.m_pTestListener
->m_bClosed
, "unexpected clase" );
368 t
.m_rControl
->terminate();
371 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
372 ERROR_ASSERT( t
.m_pTestListener
->m_bTerminated
, "should be terminiated already" );
373 // terminte leads to an error, that is no surprise, in fact
374 // one can't tell whether the error occurs because of the terminate
375 // call or for some other reason !
376 // ERROR_ASSERT( ! t.m_pTestListener->m_bError, "unexpected error" );
377 ERROR_ASSERT( t
.m_pTestListener
->m_bClosed
, "should be closed already" );
380 void OPumpTest::testFunction( const Reference
< XInterface
> &r
)
382 TestCase
t( m_rSmgr
, r
);
384 t
.m_rControl
->start();
386 t
.m_rOutSource
->writeBytes( Sequence
< sal_Int8
> ( 5 ) );
388 Sequence
< sal_Int8
> dummy
;
389 ERROR_ASSERT( 5 == t
.m_rInSink
->readBytes( dummy
, 5 ), "couldn't read the expected number of bytes" );
391 t
.m_rOutSource
->closeOutput();
394 ERROR_ASSERT( t
.m_pTestListener
->m_bStarted
, "should have been started already" );
395 ERROR_ASSERT( ! t
.m_pTestListener
->m_bTerminated
, "should be terminiated already" );
396 ERROR_ASSERT( ! t
.m_pTestListener
->m_bError
, "unexpected error" );
397 ERROR_ASSERT( t
.m_pTestListener
->m_bClosed
, "should be closed already" );
400 void OPumpTest::testWrongUsage( const Reference
< XInterface
> &r
)
402 Reference
< XActiveDataSource
> rSource ( r
, UNO_QUERY
);
403 Reference
< XActiveDataSink
> rSink( r
, UNO_QUERY
);
404 Reference
< XActiveDataControl
> rControl( r
, UNO_QUERY
);
406 Reference
< XInputStream
> rIn( m_rSmgr
->createInstance(
407 OUString("com.sun.star.io.DataInputStream")),UNO_QUERY
);
408 Reference
< XOutputStream
> rOut( m_rSmgr
->createInstance(
409 OUString("com.sun.star.io.DataOutputStream")),UNO_QUERY
);
411 rSink
->setInputStream( rIn
);
412 rSource
->setOutputStream( rOut
);
419 Reference
< XInterface
> SAL_CALL
OPumpTest_CreateInstance( const Reference
< XMultiServiceFactory
> & rSMgr
) throw( Exception
)
421 return *new OPumpTest( rSMgr
);
424 Sequence
<OUString
> OPumpTest_getSupportedServiceNames(void) throw()
426 OUString s
= OPumpTest_getServiceName();
427 Sequence
< OUString
> seq( &s
, 1 );
431 OUString
OPumpTest_getServiceName() throw()
433 return OUString( "test.com.sun.star.io.Pump" );
436 OUString
OPumpTest_getImplementationName() throw()
438 return OUString( "test.com.sun.star.comp.io.Pump" );
441 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */