1# Copyright 2018 The Chromium OS 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
5import functools
6import logging
7import numpy
8import time
9
10from autotest_lib.client.bin import fps_meter
11from autotest_lib.client.common_lib.cros import chrome
12from autotest_lib.client.cros import touch_playback_test_base
13from telemetry.internal.actions import scroll
14
15import py_utils
16
17""" List of URLs that will be used to test users gestures on. """
18_LIST_OF_URLS = ["https://www.youtube.com", "https://www.cnn.com",
19    "https://slashdot.org/"]
20
21""" Scroll bar's moving speed. """
22_SCROLL_SPEED = 1500
23
24""" The total distance that the scroll bar moved. """
25_SCROLL_DISTANCE = 3000
26
27""" Separator used in fps_meter for each VSync. """
28_SEPARATOR = " "
29
30class platform_ScrollTest(touch_playback_test_base.touch_playback_test_base):
31    """Scroll up and down pressure test."""
32    version = 1
33
34    def run_once(self):
35        """Runs the test once."""
36        perf_results = {}
37
38        def record_fps_info(fps_data, fps_info):
39            ''' record the fps info from |fps_meter| '''
40            frame_info, frame_times = fps_info
41            frame_info_str = ''.join(frame_info)
42            fps_count = sum(
43                map(int, frame_info_str.replace(_SEPARATOR, "")))
44            fps_data.append(fps_count)
45
46        fps_data = []
47        fps = fps_meter.FPSMeter(functools.partial(record_fps_info, fps_data))
48        with chrome.Chrome(init_network_controller=True) as cr:
49            for url in _LIST_OF_URLS:
50                tab = cr.browser.tabs.New()
51                tab.Navigate(url)
52                try:
53                    tab.WaitForDocumentReadyStateToBeComplete(timeout=15)
54                except py_utils.TimeoutException:
55                    logging.warning('Time out during loading url ' + url)
56
57                for x in range(0, 3):
58                    page_scroll = scroll.ScrollAction(
59                        speed_in_pixels_per_second=_SCROLL_SPEED,
60                        distance=_SCROLL_DISTANCE)
61                    cr.browser.platform.SetHTTPServerDirectories(self.bindir)
62                    page_scroll.WillRunAction(tab)
63                    fps.start()
64                    page_scroll.RunAction(tab)
65                    fps.stop()
66                    page_scroll = scroll.ScrollAction(
67                        direction="up",
68                        speed_in_pixels_per_second=_SCROLL_SPEED,
69                        distance=_SCROLL_DISTANCE)
70                    page_scroll.WillRunAction(tab)
71                    fps.start()
72                    page_scroll.RunAction(tab)
73                    fps.stop()
74                time.sleep(1)
75        value = getattr(numpy, "mean")(fps_data)
76
77        self.output_perf_value(description="fps average",
78                                value=value,
79                               units='frame per second',
80                               higher_is_better=True)
81