OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 from email.MIMEText import MIMEText | |
7 import logging | |
8 import os | |
9 import re | |
10 import smtplib | |
11 import sys | |
12 import urllib | |
13 | |
14 import pyauto_functional | |
15 import pyauto | |
16 | |
17 sys.path.append(os.path.join(pyauto.PyUITest.DataDir(), 'pyauto_private', | |
18 'chromeos', 'network')) | |
19 from gsm_sim_info import SIM, PROVIDER_TXT_SERVER | |
20 | |
21 | |
22 class ChromeosTxtMsgSanity(pyauto.PyUITest): | |
23 """Tests for ChromeOS text message handling""" | |
24 | |
25 def _SendText(self, mail_server, sender, phone_number, | |
26 mobile_provider, msg): | |
27 """Sends a text message to a specific phone | |
28 | |
29 Args: | |
30 mail_server: An SMTP instance. | |
31 sender: Sender's email address. | |
32 phone_number: The phone number the txt message is directed to. | |
33 mobile_provider: A cellular provider defined in | |
34 gsm_sim_info.PROVIDER_TXT_SERVER | |
35 msg: The message to be sent. | |
36 | |
37 """ | |
38 recipient = ('%s@%s' % (phone_number, | |
39 PROVIDER_TXT_SERVER[mobile_provider])) | |
40 self._SendMail(mail_server, sender, recipient, None, msg) | |
41 | |
42 def _SendMail(self, mail_server, sender, recipients, | |
43 msg_subject, msg_body): | |
44 """Sends an email using the provided smtp connection | |
45 | |
46 Args: | |
47 mail_server: An SMTP instace. | |
48 sender: Senders email address. | |
49 recipients: Recipients email address. | |
50 msg_subject: The subject line of the email. | |
51 msg_body: The body of the email. | |
52 """ | |
53 msg = MIMEText(msg_body) | |
54 msg['To'] = recipients | |
55 msg['From'] = sender | |
56 if msg_subject: | |
57 msg['Subject'] = msg_subject | |
58 mail_server.sendmail(sender, recipients, msg.as_string()) | |
59 | |
60 def _GetGmailServerInstance(self, email, password): | |
61 """Creates an SMTP connection with the gmail mail server | |
62 | |
63 Args: | |
64 email: A gmail address. | |
65 password: The password for the gmail address. | |
66 | |
67 Returns: | |
68 An SMTP connection instance. | |
69 """ | |
70 mail_server = smtplib.SMTP('smtp.gmail.com', 587) | |
71 mail_server.starttls() | |
72 mail_server.ehlo() | |
73 mail_server.login(email, password) | |
74 return mail_server | |
75 | |
76 def _GetIMSI(self): | |
77 """Obtains the IMSI by running modem status | |
78 | |
79 Returns: | |
80 IMSI of device | |
81 """ | |
82 modem_status = os.popen('modem status').read() | |
83 imsi = re.search('IMSI:\s(\d+)', modem_status) | |
84 if not imsi: | |
85 raise Exception('GSM Modem not detected in device') | |
86 return imsi.groups()[0] | |
87 | |
88 def _GetSIMInfo(self): | |
89 """Returns information necessary to send messages | |
90 | |
91 Returns: | |
92 A dictionary with the following format | |
93 { | |
94 'mdn' : <phone number>, | |
95 'carrier': <carrier name> | |
96 } | |
97 """ | |
98 imsi = self._GetIMSI() | |
99 sim_info = SIM.get(imsi, {}) | |
100 if not sim_info: | |
101 raise Exception('Phone number for sim with IMSI=%s is not ' | |
102 'recognized within config file' % imsi) | |
103 return sim_info | |
104 | |
105 def setUp(self): | |
106 # Connect to cellular service if not already connected. | |
107 pyauto.PyUITest.setUp(self) | |
108 connected_cellular = self.NetworkScan().get('connected_cellular') | |
109 if not connected_cellular: | |
110 self.ConnectToCellularNetwork() | |
111 if not self.NetworkScan().get('connected_cellular'): | |
112 raise Exception('Could not connect to cellular service.') | |
113 else: | |
114 logging.debug('Already connected to cellular service %s' % | |
115 connected_cellular) | |
116 | |
117 # Obtain sender, recipient, and SMTP instance. | |
118 self.credentials = self.GetPrivateInfo()['test_account_with_smtp'] | |
119 self.sim = self._GetSIMInfo() | |
120 self.mail_server = self._GetGmailServerInstance( | |
121 self.credentials['username'], | |
122 self.credentials['password']) | |
123 | |
124 def tearDown(self): | |
125 self.DisconnectFromCellularNetwork() | |
126 self.mail_server.close() | |
127 for window in range(len(self.GetActiveNotifications())): | |
128 self.CloseNotification(window) | |
129 pyauto.PyUITest.tearDown(self) | |
130 | |
131 def testTxtMsgNotification(self): | |
132 """Notifications are displayed for text messages""" | |
133 msg = 'This is the text message' | |
134 self._SendText(self.mail_server, self.credentials['username'], | |
135 self.sim['mdn'], self.sim['carrier'], msg) | |
136 self.WaitForNotificationCount(1) | |
137 notification_result = self.GetActiveNotifications()[0]['content_url'] | |
138 self.assertTrue(re.search(urllib.pathname2url(msg), | |
139 notification_result), 'Invalid message was displayed. ' | |
140 'Expected "%s" but did not find it"' % msg) | |
141 | |
142 def testLongTxtMsgNotification(self): | |
143 """Notifications are displayed for long (>160 char) text messages.""" | |
144 long_msg = 'This is a really long message with spaces. Testing to '\ | |
145 'make sure that chromeos is able to catch it and '\ | |
146 'create a notifications for this message.' | |
147 self._SendText(self.mail_server, self.credentials['username'], | |
148 self.sim['mdn'], self.sim['carrier'], long_msg) | |
149 self.WaitForNotificationCount(1) | |
150 | |
151 # GetActiveNotifications throws an exception if the text message never | |
152 # arrives. | |
153 txt_msg = self.GetActiveNotifications()[0] | |
154 txt_msg = txt_windows[0]['content_url'] | |
155 self.assertTrue(re.search(urllib.pathname2url(long_msg), | |
156 txt_msg), 'Invalid message was displayed. ' | |
157 'Expected "%s" but did not find it"' % long_msg) | |
158 | |
159 | |
160 if __name__ == '__main__': | |
161 pyauto_functional.Main() | |
OLD | NEW |