OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2015 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 import math | |
5 import os | |
6 | |
7 from telemetry.page.actions import drag | |
8 from telemetry.unittest_util import tab_test_case | |
9 | |
10 | |
11 class DragActionTest(tab_test_case.TabTestCase): | |
12 def checkWithinRange(self, value, expected, error_ratio): | |
Sami
2015/03/05 12:36:11
CamelCase naming please.
| |
13 error_range = abs(expected * error_ratio) | |
14 return abs(value - expected) <= error_range | |
15 | |
16 def testDragAction(self): | |
17 self.Navigate('draggable.html') | |
18 | |
19 with open(os.path.join(os.path.dirname(__file__), | |
20 'gesture_common.js')) as f: | |
21 js = f.read() | |
22 self._tab.ExecuteJavaScript(js) | |
23 | |
24 div_width = self._tab.EvaluateJavaScript( | |
25 '__GestureCommon_GetBoundingVisibleRect(document.body).width') | |
26 div_height = self._tab.EvaluateJavaScript( | |
27 '__GestureCommon_GetBoundingVisibleRect(document.body).height') | |
28 | |
29 i = drag.DragAction(left_start_ratio=0.5, top_start_ratio=0.5, | |
30 left_end_ratio=0.75, top_end_ratio=0.75) | |
31 i.WillRunAction(self._tab) | |
32 self._tab.ExecuteJavaScript(''' | |
33 window.__dragAction.beginMeasuringHook = function() { | |
34 window.__didBeginMeasuring = true; | |
35 }; | |
36 window.__dragAction.endMeasuringHook = function() { | |
37 window.__didEndMeasuring = true; | |
38 };''') | |
39 i.RunAction(self._tab) | |
40 | |
41 self.assertTrue(self._tab.EvaluateJavaScript('window.__didBeginMeasuring')) | |
42 self.assertTrue(self._tab.EvaluateJavaScript('window.__didEndMeasuring')) | |
43 | |
44 div_position_x = self._tab.EvaluateJavaScript( | |
45 'document.getElementById("drag_div").offsetLeft') | |
46 div_position_y = self._tab.EvaluateJavaScript( | |
47 'document.getElementById("drag_div").offsetTop') | |
48 | |
49 # 0.25 is the ratio of displacement to the initial size. | |
50 expected_x = math.floor(div_width * -0.25) | |
51 expected_y = math.floor(div_height * -0.25) | |
52 error_ratio = 0.1 | |
53 | |
54 self.assertTrue( | |
55 self.checkWithinRange(div_position_x, expected_x, error_ratio), | |
56 msg="Moved element's left coordinate: %d, expected: %d" % | |
57 (div_position_x, expected_x)) | |
58 self.assertTrue( | |
59 self.checkWithinRange(div_position_y, expected_y, error_ratio), | |
60 msg="Moved element's top coordinate: %d, expected: %d" % | |
61 (div_position_y, expected_y)) | |
OLD | NEW |