Example #1
0
 def test_image_transform_when_not_active(self):
     transformer = SimpleImageTransformer(lambda _: BLACK_IMAGE,
                                          active=False)
     self.display_controller.image_transformers.add(transformer)
     self.assertEqual(
         WHITE_IMAGE,
         self.display_controller.apply_image_transforms(WHITE_IMAGE))
Example #2
0
def create_dummy_display_controller(*, number_of_images: int = 0, number_of_image_transformers: int = 0, **kwargs) \
        -> DisplayController:
    """
    Creates a dummy display controller.
    :param number_of_images: number of images in the display controller's image store
    :param number_of_image_transformers: number of image transformers to create
    :param kwargs: key-word arguments to pass to `DisplayController`
    :return: the dummy display controller
    """
    if number_of_images != 0 and "image_store" in kwargs:
        raise ValueError(
            "Cannot specify number of images to create in an image store and pass in image store"
        )
    if "image_store" not in kwargs:
        kwargs["image_store"] = InMemoryImageStore(
            create_image() for _ in range(number_of_images))

    if number_of_image_transformers != 0:
        if "image_transformers" in kwargs:
            raise ValueError(
                "Cannot specify a number of image transformers to be created in addition to passing in "
                "image transformers")
        kwargs["image_transformers"] = [
            SimpleImageTransformer()
            for _ in range(number_of_image_transformers)
        ]

    return BaseDisplayController(driver=DummyBaseDisplayDriver(), **kwargs)
Example #3
0
 def setUp(self):
     self.image_transformers = []
     for i in range(3):
         image_transformer = SimpleImageTransformer(
             configuration={"test": i}, description=f"example-{i}")
         self.image_transformers.append(image_transformer)
     self.create_display_controller(
         image_transformers=self.image_transformers)
Example #4
0
    def test_remove_event(self):
        semaphore = Semaphore(0)
        callback_image_transformer = None
        callback_removed = None

        def callback(image_transformer: ImageTransformer, removed: bool):
            nonlocal semaphore, callback_image_transformer, callback_removed
            callback_image_transformer = image_transformer
            callback_removed = removed
            semaphore.release()

        self.image_transformers.event_listeners.add_listener(callback, SimpleImageTransformerSequence.Event.REMOVE)
        image_transformer = SimpleImageTransformer()
        self.image_transformers.remove(image_transformer)
        self.assertTrue(semaphore.acquire(timeout=15))
        self.assertEqual(image_transformer, callback_image_transformer)
        self.assertFalse(callback_removed)
Example #5
0
    def test_add_event(self):
        semaphore = Semaphore(0)
        callback_image_transformer = None
        callback_position = None

        def callback(image_transformer: ImageTransformer, position: int):
            nonlocal semaphore, callback_image_transformer, callback_position
            callback_image_transformer = image_transformer
            callback_position = position
            semaphore.release()

        self.image_transformers.event_listeners.add_listener(callback, SimpleImageTransformerSequence.Event.ADD)
        image_transformer = SimpleImageTransformer()
        self.image_transformers.add(image_transformer)
        self.assertTrue(semaphore.acquire(timeout=15))
        self.assertEqual(image_transformer, callback_image_transformer)
        self.assertEqual(len(self.image_transformers) - 1, callback_position)
Example #6
0
    def test_image_transform_multi_transformers(self):
        call_order = []

        def transform(transformer_id: int, image: Image) -> Image:
            call_order.append(transformer_id)
            return image

        transformers = []
        for i in range(16):
            transformer = SimpleImageTransformer(partial(transform, i),
                                                 active=i % 4 != 0)
            transformers.append(transformer)
            self.display_controller.image_transformers.add(transformer)
        self.display_controller.apply_image_transforms(WHITE_IMAGE)
        self.assertEqual([
            i
            for i, transformer in enumerate(transformers) if transformer.active
        ], call_order)
Example #7
0
    def test_display_applies_transforms(self):
        display_semaphore = Semaphore(0)
        displayed_image = None

        def on_display(image: Image):
            nonlocal displayed_image, display_semaphore
            displayed_image = image
            display_semaphore.release()

        transformer = SimpleImageTransformer(lambda _: BLACK_IMAGE)
        self.display_controller.driver.event_listeners.add_listener(
            on_display, ListenableDisplayDriver.Event.DISPLAY)
        self.display_controller.image_transformers.add(transformer)

        self.display_controller.image_store.add(WHITE_IMAGE)
        self.display_controller.display(WHITE_IMAGE.identifier)
        self.assertTrue(display_semaphore.acquire(timeout=15))

        self.assertEqual(WHITE_IMAGE, self.display_controller.current_image)
        self.assertEqual(BLACK_IMAGE, displayed_image)
Example #8
0
 def test_image_transform(self):
     transformer = SimpleImageTransformer(lambda _: BLACK_IMAGE)
     self.display_controller.image_transformers.add(transformer)
     self.assertEqual(
         BLACK_IMAGE,
         self.display_controller.apply_image_transforms(WHITE_IMAGE))
Example #9
0
 def test_get_position_when_does_not_exist(self):
     with self.assertRaises(KeyError):
         self.image_transformers.get_position(SimpleImageTransformer())
Example #10
0
 def test_contains(self):
     self.assertIn(self.image_transformers_list[0], self.image_transformers)
     self.assertNotIn(SimpleImageTransformer(), self.image_transformers)
Example #11
0
 def test_add(self):
     image_transformer = SimpleImageTransformer()
     self.image_transformers.add(image_transformer)
     self.assertEqual(image_transformer, self.image_transformers[-1])
Example #12
0
 def setUp(self):
     self.image_transformer = ListenableImageTransformer(SimpleImageTransformer())
Example #13
0
 def test_remove_when_does_not_exist(self):
     self.assertFalse(self.image_transformers.remove(SimpleImageTransformer()))
Example #14
0
 def test_add_with_duplicate_id(self):
     self.image_transformers.add(SimpleImageTransformer(identifier="test"))
     with self.assertRaises(ValueError):
         self.image_transformers.add(SimpleImageTransformer(identifier="test"))
Example #15
0
 def test_add_with_position(self):
     image_transformer = SimpleImageTransformer()
     self.image_transformers.add(image_transformer, 1)
     self.assertEqual(image_transformer, self.image_transformers[1])
Example #16
0
 def setUp(self):
     super().setUp()
     self.image_transformers_list = [SimpleImageTransformer(), SimpleImageTransformer(), SimpleImageTransformer()]
     self.image_transformers: ImageTransformerSequenceType = self.create_image_transformer_sequence(
         self.image_transformers_list)
Example #17
0
 def test_set_position_when_image_transformer_not_in_sequence(self):
     with self.assertRaises(KeyError):
          self.image_transformers.set_position(SimpleImageTransformer(), 0)