1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16
17import random
18from acts.base_test import BaseTestClass
19
20CONSERVATIVE_MAX_ATTEN_VALUE = 10
21MIN_ATTEN_VALUE = 0
22
23
24class AttenuatorSanityTest(BaseTestClass):
25    def __init__(self, controllers):
26        BaseTestClass.__init__(self, controllers)
27        self.tests = ("test_attenuator_validation",
28                      "test_attenuator_get_max_value", )
29        self.number_of_iteration = 2
30
31    def test_attenuator_validation(self):
32        """Validate attenuator set and get APIs works fine.
33        """
34        for atten in self.attenuators:
35            self.log.info("Attenuator: {}".format(atten))
36            try:
37                atten_max_value = atten.get_max_atten()
38            except ValueError as e:
39                self.log.error(e)
40                self.log.info("Using conservative max value.")
41                atten_max_value = CONSERVATIVE_MAX_ATTEN_VALUE
42
43            atten_value_list = [MIN_ATTEN_VALUE, atten_max_value]
44            for i in range(0, self.number_of_iteration):
45                atten_value_list.append(
46                    int(random.uniform(0, atten_max_value)))
47
48            for atten_val in atten_value_list:
49                self.log.info("Set atten to {}".format(atten_val))
50                atten.set_atten(atten_val)
51                current_atten = int(atten.get_atten())
52                self.log.info("Current atten = {}".format(current_atten))
53                assert atten_val == current_atten, "Setting attenuator failed."
54
55        return True
56
57    def test_attenuator_get_max_value(self):
58        """Validate attenuator get_max_atten APIs works fine.
59        """
60        for atten in self.attenuators:
61            try:
62                atten_max_value = atten.get_max_atten()
63            except ValueError as e:
64                self.log.error(e)
65                return False
66        return True
67