summaryrefslogtreecommitdiffstats
path: root/chrome/test/functional/chromeos_txt_msg_functional.py
blob: 9019e24b7ab2df37de088216bb3d7c26196f965a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from email.MIMEText import MIMEText
import logging
import os
import re
import smtplib
import sys
import urllib

import pyauto_functional
import pyauto

sys.path.append(os.path.join(pyauto.PyUITest.DataDir(), 'pyauto_private',
                'chromeos', 'network'))
from gsm_sim_info import SIM, PROVIDER_TXT_SERVER


class ChromeosTxtMsgSanity(pyauto.PyUITest):
    """Tests for ChromeOS text message handling"""

    def _SendText(self, mail_server, sender, phone_number,
                   mobile_provider, msg):
        """Sends a text message to a specific phone

        Args:
            mail_server: An SMTP instance.
            sender: Sender's email address.
            phone_number: The phone number the txt message is directed to.
            mobile_provider: A cellular provider defined in
                             gsm_sim_info.PROVIDER_TXT_SERVER
            msg: The message to be sent.

        """
        recipient = ('%s@%s' % (phone_number,
                     PROVIDER_TXT_SERVER[mobile_provider]))
        self._SendMail(mail_server, sender, recipient, None, msg)

    def _SendMail(self, mail_server, sender, recipients,
                   msg_subject, msg_body):
        """Sends an email using the provided smtp connection

        Args:
            mail_server: An SMTP instace.
            sender: Senders email address.
            recipients: Recipients email address.
            msg_subject: The subject line of the email.
            msg_body: The body of the email.
        """
        msg = MIMEText(msg_body)
        msg['To'] = recipients
        msg['From'] = sender
        if msg_subject:
            msg['Subject'] = msg_subject
        mail_server.sendmail(sender, recipients, msg.as_string())

    def _GetGmailServerInstance(self, email, password):
        """Creates an SMTP connection with the gmail mail server

        Args:
            email: A gmail address.
            password: The password for the gmail address.

        Returns:
            An SMTP connection instance.
        """
        mail_server = smtplib.SMTP('smtp.gmail.com', 587)
        mail_server.starttls()
        mail_server.ehlo()
        mail_server.login(email, password)
        return mail_server

    def _GetIMSI(self):
        """Obtains the IMSI by running modem status

        Returns:
            IMSI of device
        """
        modem_status = os.popen('modem status').read()
        imsi = re.search('IMSI:\s(\d+)', modem_status)
        if not imsi:
            raise Exception('GSM Modem not detected in device')
        return imsi.groups()[0]

    def _GetSIMInfo(self):
        """Returns information necessary to send messages

        Returns:
            A dictionary with the following format
            {
                'mdn' : <phone number>,
                'carrier': <carrier name>
            }
        """
        imsi = self._GetIMSI()
        sim_info = SIM.get(imsi, {})
        if not sim_info:
            raise Exception('Phone number for sim with IMSI=%s is not '
                            'recognized within config file' % imsi)
        return sim_info

    def setUp(self):
        # Connect to cellular service if not already connected.
        pyauto.PyUITest.setUp(self)
        connected_cellular = self.NetworkScan().get('connected_cellular')
        if not connected_cellular:
            self.ConnectToCellularNetwork()
            if not self.NetworkScan().get('connected_cellular'):
                raise Exception('Could not connect to cellular service.')
        else:
            logging.debug('Already connected to cellular service %s' %
                          connected_cellular)

        # Obtain sender, recipient, and SMTP instance.
        self.credentials = self.GetPrivateInfo()['test_account_with_smtp']
        self.sim = self._GetSIMInfo()
        self.mail_server = self._GetGmailServerInstance(
                             self.credentials['username'],
                             self.credentials['password'])

    def tearDown(self):
        self.DisconnectFromCellularNetwork()
        self.mail_server.close()
        for window in range(len(self.GetActiveNotifications())):
            self.CloseNotification(window)
        pyauto.PyUITest.tearDown(self)

    def testTxtMsgNotification(self):
        """Notifications are displayed for text messages"""
        msg = 'This is the text message'
        self._SendText(self.mail_server, self.credentials['username'],
                        self.sim['mdn'], self.sim['carrier'], msg)
        self.WaitForNotificationCount(1)
        notification_result = self.GetActiveNotifications()[0]['content_url']
        self.assertTrue(re.search(urllib.pathname2url(msg),
                        notification_result), 'Invalid message was displayed.  '
                        'Expected "%s" but did not find it"' % msg)

    def testLongTxtMsgNotification(self):
        """Notifications are displayed for long (>160 char) text messages."""
        long_msg = 'This is a really long message with spaces. Testing to '\
                   'make sure that chromeos is able to catch it and '\
                   'create a notifications for this message.'
        self._SendText(self.mail_server, self.credentials['username'],
                        self.sim['mdn'], self.sim['carrier'], long_msg)
        self.WaitForNotificationCount(1)

        # GetActiveNotifications throws an exception if the text message never
        # arrives.
        txt_msg = self.GetActiveNotifications()[0]
        txt_msg = txt_windows[0]['content_url']
        self.assertTrue(re.search(urllib.pathname2url(long_msg),
                        txt_msg), 'Invalid message was displayed. '
                        'Expected "%s" but did not find it"' % long_msg)


if __name__ == '__main__':
    pyauto_functional.Main()