test_shortcuts.py 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# -*- coding: utf-8 -*-
# Pitivi video editor
# Copyright (c) 2016, Jakub Brindza <jakub.brindza@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Test the keyboard shortcuts."""
20
import tempfile
21 22 23
from unittest import mock

from pitivi.shortcuts import ShortcutsManager
24
from tests import common
25 26


27
class TestShortcutsManager(common.TestCase):
28 29 30 31 32 33 34 35
    """Tests for the ShortcutsManager."""

    def test_groups(self):
        """Checks the group in which the shortcut ends up."""
        app = mock.MagicMock()
        manager = ShortcutsManager(app)

        # Test register_group method
36
        manager.register_group("alpha_group", "One", position=20)
37
        with self.assertRaises(AssertionError):
38 39 40
            manager.register_group("alpha_group", "", position=0)
        manager.register_group("beta_group", "Two", position=10)
        self.assertEqual(manager.groups, ["beta_group", "alpha_group"])
41 42

        # Test grouping using the stripping away group name from action name
43
        manager.add("alpha_group.first", ["<Primary>A"], "First action")
44 45
        self.assertIn(("alpha_group.first", "First action"),
                      manager.group_actions["alpha_group"])
46
        manager.add("alpha_group.second", ["<Primary>B"], "Second action")
47 48
        self.assertIn(("alpha_group.second", "Second action"),
                      manager.group_actions["alpha_group"])
49
        manager.add("beta_group.first", ["<Primary>C"], "First beta action")
50 51 52 53 54 55
        self.assertIn(("beta_group.first", "First beta action"),
                      manager.group_actions["beta_group"])

        # Test grouping using the group optional argument
        # if group parameter is set, the action prefix can be anything,
        # it should be disregarded in favour of the group value.
56
        manager.add("anything.third_action", ["<Primary>D"], "Third action",
57 58 59
                    group="beta_group")
        self.assertIn(("anything.third_action", "Third action"),
                      manager.group_actions["beta_group"])
60 61 62 63 64 65 66 67 68 69

    def test_add_shortcut(self):
        """Checks the number of calls to set_accels_for_action."""
        app = mock.MagicMock()
        with mock.patch("pitivi.shortcuts.xdg_config_home") as xdg_config_home,\
                tempfile.TemporaryDirectory() as temp_dir:
            xdg_config_home.return_value = temp_dir
            manager = ShortcutsManager(app)
            # Test the add is calling set_accels_for_action(),
            # since there is no shortcuts.conf in the directory.
70
            manager.register_group("general", "General group", position=0)
71
            manager.add("prefix.action1", ["<Primary>P"], "Action one")
72
            self.assertEqual(app.set_accels_for_action.call_count, 1)
73 74 75
            # Save the shortcut to the config file.
            manager.save()
            app.set_accels_for_action.reset_mock()
76 77

            manager2 = ShortcutsManager(app)
78 79 80
            # Previously saved shortcut is read from the config file
            # and 'set_accels_for_action' is called.
            self.assertEqual(app.set_accels_for_action.call_count, 1)
81
            manager2.register_group("other", "Other group", position=0)
82 83 84 85 86 87
            app.set_accels_for_action.reset_mock()
            manager2.add("prefix.action1", ["<Primary>P"], "Action one")
            # Above shortcut has been already loaded from the config file
            # and hence 'set_accels_for_action' is not called.
            self.assertEqual(app.set_accels_for_action.call_count, 0)
            manager2.add("prefix.action2", ["<Primary>W"], "Action two")
88 89 90 91 92 93 94 95 96 97 98 99 100
            self.assertEqual(app.set_accels_for_action.call_count, 1)

    def test_load_save(self):
        """Checks saved shortcuts are loaded by a new instance."""
        app = mock.MagicMock()
        with mock.patch("pitivi.shortcuts.xdg_config_home") as xdg_config_home,\
                tempfile.TemporaryDirectory() as temp_dir:
            xdg_config_home.return_value = temp_dir
            manager = ShortcutsManager(app)
            # No file exists so set_accels_for_action() is not called.
            self.assertEqual(app.set_accels_for_action.call_count, 0)

            # Set default shortcuts
101
            manager.register_group("group", "Test group", position=0)
102 103 104
            manager.add("group.action1", ["<Primary>i"], "Action 1")
            manager.add("group.action2", ["<Shift>p", "<Primary>m"], "Action 2")
            manager.add("group.action3", ["<Primary><Shift>a", "a"], "Action 3")
105 106 107

            # After saving the shortcuts, the accels should be set when
            # initializing a ShortcutsManger.
108 109 110
            app.get_accels_for_action.side_effect = [(["<Primary>i"]),
                                                     (["<Shift>p", "<Primary>m"]),
                                                     (["<Primary><Shift>a", "a"])]
111 112 113 114
            manager.save()
            app.reset_mock()
            unused_manager2 = ShortcutsManager(app)
            self.assertEqual(app.set_accels_for_action.call_count, 3)
115 116 117
            calls = [mock.call("group.action1", ["<Primary>i"]),
                     mock.call("group.action2", ["<Shift>p", "<Primary>m"]),
                     mock.call("group.action3", ["<Primary><Shift>a", "a"])]
118
            app.set_accels_for_action.assert_has_calls(calls, any_order=True)
119 120 121 122 123 124

    def test_reset_accels(self):
        """Checks if accelerators have been reset to the default settings."""
        app = mock.MagicMock()
        with mock.patch("pitivi.shortcuts.xdg_config_home") as xdg_config_home,\
                tempfile.TemporaryDirectory() as temp_dir,\
125
                mock.patch("os.remove") as os_remove_mock:
126 127 128 129
            xdg_config_home.return_value = temp_dir
            manager = ShortcutsManager(app)

            # Set default shortcuts - they will be stored in self.defaults_accelerators.
130
            manager.register_group("group", "Test group", position=0)
131
            manager.add("group.action1", ["<Primary>i"], "Action 1")
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            manager.add("group.action2", ["<Shift>p"], "Action 2")

            # Test reset of a single action. The shortcuts are saved and no file removed.
            # Only one call to set_accels_for_action() should be made.
            app.reset_mock()
            manager.save = mock.MagicMock()
            manager.reset_accels(action="group.action1")
            self.assertEqual(manager.save.call_count, 1)
            self.assertEqual(os_remove_mock.call_count, 0)
            self.assertEqual(app.set_accels_for_action.call_count, 1)

            # Test reset of all actions. Nothing is saved and the file is removed.
            # Both actions should have accelerators set.
            app.reset_mock()
            os_remove_mock.reset_mock()
            manager.save.reset_mock()
            manager.reset_accels()
            self.assertEqual(manager.save.call_count, 0)
            self.assertEqual(os_remove_mock.call_count, 1)
            self.assertEqual(app.set_accels_for_action.call_count, 2)