markdr | 2e25b4d | 2017-09-28 14:32:37 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright 2017 - The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | import unittest |
| 18 | from acts.libs.ota.ota_tools import ota_tool_factory |
| 19 | |
| 20 | |
| 21 | class MockOtaTool(object): |
| 22 | def __init__(self, command): |
| 23 | self.command = command |
| 24 | |
| 25 | |
| 26 | class OtaToolFactoryTests(unittest.TestCase): |
| 27 | def setUp(self): |
| 28 | ota_tool_factory._constructed_tools = {} |
| 29 | |
| 30 | def test_create_constructor_exists(self): |
| 31 | ota_tool_factory._CONSTRUCTORS = { |
| 32 | MockOtaTool.__name__: lambda command: MockOtaTool(command), |
| 33 | } |
| 34 | ret = ota_tool_factory.create(MockOtaTool.__name__, 'command') |
| 35 | self.assertEqual(type(ret), MockOtaTool) |
| 36 | self.assertTrue(ret in ota_tool_factory._constructed_tools.values()) |
| 37 | |
| 38 | def test_create_not_in_constructors(self): |
| 39 | ota_tool_factory._CONSTRUCTORS = {} |
| 40 | with self.assertRaises(KeyError): |
| 41 | ota_tool_factory.create(MockOtaTool.__name__, 'command') |
| 42 | |
| 43 | def test_create_returns_cached_tool(self): |
| 44 | ota_tool_factory._CONSTRUCTORS = { |
| 45 | MockOtaTool.__name__: lambda command: MockOtaTool(command), |
| 46 | } |
| 47 | ret_a = ota_tool_factory.create(MockOtaTool.__name__, 'command') |
| 48 | ret_b = ota_tool_factory.create(MockOtaTool.__name__, 'command') |
| 49 | self.assertEqual(ret_a, ret_b) |