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
17import time
18from acts.utils import rand_ascii_str
19from acts_contrib.test_utils.tel.tel_test_utils import mms_send_receive_verify
20from acts_contrib.test_utils.tel.tel_test_utils import call_setup_teardown
21from acts_contrib.test_utils.tel.tel_test_utils import get_operator_name
22from acts_contrib.test_utils.tel.tel_test_utils import mms_receive_verify_after_call_hangup
23
24message_lengths = (50, 160, 180)
25long_message_lengths = (800, 1600)
26
27def _mms_test_mo(log, ads, expected_result=True):
28    return _mms_test(log,
29        [ads[0], ads[1]], expected_result=expected_result)
30
31def _mms_test_mt(log, ads, expected_result=True):
32    return _mms_test(log,
33        [ads[1], ads[0]], expected_result=expected_result)
34
35def _mms_test(log, ads, expected_result=True):
36    """Test MMS between two phones.
37
38    Returns:
39        True if success.
40        False if failed.
41    """
42    for length in message_lengths:
43        message_array = [("Test Message", rand_ascii_str(length), None)]
44        if not mms_send_receive_verify(
45                log,
46                ads[0],
47                ads[1],
48                message_array,
49                expected_result=expected_result):
50            log.warning("MMS of body length %s test failed", length)
51            return False
52        else:
53            log.info("MMS of body length %s test succeeded", length)
54    log.info("MMS test of body lengths %s succeeded",
55                  message_lengths)
56    return True
57
58def _long_mms_test_mo(log, ads):
59    return _long_mms_test(log, [ads[0], ads[1]])
60
61def _long_mms_test_mt(log, ads):
62    return _long_mms_test(log, [ads[1], ads[0]])
63
64def _long_mms_test(log, ads):
65    """Test MMS between two phones.
66
67    Returns:
68        True if success.
69        False if failed.
70    """
71    for length in long_message_lengths:
72        message_array = [("Test Message", rand_ascii_str(length), None)]
73        if not mms_send_receive_verify(log, ads[0], ads[1],
74                                       message_array):
75            log.warning("MMS of body length %s test failed", length)
76            return False
77        else:
78            log.info("MMS of body length %s test succeeded", length)
79            time.sleep(30)
80    log.info("MMS test of body lengths %s succeeded",
81                  message_lengths)
82    return True
83
84def _mms_test_after_call_hangup(log, ads):
85    """Test MMS send out after call hang up.
86
87    Returns:
88        True if success.
89        False if failed.
90    """
91    args = [
92        log, ads[0], ads[1], [("Test Message", "Basic Message Body",
93                               None)]
94    ]
95    if get_operator_name(log, ads[0]) in ["spt", "Sprint"]:
96        args.append(30)
97    if not mms_send_receive_verify(*args):
98        log.info("MMS send in call is suspended.")
99        if not mms_receive_verify_after_call_hangup(*args):
100            log.error(
101                "MMS is not send and received after call release.")
102            return False
103        else:
104            log.info("MMS is send and received after call release.")
105            return True
106    else:
107        log.info("MMS is send and received successfully in call.")
108        return True
109
110def _mms_test_mo_after_call_hangup(log, ads):
111    return _mms_test_after_call_hangup(log, [ads[0], ads[1]])
112
113def _mms_test_mt_after_call_hangup(log, ads):
114    return _mms_test_after_call_hangup(log, [ads[1], ads[0]])
115
116def test_mms_mo_in_call(log, ads, wifi=False, caller_func=None, callee_func=None):
117    """Test MO MMS in call.
118
119        log: log object
120        ads: list of android objects, this list should have two ad.
121        wifi: If true, sending sms over wifi.
122        caller_func: function to verify caller is in correct state while in-call.
123        callee_func: function to verify callee is in correct state while in-call.
124
125    Returns:
126        True if pass; False if fail.
127
128    """
129
130    log.info("Begin In Call MMS Test.")
131    if not call_setup_teardown(
132            log,
133            ads[0],
134            ads[1],
135            ad_hangup=None,
136            verify_caller_func=caller_func,
137            verify_callee_func=callee_func):
138        return False
139
140    if ads[0].sms_over_wifi and wifi:
141        return _mms_test_mo(log, ads)
142    else:
143        return _mms_test_mo_after_call_hangup(log, ads)
144
145
146