Пример #1
0
    def test_enter_on_last_returns_right(self):
        num_elements = 3
        contents = [["A" + str(i), "a" + str(i)] for i in range(num_elements)]
        mu = GridMenu(contents, get_mock_input(), get_mock_graphical_output(), name=mu_name, config={})
        mu.refresh = lambda *args, **kwargs: None

        # Checking at other elements - shouldn't return
        def scenario():
            mu.select_entry()  # KEY_ENTER
            assert mu.is_active  # Should still be active
            mu.deactivate()  # because is not deactivated yet and would idle loop otherwise

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            return_value = mu.activate()
        assert return_value is None

        # Scrolling to the end of the list and pressing Enter - should return a correct dict
        def scenario():
            for i in range(num_elements):
                mu.move_down()  # KEY_DOWN x3
            mu.select_entry()  # KEY_ENTER
            assert not mu.is_active

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            return_value = mu.activate()
        assert isinstance(return_value, dict)
        assert all([isinstance(key, basestring) for key in return_value.keys()])
        assert all([isinstance(value, bool) for value in return_value.values()])
Пример #2
0
    def test_left_key_returns_none(self):
        """ A GridMenu is never supposed to return anything other than None"""
        num_elements = 3
        contents = [["A" + str(i), "a" + str(i)] for i in range(num_elements)]
        mu = GridMenu(contents, get_mock_input(), get_mock_graphical_output(), name=mu_name, config={})
        mu.refresh = lambda *args, **kwargs: None

        # Checking at the start of the list
        def scenario():
            mu.deactivate()  # KEY_LEFT
            assert not mu.is_active

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            return_value = mu.activate()
        assert return_value is None

        # Checking at the end of the list
        def scenario():
            for i in range(num_elements):
                mu.move_down()  # KEY_DOWN x3
            mu.deactivate()  # KEY_LEFT
            assert not mu.is_active

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            return_value = mu.activate()
        assert return_value is None
Пример #3
0
 def create_main_menu(self, menu_name, contents):
     dir = "resources/"
     icons = [f for f in os.listdir(dir) if f.endswith(".png")]
     icon_paths = [[f.rsplit('.', 1)[0],
                    os.path.join(dir, f)] for f in icons]
     used_icons = []
     for entry in contents:
         for icon_name, icon_path in icon_paths:
             if entry.basename.startswith(icon_name):
                 entry.icon = Image.open(icon_path)
                 used_icons.append(icon_name)
                 continue
         else:
             pass
     print([x for x, y, in icon_paths if x not in used_icons])
     font = ("Fixedsys62.ttf", 16)
     menu = GridMenu(contents,
                     self.i,
                     self.o,
                     font=font,
                     entry_width=32,
                     name="Main menu",
                     draw_lines=False,
                     exitable=False)
     menu.exit_entry = ["Exit", "exit"]
     menu.process_contents()
     return menu
Пример #4
0
    def test_gmlo_graphical_icon_disappears(self):
        o = get_mock_graphical_output()
        overlay = GridMenuLabelOverlay()
        mu = GridMenu([], get_mock_input(), o, name=ui_name, config={})
        mu.idle_loop = lambda *a, **k: True
        Canvas.fonts_dir = fonts_dir
        overlay.apply_to(mu)
        def activate():
            mu.before_activate()
            mu.to_foreground()
            mu.idle_loop()
            img_1 = o.display_image.call_args[0]
            mu.idle_loop()
            mu.refresh()
            img_2 = o.display_image.call_args[0]
            assert(img_1 == img_2)
            for i in range(overlay.duration):
                mu.idle_loop()
            # An internal refresh has happened
            img_2 = o.display_image.call_args[0]
            assert(img_1 != img_2)
            mu.deactivate()  # KEY_LEFT

        with patch.object(mu, 'activate', side_effect=activate) as p:
            mu.activate()
Пример #5
0
    def on_start(self):
        grid_contents = [[str(i), lambda x=i: test_func(x)] for i in range(16)]

        self.gm = GridMenu(grid_contents, self.i, self.o)
        self.overlay = GridMenuNavOverlay()
        self.overlay.apply_to(self.gm)
        self.gm.activate()
Пример #6
0
class MainMenu(ZeroApp):

	menu_name = "Main Menu"

	def on_start(self):
		grid_contents = [[str(i), lambda x=i: test_func(x)] for i in range(16)]

		self.gm = GridMenu(grid_contents, self.i, self.o)
		self.gm.activate()
Пример #7
0
 def test_exit_label_leakage(self):
     """tests whether the exit label of one GridMenu leaks into another"""
     i = get_mock_input()
     o = get_mock_graphical_output()
     c1 = GridMenu([["a", "1"]], i, o, name=mu_name + "1", final_button_name="Name1", config={})
     c2 = GridMenu([["b", "2"]], i, o, name=mu_name + "2", final_button_name="Name2", config={})
     c3 = GridMenu([["c", "3"]], i, o, name=mu_name + "3", config={})
     assert (c1.exit_entry != c2.exit_entry)
     assert (c2.exit_entry != c3.exit_entry)
     assert (c1.exit_entry != c3.exit_entry)
Пример #8
0
	def on_start(self):
		dir = "resources/"
                icons = [f for f in os.listdir(dir) if f.endswith(".png")]
                icon_paths = [[f, os.path.join(dir, f)] for f in icons]
		grid_contents = [Entry(f.rsplit('.', 1)[0].capitalize(), icon=Image.open(p), cb=lambda x=f: test_func(x)) \
                                   for f,p in icon_paths]

		self.gm = GridMenu(grid_contents, self.i, self.o, entry_width=32, draw_lines=False)
                self.overlay1 = GridMenuLabelOverlay()
                self.overlay1.apply_to(self.gm)
                self.overlay2 = GridMenuSidebarOverlay(self.sidebar_cb)
                self.overlay2.apply_to(self.gm)
		self.gm.activate()
Пример #9
0
    def test_left_key_disabled_when_not_exitable(self):
        """Tests whether a menu does not exit on KEY_LEFT when exitable is set to False"""
        num_elements = 3
        contents = [["A" + str(i), "a" + str(i)] for i in range(num_elements)]
        mu = GridMenu(contents, get_mock_input(), get_mock_graphical_output(), name=mu_name, exitable=False, config={})
        mu.refresh = lambda *args, **kwargs: None

        def scenario():
            assert "KEY_LEFT" not in mu.keymap
            mu.deactivate()
            assert not mu.is_active

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            mu.activate()
Пример #10
0
    def test_graphical_display_redraw(self):
        num_elements = 1
        o = get_mock_graphical_output()
        contents = [["A" + str(i), "a" + str(i)] for i in range(num_elements)]
        mu = GridMenu(contents, get_mock_input(), o, name=mu_name, config={})
        Canvas.fonts_dir = fonts_dir
        # Exiting immediately, but we should get at least one redraw
        def scenario():
            mu.deactivate()  # KEY_LEFT
            assert not mu.is_active

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            return_value = mu.activate()
        assert o.display_image.called
        assert o.display_image.call_count == 1 #One in to_foreground
Пример #11
0
    def test_graphical_redraw_with_eh_2(self):
        """
        Tests for a bug where a GridMenu with one two-elements-high entry would fail to render
        """
        num_elements = 3
        o = get_mock_graphical_output()
        contents = [[["A" + str(i), "B"+str(i)], "a" + str(i)] for i in range(num_elements)]
        mu = GridMenu(contents, get_mock_input(), o, name=mu_name, entry_height=2, append_exit=False, config={})
        Canvas.fonts_dir = fonts_dir
        # Exiting immediately, but we should get at least one redraw
        def scenario():
            mu.deactivate()  # KEY_LEFT
            assert not mu.is_active

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            return_value = mu.activate()
        assert o.display_image.called
        assert o.display_image.call_count == 1 #One in to_foreground
Пример #12
0
    def test_shows_data_on_screen(self):
        """Tests whether the GridMenu outputs data on screen when it's ran"""
        num_elements = 3
        contents = [["A" + str(i), "a" + str(i)] for i in range(num_elements)]
        i = get_mock_input()
        o = get_mock_graphical_output()
        mu = GridMenu(contents, i, o, name=mu_name, config={})

        def scenario():
            mu.deactivate()

        with patch.object(mu, 'idle_loop', side_effect=scenario) as p:
            mu.activate()
            #The scenario should only be called once
            assert mu.idle_loop.called
            assert mu.idle_loop.call_count == 1

        assert o.display_data.called
        assert o.display_data.call_count == 1 #One in to_foreground
        assert o.display_data.call_args[0] == ('A0', 'A1', 'A2', 'Back')
Пример #13
0
class MainMenu(ZeroApp):

	menu_name = "Main Menu"

        def sidebar_cb(self, c, ui_el, coords):
            width = coords.right-coords.left
            height = coords.bottom-coords.top
            cw = coords.left+width/2
            ch= coords.top+height/2
            c.centered_text("Hello", cw=cw, ch=ch)

	def on_start(self):
		dir = "resources/"
                icons = [f for f in os.listdir(dir) if f.endswith(".png")]
                icon_paths = [[f, os.path.join(dir, f)] for f in icons]
		grid_contents = [Entry(f.rsplit('.', 1)[0].capitalize(), icon=Image.open(p), cb=lambda x=f: test_func(x)) \
                                   for f,p in icon_paths]

		self.gm = GridMenu(grid_contents, self.i, self.o, entry_width=32, draw_lines=False)
                self.overlay1 = GridMenuLabelOverlay()
                self.overlay1.apply_to(self.gm)
                self.overlay2 = GridMenuSidebarOverlay(self.sidebar_cb)
                self.overlay2.apply_to(self.gm)
		self.gm.activate()
Пример #14
0
	def on_start(self):
		grid_contents = [[str(i), lambda x=i: test_func(x)] for i in range(16)]

		self.gm = GridMenu(grid_contents, self.i, self.o)
		self.gm.activate()
Пример #15
0
 def test_gmlo_apply(self):
     overlay = GridMenuLabelOverlay()
     mu = GridMenu([], get_mock_input(), get_mock_output(), name=ui_name, config={})
     overlay.apply_to(mu)
     self.assertIsNotNone(overlay)
     self.assertIsNotNone(mu)
Пример #16
0
 def test_keymap(self):
     """tests keymap"""
     menu = GridMenu([["Option", "option"]], get_mock_input(), get_mock_graphical_output(), name=mu_name, config={})
     self.assertIsNotNone(menu.keymap)
     for key_name, callback in menu.keymap.iteritems():
         self.assertIsNotNone(callback)
Пример #17
0
 def test_constructor(self):
     """tests constructor"""
     menu = GridMenu([["Option", "option"]], get_mock_input(), get_mock_graphical_output(), name=mu_name, config={})
     self.assertIsNotNone(menu)