From 8056e734b59a218c9ff946114a659559731de4d6 Mon Sep 17 00:00:00 2001 From: Jeremy Chen Date: Wed, 31 Dec 2014 14:40:39 +0800 Subject: [PATCH] Bug 1110917 - Part 1 - Add test cases for selectioncaret changes. r=dburns --- .../tests/marionette/test_selectioncarets.py | 113 ++++++++++++++++-- 1 file changed, 102 insertions(+), 11 deletions(-) diff --git a/layout/base/tests/marionette/test_selectioncarets.py b/layout/base/tests/marionette/test_selectioncarets.py index 50e89e0cee2..082d1071235 100644 --- a/layout/base/tests/marionette/test_selectioncarets.py +++ b/layout/base/tests/marionette/test_selectioncarets.py @@ -39,17 +39,32 @@ class SelectionCaretsTest(MarionetteTestCase): self._contenteditable = self.marionette.find_element(*self._contenteditable_selector) self._content = self.marionette.find_element(*self._content_selector) - def _long_press_to_select_first_word(self, el, sel): - # Move caret inside the first word. + def _first_word_location(self, el): + '''Get the location (x, y) of the first word in el. + + Note: this function has a side effect which changes focus to the + target element el. + + ''' + sel = SelectionManager(el) + + # Move caret behind the first character to get the location of the first + # word. el.tap() sel.move_caret_to_front() sel.move_caret_by_offset(1) - x, y = sel.caret_location() - # Long press the caret position. Selection carets should appear, and the - # first word will be selected. On Windows, those spaces after the word - # will also be selected. - long_press_without_contextmenu(self.marionette, el, self._long_press_time, x, y) + return sel.caret_location() + + def _long_press_to_select(self, el, x, y): + '''Long press the location (x, y) to select a word. + + SelectionCarets should appear. On Windows, those spaces after the + word will also be selected. + + ''' + long_press_without_contextmenu(self.marionette, el, self._long_press_time, + x, y) def _test_long_press_to_select_a_word(self, el, assertFunc): sel = SelectionManager(el) @@ -59,7 +74,8 @@ class SelectionCaretsTest(MarionetteTestCase): target_content = words[0] # Goal: Select the first word. - self._long_press_to_select_first_word(el, sel) + x, y = self._first_word_location(el) + self._long_press_to_select(el, x, y) # Ignore extra spaces selected after the word. assertFunc(target_content, sel.selected_content.rstrip()) @@ -79,7 +95,8 @@ class SelectionCaretsTest(MarionetteTestCase): sel.select_all() (_, _), (end_caret_x, end_caret_y) = sel.selection_carets_location() - self._long_press_to_select_first_word(el, sel) + x, y = self._first_word_location(el) + self._long_press_to_select(el, x, y) # Move the right caret to the end of the content. (caret1_x, caret1_y), (caret2_x, caret2_y) = sel.selection_carets_location() @@ -91,7 +108,8 @@ class SelectionCaretsTest(MarionetteTestCase): # Ignore extra spaces at the beginning of the content in comparison. assertFunc(target_content.lstrip(), sel.selected_content.lstrip()) - def _test_minimum_select_one_character(self, el, assertFunc): + def _test_minimum_select_one_character(self, el, assertFunc, + x=None, y=None): sel = SelectionManager(el) original_content = sel.content words = original_content.split() @@ -100,7 +118,13 @@ class SelectionCaretsTest(MarionetteTestCase): # Goal: Select the first character. target_content = original_content[0] - self._long_press_to_select_first_word(el, sel) + if x and y: + # If we got x and y from the arguments, use it as a hint of the + # location of the first word + pass + else: + x, y = self._first_word_location(el) + self._long_press_to_select(el, x, y) # Move the right caret to the position of the left caret. (caret1_x, caret1_y), (caret2_x, caret2_y) = sel.selection_carets_location() @@ -108,6 +132,24 @@ class SelectionCaretsTest(MarionetteTestCase): assertFunc(target_content, sel.selected_content) + def _test_focus_obtained_by_long_press(self, el1, el2): + '''Test the focus could be changed from el1 to el2 by long press. + + If the focus is changed to e2 successfully, SelectionCarets should + appear and could be dragged. + + ''' + # Goal: Tap to focus el1, and then select the first character on + # el2. + + # We want to collect the location of the first word in el2 here + # since self._first_word_location() has the side effect which would + # change the focus. + x, y = self._first_word_location(el2) + el1.tap() + self._test_minimum_select_one_character(el2, self.assertEqual, + x=x, y=y) + ######################################################################## # test cases with selection carets enabled ######################################################################## @@ -123,6 +165,18 @@ class SelectionCaretsTest(MarionetteTestCase): self.openTestHtml(enabled=True) self._test_minimum_select_one_character(self._input, self.assertEqual) + def test_input_focus_obtained_by_long_press_from_textarea(self): + self.openTestHtml(enabled=True) + self._test_focus_obtained_by_long_press(self._textarea, self._input) + + def test_input_focus_obtained_by_long_press_from_contenteditable(self): + self.openTestHtml(enabled=True) + self._test_focus_obtained_by_long_press(self._contenteditable, self._input) + + def test_input_focus_obtained_by_long_press_from_content_non_editable(self): + self.openTestHtml(enabled=True) + self._test_focus_obtained_by_long_press(self._content, self._input) + ######################################################################## # test cases with selection carets disabled ######################################################################## @@ -149,6 +203,18 @@ class SelectionCaretsTest(MarionetteTestCase): self.openTestHtml(enabled=True) self._test_minimum_select_one_character(self._textarea, self.assertEqual) + def test_textarea_focus_obtained_by_long_press_from_input(self): + self.openTestHtml(enabled=True) + self._test_focus_obtained_by_long_press(self._input, self._textarea) + + def test_textarea_focus_obtained_by_long_press_from_contenteditable(self): + self.openTestHtml(enabled=True) + self._test_focus_obtained_by_long_press(self._contenteditable, self._textarea) + + def test_textarea_focus_obtained_by_long_press_from_content_non_editable(self): + self.openTestHtml(enabled=True) + self._test_focus_obtained_by_long_press(self._content, self._textarea) + ######################################################################## #