1#/usr/bin/env python3.4
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17Automated tests for the testing send and receive SMS commands in MAP profile.
18"""
19
20import time
21import queue
22
23import acts
24from acts.test_decorators import test_tracker_info
25from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
26from acts.test_utils.bt.BluetoothCarHfpBaseTest import BluetoothCarHfpBaseTest
27from acts.test_utils.bt import bt_test_utils
28from acts.test_utils.bt import BtEnum
29from acts.test_utils.tel.tel_defines import EventSmsReceived
30from acts.test_utils.tel.tel_defines import EventSmsSentSuccess
31from acts.test_utils.tel.tel_defines import EventSmsDeliverSuccess
32from acts.test_utils.tel.tel_test_utils import get_phone_number
33from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode_by_adb
34
35EVENT_MAP_MESSAGE_RECEIVED = "MapMessageReceived"
36TIMEOUT = 2000
37MESSAGE_TO_SEND = "Don't text and Drive!"
38
39SEND_FAILED_NO_MCE = 1
40SEND_FAILED_NO_NETWORK = 2
41
42
43class BtCarMapMceTest(BluetoothCarHfpBaseTest):
44    def setup_class(self):
45        if not super(BtCarMapMceTest, self).setup_class():
46            return False
47        # MAP roles
48        # Carkit device
49        self.MCE = self.hf
50        # Phone device
51        self.MSE = self.ag
52        # Remote device
53        self.REMOTE = self.re
54        time.sleep(4)
55        return True
56
57    def message_delivered(self, device):
58        try:
59            self.MCE.ed.pop_event(EventSmsDeliverSuccess, 15)
60        except queue.Empty:
61            self.log.error("Message failed to be delivered.")
62            return False
63        return True
64
65    def send_message(self, remotes):
66        self.REMOTE.droid.smsStartTrackingIncomingSmsMessage()
67        destinations = []
68        for phone in remotes:
69            destinations.append("tel:{}".format(
70                get_phone_number(self.log, phone)))
71        self.log.info(destinations)
72        self.MCE.droid.mapSendMessage(
73            self.MSE.droid.bluetoothGetLocalAddress(), destinations,
74            MESSAGE_TO_SEND)
75        try:
76            self.MCE.ed.pop_event(EventSmsSentSuccess, 15)
77        except queue.Empty:
78            self.MCE.log.error("Message failed to send.")
79            return False
80
81        try:
82            receivedMessage = self.REMOTE.ed.pop_event(EventSmsReceived, 15)
83            self.REMOTE.log.info("Received a message: {}".format(
84                receivedMessage['data']['Text']))
85        except queue.Empty:
86            self.REMOTE.log.error("Remote did not receive message.")
87            return False
88
89        if MESSAGE_TO_SEND != receivedMessage['data']['Text']:
90            self.log.error("Messages don't match.")
91            self.log.error("Sent     {}".format(MESSAGE_TO_SEND))
92            self.log.error("Received {}".format(receivedMessage['data'][
93                'Text']))
94            return False
95        return True
96
97    @test_tracker_info(uuid='0858347a-e649-4f18-85b6-6990cc311dee')
98    @BluetoothBaseTest.bt_test_wrap
99    def test_send_message(self):
100        bt_test_utils.connect_pri_to_sec(
101            self.MCE, self.MSE, set([BtEnum.BluetoothProfile.MAP_MCE.value]))
102        return self.send_message([self.REMOTE])
103
104    @test_tracker_info(uuid='b25caa53-3c7f-4cfa-a0ec-df9a8f925fe5')
105    @BluetoothBaseTest.bt_test_wrap
106    def test_receive_message(self):
107        bt_test_utils.connect_pri_to_sec(
108            self.MCE, self.MSE, set([BtEnum.BluetoothProfile.MAP_MCE.value]))
109        self.MSE.log.info("Start Tracking SMS.")
110        self.MSE.droid.smsStartTrackingIncomingSmsMessage()
111        self.REMOTE.log.info("Ready to send")
112        self.REMOTE.droid.smsSendTextMessage(
113            get_phone_number(self.log, self.MSE), "test_receive_message",
114            False)
115        self.MCE.log.info("Check inbound Messages.")
116        receivedMessage = self.MCE.ed.pop_event(EVENT_MAP_MESSAGE_RECEIVED, 15)
117        self.MCE.log.info(receivedMessage['data'])
118        return True
119
120    @test_tracker_info(uuid='5b7b3ded-0a1a-470f-b119-9a03bc092805')
121    @BluetoothBaseTest.bt_test_wrap
122    def test_send_message_failure_no_cellular(self):
123        if not toggle_airplane_mode_by_adb(self.log, self.MSE, True):
124            return False
125        bt_test_utils.reset_bluetooth([self.MSE])
126        bt_test_utils.connect_pri_to_sec(
127            self.MCE, self.MSE, set([BtEnum.BluetoothProfile.MAP_MCE.value]))
128        return not self.send_message([self.REMOTE])
129
130    @test_tracker_info(uuid='19444142-1d07-47dc-860b-f435cba46fca')
131    @BluetoothBaseTest.bt_test_wrap
132    def test_send_message_failure_no_map_connection(self):
133        return not self.send_message([self.REMOTE])
134
135    @test_tracker_info(uuid='c7e569c0-9f6c-49a4-8132-14bc544ccb53')
136    @BluetoothBaseTest.bt_test_wrap
137    def test_send_message_failure_no_bluetooth(self):
138        if not toggle_airplane_mode_by_adb(self.log, self.MSE, True):
139            return False
140        try:
141            bt_test_utils.connect_pri_to_sec(
142                self.MCE, self.MSE,
143                set([BtEnum.BluetoothProfile.MAP_MCE.value]))
144        except acts.controllers.android.SL4AAPIError:
145            self.MCE.log.info("Failed to connect as expected")
146        return not self.send_message([self.REMOTE])
147
148    @test_tracker_info(uuid='8cdb4a54-3f18-482f-be3d-acda9c4cbeed')
149    @BluetoothBaseTest.bt_test_wrap
150    def test_disconnect_failure_send_message(self):
151        connected = bt_test_utils.connect_pri_to_sec(
152            self.MCE, self.MSE, set([BtEnum.BluetoothProfile.MAP_MCE.value]))
153        addr = self.MSE.droid.bluetoothGetLocalAddress()
154        if bt_test_utils.is_map_mce_device_connected(self.MCE, addr):
155            connected = True
156        disconnected = bt_test_utils.disconnect_pri_from_sec(
157            self.MCE, self.MSE, [BtEnum.BluetoothProfile.MAP_MCE.value])
158        # Grace time for the disconnection to complete.
159        time.sleep(3)
160        if not bt_test_utils.is_map_mce_device_connected(self.MCE, addr):
161            disconnected = True
162        self.MCE.log.info("Connected = {}, Disconnected = {}".format(
163            connected, disconnected))
164        return connected and disconnected and not self.send_message(
165            [self.REMOTE])
166
167    @test_tracker_info(uuid='2d79a896-b1c1-4fb7-9924-db8b5c698be5')
168    @BluetoothBaseTest.bt_test_wrap
169    def manual_test_send_message_to_contact(self):
170        bt_test_utils.connect_pri_to_sec(
171            self.MCE, self.MSE, set([BtEnum.BluetoothProfile.MAP_MCE.value]))
172        contacts = self.MCE.droid.contactsGetContactIds()
173        self.log.info(contacts)
174        selected_contact = self.MCE.droid.contactsDisplayContactPickList()
175        if selected_contact:
176            return self.MCE.droid.mapSendMessage(
177                self.MSE.droid.bluetoothGetLocalAddress(),
178                selected_contact['data'], "Don't Text and Drive!")
179        return False
180
181    @test_tracker_info(uuid='8ce9a7dd-3b5e-4aee-a897-30740e2439c3')
182    @BluetoothBaseTest.bt_test_wrap
183    def test_send_message_to_multiple_phones(self):
184        bt_test_utils.connect_pri_to_sec(
185            self.MCE, self.MSE, set([BtEnum.BluetoothProfile.MAP_MCE.value]))
186        return self.send_message([self.REMOTE, self.REMOTE])
187