From ae76aede5169eedea85c41b13122b8577c9b5e6e Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 7 Jun 2003 17:32:37 +0000 Subject: [PATCH] Added TestApp.{cpp,h}, since tests using the BTestApp class defined in libcppunit.so don't work anymore with it, since it uses the BApplication code of libbe.so. Several tests still fail now, but none crashes. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3447 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/storage/Jamfile | 1 + src/tests/kits/storage/TestApp.cpp | 136 +++++++++++++++++++++++++++++ src/tests/kits/storage/TestApp.h | 48 ++++++++++ 3 files changed, 185 insertions(+) create mode 100644 src/tests/kits/storage/TestApp.cpp create mode 100644 src/tests/kits/storage/TestApp.h diff --git a/src/tests/kits/storage/Jamfile b/src/tests/kits/storage/Jamfile index 3dd18ebe2a..01018ffa1e 100644 --- a/src/tests/kits/storage/Jamfile +++ b/src/tests/kits/storage/Jamfile @@ -20,6 +20,7 @@ CommonTestLib libstoragetest.so ResourceStringsTest.cpp StatableTest.cpp SymLinkTest.cpp + TestApp.cpp VolumeTest.cpp : libopenbeos.so be stdc++.r4 : be stdc++.r4 diff --git a/src/tests/kits/storage/TestApp.cpp b/src/tests/kits/storage/TestApp.cpp new file mode 100644 index 0000000000..bb3e982f8e --- /dev/null +++ b/src/tests/kits/storage/TestApp.cpp @@ -0,0 +1,136 @@ +// TestApp.cpp + +#include + +#include + +// TestHandler + +// MessageReceived +void +BTestHandler::MessageReceived(BMessage *message) +{ + // clone and push it + BMessage *clone = new BMessage(*message); + fQueue.Lock(); + fQueue.AddMessage(clone); + fQueue.Unlock(); +} + +// Queue +BMessageQueue & +BTestHandler::Queue() +{ + return fQueue; +} + + +// TestApp + +// constructor +BTestApp::BTestApp(const char *signature) + : BApplication(signature), + fAppThread(B_ERROR), + fHandlers() +{ + CreateTestHandler(); + Unlock(); +} + +// destructor +BTestApp::~BTestApp() +{ + int32 count = fHandlers.CountItems(); + for (int32 i = count - 1; i >= 0; i--) + DeleteTestHandler(TestHandlerAt(i)); +} + +// Init +status_t +BTestApp::Init() +{ + status_t error = B_OK; + fAppThread = spawn_thread(&_AppThreadStart, "query app", + B_NORMAL_PRIORITY, this); + if (fAppThread < 0) + error = fAppThread; + else { + error = resume_thread(fAppThread); + if (error != B_OK) + kill_thread(fAppThread); + } + if (error != B_OK) + fAppThread = B_ERROR; + return error; +} + +// Terminate +void +BTestApp::Terminate() +{ + PostMessage(B_QUIT_REQUESTED, this); + int32 result; + wait_for_thread(fAppThread, &result); +} + +// ReadyToRun +void +BTestApp::ReadyToRun() +{ +} + +// CreateTestHandler +BTestHandler * +BTestApp::CreateTestHandler() +{ + BTestHandler *handler = new BTestHandler; + Lock(); + AddHandler(handler); + fHandlers.AddItem(handler); + Unlock(); + return handler; +} + +// DeleteTestHandler +bool +BTestApp::DeleteTestHandler(BTestHandler *handler) +{ + bool result = false; + Lock(); + result = fHandlers.RemoveItem(handler); + if (result) + RemoveHandler(handler); + Unlock(); + if (result) + delete handler; + return result; +} + +// Handler +BTestHandler & +BTestApp::Handler() +{ + // The returned handler must never passed to DeleteTestHandler() by the + // caller! + return *TestHandlerAt(0); +} + +// TestHandlerAt +BTestHandler * +BTestApp::TestHandlerAt(int32 index) +{ + BAutolock _lock(this); + return (BTestHandler*)fHandlers.ItemAt(index); +} + +// _AppThreadStart +int32 +BTestApp::_AppThreadStart(void *data) +{ + if (BTestApp *app = (BTestApp*)data) { + app->Lock(); + app->Run(); + } + return 0; +} + diff --git a/src/tests/kits/storage/TestApp.h b/src/tests/kits/storage/TestApp.h new file mode 100644 index 0000000000..d43640545d --- /dev/null +++ b/src/tests/kits/storage/TestApp.h @@ -0,0 +1,48 @@ +// TestApp.h + +#ifndef _beos_test_app_h_ +#define _beos_test_app_h_ + +#include +#include +#include + +// TestHandler + +class BTestHandler : public BHandler { +public: + virtual void MessageReceived(BMessage *message); + BMessageQueue &Queue(); + +private: + BMessageQueue fQueue; +}; + + +// TestApp + +class BTestApp : public BApplication { +public: + BTestApp(const char *signature); + virtual ~BTestApp(); + + status_t Init(); + void Terminate(); + + virtual void ReadyToRun(); + + BTestHandler *CreateTestHandler(); + bool DeleteTestHandler(BTestHandler *handler); + + BTestHandler &Handler(); + BTestHandler *TestHandlerAt(int32 index); + +private: + static int32 _AppThreadStart(void *data); + +private: + thread_id fAppThread; + BList fHandlers; +}; + +#endif // _beos_test_app_h_