OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import time |
| 6 |
| 7 from telemetry.internal.actions import key_event |
| 8 from telemetry.internal.actions import utils |
| 9 from telemetry.testing import tab_test_case |
| 10 |
| 11 |
| 12 class KeyPressActionTest(tab_test_case.TabTestCase): |
| 13 |
| 14 @property |
| 15 def _scroll_position(self): |
| 16 return self._tab.EvaluateJavaScript( |
| 17 'document.documentElement.scrollTop || document.body.scrollTop') |
| 18 |
| 19 @property |
| 20 def _window_height(self): |
| 21 return self._tab.EvaluateJavaScript('__GestureCommon_GetWindowHeight()') |
| 22 |
| 23 def _PressKey(self, key): |
| 24 action = key_event.KeyPressAction(key) |
| 25 action.WillRunAction(self._tab) |
| 26 action.RunAction(self._tab) |
| 27 |
| 28 def setUp(self): |
| 29 tab_test_case.TabTestCase.setUp(self) |
| 30 self.Navigate('blank.html') |
| 31 utils.InjectJavaScript(self._tab, 'gesture_common.js') |
| 32 |
| 33 def testPressEndAndHome(self): |
| 34 # Make page taller than the window so it's scrollable. |
| 35 self._tab.ExecuteJavaScript('document.body.style.height =' |
| 36 '(3 * __GestureCommon_GetWindowHeight() + 1) + "px";') |
| 37 |
| 38 # Check that the browser is currently showing the top of the page and that |
| 39 # the page has non-trivial height. |
| 40 self.assertEquals(0, self._scroll_position) |
| 41 self.assertLess(50, self._window_height) |
| 42 |
| 43 self._PressKey('End') |
| 44 |
| 45 # Scroll happens *after* key press returns, so we need to wait a little. |
| 46 time.sleep(1) |
| 47 |
| 48 # We can only expect the bottom scroll position to be approximatly equal. |
| 49 self.assertAlmostEqual(2 * self._window_height, self._scroll_position, |
| 50 delta=20) |
| 51 |
| 52 self._PressKey('Home') |
| 53 |
| 54 # Scroll happens *after* key press returns, so we need to wait a little. |
| 55 time.sleep(1) |
| 56 |
| 57 self.assertEquals(self._scroll_position, 0) |
| 58 |
| 59 def testTextEntry(self): |
| 60 # Add an input box to the page. |
| 61 self._tab.ExecuteJavaScript( |
| 62 '(function() {' |
| 63 ' var elem = document.createElement("textarea");' |
| 64 ' document.body.appendChild(elem);' |
| 65 ' elem.focus();' |
| 66 '})();') |
| 67 |
| 68 # Simulate typing a sentence. |
| 69 for char in 'Hello, World!': |
| 70 self._PressKey(char) |
| 71 |
| 72 # Make changes to the sentence using special keys. |
| 73 for _ in xrange(6): |
| 74 self._PressKey('ArrowLeft') |
| 75 self._PressKey('Backspace') |
| 76 self._PressKey('Return') |
| 77 |
| 78 # Check that the contents of the textarea is correct. |
| 79 self.assertEquals('Hello,\nWorld!', |
| 80 self._tab.EvaluateJavaScript( |
| 81 'document.querySelector("textarea").value')) |
| 82 |
| 83 def testPressUnknownKey(self): |
| 84 with self.assertRaises(ValueError): |
| 85 self._PressKey('UnknownKeyName') |
OLD | NEW |