1#!/usr/bin/env python3
2#
3# Copyright (C) 2018 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 itertools
18
19from acts_contrib.test_utils.bt.bt_test_utils import enable_bluetooth
20from acts_contrib.test_utils.coex.CoexPerformanceBaseTest import CoexPerformanceBaseTest
21from acts_contrib.test_utils.coex.coex_test_utils import perform_classic_discovery
22
23
24class CoexBasicPerformanceTest(CoexPerformanceBaseTest):
25
26    def __init__(self, controllers):
27        super().__init__(controllers)
28        req_params = [
29            # A dict containing:
30            #     protocol: A list containing TCP/UDP. Ex: protocol: ['tcp'].
31            #     stream: A list containing ul/dl. Ex: stream: ['ul']
32            'standalone_params'
33        ]
34        self.unpack_userparams(req_params)
35        self.tests = self.generated_test_cases(['bt_on', 'perform_discovery'])
36
37    def perform_discovery(self):
38        """ Starts iperf client on host machine and bluetooth discovery
39        simultaneously.
40
41        Returns:
42            True if successful, False otherwise.
43        """
44        tasks = [(perform_classic_discovery,
45                  (self.pri_ad, self.iperf['duration'], self.json_file,
46                   self.dev_list)),
47                 (self.run_iperf_and_get_result, ())]
48        return self.set_attenuation_and_run_iperf(tasks)
49
50    def bt_on(self):
51        """ Turns on bluetooth and runs iperf.
52
53        Returns:
54            True on success, False otherwise.
55        """
56        if not enable_bluetooth(self.pri_ad.droid, self.pri_ad.ed):
57            return False
58        return self.set_attenuation_and_run_iperf()
59
60    def generated_test_cases(self, test_types):
61        """ Auto generates tests for basic coex tests. """
62        test_cases = []
63        for protocol, stream, test_type in itertools.product(
64                self.standalone_params['protocol'],
65                self.standalone_params['stream'], test_types):
66
67            test_name = 'test_performance_with_{}_{}_{}'.format(
68                test_type, protocol, stream)
69
70            test_function = getattr(self, test_type)
71            setattr(self, test_name, test_function)
72            test_cases.append(test_name)
73        return test_cases
74