def testAddObserverCount(self): nc = NotificationCenter() self.assertEqual(nc.observersCount(), 0) nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test) self.assertEqual(nc.observersCount(), 1)
def testRemoveIncorrectObject(self): nc = NotificationCenter() someObject = NotificationCenter() nc.addObserver(self, self.handle, TestNotificationName.test, someObject) nc.removeObserver(someObject) self.assertEqual(nc.observersCount(), 1)
def testNotificationReceived(self): dm = DeviceManager() nc = NotificationCenter() nc.addObserver(self, self.handle, DeviceManagerNotification.willStartMonitoring) nc.addObserver(self, self.handle, DeviceManagerNotification.didStartMonitoring) nc.addObserver(self, self.handle, DeviceManagerNotification.willStopMonitoring) nc.addObserver(self, self.handle, DeviceManagerNotification.didStopMonitoring) nc.addObserver(self, self.handleStatus, DeviceManagerNotification.status) self.notificationsToReceive = [ DeviceManagerNotification.willStartMonitoring, DeviceManagerNotification.didStartMonitoring, DeviceManagerNotification.willStopMonitoring, DeviceManagerNotification.didStopMonitoring ] dm.startMonitoring() time.sleep(0.5) dm.stopMonitoring() self.assertTrue(len(self.notificationsToReceive) == 0) nc.removeObserver(self) self.assertEqual(nc.observersCount(), 0)
def testRemoveManyObservers2(self): nc = NotificationCenter() someObject = NotificationCenter() nc.addObserver(self, self.handle, TestNotificationName.test, someObject) nc.addObserver(self, self.handle, TestNotificationName.test2, someObject) nc.addObserver(self, self.handle, TestNotificationName.test3, someObject) nc.addObserver(self, self.handle, TestNotificationName.test4, None) nc.removeObserver(self, observedObject=someObject) self.assertEqual(nc.observersCount(), 0)
def testAddObserverWrongSender(self): someObject = NotificationCenter() nc = NotificationCenter() nc.addObserver(self, method=self.handle, notificationName=TestNotificationName.test, observedObject=someObject) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self, userInfo="1234") self.assertFalse(self.notificationReceived) self.assertNotEqual(self.postedUserInfo, "1234") nc.removeObserver(self) self.assertEqual(nc.observersCount(), 0)
def testAddObserverNoDuplicates3(self): nc = NotificationCenter() nc.addObserver(self, self.handle, TestNotificationName.test, None) nc.addObserver(self, self.handle, TestNotificationName.test, nc) self.assertEqual(nc.observersCount(), 1)
def testRemoveMissingObserver(self): nc = NotificationCenter() self.assertEqual(nc.observersCount(), 0) nc.removeObserver(self) self.assertEqual(nc.observersCount(), 0)