1#!/usr/bin/env python3
2#
3#   Copyright 2021 - 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
18import time
19from acts.utils import rand_ascii_str
20from acts_contrib.test_utils.tel.tel_test_utils import sms_send_receive_verify
21from acts_contrib.test_utils.tel.tel_test_utils import call_setup_teardown
22from acts_contrib.test_utils.tel.tel_test_utils import hangup_call
23from acts_contrib.test_utils.tel.tel_subscription_utils import get_outgoing_message_sub_id
24
25
26message_lengths = (50, 160, 180)
27
28def _sms_test(log, ads):
29    """Test SMS between two phones.
30    Returns:
31        True if success.
32        False if failed.
33    """
34    for length in message_lengths:
35        message_array = [rand_ascii_str(length)]
36        if not sms_send_receive_verify(log, ads[0], ads[1],
37                                       message_array):
38            ads[0].log.error("SMS of length %s test failed", length)
39            return False
40        else:
41            ads[0].log.info("SMS of length %s test succeeded", length)
42    log.info("SMS test of length %s characters succeeded.",
43                  message_lengths)
44    return True
45
46def _long_sms_test(log, ads):
47    """Test SMS between two phones.
48    Returns:
49        True if success.
50        False if failed.
51    """
52
53    long_message_lengths = (800, 1600)
54    long_message_lengths_of_jp_carriers = (800, 1530)
55    sender_message_sub_id = get_outgoing_message_sub_id(ads[0])
56    sender_mcc = ads[0].telephony["subscription"][sender_message_sub_id]["mcc"]
57
58    if str(sender_mcc) in ["440", "441"]:
59        long_message_lengths = long_message_lengths_of_jp_carriers
60    for length in long_message_lengths:
61        message_array = [rand_ascii_str(length)]
62        if not sms_send_receive_verify(log, ads[0], ads[1],
63                                       message_array):
64            ads[0].log.warning("SMS of length %s test failed", length)
65            return False
66        else:
67            ads[0].log.info("SMS of length %s test succeeded", length)
68            time.sleep(30)
69    log.info("SMS test of length %s characters succeeded.",
70                  message_lengths)
71    return True
72
73def _sms_test_mo(log, ads):
74    return _sms_test(log, [ads[0], ads[1]])
75
76def _sms_test_mt(log, ads):
77    return _sms_test(log, [ads[1], ads[0]])
78
79def _long_sms_test_mo(log, ads):
80    return _long_sms_test(log, [ads[0], ads[1]])
81
82def _long_sms_test_mt(log, ads):
83    return _long_sms_test(log, [ads[1], ads[0]])
84
85
86def test_sms_mo_in_call(log, ads, caller_func=None, callee_func=None):
87    """Test MO SMS in call.
88
89        log: log object
90        ads: list of android objects, this list should have two ad.
91        caller_func: function to verify caller is in correct state while in-call.
92        callee_func: function to verify callee is in correct state while in-call.
93
94    Returns:
95        True if pass; False if fail.
96    """
97
98    log.info("Begin In Call SMS Test.")
99    if not call_setup_teardown(
100            log,
101            ads[0],
102            ads[1],
103            ad_hangup=None,
104            verify_caller_func=caller_func,
105            verify_callee_func=callee_func):
106        return False
107
108    sms_result = True
109    if not _sms_test_mo(log, ads):
110        log.error("SMS test fail.")
111        sms_result = False
112
113    if not hangup_call(log, ads[0]):
114        ads[0].log.info("Failed to hang up call!")
115        sms_result = False
116
117    return sms_result
118
119