aboutsummaryrefslogtreecommitdiff
path: root/client/common_lib/cros/manual/cfm_helper_unittest.py
blob: 4e50e6be0d60cdf44ed0d4bf44634abe3ac77110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file
"""Tests for cfm_helper.py."""

import unittest

from autotest_lib.client.common_lib.cros.manual import cfm_helper
from autotest_lib.client.common_lib.cros.manual import get_usb_devices

SPEAKERS = 'speakers'
CAMERAS = 'cameras'
DISPLAY_MIMO = 'display_mimo'
CONTROLLER_MIMO = 'controller_mimo'
DEVICE_TYPES = (SPEAKERS, CAMERAS, DISPLAY_MIMO, CONTROLLER_MIMO)


class TestExtractPeripherals(unittest.TestCase):
    """Test cfm_helper.extract_peripherals()"""

    def create_mock_device_getter(self, device_list):
        """Mock a function to take usb_data and return device_list"""

        def mock_device_getter(usb_data):
            """Return the specified device_list, ignoring usb_data."""
            return device_list

        return mock_device_getter

    def setUp(self):
        """
        Mock the various get_devices functions so that each one returns a
        key-value pair.
        In extract_peripherals(), the key represents the pid_vid, and the value
        represents the device_count.
        For these testing purposes, we use the device_type ('cameras', etc.)
        as the key, and a number 1-4 as the device_count.
        (If we used a device_count of 0 it would be ignored; hence, 1-4.)

        """
        self.original_funcs = {}
        for i in range(len(DEVICE_TYPES)):
            device_type = DEVICE_TYPES[i]
            mock = self.create_mock_device_getter({device_type: i + 1})
            setattr(cfm_helper.get_usb_devices, 'get_%s' % device_type, mock)

    def runTest(self):
        """Verify that all 4 peripheral-types are extracted."""
        peripherals = cfm_helper.extract_peripherals_for_cfm(None)
        self.assertEqual(len(peripherals), 4)
        for i in range(len(DEVICE_TYPES)):
            device_type = DEVICE_TYPES[i]
            self.assertEqual(peripherals[device_type], i + 1)

    def tearDown(self):
        """Restore the original functions, for the sake of other tests."""
        for device_type in DEVICE_TYPES:
            original_func = getattr(get_usb_devices, 'get_%s' % device_type)
            setattr(cfm_helper.get_usb_devices, 'get_%s' % device_type,
                    original_func)


if __name__ == '__main__':
    unittest.main()