Example #1
0
 def test_example_other_scrollbar() -> None:
     """
     Test scrollbar example.
     """
     pygame.event.post(PygameEventUtils.keydown(pygame.K_v, inlist=False))
     pygame.event.post(PygameEventUtils.keydown(pygame.K_h, inlist=False))
     scrollbar.main(test=True)
     scrollbar.h_changed(1)
     scrollbar.v_changed(1)
Example #2
0
 def test_example_other_calculator(self) -> None:
     """
     Test calculator example.
     """
     app = calculator.main(test=True)
     app.process_events(
         PygameEventUtils.keydown([
             pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5,
             pygame.K_PLUS
         ]))
     self.assertEqual(app.prev, '12345')
     self.assertEqual(app.op, '+')
     app.process_events(
         PygameEventUtils.keydown(
             [pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9, pygame.K_0]))
     self.assertEqual(app.curr, '67890')
     app.process_events(PygameEventUtils.keydown(pygame.K_EQUALS))
     self.assertEqual(app.op, '')
     self.assertEqual(app.curr, '80235')
     self.assertEqual(app.prev, '')
     app.process_events(PygameEventUtils.keydown([pygame.K_x, pygame.K_2]))
     self.assertEqual(app.op, 'x')
     self.assertEqual(app.curr, '2')
     self.assertEqual(app.prev, '80235')
     app.process_events(PygameEventUtils.keydown([pygame.K_x]))
     self.assertEqual(app.op, 'x')
     self.assertEqual(app.curr, '')
     self.assertEqual(app.prev, '160470')
     app.process_events(
         PygameEventUtils.keydown([pygame.K_PLUS, pygame.K_3, pygame.K_0]))
     self.assertEqual(app.op, '+')
     self.assertEqual(app.curr, '30')
     self.assertEqual(app.prev, '160470')
     app.process_events(PygameEventUtils.keydown(pygame.K_EQUALS))
     self.assertEqual(app.op, '')
     self.assertEqual(app.curr, '160500')
     self.assertEqual(app.prev, '')
     app.process_events(
         PygameEventUtils.keydown(
             [pygame.K_SLASH, pygame.K_5, pygame.K_MINUS]))
     self.assertEqual(app.op, '-')
     self.assertEqual(app.curr, '')
     self.assertEqual(app.prev, '32100')
     app.process_events(
         PygameEventUtils.keydown([
             pygame.K_3, pygame.K_2, pygame.K_1, pygame.K_0, pygame.K_EQUALS
         ]))
     self.assertEqual(app.op, '')
     self.assertEqual(app.curr, '28890')
     self.assertEqual(app.prev, '')
     app.process_events(
         PygameEventUtils.keydown([pygame.K_9, pygame.K_BACKSPACE]))
     self.assertEqual(app.op, '')
     self.assertEqual(app.curr, '')
     self.assertEqual(app.prev, '')
     test_reset_surface()
Example #3
0
 def test_example_other_ui_solar_system(self) -> None:
     """
     Test solar system.
     """
     app = ui_solarsystem.main(test=True)
     self.assertFalse(app.menu._disable_draw)
     app.process_events(PygameEventUtils.keydown([pygame.K_p]), app.menu)
     self.assertTrue(app.menu._disable_draw)
     app.process_events(
         PygameEventUtils.keydown(
             [pygame.K_p, pygame.K_q, pygame.K_e, pygame.K_s, pygame.K_c]),
         app.menu)
     self.assertFalse(app.menu._disable_draw)
Example #4
0
 def test_example_other_scrollbar_area() -> None:
     """
     Test scrollbar area example.
     """
     pygame.event.post(
         PygameEventUtils.keydown(pygame.K_ESCAPE, inlist=False))
     scrollbar_area.main(test=True)
Example #5
0
 def test_example_game_selector(self) -> None:
     """
     Test game selector example.
     """
     game_selector.main(test=True)
     font = MenuUtils.load_font(MenuUtils.random_font(), 5)
     game_selector.play_function(['EASY'], font, test=True)
     pygame.event.post(
         PygameEventUtils.keydown(pygame.K_ESCAPE, inlist=False))
     game_selector.play_function(['MEDIUM'], font, test=True)
     pygame.event.post(
         PygameEventUtils.keydown(pygame.K_ESCAPE, inlist=False))
     game_selector.play_function(['HARD'], font, test=True)
     self.assertRaises(
         ValueError,
         lambda: game_selector.play_function(['U'], font, test=True))
     game_selector.change_difficulty(('HARD', 1), 'HARD')
Example #6
0
 def test_example_timer_clock() -> None:
     """
     Test timer clock example.
     """
     pygame.event.post(
         PygameEventUtils.keydown(pygame.K_ESCAPE, inlist=False))
     timer_clock.main(test=True)
     timer_clock.mainmenu_background()
     timer_clock.reset_timer()
     timer_clock.TestCallClassMethod.update_game_settings()
     color = (-1, -1, -1)
     timer_clock.change_color_bg((
         color,
         'random',
     ),
                                 color,
                                 write_on_console=True)
Example #7
0
    def test_example_other_calculator(self) -> None:
        """
        Test calculator example.
        """
        app = calculator.main(test=True)

        # Process events
        app.process_events(
            PygameEventUtils.keydown([
                pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5,
                pygame.K_PLUS
            ]))
        self.assertEqual(app.prev, '12345')
        self.assertEqual(app.op, '+')
        app.process_events(
            PygameEventUtils.keydown(
                [pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9, pygame.K_0]))
        self.assertEqual(app.curr, '67890')
        app.process_events(PygameEventUtils.keydown(pygame.K_EQUALS))
        self.assertEqual(app.op, '')
        self.assertEqual(app.curr, '80235')
        self.assertEqual(app.prev, '')
        app.process_events(PygameEventUtils.keydown([pygame.K_x, pygame.K_2]))
        self.assertEqual(app.op, 'x')
        self.assertEqual(app.curr, '2')
        self.assertEqual(app.prev, '80235')
        app.process_events(PygameEventUtils.keydown([pygame.K_x]))
        self.assertEqual(app.op, 'x')
        self.assertEqual(app.curr, '')
        self.assertEqual(app.prev, '160470')
        app.process_events(
            PygameEventUtils.keydown([pygame.K_PLUS, pygame.K_3, pygame.K_0]))
        self.assertEqual(app.op, '+')
        self.assertEqual(app.curr, '30')
        self.assertEqual(app.prev, '160470')
        app.process_events(PygameEventUtils.keydown(pygame.K_EQUALS))
        self.assertEqual(app.op, '')
        self.assertEqual(app.curr, '160500')
        self.assertEqual(app.prev, '')
        app.process_events(
            PygameEventUtils.keydown(
                [pygame.K_SLASH, pygame.K_5, pygame.K_MINUS]))
        self.assertEqual(app.op, '-')
        self.assertEqual(app.curr, '')
        self.assertEqual(app.prev, '32100')
        app.process_events(
            PygameEventUtils.keydown([
                pygame.K_3, pygame.K_2, pygame.K_1, pygame.K_0, pygame.K_EQUALS
            ]))
        self.assertEqual(app.op, '')
        self.assertEqual(app.curr, '28890')
        self.assertEqual(app.prev, '')
        app.process_events(
            PygameEventUtils.keydown([pygame.K_9, pygame.K_BACKSPACE]))
        self.assertEqual(app.op, '')
        self.assertEqual(app.curr, '')
        self.assertEqual(app.prev, '')

        # Test methods
        self.assertRaises(ValueError, lambda: app._format('n'))
        self.assertEqual(app._format('1.2'), '1')
        self.assertEqual(app._format('2.0'), '2')

        # Test selection
        app.menu._test_print_widgets()
        b1 = app.menu.get_widgets()[4]
        b1d = b1.get_decorator()
        lay = b1.get_attribute('on_layer')
        self.assertFalse(b1d.is_enabled(lay))
        b1.select()
        self.assertTrue(b1d.is_enabled(lay))
        b1.select(False)
        self.assertFalse(b1d.is_enabled(lay))
Example #8
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())