Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2017 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 | |
| 5 import json | |
| 6 import logging | |
| 7 import os | |
| 8 import subprocess | |
| 9 import time | |
| 10 | |
| 11 import py_utils | |
| 12 | |
| 13 from telemetry.core import exceptions | |
| 14 from telemetry.value import histogram_util | |
| 15 | |
| 16 def RunRemoteCommand(dut_ip, cmd): | |
| 17 os.system('ssh root@%s %s' % (dut_ip, cmd)) | |
| 18 | |
| 19 def GetTabSwitchHistogram(tab): | |
| 20 histogram_name = 'MPArch.RWH_TabSwitchPaintDuration' | |
| 21 histogram_type = histogram_util.BROWSER_HISTOGRAM | |
| 22 return histogram_util.GetHistogram( | |
| 23 histogram_type, histogram_name, tab) | |
| 24 | |
| 25 def GetTabSwitchHistogramRetry(browser): | |
| 26 """Retry GetHistogram() as it may fail when a context was discarded.""" | |
| 27 while True: | |
| 28 try: | |
| 29 # Any tab can be used to get the TabSwitchPaintDuration histogram. | |
| 30 # At each retry, try to get valid context again. | |
| 31 return GetTabSwitchHistogram(browser.tabs[-1]) | |
| 32 except exceptions.DevtoolsTargetCrashException: | |
| 33 logging.info('GetHistogram: devtools context lost') | |
| 34 except KeyError: | |
| 35 logging.info('GetHistogram: devtools context lost') | |
| 36 except Exception as e: | |
| 37 logging.warning('GetHistogram: exception') | |
| 38 logging.warning(e) | |
| 39 time.sleep(0.5) | |
| 40 | |
| 41 def WaitTabSwitching(browser, prev_histogram): | |
| 42 """Checking the histogram to wait for tab switching completion.""" | |
| 43 def _IsDone(): | |
| 44 cur_histogram = GetTabSwitchHistogramRetry(browser) | |
| 45 | |
| 46 diff_histogram = histogram_util.SubtractHistogram(cur_histogram, | |
| 47 prev_histogram) | |
| 48 diff_histogram_count = json.loads(diff_histogram).get('count', 0) | |
| 49 return diff_histogram_count > 0 | |
| 50 | |
| 51 try: | |
| 52 py_utils.WaitFor(_IsDone, 10) | |
| 53 except py_utils.TimeoutException: | |
| 54 logging.warning('wait for histogram timed out') | |
| 55 | |
| 56 class KeyboardEmulator(object): | |
| 57 def __init__(self, dut_ip): | |
| 58 self._dut_ip = dut_ip | |
| 59 self._process = None | |
| 60 self._key_device_name = None | |
| 61 | |
| 62 def _SetupKeyDispatch(self, device_name): | |
| 63 """Uploading the script to send key to switch tabs.""" | |
| 64 cur_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 65 send_key_filename = cur_dir + '/data/send_key_tab_switch' | |
| 66 log_key_filename = cur_dir + '/data/log_key_tab_switch' | |
| 67 | |
| 68 send_key_content = '#!/bin/bash\n' | |
| 69 send_key_content += ('evemu-play --insert-slot0 ' + device_name + | |
| 70 ' < /home/root/log_key_tab_switch') | |
| 71 with open(send_key_filename, 'w') as f: | |
| 72 f.write(send_key_content) | |
| 73 os.chmod(send_key_filename, 0774) | |
| 74 | |
| 75 os.system('scp -q %s root@%s:/usr/local/tmp/' % | |
| 76 (log_key_filename, self._dut_ip)) | |
| 77 os.system('scp -q %s root@%s:/usr/local/tmp/' % | |
| 78 (send_key_filename, self._dut_ip)) | |
| 79 | |
| 80 def __enter__(self): | |
| 81 cmd = ['ssh', 'root@' + self._dut_ip, 'evemu-device', | |
| 82 '/usr/local/autotest/cros/input_playback/keyboard.prop'] | |
|
cywang
2017/05/23 02:56:57
LGTM in keyboard emulation, please make sure if th
vovoy
2017/05/23 03:24:09
Done. Added checks to through exception if the pro
| |
| 83 self._process = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
| 84 | |
| 85 # The evemu-device output format: | |
| 86 # Emulated Keyboard: /dev/input/event10 | |
| 87 output = self._process.stdout.readline() | |
| 88 self._key_device_name = output.split()[2] | |
| 89 self._SetupKeyDispatch(self._key_device_name) | |
| 90 return self | |
| 91 | |
| 92 def SwitchTab(self): | |
| 93 """Sending Ctrl-tab key to trigger tab switching.""" | |
| 94 RunRemoteCommand(self._dut_ip, '/usr/local/tmp/send_key_tab_switch') | |
| 95 | |
| 96 def __exit__(self, exc_type, exc_value, traceback): | |
| 97 # The remote process would live when the ssh process was terminated. | |
| 98 # Need to kill it explicitly. | |
| 99 self._process.kill() | |
| 100 RunRemoteCommand(self._dut_ip, 'pkill evemu-device') | |
| 101 | |
| 102 | |
| 103 def NoScreenOff(dut_ip): | |
| 104 """Set screen off and dim timer to 1 hour.""" | |
| 105 RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_off_delay=3600') | |
| 106 RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_dim_delay=3600') | |
| OLD | NEW |