Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / actions / action_runner_unittest.py
1 # Copyright 2014 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 from telemetry import benchmark
6 from telemetry.core import exceptions
7 from telemetry.core import util
8 from telemetry.page.actions import action_runner as action_runner_module
9 from telemetry.page.actions import page_action
10 from telemetry.timeline import model
11 from telemetry.unittest import tab_test_case
12 from telemetry.web_perf import timeline_interaction_record as tir_module
13
14
15 class ActionRunnerInteractionTest(tab_test_case.TabTestCase):
16
17   def GetInteractionRecords(self, trace_data):
18     timeline_model = model.TimelineModel(trace_data)
19     renderer_thread = timeline_model.GetRendererThreadFromTabId(self._tab.id)
20     return [
21         tir_module.TimelineInteractionRecord.FromAsyncEvent(e)
22         for e in renderer_thread.async_slices
23         if tir_module.IsTimelineInteractionRecord(e.name)
24         ]
25
26   def VerifyIssuingInteractionRecords(self, **interaction_kwargs):
27     action_runner = action_runner_module.ActionRunner(self._tab)
28     self.Navigate('interaction_enabled_page.html')
29     action_runner.Wait(1)
30     self._browser.StartTracing()
31     interaction = action_runner.BeginInteraction('InteractionName',
32                                                  **interaction_kwargs)
33     interaction.End()
34     trace_data = self._browser.StopTracing()
35
36     records = self.GetInteractionRecords(trace_data)
37     self.assertEqual(
38         1, len(records),
39         'Failed to issue the interaction record on the tracing timeline.'
40         ' Trace data:\n%s' % repr(trace_data.EventData()))
41     self.assertEqual('InteractionName', records[0].label)
42     for attribute_name in interaction_kwargs:
43       self.assertTrue(getattr(records[0], attribute_name))
44
45   def testIssuingMultipleMeasurementInteractionRecords(self):
46     self.VerifyIssuingInteractionRecords(is_fast=True)
47     self.VerifyIssuingInteractionRecords(is_responsive=True)
48     self.VerifyIssuingInteractionRecords(is_smooth=True)
49     self.VerifyIssuingInteractionRecords(is_fast=True, is_smooth=True)
50
51
52 class ActionRunnerTest(tab_test_case.TabTestCase):
53   def testExecuteJavaScript(self):
54     action_runner = action_runner_module.ActionRunner(self._tab)
55     self.Navigate('blank.html')
56     action_runner.ExecuteJavaScript('var testing = 42;')
57     self.assertEqual(42, self._tab.EvaluateJavaScript('testing'))
58
59   def testWaitForNavigate(self):
60     self.Navigate('page_with_link.html')
61     action_runner = action_runner_module.ActionRunner(self._tab)
62     action_runner.ClickElement('#clickme')
63     action_runner.WaitForNavigate()
64
65     self.assertTrue(self._tab.EvaluateJavaScript(
66         'document.readyState == "interactive" || '
67         'document.readyState == "complete"'))
68     self.assertEqual(
69         self._tab.EvaluateJavaScript('document.location.pathname;'),
70         '/blank.html')
71
72   def testWait(self):
73     action_runner = action_runner_module.ActionRunner(self._tab)
74     self.Navigate('blank.html')
75
76     action_runner.ExecuteJavaScript(
77         'window.setTimeout(function() { window.testing = 101; }, 50);')
78     action_runner.Wait(0.1)
79     self.assertEqual(101, self._tab.EvaluateJavaScript('window.testing'))
80
81     action_runner.ExecuteJavaScript(
82         'window.setTimeout(function() { window.testing = 102; }, 100);')
83     action_runner.Wait(0.2)
84     self.assertEqual(102, self._tab.EvaluateJavaScript('window.testing'))
85
86   def testWaitForJavaScriptCondition(self):
87     action_runner = action_runner_module.ActionRunner(self._tab)
88     self.Navigate('blank.html')
89
90     action_runner.ExecuteJavaScript('window.testing = 219;')
91     action_runner.WaitForJavaScriptCondition(
92         'window.testing == 219', timeout_in_seconds=0.1)
93     action_runner.ExecuteJavaScript(
94         'window.setTimeout(function() { window.testing = 220; }, 50);')
95     action_runner.WaitForJavaScriptCondition(
96         'window.testing == 220', timeout_in_seconds=0.1)
97     self.assertEqual(220, self._tab.EvaluateJavaScript('window.testing'))
98
99   def testWaitForElement(self):
100     action_runner = action_runner_module.ActionRunner(self._tab)
101     self.Navigate('blank.html')
102
103     action_runner.ExecuteJavaScript(
104         '(function() {'
105         '  var el = document.createElement("div");'
106         '  el.id = "test1";'
107         '  el.textContent = "foo";'
108         '  document.body.appendChild(el);'
109         '})()')
110     action_runner.WaitForElement('#test1', timeout_in_seconds=0.1)
111     action_runner.WaitForElement(text='foo', timeout_in_seconds=0.1)
112     action_runner.WaitForElement(
113         element_function='document.getElementById("test1")')
114     action_runner.ExecuteJavaScript(
115         'window.setTimeout(function() {'
116         '  var el = document.createElement("div");'
117         '  el.id = "test2";'
118         '  document.body.appendChild(el);'
119         '}, 50)')
120     action_runner.WaitForElement('#test2', timeout_in_seconds=0.1)
121     action_runner.ExecuteJavaScript(
122         'window.setTimeout(function() {'
123         '  document.getElementById("test2").textContent = "bar";'
124         '}, 50)')
125     action_runner.WaitForElement(text='bar', timeout_in_seconds=0.1)
126     action_runner.ExecuteJavaScript(
127         'window.setTimeout(function() {'
128         '  var el = document.createElement("div");'
129         '  el.id = "test3";'
130         '  document.body.appendChild(el);'
131         '}, 50)')
132     action_runner.WaitForElement(
133         element_function='document.getElementById("test3")')
134
135   def testWaitForElementWithWrongText(self):
136     action_runner = action_runner_module.ActionRunner(self._tab)
137     self.Navigate('blank.html')
138
139     action_runner.ExecuteJavaScript(
140         '(function() {'
141         '  var el = document.createElement("div");'
142         '  el.id = "test1";'
143         '  el.textContent = "foo";'
144         '  document.body.appendChild(el);'
145         '})()')
146     action_runner.WaitForElement('#test1', timeout_in_seconds=0.2)
147     def WaitForElement():
148       action_runner.WaitForElement(text='oo', timeout_in_seconds=0.2)
149     self.assertRaises(util.TimeoutException, WaitForElement)
150
151   def testClickElement(self):
152     self.Navigate('page_with_clickables.html')
153     action_runner = action_runner_module.ActionRunner(self._tab)
154
155     action_runner.ExecuteJavaScript('valueSettableByTest = 1;')
156     action_runner.ClickElement('#test')
157     self.assertEqual(1, action_runner.EvaluateJavaScript('valueToTest'))
158
159     action_runner.ExecuteJavaScript('valueSettableByTest = 2;')
160     action_runner.ClickElement(text='Click/tap me')
161     self.assertEqual(2, action_runner.EvaluateJavaScript('valueToTest'))
162
163     action_runner.ExecuteJavaScript('valueSettableByTest = 3;')
164     action_runner.ClickElement(
165         element_function='document.body.firstElementChild;')
166     self.assertEqual(3, action_runner.EvaluateJavaScript('valueToTest'))
167
168     def WillFail():
169       action_runner.ClickElement('#notfound')
170     self.assertRaises(exceptions.EvaluateException, WillFail)
171
172   @benchmark.Disabled('debug')
173   def testTapElement(self):
174     self.Navigate('page_with_clickables.html')
175     action_runner = action_runner_module.ActionRunner(self._tab)
176
177     action_runner.ExecuteJavaScript('valueSettableByTest = 1;')
178     action_runner.TapElement('#test')
179     self.assertEqual(1, action_runner.EvaluateJavaScript('valueToTest'))
180
181     action_runner.ExecuteJavaScript('valueSettableByTest = 2;')
182     action_runner.TapElement(text='Click/tap me')
183     self.assertEqual(2, action_runner.EvaluateJavaScript('valueToTest'))
184
185     action_runner.ExecuteJavaScript('valueSettableByTest = 3;')
186     action_runner.TapElement(
187         element_function='document.body.firstElementChild')
188     self.assertEqual(3, action_runner.EvaluateJavaScript('valueToTest'))
189
190     def WillFail():
191       action_runner.TapElement('#notfound')
192     self.assertRaises(exceptions.EvaluateException, WillFail)
193
194   def testScroll(self):
195     if not page_action.IsGestureSourceTypeSupported(
196         self._tab, 'touch'):
197       return
198
199     self.Navigate('page_with_swipeables.html')
200     action_runner = action_runner_module.ActionRunner(self._tab)
201
202     action_runner.ScrollElement(
203         selector='#left-right', direction='right', left_start_ratio=0.9)
204     self.assertTrue(action_runner.EvaluateJavaScript(
205         'document.querySelector("#left-right").scrollLeft') > 75)
206     action_runner.ScrollElement(
207         selector='#top-bottom', direction='down', top_start_ratio=0.9)
208     self.assertTrue(action_runner.EvaluateJavaScript(
209         'document.querySelector("#top-bottom").scrollTop') > 75)
210
211     action_runner.ScrollPage(direction='right', left_start_ratio=0.9,
212                              distance=100)
213     self.assertTrue(action_runner.EvaluateJavaScript(
214         'document.body.scrollLeft') > 75)
215
216   def testSwipe(self):
217     if not page_action.IsGestureSourceTypeSupported(
218         self._tab, 'touch'):
219       return
220
221     self.Navigate('page_with_swipeables.html')
222     action_runner = action_runner_module.ActionRunner(self._tab)
223
224     action_runner.SwipeElement(
225         selector='#left-right', direction='left', left_start_ratio=0.9)
226     self.assertTrue(action_runner.EvaluateJavaScript(
227         'document.querySelector("#left-right").scrollLeft') > 75)
228     action_runner.SwipeElement(
229         selector='#top-bottom', direction='up', top_start_ratio=0.9)
230     self.assertTrue(action_runner.EvaluateJavaScript(
231         'document.querySelector("#top-bottom").scrollTop') > 75)
232
233     action_runner.SwipePage(direction='left', left_start_ratio=0.9)
234     self.assertTrue(action_runner.EvaluateJavaScript(
235         'document.body.scrollLeft') > 75)