• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright 2020 The Chromium 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 time
8
9from autotest_lib.client.bin import test
10from autotest_lib.client.bin import utils
11from autotest_lib.client.common_lib import error
12from autotest_lib.client.common_lib.cros import chrome
13from autotest_lib.client.cros import constants as cros_constants
14from autotest_lib.client.cros.audio import cras_utils
15from autotest_lib.client.cros.multimedia import audio_facade
16
17class audio_CrasGetNodes(test.test):
18    """Verifies dbus GetNodes API of CRAS."""
19    version = 1
20
21    ALOOP_CRAS_NODE_TYPE = 'ALSA_LOOPBACK'
22    ALOOP_MODULE_NAME = 'snd-aloop'
23
24    def run_once(self):
25        """Entry point of this test."""
26        # Check CRAS server is alive. If not, restart it and wait a second to
27        # get server ready.
28        if utils.get_service_pid('cras') == 0:
29            logging.debug('CRAS server is down. Restart it.')
30            utils.start_service('cras', ignore_status=True)
31            time.sleep(1)
32
33        utils.load_module(self.ALOOP_MODULE_NAME)
34
35        try:
36            with chrome.Chrome(
37                    extension_paths=[cros_constants.AUDIO_TEST_EXTENSION],
38                    autotest_ext=True) as cr:
39                audio_facade_local = audio_facade.AudioFacadeLocal(cr)
40                audio_facade_local.set_chrome_active_node_type(
41                        self.ALOOP_CRAS_NODE_TYPE, self.ALOOP_CRAS_NODE_TYPE)
42
43            # Checks active output and input node types are correct.
44            active_output_types, active_input_types = (
45                cras_utils.get_selected_node_types())
46            if len(active_output_types) != 1:
47                raise error.TestFail(
48                    'Length of active output types is not 1, got: %d',
49                    len(active_output_types))
50            if active_output_types[0] != self.ALOOP_CRAS_NODE_TYPE:
51                raise error.TestFail(
52                    'Active output device is not %s, got: %d',
53                    self.ALOOP_CRAS_NODE_TYPE, active_output_types[0])
54            if len(active_input_types) != 1:
55                raise error.TestFail(
56                    'Length of active input types is not 1, got: %d',
57                    len(active_input_types))
58            if active_input_types[0] != self.ALOOP_CRAS_NODE_TYPE:
59                raise error.TestFail(
60                    'Active input device is not %s, got: %d',
61                    self.ALOOP_CRAS_NODE_TYPE, active_input_types[0])
62
63            # Checks active node volume is correct.
64            for target_volume in [25, 75]:
65                cras_utils.set_selected_output_node_volume(target_volume)
66                volume = cras_utils.get_active_node_volume()
67                if volume != target_volume:
68                    raise error.TestFail('Volume is as expected: %d, got: %d',
69                                         target_volume, volume)
70
71        finally:
72            utils.stop_service('cras', ignore_status=True)
73            utils.unload_module(self.ALOOP_MODULE_NAME)
74            utils.start_service('cras', ignore_status=True)
75