1# Copyright (c) 2012 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"""This module provides some utils for unit tests."""
6
7import os
8import sys
9
10
11def set_paths_for_tests():
12    """Set the project path and autotest input utility path for test modules."""
13    pwd = os.getcwd()
14    project = 'firmware_TouchMTB'
15    if os.path.basename(pwd) != project:
16        msg = 'Error: execute the unittests in the directory of %s!'
17        print msg % project
18        sys.exit(-1)
19    # Append the project path
20    sys.path.append(pwd)
21    # Append the autotest input utility path
22    sys.path.append(os.path.join(pwd, '../../bin/input/'))
23
24
25def get_tests_path():
26    """Get the path for unit tests."""
27    return os.path.join(os.getcwd(), 'tests')
28
29
30def get_tests_data_path():
31    """Get the data path for unit tests."""
32    return os.path.join(get_tests_path(), 'data')
33
34
35def get_device_description_path():
36    """Get the path for device description files."""
37    return os.path.join(get_tests_path(), 'device')
38
39
40def parse_tests_data(filename, gesture_dir=''):
41    """Parse the unit tests data."""
42    import mtb
43    filepath = os.path.join(get_tests_data_path(), gesture_dir, filename)
44    with open(filepath) as test_file:
45        return mtb.MtbParser().parse(test_file)
46
47
48def create_mocked_devices():
49    """Create mocked devices of specified platforms."""
50    from firmware_constants import PLATFORM
51    from touch_device import TouchDevice
52
53    description_path = get_device_description_path()
54    mocked_device = {}
55    for platform in PLATFORM.LIST:
56        description_filename = '%s.touchpad' % platform
57        description_filepath = os.path.join(description_path,
58                                            description_filename)
59        if not os.path.isfile(description_filepath):
60            mocked_device[platform] = None
61            warn_msg = 'Warning: device description file %s does not exist'
62            print warn_msg % description_filepath
63            continue
64        mocked_device[platform] = TouchDevice(
65                device_node='/dev/null',
66                device_description_file=description_filepath)
67    return mocked_device
68
69
70set_paths_for_tests()
71