1# Copyright 2017 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"""An interface to access the local input facade.""" 6 7 8import json 9import logging 10 11from autotest_lib.client.bin.input import input_event_recorder 12from autotest_lib.client.cros.input_playback import input_playback 13from autotest_lib.client.common_lib import error 14from autotest_lib.client.cros.graphics import graphics_utils 15 16 17class InputFacadeNativeError(Exception): 18 """Error in InputFacadeNative.""" 19 pass 20 21 22class InputFacadeNative(object): 23 """Facade to access the record input events.""" 24 25 def __init__(self): 26 """Initializes the input facade.""" 27 self.recorder = None 28 29 def initialize_input_playback(self, input_type='keyboard', property_file=None): 30 """Initialize for input events simulation. 31 32 @param input_type: the name of the input device. 33 @param property_file: Property file of device to be emulated. 34 """ 35 self._player = input_playback.InputPlayback() 36 self._player.emulate(input_type=input_type, property_file=property_file) 37 self._player.find_connected_inputs() 38 39 def initialize_input_recorder(self, device_name): 40 """Initialize an input event recorder object. 41 42 @param device_name: the name of the input device to record. 43 44 """ 45 self.recorder = input_event_recorder.InputEventRecorder(device_name) 46 logging.info('input event device: %s (%s)', 47 self.recorder.device_name, self.recorder.device_node) 48 49 50 def clear_input_events(self): 51 """Clear the event list.""" 52 if self.recorder is None: 53 raise error.TestError('input facade: input device name not given') 54 self.recorder.clear_events() 55 56 57 def start_input_recorder(self): 58 """Start the recording thread.""" 59 if self.recorder is None: 60 raise error.TestError('input facade: input device name not given') 61 self.recorder.start() 62 63 64 def stop_input_recorder(self): 65 """Stop the recording thread.""" 66 if self.recorder is None: 67 raise error.TestError('input facade: input device name not given') 68 self.recorder.stop() 69 70 71 def get_input_events(self): 72 """Get the bluetooth device input events. 73 74 @returns: the recorded input events. 75 76 """ 77 if self.recorder is None: 78 raise error.TestError('input facade: input device name not given') 79 events = self.recorder.get_events() 80 return json.dumps(events) 81 82 83 def press_keys(self, key_list): 84 """ Simulating key press 85 86 @param key_list: A list of key strings, e.g. ['LEFTCTRL', 'F4'] 87 """ 88 graphics_utils.press_keys(key_list) 89 90 91 def blocking_playback_of_default_file(self, input_type, filename): 92 """Simulate events 93 94 @param input_type: input device name 95 @param filename: input events 96 """ 97 self._player.blocking_playback_of_default_file(input_type=input_type, 98 filename=filename) 99