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