|
| 1 | +# Copyright (c) 2018-2021, Arm Limited and affiliates. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import unittest |
| 17 | +import mock |
| 18 | + |
| 19 | +from mbed_os_tools.test.host_tests_conn_proxy.conn_primitive_serial import SerialConnectorPrimitive |
| 20 | +from mbed_os_tools.test.host_tests_conn_proxy.conn_primitive import ConnectorPrimitiveException |
| 21 | + |
| 22 | +@mock.patch("mbed_os_tools.test.host_tests_conn_proxy.conn_primitive_serial.Serial") |
| 23 | +@mock.patch("mbed_os_tools.test.host_tests_plugins.host_test_plugins.detect") |
| 24 | +class ConnPrimitiveSerialTestCase(unittest.TestCase): |
| 25 | + def test_provided_serial_port_used_with_target_id(self, mock_detect, mock_serial): |
| 26 | + platform_name = "irrelevant" |
| 27 | + target_id = "1234" |
| 28 | + port = "COM256" |
| 29 | + baudrate = "9600" |
| 30 | + |
| 31 | + # The mock list_mbeds() call needs to return a list of dictionaries, |
| 32 | + # and each dictionary must have a "serial_port", or else the |
| 33 | + # check_serial_port_ready() function we are testing will sleep waiting |
| 34 | + # for the serial port to become ready. |
| 35 | + mock_detect.create().list_mbeds.return_value = [ |
| 36 | + {"target_id": target_id, |
| 37 | + "serial_port": port, |
| 38 | + "platform_name": platform_name}, |
| 39 | + ] |
| 40 | + |
| 41 | + # Set skip_reset to avoid the use of a physical serial port. |
| 42 | + config = { |
| 43 | + "port": port, |
| 44 | + "baudrate": baudrate, |
| 45 | + "image_path": "test.bin", |
| 46 | + "platform_name": "kaysixtyfoureff", |
| 47 | + "target_id": "9900", |
| 48 | + "skip_reset": True, |
| 49 | + } |
| 50 | + connector = SerialConnectorPrimitive("SERI", port, baudrate, config=config) |
| 51 | + |
| 52 | + mock_detect.create().list_mbeds.assert_not_called() |
| 53 | + |
| 54 | + def test_discovers_serial_port_with_target_id(self, mock_detect, mock_serial): |
| 55 | + platform_name = "kaysixtyfoureff" |
| 56 | + target_id = "9900" |
| 57 | + port = "COM256" |
| 58 | + baudrate = "9600" |
| 59 | + |
| 60 | + mock_detect.create().list_mbeds.return_value = [ |
| 61 | + {"target_id": target_id, |
| 62 | + "serial_port": port, |
| 63 | + "platform_name": platform_name}, |
| 64 | + ] |
| 65 | + |
| 66 | + # Set skip_reset to avoid the use of a physical serial port. Don't pass |
| 67 | + # in a port, so that auto-detection based on target_id will find the |
| 68 | + # port for us (using our mock list_mbeds data). |
| 69 | + config = { |
| 70 | + "port": None, |
| 71 | + "baudrate": baudrate, |
| 72 | + "image_path": "test.bin", |
| 73 | + "platform_name": platform_name, |
| 74 | + "target_id": target_id, |
| 75 | + "skip_reset": True, |
| 76 | + } |
| 77 | + try: |
| 78 | + connector = SerialConnectorPrimitive("SERI", None, baudrate, config=config) |
| 79 | + except ConnectorPrimitiveException: |
| 80 | + # lol bad |
| 81 | + pass |
| 82 | + |
| 83 | + mock_detect.create().list_mbeds.assert_called_once() |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + unittest.main() |
0 commit comments