blob: fc7818f3b123b40c6c964c9f5d05941eadefca1b [file] [log] [blame]
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +08001# 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
5'''Login with test account and display chart file using telemetry.'''
6
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +08007import argparse
8import contextlib
9import logging
10import os
11import signal
Kuo Jen Wei987b7eb2020-05-28 15:39:45 +080012import sys
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080013import time
14
Kuo Jen Weidb663ea2020-03-13 16:39:15 +080015# Set chart process preferred logging format before overridden by importing
16# common package.
17logging.basicConfig(
18 level=logging.DEBUG,
19 format='%(asctime)s - %(levelname)s - %(message)s')
20
21# This sets up import paths for autotest.
22import common
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080023from autotest_lib.client.bin import utils
Kuo Jen Weiedae0812020-11-30 12:32:07 +080024from autotest_lib.client.cros import constants
25from autotest_lib.client.cros.multimedia import display_facade_native
26from autotest_lib.client.cros.multimedia import facade_resource
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080027from autotest_lib.client.common_lib.cros import chrome
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080028
29
30@contextlib.contextmanager
31def set_display_brightness(display_level):
32 SET_BRIGHTNESS_CMD = 'backlight_tool --set_brightness_percent=%s'
33
34 original_display_level = utils.system_output(
35 'backlight_tool --get_brightness_percent')
36 logging.info('Save original display brightness %r '
37 'and fix display brightness to %r', original_display_level,
38 display_level)
39 utils.system(SET_BRIGHTNESS_CMD % display_level)
40 utils.system('stop powerd', ignore_status=True)
41 yield
42 logging.info('Restore display brightness %r', original_display_level)
43 utils.system('start powerd', ignore_status=True)
44 utils.system(SET_BRIGHTNESS_CMD % original_display_level)
45
46
47def display(filepath):
48 """Display chart with filepath on device by using telemetry."""
Kuo Jen Weiedae0812020-11-30 12:32:07 +080049 DISPLAY_LEVEL = 96.0
50 DISPLAY_ORIENTATION = 90
51
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080052 assert os.path.isfile(filepath), 'filepath %r not found.' % filepath
53 filepath = os.path.abspath(filepath)
54
55 logging.info('Setup SIGINT listener for stop displaying.')
56 displaying = [True]
57
58 def handler(signum, frame):
59 """Wait signal to clear running flag."""
60 if signum == signal.SIGINT:
61 displaying.pop()
62
63 signal.signal(signal.SIGINT, handler)
64
Kuo Jen Weiedae0812020-11-30 12:32:07 +080065 with chrome.Chrome(
66 extension_paths=[constants.DISPLAY_TEST_EXTENSION],
67 autotest_ext=True,
68 init_network_controller=True) as cr, set_display_brightness(
69 DISPLAY_LEVEL):
70 logging.info('Set fullscreen.')
71 facade = facade_resource.FacadeResource(cr)
72 display_facade = display_facade_native.DisplayFacadeNative(facade)
73 display_facade.set_fullscreen(True)
74
75 logging.info('Fix screen rotation %d.', DISPLAY_ORIENTATION)
76 internal_display_id = display_facade.get_internal_display_id()
77 display_facade.set_display_rotation(internal_display_id,
78 rotation=DISPLAY_ORIENTATION)
79
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080080 logging.info('Display chart file of path %r.', filepath)
Kuo Jen Wei377e99b2020-02-25 16:39:42 +080081 cr.browser.platform.SetHTTPServerDirectories(os.path.dirname(filepath))
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080082 tab = cr.browser.tabs[0]
Kuo Jen Wei377e99b2020-02-25 16:39:42 +080083 tab.Navigate(cr.browser.platform.http_server.UrlOf(filepath))
Kuo Jen Weiedae0812020-11-30 12:32:07 +080084 tab.WaitForDocumentReadyStateToBeComplete()
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080085
Kuo Jen Weidb663ea2020-03-13 16:39:15 +080086 logging.info('Chart is ready.')
Kuo Jen Wei987b7eb2020-05-28 15:39:45 +080087
88 # Flush the 'is ready' message for server test to sync with ready state.
89 sys.stdout.flush()
90 sys.stderr.flush()
91
Kuo Jen Wei7f00bb32018-11-01 15:35:24 +080092 while displaying:
93 time.sleep(1)
94
95
96if __name__ == '__main__':
97 argparser = argparse.ArgumentParser(
98 description='Display chart file on chrome by using telemetry.'
99 ' Send SIGINT or keyboard interrupt to stop displaying.')
100 argparser.add_argument('filepath', help='Path of displayed chart file.')
101
102 args = argparser.parse_args()
103 display(args.filepath)