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"""
17Test script to test if Bluetooth will reboot successfully
18if it is killed.
19"""
20
21import re
22import time
23from acts.test_decorators import test_tracker_info
24from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
25
26
27class BtKillProcessTest(BluetoothBaseTest):
28    def setup_class(self):
29        super().setup_class()
30        self.dut = self.android_devices[0]
31
32    def _get_bt_pid(self):
33        process_grep_string = "com.android.bluetooth"
34        awk = "awk '{print $2}'"
35        pid = self.dut.adb.shell("ps | grep com.android.bluetooth")
36        if not pid:
37            return None
38        return (re.findall("\d+\W", pid)[0])
39
40    def _is_bt_process_running(self):
41        if self._get_bt_pid():
42            return True
43        else:
44            return False
45
46    @BluetoothBaseTest.bt_test_wrap
47    @test_tracker_info(uuid='c51186e9-4ba8-406c-b609-ea552868e4c9')
48    def test_kill_process(self):
49        """Test that a killed Bluetooth process restarts
50
51        If the Bluetooth process is killed out of band with kill -9
52        the process should be automatically restarted by Android.
53
54        Steps:
55        1. Verify Bluetooth process is running
56        2. Kill the Bluetooth process with kill -9
57        3. Verify the process is restarted
58
59        Expected Result:
60        Bluetooth should be restarted automagically by the system.
61
62        Returns:
63          Pass if True
64          Fail if False
65
66        TAGS: Bluetooth
67        Priority: 3
68        """
69        pid = self._get_bt_pid()
70        if not pid:
71            self.log.error("Failed to find Bluetooth process...")
72            return False
73        self.dut.adb.shell("kill -9 {}".format(pid))
74        #Give up to 5 seconds for the process to restart
75        search_tries = 5
76        while search_tries > 0:
77            if self._is_bt_process_running():
78                self.log.info(
79                    "Bluetooth process is successfully running again")
80                return True
81            search_tries -= 1
82            #Try looking for the process after waiting an additional second
83            time.sleep(1)
84
85        self.log.error("Bluetooth process did not restart")
86        return False
87