TEST( Notifier, TestOfNotificationUseAfterRelease ) { Notifier notifier; TestClazzReleased* pReleasedNode = new TestClazzReleased( notifier ); pReleasedNode->init(); CCNode* pHolder = new CCNode(); pHolder->init(); notifier.addNotification( getNotificationTestUseAfterRelease(), {pHolder, &CCNode::removeAllChildren } ); EXPECT_EQ( 1u, pReleasedNode->retainCount() ); EXPECT_EQ( 1u, pHolder->retainCount() ); pHolder->addChild( pReleasedNode ); EXPECT_EQ( 2u, pReleasedNode->retainCount() ); EXPECT_EQ( 1u, pHolder->retainCount() ); pReleasedNode->release(); EXPECT_EQ( 1u, pReleasedNode->retainCount() ); EXPECT_EQ( 1u, pHolder->retainCount() ); //OLD: Now when we notify we have problem. We have use after release. //NEW: Now everything should work because we mark second notification as toRemove so we simply skip it :) notifier.notify( getNotificationTestUseAfterRelease() ); pHolder->release(); }
TEST( Notifier, TestOfNotificationChangesStack ) { Notifier notifier; TestClazz testClazz; notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 0, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.removeAllForObject( &testClazz ); notifier.notify( getNotificationTest3(), 0x01 ); }
TEST( Notifier, DISABLED_TestOfNotificationChangesStackDoubleCommands ) { Notifier notifier; TestClazz testClazz; notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 0, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.removeAllForObject( &testClazz ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.notify( getNotificationTest3(), 0x01 ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 2, testClazz.deliveredCount ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); //Now we have double notification notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); notifier.removeNotification( &testClazz, getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); }
TEST( Notifier, TestOfNotification ) { Notifier notifier; TestClazz testClazz; notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 0, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 1, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 3, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 3, testClazz.deliveredCount ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); EXPECT_EQ( 3, testClazz.deliveredCount ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); notifier.removeNotification( &testClazz, getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 4, testClazz.deliveredCount ); ////////////////////////////////////////////////////////////////////////////////////// notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.addNotification( getNotificationTest2(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 5, testClazz.deliveredCount ); notifier.notify( getNotificationTest2() ); EXPECT_EQ( 6, testClazz.deliveredCount ); notifier.removeNotification( &testClazz, getNotificationTest1() ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 6, testClazz.deliveredCount ); notifier.notify( getNotificationTest2() ); EXPECT_EQ( 7, testClazz.deliveredCount ); notifier.addNotification( getNotificationTest1(), Utils::makeCallback( &testClazz, &TestClazz::onDeliverNotification ) ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 8, testClazz.deliveredCount ); notifier.removeAllForObject( &testClazz ); notifier.notify( getNotificationTest1() ); EXPECT_EQ( 8, testClazz.deliveredCount ); }
TestClazzReleased( Notifier& notifier ) : m_pNotifier( notifier ) { notifier.addNotification( getNotificationTestUseAfterRelease(), Utils::makeCallback( this, &TestClazzReleased::onCall ) ); }