BHandler: fix IsWatched
Using map[key] inserts the key in the map if it wasn't there, making it non-empty. Change-Id: I4404b896126aee202780b6e93e7c17c69ca8f34d Reviewed-on: https://review.haiku-os.org/c/haiku/+/7787 Tested-by: Commit checker robot <[email protected]> Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
committed by
waddlesplash
parent
d7d7d543bc
commit
d6669c48ad
+16
-10
@@ -717,6 +717,8 @@ ObserverList::_ValidateHandlers(uint32 what)
|
||||
Add(target, what);
|
||||
iterator = handlers.erase(iterator);
|
||||
}
|
||||
if (handlers.empty())
|
||||
fHandlerMap.erase(what);
|
||||
}
|
||||
|
||||
|
||||
@@ -740,6 +742,8 @@ ObserverList::_SendNotices(uint32 what, BMessage* notice)
|
||||
(*iterator).SendMessage(notice);
|
||||
iterator++;
|
||||
}
|
||||
if (messengers.empty())
|
||||
fMessengerMap.erase(what);
|
||||
}
|
||||
|
||||
|
||||
@@ -818,38 +822,40 @@ ObserverList::Remove(const BHandler* handler, uint32 what)
|
||||
if (target.IsValid() && Remove(target, what) == B_OK)
|
||||
return B_OK;
|
||||
|
||||
status_t status = B_BAD_HANDLER;
|
||||
|
||||
vector<const BHandler*> &handlers = fHandlerMap[what];
|
||||
|
||||
vector<const BHandler*>::iterator iterator = find(handlers.begin(),
|
||||
handlers.end(), handler);
|
||||
if (iterator != handlers.end()) {
|
||||
handlers.erase(iterator);
|
||||
if (handlers.empty())
|
||||
fHandlerMap.erase(what);
|
||||
|
||||
return B_OK;
|
||||
status = B_OK;
|
||||
}
|
||||
if (handlers.empty())
|
||||
fHandlerMap.erase(what);
|
||||
|
||||
return B_BAD_HANDLER;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
ObserverList::Remove(const BMessenger &messenger, uint32 what)
|
||||
{
|
||||
status_t status = B_BAD_HANDLER;
|
||||
|
||||
vector<BMessenger> &messengers = fMessengerMap[what];
|
||||
|
||||
vector<BMessenger>::iterator iterator = find(messengers.begin(),
|
||||
messengers.end(), messenger);
|
||||
if (iterator != messengers.end()) {
|
||||
messengers.erase(iterator);
|
||||
if (messengers.empty())
|
||||
fMessengerMap.erase(what);
|
||||
|
||||
return B_OK;
|
||||
status = B_OK;
|
||||
}
|
||||
if (messengers.empty())
|
||||
fMessengerMap.erase(what);
|
||||
|
||||
return B_BAD_HANDLER;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -29,6 +29,9 @@ case 1: feed meaningless data, should return B_ERROR
|
||||
IsWatched()
|
||||
case 1: No added watchers; should return false
|
||||
case 2: Add watcher, should return true; remove watcher, should return false
|
||||
case 3: Add watcher, should return true; send notice, remove watcher, should return false
|
||||
case 4: Remove watcher, should return false
|
||||
case 5: Send notices without watchers, should return false
|
||||
|
||||
Looper()
|
||||
case 1: Not added to a BLooper, should return NULL
|
||||
@@ -112,4 +115,4 @@ StartWatchingAll(BHandler *)
|
||||
StopWatching(BHandler *, uint32 what)
|
||||
StopWatchingAll(BHandler *)
|
||||
|
||||
SendNotices(uint32 what, const BMessage * = 0)
|
||||
SendNotices(uint32 what, const BMessage * = 0)
|
||||
|
||||
@@ -47,12 +47,62 @@ void TIsWatchedTest::IsWatched2()
|
||||
#endif
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
IsWatched()
|
||||
@case Add watcher, send notice, then remove watcher
|
||||
@results Returns true after add, returns false after remove
|
||||
*/
|
||||
void TIsWatchedTest::IsWatched3()
|
||||
{
|
||||
BHandler Watcher;
|
||||
fHandler.StartWatching(&Watcher, '1234');
|
||||
CPPUNIT_ASSERT(fHandler.IsWatched() == true);
|
||||
|
||||
fHandler.SendNotices('1234');
|
||||
|
||||
fHandler.StopWatching(&Watcher, '1234');
|
||||
CPPUNIT_ASSERT(fHandler.IsWatched() == false);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
IsWatched()
|
||||
@case Remove inexistent watcher
|
||||
@results Returns false
|
||||
*/
|
||||
void TIsWatchedTest::IsWatched4()
|
||||
{
|
||||
BHandler Watcher;
|
||||
|
||||
fHandler.StopWatching(&Watcher, '1234');
|
||||
CPPUNIT_ASSERT(fHandler.IsWatched() == false);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
IsWatched()
|
||||
@case Send notices without watchers
|
||||
@results Returns false
|
||||
*/
|
||||
void TIsWatchedTest::IsWatched5()
|
||||
{
|
||||
BHandler Watcher;
|
||||
|
||||
// Create handler's internal list
|
||||
fHandler.StartWatching(&Watcher, '1234');
|
||||
fHandler.StopWatching(&Watcher, '1234');
|
||||
|
||||
fHandler.SendNotices('1234');
|
||||
CPPUNIT_ASSERT(fHandler.IsWatched() == false);
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
Test* TIsWatchedTest::Suite()
|
||||
{
|
||||
TestSuite* SuiteOfTests = new TestSuite("BHandler::IsWatched");
|
||||
|
||||
ADD_TEST4(BHandler, SuiteOfTests, TIsWatchedTest, IsWatched1);
|
||||
ADD_TEST4(BHandler, SuiteOfTests, TIsWatchedTest, IsWatched2);
|
||||
ADD_TEST4(BHandler, SuiteOfTests, TIsWatchedTest, IsWatched3);
|
||||
ADD_TEST4(BHandler, SuiteOfTests, TIsWatchedTest, IsWatched4);
|
||||
ADD_TEST4(BHandler, SuiteOfTests, TIsWatchedTest, IsWatched5);
|
||||
|
||||
return SuiteOfTests;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ class TIsWatchedTest : public TestCase
|
||||
|
||||
void IsWatched1();
|
||||
void IsWatched2();
|
||||
void IsWatched3();
|
||||
void IsWatched4();
|
||||
void IsWatched5();
|
||||
|
||||
static Test* Suite();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user