1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - 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 Telephony Pre Flight check.
18"""
19
20import time
21from queue import Empty
22
23from acts import signals
24from acts import utils
25from acts.controllers.android_device import get_info
26from acts.libs.ota import ota_updater
27from acts.test_decorators import test_tracker_info
28from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
29from acts.test_utils.tel.tel_defines import AOSP_PREFIX
30from acts.test_utils.tel.tel_defines import CAPABILITY_PHONE
31from acts.test_utils.tel.tel_defines import CAPABILITY_VOLTE
32from acts.test_utils.tel.tel_defines import CAPABILITY_VT
33from acts.test_utils.tel.tel_defines import CAPABILITY_WFC
34from acts.test_utils.tel.tel_defines import CAPABILITY_MSIM
35from acts.test_utils.tel.tel_defines import CAPABILITY_OMADM
36from acts.test_utils.tel.tel_defines import INVALID_SUB_ID
37from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
38from acts.test_utils.tel.tel_defines import PRECISE_CALL_STATE_LISTEN_LEVEL_BACKGROUND
39from acts.test_utils.tel.tel_defines import PRECISE_CALL_STATE_LISTEN_LEVEL_FOREGROUND
40from acts.test_utils.tel.tel_defines import PRECISE_CALL_STATE_LISTEN_LEVEL_RINGING
41from acts.test_utils.tel.tel_defines import WAIT_TIME_AFTER_REBOOT
42from acts.test_utils.tel.tel_lookup_tables import device_capabilities
43from acts.test_utils.tel.tel_lookup_tables import operator_capabilities
44from acts.test_utils.tel.tel_test_utils import abort_all_tests
45from acts.test_utils.tel.tel_test_utils import ensure_phones_default_state
46from acts.test_utils.tel.tel_test_utils import ensure_phone_subscription
47from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
48from acts.test_utils.tel.tel_test_utils import get_operator_name
49from acts.test_utils.tel.tel_test_utils import is_sim_locked
50from acts.test_utils.tel.tel_test_utils import run_multithread_func
51from acts.test_utils.tel.tel_test_utils import setup_droid_properties
52from acts.test_utils.tel.tel_test_utils import set_phone_screen_on
53from acts.test_utils.tel.tel_test_utils import set_phone_silent_mode
54from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode_by_adb
55from acts.test_utils.tel.tel_test_utils import unlock_sim
56from acts.test_utils.tel.tel_test_utils import verify_http_connection
57from acts.test_utils.tel.tel_test_utils import wait_for_voice_attach_for_subscription
58from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection
59from acts.test_utils.tel.tel_test_utils import wifi_toggle_state
60from acts.test_utils.tel.tel_voice_utils import phone_setup_volte
61from acts.asserts import abort_all
62from acts.asserts import fail
63
64
65class TelLivePreflightTest(TelephonyBaseTest):
66    def __init__(self, controllers):
67        TelephonyBaseTest.__init__(self, controllers)
68
69        self.wifi_network_ssid = self.user_params.get(
70            "wifi_network_ssid") or self.user_params.get(
71                "wifi_network_ssid_2g") or self.user_params.get(
72                    "wifi_network_ssid_5g")
73        self.wifi_network_pass = self.user_params.get(
74            "wifi_network_pass") or self.user_params.get(
75                "wifi_network_pass_2g") or self.user_params.get(
76                    "wifi_network_ssid_5g")
77
78    def setup_class(self):
79        for ad in self.android_devices:
80            toggle_airplane_mode_by_adb(self.log, ad, False)
81
82    def teardown_class(self):
83        pass
84
85    def setup_test(self):
86        pass
87
88    """ Tests Begin """
89
90    @test_tracker_info(uuid="cb897221-99e1-4697-927e-02d92d969440")
91    @TelephonyBaseTest.tel_test_wrap
92    def test_ota_upgrade(self):
93        ota_package = self.user_params.get("ota_package")
94        if isinstance(ota_package, list):
95            ota_package = ota_package[0]
96        if ota_package and "dev/null" not in ota_package:
97            self.log.info("Upgrade with ota_package %s", ota_package)
98            self.log.info("Before OTA upgrade: %s",
99                          get_info(self.android_devices))
100        else:
101            raise signals.TestSkip("No ota_package is defined")
102        ota_util = self.user_params.get("ota_util")
103        if isinstance(ota_util, list):
104            ota_util = ota_util[0]
105        if ota_util:
106            if "update_engine_client.zip" in ota_util:
107                self.user_params["UpdateDeviceOtaTool"] = ota_util
108                self.user_params["ota_tool"] = "UpdateDeviceOtaTool"
109            else:
110                self.user_params["AdbSideloadOtaTool"] = ota_util
111                self.user_params["ota_tool"] = "AdbSideloadOtaTool"
112        self.log.info("OTA upgrade with %s by %s", ota_package,
113                      self.user_params["ota_tool"])
114        ota_updater.initialize(self.user_params, self.android_devices)
115        tasks = [(ota_updater.update, [ad]) for ad in self.android_devices]
116        try:
117            run_multithread_func(self.log, tasks)
118        except Exception as err:
119            abort_all_tests(self.log, "Unable to do ota upgrade: %s" % err)
120        device_info = get_info(self.android_devices)
121        self.log.info("After OTA upgrade: %s", device_info)
122        self.results.add_controller_info("AndroidDevice", device_info)
123        for ad in self.android_devices:
124            if is_sim_locked(ad):
125                ad.log.info("After OTA, SIM keeps the locked state")
126            elif getattr(ad, "is_sim_locked", False):
127                ad.log.error("After OTA, SIM loses the locked state")
128            if not unlock_sim(ad):
129                abort_all_tests(ad.log, "unable to unlock SIM")
130        return True
131
132    @test_tracker_info(uuid="8390a2eb-a744-4cda-bade-f94a2cc83f02")
133    @TelephonyBaseTest.tel_test_wrap
134    def test_check_environment(self):
135        ad = self.android_devices[0]
136        # Check WiFi environment.
137        # 1. Connect to WiFi.
138        # 2. Check WiFi have Internet access.
139        toggle_airplane_mode(self.log, ad, False, strict_checking=False)
140        try:
141            if not ensure_wifi_connected(self.log, ad, self.wifi_network_ssid,
142                                         self.wifi_network_pass):
143                abort_all_tests(ad.log, "WiFi connect fail")
144            if (not wait_for_wifi_data_connection(self.log, ad, True) or
145                    not verify_http_connection(self.log, ad)):
146                abort_all_tests(ad.log, "Data not available on WiFi")
147        finally:
148            wifi_toggle_state(self.log, ad, False)
149        # TODO: add more environment check here.
150        return True
151
152    @test_tracker_info(uuid="7bb23ac7-6b7b-4d5e-b8d6-9dd10032b9ad")
153    @TelephonyBaseTest.tel_test_wrap
154    def test_pre_flight_check(self):
155        for ad in self.android_devices:
156            #check for sim and service
157            if not ensure_phone_subscription(self.log, ad):
158                abort_all_tests(ad.log, "Unable to find a valid subscription!")
159        return True
160
161    @test_tracker_info(uuid="1070b160-902b-43bf-92a0-92cc2d05bb13")
162    @TelephonyBaseTest.tel_test_wrap
163    def test_check_crash(self):
164        result = True
165        begin_time = None
166        for ad in self.android_devices:
167            output = ad.adb.shell("cat /proc/uptime")
168            epoch_up_time = utils.get_current_epoch_time() - 1000 * float(
169                output.split(" ")[0])
170            ad.crash_report_preflight = ad.check_crash_report(
171                self.test_name,
172                begin_time=epoch_up_time,
173                log_crash_report=True)
174            if ad.crash_report_preflight:
175                msg = "Find crash reports %s before test starts" % (
176                    ad.crash_report_preflight)
177                ad.log.warn(msg)
178                result = False
179        return result
180