1 //------------------------------------------------------------------------------ 2 // MessengerAssignmentTester.cpp 3 // 4 //------------------------------------------------------------------------------ 5 6 // Standard Includes ----------------------------------------------------------- 7 #include <stdio.h> 8 9 // System Includes ------------------------------------------------------------- 10 #include <Message.h> 11 #include <OS.h> 12 #include <Handler.h> 13 #include <Looper.h> 14 #include <Messenger.h> 15 16 // Project Includes ------------------------------------------------------------ 17 #include <TestUtils.h> 18 #include <ThreadedTestCaller.h> 19 #include <cppunit/TestSuite.h> 20 21 // Local Includes -------------------------------------------------------------- 22 #include "Helpers.h" 23 #include "MessengerAssignmentTester.h" 24 25 // Local Defines --------------------------------------------------------------- 26 27 // Globals --------------------------------------------------------------------- 28 29 //------------------------------------------------------------------------------ 30 31 // constructor 32 MessengerAssignmentTester::MessengerAssignmentTester() 33 : BThreadedTestCase(), 34 fHandler(NULL), 35 fLooper(NULL) 36 { 37 } 38 39 // constructor 40 MessengerAssignmentTester::MessengerAssignmentTester(std::string name) 41 : BThreadedTestCase(name), 42 fHandler(NULL), 43 fLooper(NULL) 44 { 45 } 46 47 // destructor 48 MessengerAssignmentTester::~MessengerAssignmentTester() 49 { 50 if (fLooper) { 51 fLooper->Lock(); 52 if (fHandler) { 53 fLooper->RemoveHandler(fHandler); 54 delete fHandler; 55 } 56 fLooper->Quit(); 57 } 58 } 59 60 /* 61 BMessenger &operator=(const BMessenger &from) 62 @case 1 from is uninitialized 63 @results IsValid() and IsTargetLocal() should return false 64 Target() should return NULL and NULL for looper. 65 Team() should return -1. 66 */ 67 void MessengerAssignmentTester::AssignmentTest1() 68 { 69 BMessenger from; 70 BMessenger messenger; 71 CHK(&(messenger = from) == &messenger); 72 CHK(messenger.IsValid() == false); 73 CHK(messenger.IsTargetLocal() == false); 74 BLooper *looper; 75 CHK(messenger.Target(&looper) == NULL); 76 CHK(looper == NULL); 77 CHK(messenger.Team() == -1); 78 } 79 80 /* 81 BMessenger &operator=(const BMessenger &from) 82 @case 2 from is properly initialized to a local target 83 (preferred handler) 84 @results IsValid() and IsTargetLocal() should return true 85 Target() should return the same values as for from. 86 Team() should return this team. 87 */ 88 void MessengerAssignmentTester::AssignmentTest2() 89 { 90 // create looper 91 status_t result = B_OK; 92 BLooper *looper = new BLooper; 93 looper->Run(); 94 LooperQuitter quitter(looper); 95 // create the from messenger 96 BMessenger from(NULL, looper, &result); 97 CHK(from.IsValid() == true); 98 CHK(from.IsTargetLocal() == true); 99 BLooper *resultLooper; 100 CHK(from.Target(&resultLooper) == NULL); 101 CHK(resultLooper == looper); 102 CHK(from.Team() == get_this_team()); 103 CHK(result == B_OK); 104 // create the messenger and do the checks 105 BMessenger messenger; 106 CHK(&(messenger = from) == &messenger); 107 CHK(messenger.IsValid() == true); 108 CHK(messenger.IsTargetLocal() == true); 109 resultLooper = NULL; 110 CHK(messenger.Target(&resultLooper) == NULL); 111 CHK(resultLooper == looper); 112 CHK(messenger.Team() == get_this_team()); 113 } 114 115 /* 116 BMessenger &operator=(const BMessenger &from) 117 @case 3 from is properly initialized to a local target 118 (specific handler) 119 @results IsValid() and IsTargetLocal() should return true 120 Target() should return the same values as for from. 121 Team() should return this team. 122 */ 123 void MessengerAssignmentTester::AssignmentTest3() 124 { 125 // create looper and handler 126 status_t result = B_OK; 127 BLooper *looper = new BLooper; 128 looper->Run(); 129 LooperQuitter quitter(looper); 130 BHandler *handler = new BHandler; 131 HandlerDeleter deleter(handler); 132 CHK(looper->Lock()); 133 looper->AddHandler(handler); 134 looper->Unlock(); 135 // create the from messenger 136 BMessenger from(handler, looper, &result); 137 CHK(from.IsValid() == true); 138 CHK(from.IsTargetLocal() == true); 139 BLooper *resultLooper; 140 CHK(from.Target(&resultLooper) == handler); 141 CHK(resultLooper == looper); 142 CHK(from.Team() == get_this_team()); 143 CHK(result == B_OK); 144 // create the messenger and do the checks 145 BMessenger messenger; 146 CHK(&(messenger = from) == &messenger); 147 CHK(messenger.IsValid() == true); 148 CHK(messenger.IsTargetLocal() == true); 149 resultLooper = NULL; 150 CHK(messenger.Target(&resultLooper) == handler); 151 CHK(resultLooper == looper); 152 CHK(messenger.Team() == get_this_team()); 153 } 154 155 156 Test* MessengerAssignmentTester::Suite() 157 { 158 typedef BThreadedTestCaller<MessengerAssignmentTester> TC; 159 160 TestSuite* testSuite = new TestSuite; 161 162 ADD_TEST4(BMessenger, testSuite, MessengerAssignmentTester, AssignmentTest1); 163 ADD_TEST4(BMessenger, testSuite, MessengerAssignmentTester, AssignmentTest2); 164 ADD_TEST4(BMessenger, testSuite, MessengerAssignmentTester, AssignmentTest3); 165 166 return testSuite; 167 } 168 169 170