1#
2# Copyright (C) 2017 The Android Open Source Project
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
17import unittest
18
19from vts.utils.python.hal import hal_service_name_utils
20
21
22class HalServiceNameUtilsUnitTest(unittest.TestCase):
23    """Tests for hal hidl gtest template"""
24
25    def testGetServiceInstancesFullCombinations(self):
26        """Test the function to get service instance combinations"""
27
28        comb1 = hal_service_name_utils.GetServiceInstancesCombinations([], {})
29        self.assertEquals(0, len(comb1))
30        comb2 = hal_service_name_utils.GetServiceInstancesCombinations(["s1"],
31                                                                       {})
32        self.assertEquals(0, len(comb2))
33        comb3 = hal_service_name_utils.GetServiceInstancesCombinations(
34            ["s1"], {"s1": ["n1"]})
35        self.assertEqual([["s1/n1"]], comb3)
36        comb4 = hal_service_name_utils.GetServiceInstancesCombinations(
37            ["s1"], {"s1": ["n1", "n2"]})
38        self.assertEqual([["s1/n1"], ["s1/n2"]], comb4)
39        comb5 = hal_service_name_utils.GetServiceInstancesCombinations(
40            ["s1", "s2"], {"s1": ["n1", "n2"]})
41        self.assertEqual([["s1/n1"], ["s1/n2"]], comb5)
42        comb6 = hal_service_name_utils.GetServiceInstancesCombinations(
43            ["s1", "s2"], {"s1": ["n1", "n2"],
44                           "s2": ["n3"]})
45        self.assertEqual([["s2/n3", "s1/n1"], ["s2/n3", "s1/n2"]], comb6)
46        comb7 = hal_service_name_utils.GetServiceInstancesCombinations(
47            ["s1", "s2"], {"s1": ["n1", "n2"],
48                           "s2": ["n3", "n4"]})
49        self.assertEqual([["s2/n3", "s1/n1"], ["s2/n3", "s1/n2"],
50                          ["s2/n4", "s1/n1"], ["s2/n4", "s1/n2"]], comb7)
51        comb8 = hal_service_name_utils.GetServiceInstancesCombinations(
52            ["s1", "s2"], {"s1": ["n1", "n2"],
53                           "s2": []})
54        self.assertEqual([["s1/n1"], ["s1/n2"]], comb8)
55        comb9 = hal_service_name_utils.GetServiceInstancesCombinations(
56            ["s1", "s2"], {"s1": [],
57                           "s2": ["n1", "n2"]})
58        self.assertEqual([["s2/n1"], ["s2/n2"]], comb9)
59
60    def testGetServiceInstancesNameMatchCombinations(self):
61        """Test the function to get service instance combinations"""
62
63        mode = hal_service_name_utils.CombMode.NAME_MATCH
64        comb1 = hal_service_name_utils.GetServiceInstancesCombinations([], {},
65                                                                       mode)
66        self.assertEquals(0, len(comb1))
67        comb2 = hal_service_name_utils.GetServiceInstancesCombinations(
68            ["s1"], {}, mode)
69        self.assertEquals(0, len(comb2))
70        comb3 = hal_service_name_utils.GetServiceInstancesCombinations(
71            ["s1"], {"s1": ["n1"]}, mode)
72        #self.assertEqual([["s1/n1"]], comb3)
73        comb4 = hal_service_name_utils.GetServiceInstancesCombinations(
74            ["s1"], {"s1": ["n1", "n2"]}, mode)
75        self.assertEqual([["s1/n1"], ["s1/n2"]], comb4)
76        comb5 = hal_service_name_utils.GetServiceInstancesCombinations(
77            ["s1", "s2"], {"s1": ["n1", "n2"]}, mode)
78        self.assertEqual(0, len(comb5))
79        comb6 = hal_service_name_utils.GetServiceInstancesCombinations(
80            ["s1", "s2"], {"s1": ["n1", "n2"],
81                           "s2": ["n3"]}, mode)
82        self.assertEqual(0, len(comb6))
83        comb7 = hal_service_name_utils.GetServiceInstancesCombinations(
84            ["s1", "s2"], {"s1": ["n1", "n2"],
85                           "s2": ["n3", "n4"]}, mode)
86        self.assertEqual(0, len(comb7))
87        comb8 = hal_service_name_utils.GetServiceInstancesCombinations(
88            ["s1", "s2"], {"s1": ["n1", "n2"],
89                           "s2": []}, mode)
90        self.assertEqual(0, len(comb8))
91        comb9 = hal_service_name_utils.GetServiceInstancesCombinations(
92            ["s1", "s2"], {"s1": [],
93                           "s2": ["n1", "n2"]}, mode)
94        self.assertEqual(0, len(comb9))
95        comb10 = hal_service_name_utils.GetServiceInstancesCombinations(
96            ["s1", "s2"], {"s1": ["n1"],
97                           "s2": ["n1", "n2"]}, mode)
98        self.assertEqual([["s1/n1", "s2/n1"]], comb10)
99        comb11 = hal_service_name_utils.GetServiceInstancesCombinations(
100            ["s1", "s2"], {"s1": ["n1", "n2"],
101                           "s2": ["n1", "n2"]}, mode)
102        self.assertEqual([["s1/n1", "s2/n1"], ["s1/n2", "s2/n2"]], comb11)
103
104
105if __name__ == '__main__':
106    unittest.main()
107