[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / perf / page_sets / polymer.py
blobf8d4512c58a98d16df50247c84c23da50cbbfd9e
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 from telemetry.page import page as page_module
5 from telemetry.page import page_set as page_set_module
7 class PolymerPage(page_module.Page):
9 def __init__(self, url, page_set, run_no_page_interactions):
10 """ Base class for all polymer pages.
12 Args:
13 run_no_page_interactions: whether the page will run any interactions after
14 navigate steps.
15 """
16 super(PolymerPage, self).__init__(
17 url=url,
18 page_set=page_set)
19 self.script_to_evaluate_on_commit = '''
20 document.addEventListener("polymer-ready", function() {
21 window.__polymer_ready = true;
22 });
23 '''
24 self._run_no_page_interactions = run_no_page_interactions
26 def RunPageInteractions(self, action_runner):
27 # If a polymer page wants to customize its actions, it should
28 # override the PerformPageInteractions method instead of this method.
29 if self._run_no_page_interactions:
30 return
31 self.PerformPageInteractions(action_runner)
33 def PerformPageInteractions(self, action_runner):
34 """ Override this to perform actions after the page has navigated. """
35 pass
37 def RunNavigateSteps(self, action_runner):
38 super(PolymerPage, self).RunNavigateSteps(action_runner)
39 action_runner.WaitForJavaScriptCondition(
40 'window.__polymer_ready')
43 class PolymerCalculatorPage(PolymerPage):
45 def __init__(self, page_set, run_no_page_interactions):
46 super(PolymerCalculatorPage, self).__init__(
47 url=('http://www.polymer-project.org/components/paper-calculator/'
48 'demo.html'),
49 page_set=page_set, run_no_page_interactions=run_no_page_interactions)
51 def PerformPageInteractions(self, action_runner):
52 self.TapButton(action_runner)
53 self.SlidePanel(action_runner)
55 def TapButton(self, action_runner):
56 interaction = action_runner.BeginInteraction(
57 'Action_TapAction')
58 action_runner.TapElement(element_function='''
59 document.querySelector(
60 'body /deep/ #outerPanels'
61 ).querySelector(
62 '#standard'
63 ).shadowRoot.querySelector(
64 'paper-calculator-key[label="5"]'
65 )''')
66 action_runner.Wait(2)
67 interaction.End()
69 def SlidePanel(self, action_runner):
70 # only bother with this interaction if the drawer is hidden
71 opened = action_runner.EvaluateJavaScript('''
72 (function() {
73 var outer = document.querySelector("body /deep/ #outerPanels");
74 return outer.opened || outer.wideMode;
75 }());''')
76 if not opened:
77 interaction = action_runner.BeginInteraction(
78 'Action_SwipeAction')
79 action_runner.SwipeElement(
80 left_start_ratio=0.1, top_start_ratio=0.2,
81 direction='left', distance=300, speed_in_pixels_per_second=5000,
82 element_function='''
83 document.querySelector(
84 'body /deep/ #outerPanels'
85 ).querySelector(
86 '#advanced'
87 ).shadowRoot.querySelector(
88 '.handle-bar'
89 )''')
90 action_runner.WaitForJavaScriptCondition('''
91 var outer = document.querySelector("body /deep/ #outerPanels");
92 outer.opened || outer.wideMode;''')
93 interaction.End()
96 class PolymerShadowPage(PolymerPage):
98 def __init__(self, page_set, run_no_page_interactions):
99 super(PolymerShadowPage, self).__init__(
100 url='http://www.polymer-project.org/components/paper-shadow/demo.html',
101 page_set=page_set, run_no_page_interactions=run_no_page_interactions)
103 def PerformPageInteractions(self, action_runner):
104 action_runner.ExecuteJavaScript(
105 "document.getElementById('fab').scrollIntoView()")
106 action_runner.Wait(5)
107 self.AnimateShadow(action_runner, 'card')
108 #FIXME(wiltzius) disabling until this issue is fixed:
109 # https://github.com/Polymer/paper-shadow/issues/12
110 #self.AnimateShadow(action_runner, 'fab')
112 def AnimateShadow(self, action_runner, eid):
113 for i in range(1, 6):
114 action_runner.ExecuteJavaScript(
115 'document.getElementById("{0}").z = {1}'.format(eid, i))
116 action_runner.Wait(1)
119 class PolymerSampler(PolymerPage):
121 def __init__(self, page_set, anchor, run_no_page_interactions,
122 scrolling_page=False):
123 """Page exercising interactions with a single Paper Sampler subpage.
125 Args:
126 page_set: Page set to inforporate this page into.
127 anchor: string indicating which subpage to load (matches the element
128 type that page is displaying)
129 scrolling_page: Whether scrolling the content pane is relevant to this
130 content page or not.
132 super(PolymerSampler, self).__init__(
133 url=('http://www.polymer-project.org/components/%s/demo.html' % anchor),
134 page_set=page_set, run_no_page_interactions=run_no_page_interactions)
135 self.scrolling_page = scrolling_page
136 self.iframe_js = 'document'
138 def RunNavigateSteps(self, action_runner):
139 super(PolymerSampler, self).RunNavigateSteps(action_runner)
140 waitForLoadJS = """
141 window.Polymer.whenPolymerReady(function() {
142 %s.contentWindow.Polymer.whenPolymerReady(function() {
143 window.__polymer_ready = true;
146 """ % self.iframe_js
147 action_runner.ExecuteJavaScript(waitForLoadJS)
148 action_runner.WaitForJavaScriptCondition(
149 'window.__polymer_ready')
151 def PerformPageInteractions(self, action_runner):
152 #TODO(wiltzius) Add interactions for input elements and shadow pages
153 if self.scrolling_page:
154 # Only bother scrolling the page if its been marked as worthwhile
155 self.ScrollContentPane(action_runner)
156 self.TouchEverything(action_runner)
158 def ScrollContentPane(self, action_runner):
159 element_function = (self.iframe_js + '.querySelector('
160 '"core-scroll-header-panel").$.mainContainer')
161 interaction = action_runner.BeginInteraction('Scroll_Page')
162 action_runner.ScrollElement(use_touch=True,
163 direction='down',
164 distance='900',
165 element_function=element_function)
166 interaction.End()
167 interaction = action_runner.BeginInteraction('Scroll_Page')
168 action_runner.ScrollElement(use_touch=True,
169 direction='up',
170 distance='900',
171 element_function=element_function)
172 interaction.End()
174 def TouchEverything(self, action_runner):
175 tappable_types = [
176 'paper-button',
177 'paper-checkbox',
178 'paper-fab',
179 'paper-icon-button',
180 # crbug.com/394756
181 # 'paper-radio-button',
182 'paper-tab',
183 'paper-toggle-button',
184 'x-shadow',
186 for tappable_type in tappable_types:
187 self.DoActionOnWidgetType(action_runner, tappable_type, self.TapWidget)
188 swipeable_types = ['paper-slider']
189 for swipeable_type in swipeable_types:
190 self.DoActionOnWidgetType(action_runner, swipeable_type, self.SwipeWidget)
192 def DoActionOnWidgetType(self, action_runner, widget_type, action_function):
193 # Find all widgets of this type, but skip any that are disabled or are
194 # currently active as they typically don't produce animation frames.
195 element_list_query = (self.iframe_js +
196 ('.querySelectorAll("body %s:not([disabled]):'
197 'not([active])")' % widget_type))
198 roles_count_query = element_list_query + '.length'
199 for i in range(action_runner.EvaluateJavaScript(roles_count_query)):
200 element_query = element_list_query + ("[%d]" % i)
201 if action_runner.EvaluateJavaScript(
202 element_query + '.offsetParent != null'):
203 # Only try to tap on visible elements (offsetParent != null)
204 action_runner.ExecuteJavaScript(element_query + '.scrollIntoView()')
205 action_runner.Wait(1) # wait for page to settle after scrolling
206 action_function(action_runner, element_query)
208 def TapWidget(self, action_runner, element_function):
209 interaction = action_runner.BeginInteraction(
210 'Tap_Widget')
211 action_runner.TapElement(element_function=element_function)
212 action_runner.Wait(1) # wait for e.g. animations on the widget
213 interaction.End()
215 def SwipeWidget(self, action_runner, element_function):
216 interaction = action_runner.BeginInteraction(
217 'Swipe_Widget')
218 action_runner.SwipeElement(element_function=element_function,
219 left_start_ratio=0.75,
220 speed_in_pixels_per_second=300)
221 interaction.End()
224 class PolymerPageSet(page_set_module.PageSet):
226 def __init__(self, run_no_page_interactions=False):
227 super(PolymerPageSet, self).__init__(
228 user_agent_type='mobile',
229 archive_data_file='data/polymer.json',
230 bucket=page_set_module.PUBLIC_BUCKET)
232 self.AddUserStory(PolymerCalculatorPage(self, run_no_page_interactions))
233 self.AddUserStory(PolymerShadowPage(self, run_no_page_interactions))
235 # Polymer Sampler subpages that are interesting to tap / swipe elements on
236 TAPPABLE_PAGES = [
237 'paper-button',
238 'paper-checkbox',
239 'paper-fab',
240 'paper-icon-button',
241 # crbug.com/394756
242 # 'paper-radio-button',
243 #FIXME(wiltzius) Disabling x-shadow until this issue is fixed:
244 # https://github.com/Polymer/paper-shadow/issues/12
245 #'paper-shadow',
246 'paper-tabs',
247 'paper-toggle-button',
249 for p in TAPPABLE_PAGES:
250 self.AddUserStory(PolymerSampler(
251 self, p, run_no_page_interactions=run_no_page_interactions))
253 # Polymer Sampler subpages that are interesting to scroll
254 SCROLLABLE_PAGES = [
255 'core-scroll-header-panel',
257 for p in SCROLLABLE_PAGES:
258 self.AddUserStory(PolymerSampler(
259 self, p, run_no_page_interactions=run_no_page_interactions,
260 scrolling_page=True))
262 for page in self:
263 assert (page.__class__.RunPageInteractions ==
264 PolymerPage.RunPageInteractions), (
265 'Pages in this page set must not override PolymerPage\' '
266 'RunPageInteractions method.')