• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (C) 2017, ARM Limited and contributors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import re
19import os
20import logging
21
22from time import sleep
23
24from android import Screen, System, Workload
25
26class ViewerWorkload(Workload):
27    """
28    Android generic Viewer workload
29
30    This workload will use a given URI and will let Android pick the best
31    application for viewing the item designated by that URI. For instance,
32    a Youtube video URL would lead to opening the Youtube App if Google
33    Services are available, if not the default web browser will be used
34    to load the Youtube page.
35
36    Three methods are available for customizing the workload in a subclass,
37    see their respective docstring for more details. At least interact() must
38    be implemented.
39
40    Here's a minimalist example use case of this class, that loads a gif
41    and keeps it on display for 10 seconds:
42
43    definition
44    -----------
45    class Example(ViewerWorkload):
46        def interact(self):
47            sleep(10)
48
49    execution
50    ----------
51    wload = Workload.getInstance(te, 'Example')
52    wload.run(out_dir=te.res_dir,
53        uri="https://media.giphy.com/media/XIqCQx02E1U9W/giphy.gif")
54    """
55
56    # Let the system pick the best package
57    package = ''
58
59    def __init__(self, test_env):
60        super(ViewerWorkload, self).__init__(test_env)
61
62        # Set of output data reported by the viewer
63        self.db_file = None
64
65    def pre_interact(self):
66        """
67        This method will be called right before tracing starts, but after the
68        item-viewing app has been launched. This can be useful to configure
69        some app-specific settings, to press buttons, start a video, etc.
70        """
71        pass
72
73    def interact(self):
74        """
75        This method will be called right after the tracing starts. Tracing will
76        continue as long as this method is running, so it can be tailored to
77        your workload requirements. It could simply be sleeping for x seconds,
78        or monitoring logcat to wait for a certain event, or issuing input
79        commands to swipe around a gallery/web page/app, etc.
80        """
81        raise NotImplemented("interact() must be implemented")
82
83    def post_interact(self):
84        """
85        This method will be called right after tracing stops, but before the
86        item-viewing app has been closed. This can be useful to dump some
87        app-specific statistics.
88        """
89        pass
90
91    def run(self, out_dir, uri, portrait=True, collect=''):
92        """
93        Run viewer workload
94
95        :param out_dir: Path to experiment directory where to store results.
96        :type out_dir: str
97
98        :param uri: The URI of the item to display
99        :type location_search: str
100
101        :param portrait: If True, display mode will be set to 'portrait' prior
102            to viewing the item. If False, 'landscape' mode will be set.
103
104        :param collect: Specifies what to collect. Possible values:
105            - 'energy'
106            - 'systrace'
107            - 'ftrace'
108            - any combination of the above
109        :type collect: list(str)
110        """
111
112        # Keep track of mandatory parameters
113        self.out_dir = out_dir
114        self.collect = collect
115
116        # Set min brightness
117        Screen.set_brightness(self._target, auto=False, percent=0)
118        # Unlock device screen (assume no password required)
119        Screen.unlock(self._target)
120
121        # Force screen in requested orientation
122        Screen.set_orientation(self._target, portrait=portrait)
123
124        System.gfxinfo_reset(self._target, self.package)
125        # Wait for gfxinfo reset to be completed
126        sleep(1)
127
128        # Open the requested uri
129        System.view_uri(self._target, uri)
130
131        self.pre_interact()
132        self.tracingStart()
133
134        self.interact()
135
136        self.tracingStop()
137        self.post_interact()
138
139        # Get frame stats
140        self.db_file = os.path.join(out_dir, "framestats.txt")
141        System.gfxinfo_get(self._target, self.package, self.db_file)
142
143        # Go back to home screen
144        System.home(self._target)
145
146        # Set brightness back to auto
147        Screen.set_brightness(self._target, auto=True)
148
149        # Switch back to screen auto rotation
150        Screen.set_orientation(self._target, auto=True)
151
152# vim :set tabstop=4 shiftwidth=4 expandtab
153