1#!/usr/bin/env python3.4
2#
3#   Copyright 2022 - Google
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    Test Script for CellBroadcast initialization Test
18"""
19
20import time
21
22from acts.logger import epoch_to_log_line_timestamp
23from acts.keys import Config
24from acts.base_test import BaseTestClass
25from acts.test_decorators import test_tracker_info
26from acts.utils import load_config
27from acts_contrib.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
28from acts_contrib.test_utils.tel.tel_test_utils import reboot_device
29from acts_contrib.test_utils.tel.tel_test_utils import get_device_epoch_time
30
31
32class CellBroadcastInitializationTest(TelephonyBaseTest):
33    def setup_test(self):
34        TelephonyBaseTest.setup_class()
35        self.number_of_devices = 1
36        self.cbr_init_iteration = self.user_params.get("cbr_init_iteration",
37                                                       50)
38
39    def teardown_class(self):
40        TelephonyBaseTest.teardown_class(self)
41
42    def _get_current_time_in_secs(self, ad):
43        try:
44            c_time = get_device_epoch_time(ad)
45            c_time = epoch_to_log_line_timestamp(c_time).split()[1].split(
46                '.')[0]
47            return self._convert_formatted_time_to_secs(c_time)
48        except Exception as e:
49            ad.log.error(e)
50
51    def _convert_formatted_time_to_secs(self, formatted_time):
52        try:
53            time_list = formatted_time.split(":")
54            return int(time_list[0]) * 3600 + int(time_list[1]) * 60 + int(
55                time_list[2])
56        except Exception as e:
57            self.log.error(e)
58
59    def _verify_channel_config_4400(self, ad):
60        #TODO add all channel checks as constants in tel_defines
61        channel_4400__log = 'SmsBroadcastConfigInfo: Id \\[4400'
62        return ad.search_logcat(channel_4400__log)
63
64    @test_tracker_info(uuid="30f30fa4-f57a-40bd-a37a-141a8efb5a04")
65    @TelephonyBaseTest.tel_test_wrap
66    def test_reboot_stress(self):
67        """ Verifies channel 4400 is set correctly after device boot up
68        only applicable to US carriers
69        after every boot up, search logcat to verify channel 4400 is set
70        default iterations is 50
71        config param : cbr_init_iteration
72
73        """
74        ad = self.android_devices[0]
75
76        current_cbr_version = ad.get_apk_version(
77            'com.google.android.cellbroadcast')
78        ad.log.info("Current cbr apk version is %s.", current_cbr_version)
79
80        failure_count = 0
81        begin_time = self._get_current_time_in_secs(ad)
82        for iteration in range(1, self.cbr_init_iteration + 1):
83            msg = "Stress CBR reboot initialization test Iteration: <%s>/<%s>" % (
84                iteration, self.cbr_init_iteration)
85            self.log.info(msg)
86            ad.reboot()
87            ad.wait_for_boot_completion()
88            self.log.info("Rebooted")
89            #TODO make sleep time a constant in tel_defines WAIT_TIME_CBR_INIT_AFTER_REBOOT
90            time.sleep(40)
91            if not self._verify_channel_config_4400(ad):
92                failure_count += 1
93                self.log.error('Iteration failed at %d ' % iteration)
94        end_time = self._get_current_time_in_secs(ad)
95        self.log.debug('Test completed from %s to %s' % (begin_time, end_time))
96        result = True
97        if failure_count > 0:
98            result = False
99            self.log.error(
100                'CBR reboot init stress test: <%s> failures in %s iterations',
101                failure_count, self.cbr_init_iteration)
102        return result
103