1# Lint as: python2, python3 2# Copyright 2016 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import logging 7import re 8import time 9 10from autotest_lib.client.bin import test 11from autotest_lib.client.bin import utils 12from autotest_lib.client.common_lib import error 13from autotest_lib.client.common_lib.cros import chrome 14from autotest_lib.client.cros.input_playback import input_playback 15from autotest_lib.client.cros.audio import cras_utils 16from telemetry.core import exceptions 17 18 19class platform_InputVolume(test.test): 20 """Tests if device suspends using shortcut keys.""" 21 version = 1 22 _WAIT = 15 23 MUTE_STATUS = 'Muted' 24 CTC_GREP_FOR = "cras_test_client --dump_server_info | grep " 25 26 def warmup(self): 27 """Test setup.""" 28 # Emulate keyboard. 29 # See input_playback. The keyboard is used to play back shortcuts. 30 self._player = input_playback.InputPlayback() 31 self._player.emulate(input_type='keyboard') 32 self._player.find_connected_inputs() 33 34 def test_volume_down(self, volume): 35 """ 36 Use keyboard shortcut to test Volume Down (F9) key. 37 38 @param volume: expected volume. 39 40 @raises: error.TestFail if system volume did not decrease or is muted. 41 42 """ 43 self._player.blocking_playback_of_default_file( 44 input_type='keyboard', filename='keyboard_f9') 45 # If expected volume is 0, we should be muted. 46 if volume == 0 and not self.is_muted(): 47 raise error.TestFail("Volume should be muted.") 48 sys_volume = self.get_active_volume() 49 if sys_volume != volume: 50 raise error.TestFail("Volume did not decrease: %s" % sys_volume) 51 52 def test_volume_up(self, volume): 53 """ 54 Use keyboard shortcut to test Volume Up (F10) key. 55 56 @param volume: expected volume 57 58 @raises: error.TestFail if system volume muted or did not increase. 59 60 """ 61 self._player.blocking_playback_of_default_file( 62 input_type='keyboard', filename='keyboard_f10') 63 if self.is_muted(): 64 raise error.TestFail("Volume is muted when it shouldn't be.") 65 sys_volume = self.get_active_volume() 66 if sys_volume != volume: 67 raise error.TestFail("Volume did not increase: %s" % sys_volume) 68 69 def test_mute(self, volume): 70 """Use keyboard shortcut to test Mute (F8) key. 71 72 @param volume: expected volume 73 74 @raises: error.TestFail if system volume not muted. 75 76 """ 77 self._player.blocking_playback_of_default_file( 78 input_type='keyboard', filename='keyboard_f8') 79 sys_volume = self.get_active_volume() 80 if not self.is_muted(): 81 raise error.TestFail("Volume not muted.") 82 if sys_volume != volume: 83 raise error.TestFail("Volume changed while mute: %s" % sys_volume) 84 85 def get_active_volume(self): 86 """ 87 Get current active node volume (0-100). 88 89 @returns: current volume on active node. 90 """ 91 return cras_utils.get_active_node_volume() 92 93 def is_muted(self): 94 """ 95 Returns mute status of system. 96 97 @returns: True if system muted, False if not 98 99 """ 100 output = utils.system_output(self.CTC_GREP_FOR + 'muted') 101 muted = output.split(':')[-1].strip() 102 return muted == self.MUTE_STATUS 103 104 def run_once(self): 105 """ 106 Open browser, and affect volume using mute, up, and down functions. 107 108 """ 109 with chrome.Chrome(disable_default_apps=False): 110 current_volume = self.get_active_volume() 111 self.test_volume_down(current_volume - 4) 112 self.test_volume_up(current_volume) 113 self.test_mute(current_volume) 114 self.test_volume_up(current_volume) 115 116 def cleanup(self): 117 """Test cleanup.""" 118 self._player.close() 119