Exemplo n.º 1
0
def test_broadcast(faker):
    for _ in range(1000):
        i = faker.pyint(max_value=0xfffe)
        tally = Tally(i)
        assert not tally.is_broadcast
        assert not Display.from_tally(tally).is_broadcast

    tally1 = Tally(0xffff)
    tally2 = Tally.broadcast()
    assert tally1.is_broadcast
    assert tally2.is_broadcast
    assert Display.from_tally(tally1).is_broadcast
    assert Display.from_tally(tally2).is_broadcast
Exemplo n.º 2
0
    async def bind_to_tally(self, inp: BaseInput, tally: Tally):
        """Update current state and subscribe to changes from the given
        :class:`~tslumd.tallyobj.Tally`

        Calls :meth:`~BaseIO.on_receiver_tally_change` and binds tally update
        events to it
        """

        loop = asyncio.get_event_loop()
        tally_key = tally.id
        if tally_key not in self.bound_input_tally_keys:
            self.bound_input_tally_keys[tally_key] = set()
        self.bound_input_tally_keys[tally_key].add(inp.id)
        tally.bind_async(loop, on_update=self.on_receiver_tally_change)
        props_changed = ('rh_tally', 'txt_tally', 'lh_tally')
        await self.on_receiver_tally_change(tally, props_changed=props_changed)
Exemplo n.º 3
0
async def test_control_event(faker):
    loop = asyncio.get_event_loop()
    listener = Listener()

    disp = Display(index=0)
    tally = Tally.from_display(disp)

    tally.bind_async(loop, on_control=listener.callback)

    for _ in range(100):
        data_len = faker.pyint(min_value=1, max_value=1024)
        control_data = faker.binary(length=data_len)

        disp = Display(index=0, control=control_data)
        tally.update_from_display(disp)

        _, rx_data = await listener.get()
        assert rx_data == tally.control == disp.control == control_data
        assert disp == tally == Tally.from_display(disp)
Exemplo n.º 4
0
async def test_update_event(faker):
    loop = asyncio.get_event_loop()
    listener = Listener()

    tally = Tally(0)
    tally.bind_async(loop, on_update=listener.callback)
    tally.text = 'foo'

    _, props_changed = await listener.get()
    assert set(props_changed) == set(['text'])

    d = dict(rh_tally=TallyColor.RED, txt_tally=TallyColor.GREEN, lh_tally=TallyColor.AMBER)
    tally.update(**d)

    _, props_changed = await listener.get()
    assert set(props_changed) == set(d.keys())


    disp = Display(index=0, text=tally.text)
    tally.update_from_display(disp)
    assert disp == tally

    _, props_changed = await listener.get()
    assert set(props_changed) == set(['rh_tally', 'txt_tally', 'lh_tally'])


    for tally_type, color in iter_tally_types_and_colors():
        attr = tally_type.name
        should_change = getattr(tally, attr) != color
        # print(f'{tally_type=}, {color=}, {should_change=}')
        setattr(tally, attr, color)
        if should_change:
            _, props_changed = await listener.get()
            assert set(props_changed) == set([attr])
        else:
            await asyncio.sleep(.01)
            assert listener.results.empty()
Exemplo n.º 5
0
async def test_control_event_with_text(faker):
    loop = asyncio.get_event_loop()

    text_listener = Listener()
    ctrl_listener = Listener()

    tally_text = 'foo'

    disp = Display(index=0, text=tally_text)
    tally = Tally.from_display(disp)

    assert disp == tally

    tally.bind_async(loop,
        on_update=text_listener.callback,
        on_control=ctrl_listener.callback,
    )

    for _ in range(100):
        for word in faker.words(3):
            data_len = faker.pyint(min_value=1, max_value=1024)
            control_data = faker.binary(length=data_len)

            disp = Display(index=0, control=control_data)
            tally.update_from_display(disp)

            _, rx_data = await ctrl_listener.get()
            assert rx_data == tally.control == disp.control == control_data
            assert tally.text == tally_text

            _, props_changed = await text_listener.get()
            assert set(props_changed) == set(['control'])

            tally_text=word
            disp = Display(index=0, text=tally_text)
            tally.update_from_display(disp)

            _, props_changed = await text_listener.get()
            assert set(props_changed) == set(['text'])

            assert tally.text == tally_text
            assert tally.control == control_data
Exemplo n.º 6
0
def test_tally_display_conversion(faker):
    # i = 0
    # if True:
    for _ in range(100):
        i = faker.pyint(max_value=0xfffe)
        disp = Display(index=i)
        tally = Tally(i)
        for tally_type, color in iter_tally_types_and_colors():
            brightness = faker.pyint(max_value=3)
            # print(f'{i=}, {tally_type=}, {color=}')
            disp.brightness = brightness
            tally.brightness = brightness
            assert 0 <= tally.normalized_brightness <= 1
            assert tally.normalized_brightness == brightness / 3
            setattr(tally, tally_type.name, color)
            setattr(disp, tally_type.name, color)
            for word in faker.words(3):
                # print(f'{word=}')
                tally.text = word
                disp.text = word

                assert disp == Tally.from_display(disp) == tally
                assert disp == Display.from_tally(tally) == tally
                assert Tally.from_display(disp).normalized_brightness == tally.normalized_brightness
Exemplo n.º 7
0
 def add_tally(self, tally: Tally):
     insert_ix = bisect_left(self.tally_key_indices, tally.id)
     self.beginInsertRows(QtCore.QModelIndex(), insert_ix, insert_ix)
     self.tally_key_indices.insert(insert_ix, tally.id)
     self.endInsertRows()
     tally.bind(on_update=self.update_tally)
Exemplo n.º 8
0
def test_color_merge():
    t1 = Tally(0)

    t1.set_color(TallyType.rh_tally | TallyType.lh_tally, TallyColor.RED)
    assert t1.rh_tally == TallyColor.RED
    assert t1.txt_tally == TallyColor.OFF
    assert t1.lh_tally == TallyColor.RED

    t1.set_color(TallyType.rh_tally | TallyType.lh_tally, TallyColor.GREEN)
    assert t1.rh_tally == TallyColor.GREEN
    assert t1.txt_tally == TallyColor.OFF
    assert t1.lh_tally == TallyColor.GREEN

    t1.merge_color(TallyType.all_tally, TallyColor.RED)
    assert t1.rh_tally == TallyColor.AMBER
    assert t1.txt_tally == TallyColor.RED
    assert t1.lh_tally == TallyColor.AMBER

    t1.merge_color(TallyType.all_tally, TallyColor.GREEN)
    assert t1.rh_tally == TallyColor.AMBER
    assert t1.txt_tally == TallyColor.AMBER
    assert t1.lh_tally == TallyColor.AMBER

    # Reset t1 to OFF, OFF, RED
    t1.rh_tally = TallyColor.OFF
    t1.txt_tally = TallyColor.OFF
    t1.lh_tally = TallyColor.RED

    # Another Tally with only `txt_tally` set
    t2 = Tally(1, txt_tally=TallyColor.GREEN)

    # Only `txt_tally` should change
    t1.merge(t2, TallyType.all_tally)
    assert t1.rh_tally == TallyColor.OFF
    assert t1.txt_tally == TallyColor.GREEN
    assert t1.lh_tally == TallyColor.RED

    # Reset t2 to GREEN, RED, GREEN
    # t1 is still OFF, GREEN, RED
    t2.rh_tally = TallyColor.GREEN
    t2.txt_tally = TallyColor.RED
    t2.lh_tally = TallyColor.GREEN

    t1.merge(t2, TallyType.rh_tally | TallyType.lh_tally)
    assert t1.rh_tally == TallyColor.GREEN
    assert t1.txt_tally == TallyColor.GREEN
    assert t1.lh_tally == TallyColor.AMBER

    t1.merge(t2, TallyType.all_tally)
    assert t1.rh_tally == TallyColor.GREEN
    assert t1.txt_tally == TallyColor.AMBER
    assert t1.lh_tally == TallyColor.AMBER

    t2.rh_tally = TallyColor.RED
    t1.merge(t2, TallyType.all_tally)
    assert t1.rh_tally == TallyColor.AMBER
    assert t1.txt_tally == TallyColor.AMBER
    assert t1.lh_tally == TallyColor.AMBER