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 return os.system('ssh root@%s %s' % (dut_ip, cmd)) | |
| 18 | |
|
deanliao_goog
2017/05/23 08:54:25
Ah, Python style rule said that top level methods
vovoy
2017/05/24 09:17:21
Done.
| |
| 19 def GetTabSwitchHistogram(tab): | |
|
deanliao_goog
2017/05/23 08:54:25
Please add description including Args: and Returns
vovoy
2017/05/24 09:17:21
Done.
| |
| 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.""" | |
|
deanliao_goog
2017/05/23 08:54:26
Retries...
Same below.
vovoy
2017/05/24 09:17:21
Done.
| |
| 27 while True: | |
|
deanliao_goog
2017/05/23 08:54:26
So it is unlikely that GetTabSwitchHistogram faile
vovoy
2017/05/24 09:17:21
Done. Using py_utils.WaitFor().
| |
| 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.""" | |
|
deanliao_goog
2017/05/23 08:54:25
Waits for tab switching complete.
It is done by c
vovoy
2017/05/24 09:17:21
Done.
| |
| 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') | |
|
deanliao_goog
2017/05/23 08:54:25
Wait
vovoy
2017/05/24 09:17:21
Done.
| |
| 55 | |
| 56 class KeyboardEmulator(object): | |
|
deanliao_goog
2017/05/23 08:54:25
class description.
vovoy
2017/05/24 09:17:21
Done.
| |
| 57 def __init__(self, dut_ip): | |
| 58 self._dut_ip = dut_ip | |
| 59 self._process = None | |
| 60 | |
| 61 def _SetupKeyDispatch(self, device_name): | |
| 62 """Uploading the script to send key to switch tabs.""" | |
| 63 cur_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 64 send_key_filename = cur_dir + '/data/send_key_tab_switch' | |
|
deanliao_goog
2017/05/23 08:54:26
Though it is okay, but using
os.path.join(cur_dir
vovoy
2017/05/24 09:17:21
Done.
| |
| 65 log_key_filename = cur_dir + '/data/log_key_tab_switch' | |
| 66 | |
| 67 send_key_content = '#!/bin/bash\n' | |
| 68 send_key_content += ('evemu-play --insert-slot0 ' + device_name + | |
|
deanliao_goog
2017/05/23 08:54:25
How about just
with open(...) as f:
f.write('#!
vovoy
2017/05/24 09:17:21
Done. It shall be OK to direct /home/root/log_key_
| |
| 69 ' < /home/root/log_key_tab_switch') | |
| 70 with open(send_key_filename, 'w') as f: | |
| 71 f.write(send_key_content) | |
| 72 os.chmod(send_key_filename, 0774) | |
| 73 | |
| 74 os.system('scp -q %s root@%s:/usr/local/tmp/' % | |
| 75 (log_key_filename, self._dut_ip)) | |
| 76 os.system('scp -q %s root@%s:/usr/local/tmp/' % | |
| 77 (send_key_filename, self._dut_ip)) | |
| 78 | |
| 79 def __enter__(self): | |
| 80 kbd_prop_filename = '/usr/local/autotest/cros/input_playback/keyboard.prop' | |
| 81 | |
| 82 ret = RunRemoteCommand(self._dut_ip, 'test -e %s' % kbd_prop_filename) | |
| 83 if ret is not 0: | |
|
deanliao_goog
2017/05/23 08:54:25
if ret != 0:
vovoy
2017/05/24 09:17:21
Done.
I think 0 (integer literal) is singleton in
deanliao_goog
2017/05/24 10:10:52
When handling integers, implicit false may involve
| |
| 84 raise RuntimeError('Keyboard property file does not exist.') | |
| 85 | |
| 86 cmd = ['ssh', 'root@' + self._dut_ip, 'evemu-device', kbd_prop_filename] | |
|
deanliao_goog
2017/05/23 08:54:25
FYI, looks like
'root@' + self._dut_ip
occurs thr
vovoy
2017/05/24 09:17:21
Done.
| |
| 87 self._process = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
| 88 | |
| 89 # The evemu-device output format: | |
| 90 # Emulated Keyboard: /dev/input/event10 | |
| 91 output = self._process.stdout.readline() | |
| 92 if not output.startswith('Emulated Keyboard:'): | |
| 93 raise RuntimeError('Keyboard emulation failed.') | |
| 94 key_device_name = output.split()[2] | |
| 95 self._SetupKeyDispatch(key_device_name) | |
| 96 return self | |
| 97 | |
| 98 def SwitchTab(self): | |
| 99 """Sending Ctrl-tab key to trigger tab switching.""" | |
| 100 RunRemoteCommand(self._dut_ip, '/usr/local/tmp/send_key_tab_switch') | |
| 101 | |
| 102 def __exit__(self, exc_type, exc_value, traceback): | |
| 103 # The remote process would live when the ssh process was terminated. | |
|
deanliao_goog
2017/05/23 08:54:26
Since the remote process stays alive when SSH is t
vovoy
2017/05/24 09:17:21
Done.
| |
| 104 # Need to kill it explicitly. | |
| 105 self._process.kill() | |
| 106 RunRemoteCommand(self._dut_ip, 'pkill evemu-device') | |
| 107 | |
| 108 | |
| 109 def NoScreenOff(dut_ip): | |
| 110 """Set screen off and dim timer to 1 hour.""" | |
|
deanliao_goog
2017/05/23 08:54:26
Sets screen always on for one hour.
vovoy
2017/05/24 09:17:21
Done.
| |
| 111 RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_off_delay=3600') | |
| 112 RunRemoteCommand(dut_ip, 'set_power_policy --ac_screen_dim_delay=3600') | |
| OLD | NEW |