[pitivi] shortcuts: add reset_accels() functionality



commit 3de3d88b0933fb4f3a93a2496687ff131407480b
Author: Jakub Brindza <jakub brindza gmail com>
Date:   Wed Jul 20 19:01:41 2016 +0100

    shortcuts: add reset_accels() functionality
    
    Differential Revision: https://phabricator.freedesktop.org/D1180

 pitivi/shortcuts.py     |   21 +++++++++++++++++++++
 tests/test_shortcuts.py |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 0 deletions(-)
---
diff --git a/pitivi/shortcuts.py b/pitivi/shortcuts.py
index 456a2b7..4007052 100644
--- a/pitivi/shortcuts.py
+++ b/pitivi/shortcuts.py
@@ -33,6 +33,7 @@ class ShortcutsManager:
         self.groups = []
         self.group_titles = {}
         self.group_actions = {}
+        self.default_accelerators = {}
         self.config_path = os.path.sep.join([xdg_config_home(),
                                              "shortcuts.conf"])
         self.__loaded = self.__load()
@@ -77,6 +78,7 @@ class ShortcutsManager:
             group (Optional[str]): The group id registered with `register_group`
                 to be used instead of that extracted from `action`.
         """
+        self.default_accelerators[action] = accelerators
         if not self.__loaded:
             self.app.set_accels_for_action(action, accelerators)
 
@@ -97,6 +99,25 @@ class ShortcutsManager:
             self.groups.append(action_prefix)
         self.group_titles[action_prefix] = title
 
+    def reset_accels(self, action=None):
+        """Resets accelerators to their default values.
+
+        Args:
+            action (Optional(str)): The action name.
+                If specified, reset the specified action's accelerators.
+                Otherwise reset accelerators for all actions.
+        """
+        if action:
+            self.app.set_accels_for_action(action, self.default_accelerators[action])
+            self.save()
+        else:
+            for action, accelerators in self.default_accelerators.items():
+                self.app.set_accels_for_action(action, accelerators)
+            try:
+                os.remove(self.config_path)
+            except FileNotFoundError:
+                pass
+
 
 class ShortcutsWindow(Gtk.ShortcutsWindow):
     """Dialog for displaying the accelerators."""
diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py
index 64fc154..254c6ca 100644
--- a/tests/test_shortcuts.py
+++ b/tests/test_shortcuts.py
@@ -112,3 +112,36 @@ class TestShortcutsManager(TestCase):
                      mock.call("group.action2", ["<Shift>p", "<Control>m"]),
                      mock.call("group.action3", ["<Control><Shift>a", "a"])]
             app.set_accels_for_action.assert_has_calls(calls, any_order=True)
+
+    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,\
+                    mock.patch("os.remove") as os_remove_mock:
+            xdg_config_home.return_value = temp_dir
+            manager = ShortcutsManager(app)
+
+            # Set default shortcuts - they will be stored in self.defaults_accelerators.
+            manager.register_group("group", "Test group")
+            manager.add("group.action1", ["<Control>i"], "Action 1")
+            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)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]