152a38012Sejakowatz /* 2*571d840aSOliver Tappe $Id: AutolockLooperTest.cpp 332 2002-07-19 06:45:28Z tylerdauwalder $ 352a38012Sejakowatz 452a38012Sejakowatz This file tests all use cases of the BAutolock when used with a BLooper. 552a38012Sejakowatz BLocker based tests are done seperately. 652a38012Sejakowatz 752a38012Sejakowatz */ 852a38012Sejakowatz 952a38012Sejakowatz 10a6b33ea3STyler Dauwalder #include "ThreadedTestCaller.h" 1152a38012Sejakowatz #include "AutolockLooperTest.h" 12a6b33ea3STyler Dauwalder #include <Autolock.h> 13a6b33ea3STyler Dauwalder #include <Looper.h> 1452a38012Sejakowatz #include <OS.h> 1552a38012Sejakowatz 1652a38012Sejakowatz 1752a38012Sejakowatz const bigtime_t SNOOZE_TIME = 250000; 1852a38012Sejakowatz 1952a38012Sejakowatz 2052a38012Sejakowatz /* 21a6b33ea3STyler Dauwalder * Method: AutolockLooperTest::AutolockLooperTest() 2252a38012Sejakowatz * Descr: This method is the only constructor for the AutolockLooperTest 2352a38012Sejakowatz * class. 2452a38012Sejakowatz */ 2552a38012Sejakowatz 26a6b33ea3STyler Dauwalder AutolockLooperTest(std::string name)27a6b33ea3STyler Dauwalder AutolockLooperTest::AutolockLooperTest(std::string name) : 28a6b33ea3STyler Dauwalder BThreadedTestCase(name), theLooper(new BLooper) 2952a38012Sejakowatz { 3052a38012Sejakowatz theLooper->Run(); 3152a38012Sejakowatz } 3252a38012Sejakowatz 3352a38012Sejakowatz 3452a38012Sejakowatz /* 35a6b33ea3STyler Dauwalder * Method: AutolockLooperTest::~AutolockLooperTest() 3652a38012Sejakowatz * Descr: This method is the destructor for the AutolockLooperTest class. 3752a38012Sejakowatz * It only deallocates the autoLooper and Looper. 3852a38012Sejakowatz */ 3952a38012Sejakowatz 40a6b33ea3STyler Dauwalder ~AutolockLooperTest()41a6b33ea3STyler Dauwalder AutolockLooperTest::~AutolockLooperTest() 4252a38012Sejakowatz { 4352a38012Sejakowatz if (theLooper != NULL) 4452a38012Sejakowatz theLooper->Lock(); 4552a38012Sejakowatz theLooper->Quit(); 4652a38012Sejakowatz } 4752a38012Sejakowatz 4852a38012Sejakowatz 4952a38012Sejakowatz /* 50a6b33ea3STyler Dauwalder * Method: AutolockLooperTest::TestThread1() 5152a38012Sejakowatz * Descr: This method performs the tests on the Autolock. It constructs a new 5252a38012Sejakowatz * Autolock and checks that both the Autolock and the Looper are 5352a38012Sejakowatz * both locked. Then, the Autolock is released by deleting it. The Looper 5452a38012Sejakowatz * is checked to see that it is now released. 5552a38012Sejakowatz */ 5652a38012Sejakowatz 57a6b33ea3STyler Dauwalder TestThread1(void)58a6b33ea3STyler Dauwalder void AutolockLooperTest::TestThread1(void) 5952a38012Sejakowatz { 60a6b33ea3STyler Dauwalder BAutolock *theAutolock = new BAutolock(theLooper); 6152a38012Sejakowatz 62a6b33ea3STyler Dauwalder NextSubTest(); 63a6b33ea3STyler Dauwalder CPPUNIT_ASSERT(theLooper->IsLocked()); 64a6b33ea3STyler Dauwalder CPPUNIT_ASSERT(theLooper->LockingThread() == find_thread(NULL)); 65a6b33ea3STyler Dauwalder CPPUNIT_ASSERT(theAutolock->IsLocked()); 6652a38012Sejakowatz 67a6b33ea3STyler Dauwalder NextSubTest(); 6852a38012Sejakowatz delete theAutolock; 6952a38012Sejakowatz theAutolock = NULL; 70a6b33ea3STyler Dauwalder CPPUNIT_ASSERT(theLooper->LockingThread() != find_thread(NULL)); 7152a38012Sejakowatz } 7252a38012Sejakowatz 7352a38012Sejakowatz 7452a38012Sejakowatz /* 75a6b33ea3STyler Dauwalder * Method: AutolockLooperTest::suite() 7652a38012Sejakowatz * Descr: This static member function returns a test caller for performing 7752a38012Sejakowatz * the "AutolockLooperTest" test. The test caller 7852a38012Sejakowatz * is created as a ThreadedTestCaller (typedef'd as 7952a38012Sejakowatz * BenaphoreLockCountTest1Caller) with three independent threads. 8052a38012Sejakowatz */ 8152a38012Sejakowatz 82a6b33ea3STyler Dauwalder suite(void)83a6b33ea3STyler DauwalderCppUnit::Test *AutolockLooperTest::suite(void) 8452a38012Sejakowatz { 85a6b33ea3STyler Dauwalder typedef BThreadedTestCaller <AutolockLooperTest > 86a6b33ea3STyler Dauwalder AutolockLooperTestCaller; 87a6b33ea3STyler Dauwalder 88a6b33ea3STyler Dauwalder AutolockLooperTest *theTest = new AutolockLooperTest(""); 89a6b33ea3STyler Dauwalder AutolockLooperTestCaller *threadedTest = new AutolockLooperTestCaller("BAutolock::Looper Test", theTest); 90a6b33ea3STyler Dauwalder threadedTest->addThread("A", &AutolockLooperTest::TestThread1); 9152a38012Sejakowatz return(threadedTest); 9252a38012Sejakowatz } 9352a38012Sejakowatz 9452a38012Sejakowatz 95a6b33ea3STyler Dauwalder 96a6b33ea3STyler Dauwalder 97