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 testNotificationReceivedWhileAddingDevices(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) self.addRemoveManyDevices() time.sleep(0.5) dm.stopMonitoring() self.assertTrue(len(self.notificationsToReceive) == 0) nc.removeObserver(self)
def testRemoveIncorrectObject(self): nc = NotificationCenter() someObject = NotificationCenter() nc.addObserver(self, self.handle, TestNotificationName.test, someObject) nc.removeObserver(someObject) self.assertEqual(nc.observersCount(), 1)
def testAddObserverAnySenderAndPostithObject(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self) self.assertTrue(self.notificationReceived) nc.removeObserver(self)
def testPostNotificationDidInitialize(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=PhysicalDeviceNotification. didInitializeDevice, observedObject=self.device) self.assertIsNone(self.notificationReceived) self.device.initializeDevice() self.assertIsNotNone(self.notificationReceived) nc.removeObserver(self)
def testAddObserverWrongNotification(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.wrong) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self, userInfo="1234") self.assertFalse(self.notificationReceived) self.assertNotEqual(self.postedUserInfo, "1234") nc.removeObserver(self)
def testAddObserverAnySenderAndPostWithUserInfo(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test) nc.postNotification(notificationName=TestNotificationName.test, notifyingObject=self, userInfo="1234") self.assertTrue(self.notificationReceived) self.assertEqual(self.postedUserInfo, "1234") nc.removeObserver(self)
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 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 testNotificationReceivedFromAddingDevices(self): dm = DeviceManager() nc = NotificationCenter() dm.startMonitoring() time.sleep(1.0) # let newlyConnected devices be added. nc.addObserver(self, self.handle, DeviceManagerNotification.willAddDevice) nc.addObserver(self, self.handle, DeviceManagerNotification.didAddDevice) nc.addObserver(self, self.handle, DeviceManagerNotification.willRemoveDevice) nc.addObserver(self, self.handle, DeviceManagerNotification.didRemoveDevice) N = 1000 self.notificationsToReceive = [ DeviceManagerNotification.willAddDevice, DeviceManagerNotification.didAddDevice ] * N self.notificationsToReceive.extend([ DeviceManagerNotification.willRemoveDevice, DeviceManagerNotification.didRemoveDevice ] * N) time.sleep(0.5) self.addRemoveManyDevices(N) time.sleep(0.5) with self.lock: self.assertEqual(len(self.notificationsToReceive), 0) nc.removeObserver(self) dm.stopMonitoring()
def testAddObserver(self): nc = NotificationCenter() nc.addObserver(observer=self, method=self.handle, notificationName=TestNotificationName.test)
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 showNotifications(self): nc = NotificationCenter() nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.status) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.willStartMonitoring) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.didStartMonitoring) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.willStopMonitoring) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.didStopMonitoring) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.willAddDevice) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.didAddDevice) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.willRemoveDevice) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.didRemoveDevice) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.usbDeviceDidConnect) nc.addObserver(self, self.handleNotifications, DeviceManagerNotification.usbDeviceDidDisconnect)