Пример #1
0
    def test_highlight(self) -> None:
        """
        Test highlight selection.
        """
        w = Button('epic')
        border_width = 1
        margin_x = 18
        margin_y = 10
        w.set_selection_effect(
            HighlightSelection(border_width=border_width,
                               margin_x=margin_x,
                               margin_y=margin_y))
        self.menu.add.generic_widget(w)
        self.menu.draw(surface)

        sel = w.get_selection_effect()
        self.assertEqual(sel.get_height(), margin_y)
        self.assertEqual(sel.get_width(), margin_x)

        # Test inflate
        rect = w.get_rect()
        inflate_rect = sel.inflate(rect)
        self.assertEqual(-inflate_rect.x + rect.x, sel.get_width() / 2)
        self.assertEqual(-inflate_rect.y + rect.y, sel.get_height() / 2)

        # Test margin xy
        sel.margin_xy(10, 20)
        self.assertEqual(sel.margin_left, 10)
        self.assertEqual(sel.margin_right, 10)
        self.assertEqual(sel.margin_top, 20)
        self.assertEqual(sel.margin_bottom, 20)
Пример #2
0
 def test_none(self):
     """
     Test highlight selection.
     """
     w = Button('epic')
     w.set_selection_effect(NoneSelection())
     self.menu.add_generic_widget(w)
     self.menu.draw(surface)
Пример #3
0
 def test_arrow(self) -> None:
     """
     Test arrow selection.
     """
     w = Button('epic')
     w.set_selection_effect(LeftArrowSelection())
     self.menu.add.generic_widget(w)
     self.menu.draw(surface)
     w.set_selection_effect(RightArrowSelection())
     self.menu.draw(surface)
Пример #4
0
    def test_none(self) -> None:
        """
        Test highlight selection.
        """
        w = Button('epic')
        w.set_selection_effect(NoneSelection())
        self.menu.add.generic_widget(w)
        self.menu.draw(surface)

        rect = w.get_rect()
        new_rect = w.get_selection_effect().inflate(rect)
        self.assertTrue(rect == new_rect)
Пример #5
0
 def test_add_generic_widget(self):
     """
     Test generic widget.
     """
     self.menu.clear()
     menu = MenuUtils.generic_menu()
     btn = menu.add_button('nice', None)
     w = Button('title')
     self.menu.add_generic_widget(w)
     self.assertRaises(ValueError, lambda: menu.add_generic_widget(w))
     btn._menu = None
     self.menu.add_generic_widget(btn)
Пример #6
0
    def test_abstracts(self) -> None:
        """
        Test abstract objects errors.
        """
        w = Button('epic')

        # Create abstract selection object
        sel = Selection(0, 0, 0, 0)
        self.assertRaises(NotImplementedError, lambda: sel.draw(surface, w))

        # Create abstract arrow selection
        arrow = ArrowSelection(0, 0, 0, 0)
        self.assertRaises(NotImplementedError, lambda: arrow.draw(surface, w))
Пример #7
0
    def test_simple(self) -> None:
        """
        Test simple selection.
        """
        w = Button('epic')
        w.set_selection_effect(SimpleSelection())
        self.menu.add.generic_widget(w)
        self.menu.draw(surface)

        rect = w.get_rect()
        new_rect = w.get_selection_effect().inflate(rect)
        self.assertTrue(rect == new_rect)
        self.assertTrue(w.get_selection_effect().widget_apply_font_color)
Пример #8
0
    def test_arrow(self) -> None:
        """
        Test arrow selection.
        """
        w = Button('epic')
        w.set_selection_effect(LeftArrowSelection())
        self.menu.add.generic_widget(w)
        self.menu.draw(surface)
        w.set_selection_effect(RightArrowSelection())
        self.menu.draw(surface)

        # Create abstract arrow selection
        arrow = ArrowSelection(0, 0, 0, 0)
        self.assertRaises(NotImplementedError, lambda: arrow.draw(surface, w))
Пример #9
0
    def test_visibility(self) -> None:
        """
        Test widget visibility.
        """
        menu = MenuUtils.generic_menu()
        w = menu.add.label('Text')
        last_hash = w._last_render_hash
        w.hide()
        self.assertFalse(w.is_visible())
        self.assertNotEqual(w._last_render_hash, last_hash)
        last_hash = w._last_render_hash
        w.show()
        self.assertTrue(w.is_visible())
        self.assertNotEqual(w._last_render_hash, last_hash)

        w = Button('title')
        menu.add.generic_widget(w)
        w.hide()
Пример #10
0
    def test_visibility(self):
        """
        Test widget visibility.
        """
        self.menu.clear()
        w = self.menu.add_label('Text')  # type: Label
        lasthash = w._last_render_hash
        w.hide()
        self.assertFalse(w.visible)
        self.assertNotEqual(w._last_render_hash, lasthash)
        lasthash = w._last_render_hash
        w.show()
        self.assertTrue(w.visible)
        self.assertNotEqual(w._last_render_hash, lasthash)

        w = Button('title')
        self.menu.add_generic_widget(w)
        w.hide()
Пример #11
0
    def test_none(self) -> None:
        """
        Test none selection.
        """
        w = Button('epic')
        w.set_selection_effect(NoneSelection())
        self.menu.add.generic_widget(w)
        self.menu.draw(surface)

        rect = w.get_rect()
        new_rect = w.get_selection_effect().inflate(rect)
        self.assertTrue(rect == new_rect)
        self.assertFalse(w.get_selection_effect().widget_apply_font_color)

        # Widgets default selection effect is None
        last_selection = w.get_selection_effect()
        w.set_selection_effect()
        self.assertIsInstance(w.get_selection_effect(), NoneSelection)
        self.assertNotEqual(w.get_selection_effect(), last_selection)
Пример #12
0
    def test_highlight(self) -> None:
        """
        Test highlight selection.
        """
        w = Button('epic')
        border_width = 1
        margin_x = 18
        margin_y = 10
        w.set_selection_effect(
            HighlightSelection(border_width=border_width,
                               margin_x=margin_x,
                               margin_y=margin_y))
        self.menu.add.generic_widget(w)
        self.menu.draw(surface)

        # noinspection PyTypeChecker
        sel: 'HighlightSelection' = w.get_selection_effect()
        self.assertEqual(sel.get_height(), margin_y)
        self.assertEqual(sel.get_width(), margin_x)

        # Test inflate
        rect = w.get_rect()
        inflate_rect = sel.inflate(rect)
        self.assertEqual(-inflate_rect.x + rect.x, sel.get_width() / 2)
        self.assertEqual(-inflate_rect.y + rect.y, sel.get_height() / 2)

        # Test margin xy
        sel.margin_xy(10, 20)
        self.assertEqual(sel.margin_left, 10)
        self.assertEqual(sel.margin_right, 10)
        self.assertEqual(sel.margin_top, 20)
        self.assertEqual(sel.margin_bottom, 20)

        # Test null border
        sel._border_width = 0
        sel.draw(surface, w)

        # Test background color
        sel.set_background_color('red')
        self.assertEqual(sel.get_background_color(), (255, 0, 0, 255))
Пример #13
0
    def test_button(self) -> None:
        """
        Test button widget.
        """
        menu = MenuUtils.generic_menu()
        menu2 = MenuUtils.generic_menu()

        # Valid
        def test() -> bool:
            """
            Callback.
            """
            return True

        # Invalid ones
        invalid = [
            bool,  # type
            object,  # object
            1,  # int
            'a',  # str
            True,  # bool
            pygame,  # module
            surface,  # pygame
            1.1,  # float
            menu.add.button('eee'),  # widget
            [1, 2, 3],  # list
            (1, 2, 3),  # tuple
            pygame_menu.BaseImage(
                pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES)  # baseimage
        ]
        for i in invalid:
            self.assertRaises(ValueError, lambda: menu.add.button('b1', i))

        # Valid
        valid = [
            menu2, test, pygame_menu.events.NONE,
            pygame_menu.events.PYGAME_QUIT,
            pygame_menu.events.PYGAME_WINDOWCLOSE, None, lambda: test(), None
        ]
        for v in valid:
            self.assertTrue(menu.add.button('b1', v) is not None)

        btn = menu.add.button('b1', menu2)
        for v in [menu, 1, bool, object, [1, 2, 3], (1, 2, 3)]:
            self.assertRaises(AssertionError, lambda: btn.update_callback(v))
        btn.update_callback(test)

        # Invalid recursive menu
        self.assertRaises(ValueError, lambda: menu.add.button('bt', menu))

        # Test callback
        test = [False]

        def callback(t=False) -> None:
            """
            Callback.
            """
            test[0] = t

        btn = Button('epic', t=True, onreturn=callback)
        btn.apply()
        self.assertTrue(test[0])
        test[0] = False

        def callback() -> None:
            """
            Callback.
            """
            test[0] = False

        btn = Button('epic', onreturn=callback)
        btn.apply()
        self.assertFalse(test[0])

        # Test with no kwargs
        def callback(**kwargs) -> None:
            """
            Callback.
            """
            self.assertEqual(len(kwargs.keys()), 0)

        btn = menu.add.button('epic', callback, accept_kwargs=False)
        btn.apply()

        # Test with kwargs
        def callback(**kwargs) -> None:
            """
            Callback.
            """
            self.assertEqual(len(kwargs.keys()), 1)
            self.assertTrue(kwargs.get('key', False))

        btn = Button('epic', onreturn=callback, key=True)
        btn.apply()
        btn = menu.add.button('epic', callback, accept_kwargs=True, key=True)
        btn.apply()

        # Test pygame events
        btn = menu.add.button('epic', pygame_menu.events.PYGAME_QUIT)
        self.assertEqual(btn._onreturn, menu._exit)
        btn = menu.add.button('epic', pygame_menu.events.PYGAME_WINDOWCLOSE)
        self.assertEqual(btn._onreturn, menu._exit)

        # Test None
        btn = menu.add.button('epic', pygame_menu.events.NONE)
        self.assertIsNone(btn._onreturn)
        btn = menu.add.button('epic')
        self.assertIsNone(btn._onreturn)

        # Test invalid kwarg
        self.assertRaises(ValueError,
                          lambda: menu.add.button('epic', callback, key=True))

        # Remove button
        menu.remove_widget(btn)
        self.assertRaises(ValueError, lambda: menu.remove_widget(btn))

        # Test underline
        # Add underline
        btn = menu.add.button('epic', pygame_menu.events.NONE)
        self.assertEqual(btn._decorator._total_decor(), 0)
        btn.add_underline((0, 0, 0), 1, 1, force_render=True)
        self.assertNotEqual(btn._last_underline[0], '')
        self.assertEqual(btn._decorator._total_decor(), 1)
        btn.remove_underline()
        self.assertEqual(btn._last_underline[0], '')

        # Test return fun
        def fun() -> str:
            """
            This should return "nice".
            """
            return 'nice'

        btn = menu.add.button('', fun)
        self.assertEqual(btn.apply(), 'nice')
        btn.readonly = True
        self.assertIsNone(btn.apply())
        self.assertFalse(
            btn.update(PygameEventUtils.keydown(
                pygame_menu.controls.KEY_APPLY)))

        # Test button to menu
        btn_menu = menu.add.button('to2', menu2)
        self.assertTrue(btn_menu.to_menu)
        menu.full_reset()
        self.assertTrue(
            btn_menu.update(
                PygameEventUtils.keydown(pygame_menu.controls.KEY_APPLY)))
        self.assertEqual(menu.get_current(), menu2)
        menu.full_reset()
        self.assertEqual(menu.get_current(), menu)

        # Warns if adding button to menu
        btn.set_menu(None)
        btn.to_menu = True
        menu2.add.generic_widget(btn)

        # Test extreme resize
        btn.resize(1, 1)
        btn.set_max_height(None)
        btn.set_max_width(None)
        btn.flip(True, True)
        self.assertEqual(btn._flip, (True, True))

        # Test consistency if active
        btn.active = True
        btn._selected = False
        btn.draw(surface)
        self.assertFalse(btn.active)

        # Try onchange
        btn._onchange = lambda: None
        self.assertIsNone(btn.change())