[pitivi] tests: Consolidate timeline selection testing



commit f961d389762dae3a670b6f3355817f35fb11ba93
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Tue Jan 25 21:24:45 2022 +0100

    tests: Consolidate timeline selection testing

 tests/common.py                 |  33 +++++++++---
 tests/test_timeline_timeline.py | 108 +++++++++++++++++++++-------------------
 tests/test_undo_timeline.py     |   7 +--
 3 files changed, 86 insertions(+), 62 deletions(-)
---
diff --git a/tests/common.py b/tests/common.py
index 5b0404e34..5ea2028ca 100644
--- a/tests/common.py
+++ b/tests/common.py
@@ -430,10 +430,15 @@ class TestCase(unittest.TestCase, Loggable):
         self.assertEqual(len(clips), num_clips)
         return clips
 
-    def toggle_clip_selection(self, ges_clip, expect_selected):
-        """Toggles the selection state of @ges_clip."""
-        selected = bool(ges_clip.ui.get_state_flags() & Gtk.StateFlags.SELECTED)
-        self.assertEqual(ges_clip.selected.selected, selected)
+    def click_clip(self, ges_clip: GES.Clip, expect_selected: bool, ctrl_key: bool = False):
+        """Clicks the specified clip."""
+        self.assert_clip_selected(ges_clip, not expect_selected)
+
+        timeline = ges_clip.ui.timeline
+        original_control_mask = timeline.get_parent().control_mask
+        if ctrl_key:
+            # Pretend the Ctrl key is being pressed.
+            timeline.get_parent().control_mask = True
 
         # Simulate a click on the clip.
         event = mock.Mock(spec=Gdk.EventButton)
@@ -442,18 +447,32 @@ class TestCase(unittest.TestCase, Loggable):
         event.get_button.return_value = (True, 1)
         with mock.patch.object(Gtk, "get_event_widget") as get_event_widget:
             get_event_widget.return_value = ges_clip.ui
-            ges_clip.ui.timeline._button_press_event_cb(None, event)
+            timeline._button_press_event_cb(None, event)
             with mock.patch.object(ges_clip.ui, "translate_coordinates") as translate_coordinates:
                 translate_coordinates.return_value = (0, 0)
-                with mock.patch.object(ges_clip.ui.timeline, "get_layer_at") as get_layer_at:
+                with mock.patch.object(timeline, "get_layer_at") as get_layer_at:
                     get_layer_at.return_value = ges_clip.props.layer, None
                     ges_clip.ui._button_release_event_cb(None, event)
-                    ges_clip.ui.timeline._button_release_event_cb(None, event)
+                    timeline._button_release_event_cb(None, event)
+
+        if ctrl_key:
+            timeline.get_parent().control_mask = original_control_mask
+
+        self.assert_clip_selected(ges_clip, expect_selected)
 
+    def assert_clip_selected(self, ges_clip, expect_selected):
         self.assertEqual(bool(ges_clip.ui.get_state_flags() & Gtk.StateFlags.SELECTED),
                          expect_selected)
         self.assertEqual(ges_clip.selected.selected, expect_selected)
 
+        for child in ges_clip.ui.get_children():
+            if not hasattr(child, "selected"):
+                continue
+
+            self.assertEqual(bool(child.get_state_flags() & Gtk.StateFlags.SELECTED),
+                             expect_selected)
+            self.assertEqual(child.selected, expect_selected)
+
     def assert_caps_equal(self, caps1, caps2):
         if isinstance(caps1, str):
             caps1 = Gst.Caps(caps1)
diff --git a/tests/test_timeline_timeline.py b/tests/test_timeline_timeline.py
index d90393872..0cf86bdc9 100644
--- a/tests/test_timeline_timeline.py
+++ b/tests/test_timeline_timeline.py
@@ -243,7 +243,7 @@ class TestGrouping(common.TestCase):
         timeline = timeline_container.timeline
         self.__check_can_group_ungroup(timeline_container, False, False)
         ges_clip, = self.add_clips_simple(timeline, 1)
-        self.toggle_clip_selection(ges_clip, expect_selected=True)
+        self.click_clip(ges_clip, expect_selected=True)
         self.__check_can_group_ungroup(timeline_container, False, True)
 
         timeline_container.ungroup_action.emit("activate", None)
@@ -251,7 +251,7 @@ class TestGrouping(common.TestCase):
 
         layer, = timeline.ges_timeline.get_layers()
         ges_clip0, ges_clip1 = layer.get_clips()
-        self.toggle_clip_selection(ges_clip0, expect_selected=True)
+        self.click_clip(ges_clip0, expect_selected=True)
         self.__check_can_group_ungroup(timeline_container, False, False)
 
         # Press <ctrl> so selecting in ADD mode
@@ -259,7 +259,7 @@ class TestGrouping(common.TestCase):
         event.keyval = Gdk.KEY_Control_L
         timeline_container.do_key_press_event(event)
 
-        self.toggle_clip_selection(ges_clip1, expect_selected=True)
+        self.click_clip(ges_clip1, expect_selected=True)
         self.__check_can_group_ungroup(timeline_container, True, False)
 
         timeline_container.group_action.emit("activate", None)
@@ -279,7 +279,7 @@ class TestGrouping(common.TestCase):
         # Select the 2 clips
         for clip in clips:
             self.assertIsNone(clip.get_parent())
-            self.toggle_clip_selection(clip, expect_selected=True)
+            self.click_clip(clip, expect_selected=True)
 
         timeline_container.group_action.emit("activate", None)
 
@@ -291,8 +291,7 @@ class TestGrouping(common.TestCase):
                 self.assertEqual(clip.get_toplevel_parent(), selected_clip.get_toplevel_parent())
 
             self.assertEqual(clips[0].get_parent(), clip.get_parent())
-            self.assertTrue(bool(clip.ui.get_state_flags() & Gtk.StateFlags.SELECTED))
-            self.assertTrue(clip.selected)
+            self.assert_clip_selected(clip, expect_selected=True)
 
         group = clips[0].get_parent()
         self.assertEqual(len(group.get_children(False)), len(clips))
@@ -314,12 +313,11 @@ class TestGrouping(common.TestCase):
         self.assertEqual(len(clips), num_clips)
 
         # Deselect one grouped clip clips
-        self.toggle_clip_selection(clips[0], expect_selected=False)
+        self.click_clip(clips[0], expect_selected=False)
 
         # Make sure all the clips have been deselected
         for clip in clips:
-            self.assertFalse(bool(clip.ui.get_state_flags() & Gtk.StateFlags.SELECTED))
-            self.assertFalse(clip.selected)
+            self.assert_clip_selected(clip, expect_selected=False)
 
     def test_group_ungroup(self):
         num_clips = 2
@@ -339,49 +337,63 @@ class TestGrouping(common.TestCase):
             self.assertIsNone(clip.get_parent())
 
     def test_group_splitted_clip_and_select_group(self):
-
+        # Create a clip and select it
         timeline_container = common.create_timeline_container()
         timeline = timeline_container.timeline
         clips = self.add_clips_simple(timeline, 1)
         clips[0].props.duration = timeline.ges_timeline.get_frame_time(4)
-        position = timeline.ges_timeline.get_frame_time(2)
-        self.toggle_clip_selection(clips[0], expect_selected=True)
+        self.click_clip(clips[0], expect_selected=True)
 
+        # Split the clip
+        position = timeline.ges_timeline.get_frame_time(2)
         timeline.ges_timeline.get_asset().pipeline.get_position = mock.Mock(return_value=position)
-        layer = timeline.ges_timeline.get_layers()[0]
-
-        # Split
         timeline_container.split_action.emit("activate", None)
+        layer = timeline.ges_timeline.get_layers()[0]
         clips = layer.get_clips()
         self.assertEqual(len(clips), 2)
 
-        # Only the first clip is selected so select the
-        # second one
-        self.assertTrue(clips[0].selected)
-        self.assertFalse(clips[1].selected)
+        # Only the first clip is selected
+        self.assert_clip_selected(clips[0], expect_selected=True)
+        self.assert_clip_selected(clips[1], expect_selected=False)
 
+        # Select the second clip
         event = mock.Mock()
         event.keyval = Gdk.KEY_Control_L
         timeline_container.do_key_press_event(event)
+        self.click_clip(clips[1], expect_selected=True)
         timeline.get_clicked_layer_and_pos = mock.Mock()
         timeline.get_clicked_layer_and_pos.return_value = (None, None)
-        self.toggle_clip_selection(clips[1], expect_selected=True)
         timeline_container.do_key_release_event(event)
 
-        for clip in clips:
-            self.assertTrue(clip.selected)
+        # Both clips are selected
+        self.assert_clip_selected(clips[0], expect_selected=True)
+        self.assert_clip_selected(clips[1], expect_selected=True)
 
-        # Group the two parts
+        # Group the two selected clips
         timeline_container.group_action.emit("activate", None)
 
-        self.toggle_clip_selection(clips[1], expect_selected=True)
+        # Deselect the second clip, notice both clips have been deselected
+        self.click_clip(clips[1], expect_selected=False, ctrl_key=True)
+        self.assert_clip_selected(clips[0], expect_selected=False)
+
+        # Select the second clip, notice both clips have been selected
+        self.click_clip(clips[1], expect_selected=True)
+        self.assert_clip_selected(clips[0], expect_selected=True)
+
+        # Deselect the first clip, notice both clips have been deselected
+        self.click_clip(clips[0], expect_selected=False, ctrl_key=True)
+        self.assert_clip_selected(clips[1], expect_selected=False)
+
+        # Select the first clip, notice both clips have been selected
+        self.click_clip(clips[0], expect_selected=True)
+        self.assert_clip_selected(clips[1], expect_selected=True)
 
     def test_ungroup_clip(self):
         timeline_container = common.create_timeline_container()
         timeline = timeline_container.timeline
         ges_clip, = self.add_clips_simple(timeline, 1)
 
-        self.toggle_clip_selection(ges_clip, expect_selected=True)
+        self.click_clip(ges_clip, expect_selected=True)
 
         timeline_container.ungroup_action.emit("activate", None)
         layer = timeline.ges_timeline.get_layers()[0]
@@ -466,7 +478,7 @@ class TestCopyPaste(common.TestCase):
 
         # Select the 2 clips
         for clip in clips:
-            self.toggle_clip_selection(clip, expect_selected=True)
+            self.click_clip(clip, expect_selected=True)
 
         self.assertTrue(timeline_container.copy_action.props.enabled)
         self.assertFalse(timeline_container.paste_action.props.enabled)
@@ -536,10 +548,11 @@ class TestCopyPaste(common.TestCase):
         timeline_container.paste_action.emit("activate", None)
 
         clips = layer.get_clips()
-        self.assertListEqual([bool(clip.selected) for clip in clips], [False, True])
+        self.assert_clip_selected(clips[0], expect_selected=False)
+        self.assert_clip_selected(clips[1], expect_selected=True)
 
         # The copy_clips() above simulates CTRL button press, so this will add 1st clip to the selection.
-        self.toggle_clip_selection(clips[0], True)
+        self.click_clip(clips[0], expect_selected=True)
         timeline_container.copy_action.emit("activate", None)
 
         # Paste two clips.
@@ -548,7 +561,10 @@ class TestCopyPaste(common.TestCase):
         timeline_container.paste_action.emit("activate", None)
 
         clips = layer.get_clips()
-        self.assertListEqual([bool(clip.selected) for clip in clips], [False, False, True, True])
+        self.assert_clip_selected(clips[0], expect_selected=False)
+        self.assert_clip_selected(clips[1], expect_selected=False)
+        self.assert_clip_selected(clips[2], expect_selected=True)
+        self.assert_clip_selected(clips[3], expect_selected=True)
 
 
 class TestEditing(common.TestCase):
@@ -600,9 +616,9 @@ class TestShiftSelection(common.TestCase):
 
     def __check_selected(self, selected_clips, not_selected_clips):
         for clip in selected_clips:
-            self.assertEqual(clip.selected._selected, True)
+            self.assert_clip_selected(clip, expect_selected=True)
         for clip in not_selected_clips:
-            self.assertEqual(clip.selected._selected, False)
+            self.assert_clip_selected(clip, expect_selected=False)
 
     def __check_simple(self, left_click_also_seeks):
         timeline_container = common.create_timeline_container()
@@ -882,7 +898,7 @@ class TestKeyboardShiftClips(common.TestCase):
         event.keyval = Gdk.KEY_Control_L
         self.timeline_container.do_key_press_event(event)
         for clip in ges_clips:
-            self.toggle_clip_selection(clip, expect_selected=True)
+            self.click_clip(clip, expect_selected=True)
         self.timeline_container.do_key_release_event(event)
 
         self.timeline_container.shift_forward_action.emit("activate", None)
@@ -921,15 +937,10 @@ class TestKeyboardShiftClips(common.TestCase):
         ges_clip3 = self.add_clip(self.layer, 13 * Gst.SECOND, duration=2 * Gst.SECOND)
         ges_clip4 = self.add_clip(self.layer, 15 * Gst.SECOND, duration=2 * Gst.SECOND)
 
-        event = mock.Mock()
-        event.keyval = Gdk.KEY_Control_L
-        self.timeline_container.do_key_press_event(event)
-        self.toggle_clip_selection(ges_clip1, expect_selected=True)
-        self.toggle_clip_selection(ges_clip2, expect_selected=True)
-        self.toggle_clip_selection(ges_clip3, expect_selected=True)
-        self.toggle_clip_selection(ges_clip4, expect_selected=True)
-
-        self.timeline_container.do_key_release_event(event)
+        self.click_clip(ges_clip1, expect_selected=True, ctrl_key=True)
+        self.click_clip(ges_clip2, expect_selected=True, ctrl_key=True)
+        self.click_clip(ges_clip3, expect_selected=True, ctrl_key=True)
+        self.click_clip(ges_clip4, expect_selected=True, ctrl_key=True)
 
         self.timeline_container.shift_forward_action.emit("activate", None)
 
@@ -954,7 +965,7 @@ class TestSnapClips(common.TestCase):
         ges_clip1 = self.add_clip(self.layer, 5 * Gst.SECOND, duration=2 * Gst.SECOND)
         ges_clip2 = self.add_clip(self.layer, 9 * Gst.SECOND, duration=2 * Gst.SECOND)
 
-        self.toggle_clip_selection(ges_clip1, expect_selected=True)
+        self.click_clip(ges_clip1, expect_selected=True)
 
         self.timeline_container.snap_clips_forward_action.emit("activate", None)
         self.assertEqual(ges_clip1.start, ges_clip2.start - ges_clip1.duration)
@@ -969,11 +980,8 @@ class TestSnapClips(common.TestCase):
         ges_clip2 = self.add_clip(self.layer, 7 * Gst.SECOND, duration=2 * Gst.SECOND)
         ges_clip3 = self.add_clip(self.layer, 11 * Gst.SECOND, duration=2 * Gst.SECOND)
 
-        event = mock.Mock()
-        event.keyval = Gdk.KEY_Control_L
-        self.timeline_container.do_key_press_event(event)
-        self.toggle_clip_selection(ges_clip1, expect_selected=True)
-        self.toggle_clip_selection(ges_clip2, expect_selected=True)
+        self.click_clip(ges_clip1, expect_selected=True, ctrl_key=True)
+        self.click_clip(ges_clip2, expect_selected=True, ctrl_key=True)
 
         self.timeline_container.snap_clips_forward_action.emit("activate", None)
         self.assertEqual(ges_clip2.start, ges_clip3.start - ges_clip2.duration)
@@ -993,7 +1001,7 @@ class TestSnapClips(common.TestCase):
         clip = self.add_clip(layer2, 5 * Gst.SECOND, duration=2 * Gst.SECOND)
         end_clip = self.add_clip(layer2, 30 * Gst.SECOND, duration=2 * Gst.SECOND)
 
-        self.toggle_clip_selection(clip, expect_selected=True)
+        self.click_clip(clip, expect_selected=True)
 
         self.timeline_container.snap_clips_forward_action.emit("activate", None)
         self.assertEqual(clip.start, end_clip.start - clip.duration)
@@ -1007,7 +1015,7 @@ class TestSnapClips(common.TestCase):
         ges_clip = self.add_clip(self.layer, 5 * Gst.SECOND, duration=2 * Gst.SECOND)
         clip_start = ges_clip.start
 
-        self.toggle_clip_selection(ges_clip, expect_selected=True)
+        self.click_clip(ges_clip, expect_selected=True)
 
         self.timeline_container.snap_clips_forward_action.emit("activate", None)
         self.assertEqual(ges_clip.start, clip_start)
@@ -1019,7 +1027,7 @@ class TestSnapClips(common.TestCase):
         self.add_clip(self.layer, 30 * Gst.SECOND, duration=2 * Gst.SECOND)
         ges_clip = self.add_clip(layer2, 5 * Gst.SECOND, duration=2 * Gst.SECOND)
 
-        self.toggle_clip_selection(ges_clip, expect_selected=True)
+        self.click_clip(ges_clip, expect_selected=True)
 
         self.timeline_container.snap_clips_forward_action.emit("activate", None)
         self.assertEqual(ges_clip.start, self.timeline.get_duration() - ges_clip.duration)
diff --git a/tests/test_undo_timeline.py b/tests/test_undo_timeline.py
index 60012a854..a06c34379 100644
--- a/tests/test_undo_timeline.py
+++ b/tests/test_undo_timeline.py
@@ -50,10 +50,7 @@ class TestSelectionResetWhenRemovingClip(common.TestCase):
     def check_selection(self, *expected_selected_clips):
         self.assertSetEqual(set(self.timeline_container.timeline.selection), set(expected_selected_clips))
         for clip in self.get_timeline_clips():
-            if clip in expected_selected_clips:
-                self.assertTrue(clip.selected)
-            else:
-                self.assertFalse(clip.selected)
+            self.assert_clip_selected(clip, expect_selected=clip in expected_selected_clips)
 
     @common.setup_timeline
     def test_redo_delete_when_selected(self):
@@ -203,7 +200,7 @@ class TestTimelineObserver(common.TestCase):
         self.assertEqual(len(clips), 1, clips)
         self.assertEqual(len(clips[0].get_children(False)), 2)
 
-        common.TestCase.toggle_clip_selection(self, clips[0], True)
+        self.click_clip(clips[0], expect_selected=True)
         self.timeline_container.ungroup_action.activate(None)
         clips = list(self.get_timeline_clips())
         self.assertEqual(len(clips), 2, clips)


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