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