def testRemoveIncorrectObject(self):
     nc = NotificationCenter()
     someObject = NotificationCenter()
     nc.addObserver(self, self.handle, TestNotificationName.test,
                    someObject)
     nc.removeObserver(someObject)
     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 testAddObserverRemoveObserver(self):
     nc = NotificationCenter()
     nc.addObserver(observer=self,
                    method=self.handle,
                    notificationName=TestNotificationName.test)
     self.assertEqual(nc.observersCount(), 1)
     nc.removeObserver(observer=self)
     self.assertEqual(nc.observersCount(), 0)
    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)
예제 #5
0
 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 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)
예제 #10
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 testRemoveMissingObserver(self):
     nc = NotificationCenter()
     self.assertEqual(nc.observersCount(), 0)
     nc.removeObserver(self)
     self.assertEqual(nc.observersCount(), 0)