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
):
10 super(PolymerPage
, self
).__init
__(
13 self
.script_to_evaluate_on_commit
= '''
14 document.addEventListener("polymer-ready", function() {
15 window.__polymer_ready = true;
19 def RunNavigateSteps(self
, action_runner
):
20 action_runner
.NavigateToPage(self
)
21 action_runner
.WaitForJavaScriptCondition(
22 'window.__polymer_ready')
25 class PolymerCalculatorPage(PolymerPage
):
27 def __init__(self
, page_set
):
28 super(PolymerCalculatorPage
, self
).__init
__(
29 url
=('http://www.polymer-project.org/components/paper-calculator/'
33 def RunSmoothness(self
, action_runner
):
34 self
.TapButton(action_runner
)
35 self
.SlidePanel(action_runner
)
37 def TapButton(self
, action_runner
):
38 interaction
= action_runner
.BeginInteraction(
39 'Action_TapAction', is_smooth
=True)
40 action_runner
.TapElement(element_function
='''
41 document.querySelector(
42 'body /deep/ #outerPanels'
45 ).shadowRoot.querySelector(
46 'paper-calculator-key[label="5"]'
51 def SlidePanel(self
, action_runner
):
52 interaction
= action_runner
.BeginInteraction(
53 'Action_SwipeAction', is_smooth
=True)
54 action_runner
.SwipeElement(
55 left_start_ratio
=0.1, top_start_ratio
=0.2,
56 direction
='left', distance
=300, speed_in_pixels_per_second
=5000,
58 document.querySelector(
59 'body /deep/ #outerPanels'
62 ).shadowRoot.querySelector(
65 action_runner
.WaitForJavaScriptCondition('''
66 var outer = document.querySelector("body /deep/ #outerPanels");
67 outer.opened || outer.wideMode;''')
71 class PolymerShadowPage(PolymerPage
):
73 def __init__(self
, page_set
):
74 super(PolymerShadowPage
, self
).__init
__(
75 url
='http://www.polymer-project.org/components/paper-shadow/demo.html',
78 def RunSmoothness(self
, action_runner
):
79 action_runner
.ExecuteJavaScript(
80 "document.getElementById('fab').scrollIntoView()")
82 self
.AnimateShadow(action_runner
, 'card')
83 #FIXME(wiltzius) disabling until this issue is fixed:
84 # https://github.com/Polymer/paper-shadow/issues/12
85 #self.AnimateShadow(action_runner, 'fab')
87 def AnimateShadow(self
, action_runner
, eid
):
89 action_runner
.ExecuteJavaScript(
90 'document.getElementById("{0}").z = {1}'.format(eid
, i
))
94 class PolymerSampler(PolymerPage
):
96 def __init__(self
, page_set
, anchor
, scrolling_page
=False):
97 """Page exercising interactions with a single Paper Sampler subpage.
100 page_set: Page set to inforporate this page into.
101 anchor: string indicating which subpage to load (matches the element
102 type that page is displaying)
103 scrolling_page: Whether scrolling the content pane is relevant to this
106 super(PolymerSampler
, self
).__init
__(
107 url
=('http://www.polymer-project.org/components/%s/demo.html' % anchor
),
109 self
.scrolling_page
= scrolling_page
110 self
.iframe_js
= 'document'
112 def RunNavigateSteps(self
, action_runner
):
113 super(PolymerSampler
, self
).RunNavigateSteps(action_runner
)
115 window.Polymer.whenPolymerReady(function() {
116 %s.contentWindow.Polymer.whenPolymerReady(function() {
117 window.__polymer_ready = true;
121 action_runner
.ExecuteJavaScript(waitForLoadJS
)
122 action_runner
.WaitForJavaScriptCondition(
123 'window.__polymer_ready')
125 def RunSmoothness(self
, action_runner
):
126 #TODO(wiltzius) Add interactions for input elements and shadow pages
127 if self
.scrolling_page
:
128 # Only bother scrolling the page if its been marked as worthwhile
129 self
.ScrollContentPane(action_runner
)
130 self
.TouchEverything(action_runner
)
132 def ScrollContentPane(self
, action_runner
):
133 element_function
= (self
.iframe_js
+ '.querySelector('
134 '"core-scroll-header-panel").$.mainContainer')
135 interaction
= action_runner
.BeginInteraction('Scroll_Page', is_smooth
=True)
136 action_runner
.ScrollElement(use_touch
=True,
139 element_function
=element_function
)
141 interaction
= action_runner
.BeginInteraction('Scroll_Page', is_smooth
=True)
142 action_runner
.ScrollElement(use_touch
=True,
145 element_function
=element_function
)
148 def TouchEverything(self
, action_runner
):
155 # 'paper-radio-button',
157 'paper-toggle-button',
160 for tappable_type
in tappable_types
:
161 self
.DoActionOnWidgetType(action_runner
, tappable_type
, self
.TapWidget
)
162 swipeable_types
= ['paper-slider']
163 for swipeable_type
in swipeable_types
:
164 self
.DoActionOnWidgetType(action_runner
, swipeable_type
, self
.SwipeWidget
)
166 def DoActionOnWidgetType(self
, action_runner
, widget_type
, action_function
):
167 # Find all widgets of this type, but skip any that are disabled or are
168 # currently active as they typically don't produce animation frames.
169 element_list_query
= (self
.iframe_js
+
170 ('.querySelectorAll("body %s:not([disabled]):'
171 'not([active])")' % widget_type
))
172 roles_count_query
= element_list_query
+ '.length'
173 for i
in range(action_runner
.EvaluateJavaScript(roles_count_query
)):
174 element_query
= element_list_query
+ ("[%d]" % i
)
175 if action_runner
.EvaluateJavaScript(
176 element_query
+ '.offsetParent != null'):
177 # Only try to tap on visible elements (offsetParent != null)
178 action_runner
.ExecuteJavaScript(element_query
+ '.scrollIntoView()')
179 action_runner
.Wait(1) # wait for page to settle after scrolling
180 action_function(action_runner
, element_query
)
182 def TapWidget(self
, action_runner
, element_function
):
183 interaction
= action_runner
.BeginInteraction(
184 'Tap_Widget', is_smooth
=True)
185 action_runner
.TapElement(element_function
=element_function
)
186 action_runner
.Wait(1) # wait for e.g. animations on the widget
189 def SwipeWidget(self
, action_runner
, element_function
):
190 interaction
= action_runner
.BeginInteraction(
191 'Swipe_Widget', is_smooth
=True)
192 action_runner
.SwipeElement(element_function
=element_function
,
193 left_start_ratio
=0.75,
194 speed_in_pixels_per_second
=300)
198 class PolymerPageSet(page_set_module
.PageSet
):
201 super(PolymerPageSet
, self
).__init
__(
202 user_agent_type
='mobile',
203 archive_data_file
='data/polymer.json',
204 bucket
=page_set_module
.PUBLIC_BUCKET
)
206 self
.AddPage(PolymerCalculatorPage(self
))
207 self
.AddPage(PolymerShadowPage(self
))
209 # Polymer Sampler subpages that are interesting to tap / swipe elements on
216 # 'paper-radio-button',
217 #FIXME(wiltzius) Disabling x-shadow until this issue is fixed:
218 # https://github.com/Polymer/paper-shadow/issues/12
221 'paper-toggle-button',
223 for p
in TAPPABLE_PAGES
:
224 self
.AddPage(PolymerSampler(self
, p
))
226 # Polymer Sampler subpages that are interesting to scroll
228 'core-scroll-header-panel',
230 for p
in SCROLLABLE_PAGES
:
231 self
.AddPage(PolymerSampler(self
, p
, scrolling_page
=True))