Bug 1253989 Part 2 - Use @parameterized to rewrite cursor mode tests. r?mtseng draft
authorTing-Yu Lin <tlin@mozilla.com>
Tue, 08 Mar 2016 22:21:31 +0800
changeset 338113 ed763f6fd58f1ede2785b20d3f98df225ce9762f
parent 338112 11c4464ba4387ee74234070e7bd8bc6af930ea5c
child 338114 aff04b2275b5a727a7e84db9a0c489b164412ce1
push id12431
push usertlin@mozilla.com
push dateTue, 08 Mar 2016 14:22:21 +0000
reviewersmtseng
bugs1253989, 1221459
milestone48.0a1
Bug 1253989 Part 2 - Use @parameterized to rewrite cursor mode tests. r?mtseng Remove _test_touch_caret_hides_after_receiving_wheel_event() completely since it was a test only for touch caret, which is a leftover in bug 1221459. MozReview-Commit-ID: 4szwuG6t5SF
layout/base/tests/marionette/test_accessiblecaret_cursor_mode.py
--- a/layout/base/tests/marionette/test_accessiblecaret_cursor_mode.py
+++ b/layout/base/tests/marionette/test_accessiblecaret_cursor_mode.py
@@ -1,53 +1,56 @@
 # -*- coding: utf-8 -*-
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import string
 
-from marionette import MarionetteTestCase
-
+from marionette.marionette_test import (
+    MarionetteTestCase, parameterized
+)
 from marionette_driver.by import By
 from marionette_driver.marionette import Actions
 from marionette_driver.selection import SelectionManager
 
 
 class AccessibleCaretCursorModeTestCase(MarionetteTestCase):
-    '''Test cases for AccessibleCaret under cursor mode, aka touch caret.
+    '''Test cases for AccessibleCaret under cursor mode.
+
+    We call the blinking cursor (nsCaret) as cursor, and call AccessibleCaret as
+    caret for short.
 
     '''
+    _input_id = 'input'
+    _textarea_id = 'textarea'
+    _contenteditable_id = 'contenteditable'
 
     def setUp(self):
-        # Code to execute before a test is being run.
+        # Code to execute before every test is running.
         super(AccessibleCaretCursorModeTestCase, self).setUp()
         self.caret_tested_pref = 'layout.accessiblecaret.enabled'
         self.caret_timeout_ms_pref = 'layout.accessiblecaret.timeout_ms'
         self.prefs = {
             self.caret_tested_pref: True,
             self.caret_timeout_ms_pref: 0,
         }
         self.marionette.set_prefs(self.prefs)
         self.actions = Actions(self.marionette)
 
-    def timeout_ms(self):
-        'Return touch caret expiration time in milliseconds.'
-        return self.marionette.get_pref(self.caret_timeout_ms_pref)
-
     def open_test_html(self):
-        'Open html for testing and locate elements.'
         test_html = self.marionette.absolute_url('test_touchcaret.html')
         self.marionette.navigate(test_html)
 
-        self._input = self.marionette.find_element(By.ID, 'input')
-        self._textarea = self.marionette.find_element(By.ID, 'textarea')
-        self._contenteditable = self.marionette.find_element(By.ID, 'contenteditable')
-
-    def _test_move_caret_to_the_right_by_one_character(self, el, assertFunc):
+    @parameterized(_input_id, el_id=_input_id)
+    @parameterized(_textarea_id, el_id=_textarea_id)
+    @parameterized(_contenteditable_id, el_id=_contenteditable_id)
+    def test_move_cursor_to_the_right_by_one_character(self, el_id):
+        self.open_test_html()
+        el = self.marionette.find_element(By.ID, el_id)
         sel = SelectionManager(el)
         content_to_add = '!'
         target_content = sel.content
         target_content = target_content[:1] + content_to_add + target_content[1:]
 
         # Get touch caret (x, y) at position 1 and 2.
         el.tap()
         sel.move_caret_to_front()
@@ -59,37 +62,47 @@ class AccessibleCaretCursorModeTestCase(
         # Tap the front of the input to make touch caret appear.
         el.tap(caret0_x, caret0_y)
 
         # Move touch caret
         self.actions.flick(el, touch_caret0_x, touch_caret0_y,
                            touch_caret1_x, touch_caret1_y).perform()
 
         self.actions.key_down(content_to_add).key_up(content_to_add).perform()
-        assertFunc(target_content, sel.content)
+        self.assertEqual(target_content, sel.content)
 
-    def _test_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self, el, assertFunc):
+    @parameterized(_input_id, el_id=_input_id)
+    @parameterized(_textarea_id, el_id=_textarea_id)
+    @parameterized(_contenteditable_id, el_id=_contenteditable_id)
+    def test_move_cursor_to_end_by_dragging_caret_to_bottom_right_corner(self, el_id):
+        self.open_test_html()
+        el = self.marionette.find_element(By.ID, el_id)
         sel = SelectionManager(el)
         content_to_add = '!'
         target_content = sel.content + content_to_add
 
         # Tap the front of the input to make touch caret appear.
         el.tap()
         sel.move_caret_to_front()
         el.tap(*sel.caret_location())
 
         # Move touch caret to the bottom-right corner of the element.
         src_x, src_y = sel.touch_caret_location()
         dest_x, dest_y = el.size['width'], el.size['height']
         self.actions.flick(el, src_x, src_y, dest_x, dest_y).perform()
 
         self.actions.key_down(content_to_add).key_up(content_to_add).perform()
-        assertFunc(target_content, sel.content)
+        self.assertEqual(target_content, sel.content)
 
-    def _test_move_caret_to_front_by_dragging_touch_caret_to_front_of_content(self, el, assertFunc):
+    @parameterized(_input_id, el_id=_input_id)
+    @parameterized(_textarea_id, el_id=_textarea_id)
+    @parameterized(_contenteditable_id, el_id=_contenteditable_id)
+    def test_move_cursor_to_front_by_dragging_caret_to_front(self, el_id):
+        self.open_test_html()
+        el = self.marionette.find_element(By.ID, el_id)
         sel = SelectionManager(el)
         content_to_add = '!'
         target_content = content_to_add + sel.content
 
         # Get touch caret location at the front.
         el.tap()
         sel.move_caret_to_front()
         dest_x, dest_y = sel.touch_caret_location()
@@ -102,25 +115,31 @@ class AccessibleCaretCursorModeTestCase(
         sel.move_caret_by_offset(1, backward=True)
         el.tap(*sel.caret_location())
         src_x, src_y = sel.touch_caret_location()
 
         # Move touch caret to the front of the input box.
         self.actions.flick(el, src_x, src_y, dest_x, dest_y).perform()
 
         self.actions.key_down(content_to_add).key_up(content_to_add).perform()
-        assertFunc(target_content, sel.content)
+        self.assertEqual(target_content, sel.content)
 
-    def _test_touch_caret_timeout_by_dragging_it_to_top_left_corner_after_timout(self, el, assertFunc):
+    @parameterized(_input_id, el_id=_input_id)
+    @parameterized(_textarea_id, el_id=_textarea_id)
+    @parameterized(_contenteditable_id, el_id=_contenteditable_id)
+    def test_dragging_caret_to_top_left_corner_after_timeout(self, el_id):
+        self.open_test_html()
+        el = self.marionette.find_element(By.ID, el_id)
         sel = SelectionManager(el)
         content_to_add = '!'
         non_target_content = content_to_add + sel.content
 
-        # Get touch caret expiration time in millisecond, and convert it to second.
-        timeout = self.timeout_ms() / 1000.0
+        # Set caret timeout to be 1 second.
+        timeout = 1
+        self.marionette.set_pref(self.caret_timeout_ms_pref, timeout * 1000)
 
         # Set a 3x timeout margin to prevent intermittent test failures.
         timeout *= 3
 
         # Tap to make touch caret appear. Note: it's strange that when the caret
         # is at the end, the rect of the caret in <textarea> cannot be obtained.
         # A bug perhaps.
         el.tap()
@@ -130,56 +149,22 @@ class AccessibleCaretCursorModeTestCase(
 
         # Wait until touch caret disappears, then pretend to move it to the
         # top-left corner of the input box.
         src_x, src_y = sel.touch_caret_location()
         dest_x, dest_y = 0, 0
         self.actions.wait(timeout).flick(el, src_x, src_y, dest_x, dest_y).perform()
 
         self.actions.key_down(content_to_add).key_up(content_to_add).perform()
-        assertFunc(non_target_content, sel.content)
-
-    def _test_touch_caret_hides_after_receiving_wheel_event(self, el, assertFunc):
-        sel = SelectionManager(el)
-        content_to_add = '!'
-        non_target_content = content_to_add + sel.content
-
-        # Tap to make touch caret appear. Note: it's strange that when the caret
-        # is at the end, the rect of the caret in <textarea> cannot be obtained.
-        # A bug perhaps.
-        el.tap()
-        sel.move_caret_to_end()
-        sel.move_caret_by_offset(1, backward=True)
-        el.tap(*sel.caret_location())
+        self.assertNotEqual(non_target_content, sel.content)
 
-        # Send an arbitrary scroll-down-10px wheel event to the center of the
-        # input box to hide touch caret. Then pretend to move it to the top-left
-        # corner of the input box.
-        src_x, src_y = sel.touch_caret_location()
-        dest_x, dest_y = 0, 0
-        el_center_x, el_center_y = el.rect['x'], el.rect['y']
-        self.marionette.execute_script(
-            '''
-            var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
-                              .getInterface(Components.interfaces.nsIDOMWindowUtils);
-            utils.sendWheelEvent(arguments[0], arguments[1],
-                                 0, 10, 0, WheelEvent.DOM_DELTA_PIXEL,
-                                 0, 0, 0, 0);
-            ''',
-            script_args=[el_center_x, el_center_y],
-            sandbox='system'
-        )
-        self.actions.flick(el, src_x, src_y, dest_x, dest_y).perform()
-
-        self.actions.key_down(content_to_add).key_up(content_to_add).perform()
-        assertFunc(non_target_content, sel.content)
-
-    def _test_caret_not_appear_when_typing_in_scrollable_content(self, el, assertFunc):
+    def test_caret_not_appear_when_typing_in_scrollable_content(self):
+        self.open_test_html()
+        el = self.marionette.find_element(By.ID, self._input_id)
         sel = SelectionManager(el)
-
         content_to_add = '!'
         target_content = sel.content + string.ascii_letters + content_to_add
 
         el.tap()
         sel.move_caret_to_end()
 
         # Insert a long string to the end of the <input>, which triggers
         # ScrollPositionChanged event.
@@ -190,85 +175,21 @@ class AccessibleCaretCursorModeTestCase(
         # <input> to change the cursor position.
         src_x, src_y = sel.touch_caret_location()
         dest_x, dest_y = 0, 0
         self.actions.flick(el, src_x, src_y, dest_x, dest_y).perform()
 
         # The content should be inserted at the end of the <input>.
         el.send_keys(content_to_add)
 
-        assertFunc(target_content, sel.content)
-
-    ########################################################################
-    # <input> test cases with touch caret enabled
-    ########################################################################
-    def test_input_move_caret_to_the_right_by_one_character(self):
-        self.open_test_html()
-        self._test_move_caret_to_the_right_by_one_character(self._input, self.assertEqual)
-
-    def test_input_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self):
-        self.open_test_html()
-        self._test_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self._input, self.assertEqual)
-
-    def test_input_move_caret_to_front_by_dragging_touch_caret_to_top_left_corner(self):
-        self.open_test_html()
-        self._test_move_caret_to_front_by_dragging_touch_caret_to_front_of_content(self._input, self.assertEqual)
+        self.assertEqual(target_content, sel.content)
 
-    def test_input_caret_not_appear_when_typing_in_scrollable_content(self):
-        self.open_test_html()
-        self._test_caret_not_appear_when_typing_in_scrollable_content(self._input, self.assertEqual)
-
-    def test_input_touch_caret_timeout(self):
-        with self.marionette.using_prefs({self.caret_timeout_ms_pref: 1000}):
-            self.open_test_html()
-            self._test_touch_caret_timeout_by_dragging_it_to_top_left_corner_after_timout(self._input, self.assertNotEqual)
-
-    ########################################################################
-    # <textarea> test cases with touch caret enabled
-    ########################################################################
-    def test_textarea_move_caret_to_the_right_by_one_character(self):
-        self.open_test_html()
-        self._test_move_caret_to_the_right_by_one_character(self._textarea, self.assertEqual)
-
-    def test_textarea_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self):
+    def test_caret_not_jump_when_dragging_to_editable_content_boundary(self):
         self.open_test_html()
-        self._test_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self._textarea, self.assertEqual)
-
-    def test_textarea_move_caret_to_front_by_dragging_touch_caret_to_top_left_corner(self):
-        self.open_test_html()
-        self._test_move_caret_to_front_by_dragging_touch_caret_to_front_of_content(self._textarea, self.assertEqual)
-
-    def test_textarea_touch_caret_timeout(self):
-        with self.marionette.using_prefs({self.caret_timeout_ms_pref: 1000}):
-            self.open_test_html()
-            self._test_touch_caret_timeout_by_dragging_it_to_top_left_corner_after_timout(self._textarea, self.assertNotEqual)
-
-    ########################################################################
-    # <div> contenteditable test cases with touch caret enabled
-    ########################################################################
-    def test_contenteditable_move_caret_to_the_right_by_one_character(self):
-        self.open_test_html()
-        self._test_move_caret_to_the_right_by_one_character(self._contenteditable, self.assertEqual)
-
-    def test_contenteditable_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self):
-        self.open_test_html()
-        self._test_move_caret_to_end_by_dragging_touch_caret_to_bottom_right_corner(self._contenteditable, self.assertEqual)
-
-    def test_contenteditable_move_caret_to_front_by_dragging_touch_caret_to_top_left_corner(self):
-        self.open_test_html()
-        self._test_move_caret_to_front_by_dragging_touch_caret_to_front_of_content(self._contenteditable, self.assertEqual)
-
-    def test_contenteditable_touch_caret_timeout(self):
-        with self.marionette.using_prefs({self.caret_timeout_ms_pref: 1000}):
-            self.open_test_html()
-            self._test_touch_caret_timeout_by_dragging_it_to_top_left_corner_after_timout(self._contenteditable, self.assertNotEqual)
-
-    def test_caret_does_not_jump_when_dragging_to_editable_content_boundary(self):
-        self.open_test_html()
-        el = self._input
+        el = self.marionette.find_element(By.ID, self._input_id)
         sel = SelectionManager(el)
         content_to_add = '!'
         non_target_content = sel.content + content_to_add
 
         # Goal: the cursor position does not being changed after dragging the
         # caret down on the Y-axis.
         el.tap()
         sel.move_caret_to_front()