1package com.android.bluetooth.tests;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.InputStream;
6import java.io.UnsupportedEncodingException;
7import java.text.SimpleDateFormat;
8import java.util.Calendar;
9import java.util.Date;
10
11import android.test.AndroidTestCase;
12import android.util.Log;
13
14import com.android.bluetooth.map.BluetoothMapAppParams;
15import com.android.bluetooth.map.BluetoothMapSmsPdu;
16import com.android.bluetooth.map.BluetoothMapUtils;
17import com.android.bluetooth.map.BluetoothMapUtils.TYPE;
18import com.android.bluetooth.map.BluetoothMapbMessage;
19import com.android.bluetooth.map.BluetoothMapbMessageMime;
20import com.android.bluetooth.map.BluetoothMapbMessageSms;
21
22/***
23 *
24 * Test cases for the bMessage class. (encoding and decoding)
25 *
26 */
27public class BluetoothMapbMessageTest extends AndroidTestCase {
28    protected static String TAG = "BluetoothMapbMessageTest";
29    protected static final boolean D = true;
30
31    public BluetoothMapbMessageTest() {
32        super();
33    }
34
35    /***
36     * Test encoding of a simple SMS text message (UTF8). This validates most parameters.
37     */
38    public void testSmsEncodeText() {
39        BluetoothMapbMessageSms msg = new BluetoothMapbMessageSms();
40        String str1 =
41                 "BEGIN:BMSG\r\n" +
42                    "VERSION:1.0\r\n" +
43                    "STATUS:UNREAD\r\n" +
44                    "TYPE:SMS_GSM\r\n" +
45                    "FOLDER:telecom/msg/inbox\r\n" +
46                    "BEGIN:VCARD\r\n" +
47                        "VERSION:3.0\r\n" +
48                        "FN:Casper Bonde\r\n" +
49                        "N:Bonde,Casper\r\n" +
50                        "TEL:+4512345678\r\n" +
51                        "TEL:+4587654321\r\n" +
52                        "EMAIL:casper@email.add\r\n" +
53                        "EMAIL:bonde@email.add\r\n" +
54                    "END:VCARD\r\n" +
55                    "BEGIN:VCARD\r\n" +
56                    "VERSION:3.0\r\n" +
57                    "FN:Casper Bonde\r\n" +
58                    "N:Bonde,Casper\r\n" +
59                    "TEL:+4512345678\r\n" +
60                    "TEL:+4587654321\r\n" +
61                    "EMAIL:casper@email.add\r\n" +
62                    "EMAIL:bonde@email.add\r\n" +
63                "END:VCARD\r\n" +
64                    "BEGIN:BENV\r\n" +
65                        "BEGIN:VCARD\r\n" +
66                            "VERSION:3.0\r\n" +
67                            "FN:Jens Hansen\r\n" +
68                            "N:\r\n" +
69                            "TEL:+4512345678\r\n" +
70                            "TEL:+4587654321\r\n" +
71                            "EMAIL:casper@email.add\r\n" +
72                            "EMAIL:bonde@email.add\r\n" +
73                        "END:VCARD\r\n" +
74                        "BEGIN:VCARD\r\n" +
75                            "VERSION:3.0\r\n" +
76                            "FN:Jens Hansen\r\n" +
77                            "N:\r\n" +
78                            "TEL:+4512345678\r\n" +
79                            "TEL:+4587654321\r\n" +
80                            "EMAIL:casper@email.add\r\n" +
81                            "EMAIL:bonde@email.add\r\n" +
82                        "END:VCARD\r\n" +
83                        "BEGIN:BBODY\r\n" +
84                            "CHARSET:UTF-8\r\n" +
85                            "LENGTH:45\r\n" +
86                            "BEGIN:MSG\r\n" +
87                                "This is a short message\r\n" +
88                            "END:MSG\r\n" +
89                        "END:BBODY\r\n" +
90                    "END:BENV\r\n" +
91                 "END:BMSG\r\n";
92
93        String encoded;
94        String[] phone = {"+4512345678", "+4587654321"};
95        String[] email = {"casper@email.add", "bonde@email.add"};
96        msg.addOriginator("Bonde,Casper", "Casper Bonde", phone, email, null, null);
97        msg.addOriginator("Bonde,Casper", "Casper Bonde", phone, email, null, null);
98        msg.addRecipient("", "Jens Hansen", phone, email, null, null);
99        msg.addRecipient("", "Jens Hansen", phone, email, null, null);
100        msg.setFolder("inbox");
101        msg.setSmsBody("This is a short message");
102        msg.setStatus(false);
103        msg.setType(TYPE.SMS_GSM);
104        try {
105            encoded = new String(msg.encode());
106            if(D) Log.d(TAG, encoded);
107            assertTrue(str1.equals(encoded));
108        } catch (UnsupportedEncodingException e) {
109            Log.d(TAG, "Encoding failed.",e);
110            assertTrue("Encoding failed.", true);
111        }
112    }
113
114    /***
115     * Test native Deliver PDU encoding (decoding not possible), based on the example in the MAP 1.1 specification.
116     * The difference between this PDU, and the one in the specification:
117     *  - The invalid SC address 0191 is replaced with no address 00
118     *  - The "No more messages flag" is set (bit 2 in the second byte)
119     *  - The phone number type is changed from private 91 to international 81
120     *  - The time is changed to local time, since the time zone cannot be controlled through the API
121     */
122    public void testSmsEncodeNativeDeliverPdu() {
123        BluetoothMapbMessageSms msg = new BluetoothMapbMessageSms();
124        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
125        Date date = new Date(System.currentTimeMillis());
126        String timeStr = format.format(date); // Format to YYMMDDTHHMMSS UTC time
127        ByteArrayOutputStream scTime = new ByteArrayOutputStream(7);
128        StringBuilder scTimeSb = new StringBuilder();
129        byte[] timeChars;
130        try {
131            timeChars = timeStr.getBytes("US-ASCII");
132        } catch (UnsupportedEncodingException e1) {
133            assertTrue("Failed to extract bytes from string using US-ASCII", true);
134            return;
135        }
136
137        for(int i = 0, n = timeStr.length(); i < n; i+=2) {
138            scTime.write((timeChars[i+1]-0x30) << 4 | (timeChars[i]-0x30)); // Offset from ascii char to decimal value
139        }
140
141        Calendar cal = Calendar.getInstance();
142        int offset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (15 * 60 * 1000); /* offset in quarters of an hour */
143        String offsetString;
144        if(offset < 0) {
145            offsetString = String.format("%1$02d", -(offset));
146            char[] offsetChars = offsetString.toCharArray();
147            scTime.write((offsetChars[1]-0x30) << 4 | 0x40 | (offsetChars[0]-0x30));
148        }
149        else {
150            offsetString = String.format("%1$02d", offset);
151            char[] offsetChars = offsetString.toCharArray();
152            scTime.write((offsetChars[1]-0x30) << 4 | (offsetChars[0]-0x30));
153        }
154        byte[] scTimeData = scTime.toByteArray();
155        for(int i = 0; i < scTimeData.length; i++) {
156            scTimeSb.append(Integer.toString((scTimeData[i] >> 4) & 0x0f,16)); // MS-nibble first
157            scTimeSb.append(Integer.toString( scTimeData[i]       & 0x0f,16));
158        }
159        if(D) Log.v(TAG, "Generated time string: " + scTimeSb.toString());
160        String expected =
161                 "BEGIN:BMSG\r\n" +
162                    "VERSION:1.0\r\n" +
163                    "STATUS:UNREAD\r\n" +
164                    "TYPE:SMS_GSM\r\n" +
165                    "FOLDER:telecom/msg/inbox\r\n" +
166                    "BEGIN:VCARD\r\n" +
167                        "VERSION:3.0\r\n" +
168                        "FN:Casper Bonde\r\n" +
169                        "N:Bonde,Casper\r\n" +
170                        "TEL:00498912345678\r\n" +
171                        "TEL:+4587654321\r\n" +
172                        "EMAIL:casper@email.add\r\n" +
173                        "EMAIL:bonde@email.add\r\n" +
174                    "END:VCARD\r\n" +
175                    "BEGIN:BENV\r\n" +
176                        "BEGIN:VCARD\r\n" +
177                            "VERSION:3.0\r\n" +
178                            "FN:Jens Hansen\r\n" +
179                            "N:\r\n" +
180                            "TEL:00498912345678\r\n" +
181                            "TEL:+4587654321\r\n" +
182                            "EMAIL:casper@email.add\r\n" +
183                            "EMAIL:bonde@email.add\r\n" +
184                        "END:VCARD\r\n" +
185                        "BEGIN:BBODY\r\n" +
186                            "ENCODING:G-7BIT\r\n" +
187                            "LENGTH:94\r\n" +
188                            "BEGIN:MSG\r\n" +
189                                "00040E81009498214365870000" + scTimeSb.toString() +
190                                "11CC32FD34079DDF20737A8E4EBBCF21\r\n" +
191                            "END:MSG\r\n" +
192                        "END:BBODY\r\n" +
193                    "END:BENV\r\n" +
194                 "END:BMSG\r\n";
195
196        String encoded;
197        String[] phone = {"00498912345678", "+4587654321"};
198        String[] email = {"casper@email.add", "bonde@email.add"};
199        msg.addOriginator("Bonde,Casper", "Casper Bonde", phone, email, null, null);
200        msg.addRecipient("", "Jens Hansen", phone, email, null, null);
201        msg.setFolder("inbox");
202        /* TODO: extract current time, and build the expected string */
203        msg.setSmsBodyPdus(BluetoothMapSmsPdu.getDeliverPdus("Let's go fishing!", "00498912345678", date.getTime()));
204        msg.setStatus(false);
205        msg.setType(TYPE.SMS_GSM);
206        try {
207            byte[] encodedBytes = msg.encode();
208//            InputStream is = new ByteArrayInputStream(encodedBytes);
209            encoded = new String(encodedBytes);
210//            BluetoothMapbMessage newMsg = BluetoothMapbMessage.parse(is, BluetoothMapAppParams.CHARSET_NATIVE);
211//            String decoded = ((BluetoothMapbMessageSms) newMsg).getSmsBody();
212            if(D) Log.d(TAG, "\nExpected: \n" + expected);
213            if(D) Log.d(TAG, "\nEncoded: \n" + encoded);
214//            if(D) Log.d(TAG, "\nDecoded: \n" + decoded);
215            assertTrue(expected.equalsIgnoreCase(encoded));
216        } catch (UnsupportedEncodingException e) {
217            Log.d(TAG, "Encoding failed.",e);
218            assertTrue("Encoding failed.", true);
219        }
220    }
221
222    /***
223     * Test native Submit PDU encoding and decoding, based on the example in the MAP 1.1 specification.
224     * The difference between this PDU, and the one in the specification:
225     *  - The invalid SC address 0191 is replaced with no address 00
226     *  - The PDU is converted to a submit PDU by adding the TP-MR and removing the service center time stamp.
227     *  - The phone number type is changed from private 91 to international 81
228     */
229    public void testSmsEncodeDecodeNativeSubmitPdu() {
230        BluetoothMapbMessageSms msg = new BluetoothMapbMessageSms();
231        String expected =
232                 "BEGIN:BMSG\r\n" +
233                    "VERSION:1.0\r\n" +
234                    "STATUS:UNREAD\r\n" +
235                    "TYPE:SMS_GSM\r\n" +
236                    "FOLDER:telecom/msg/outbox\r\n" +
237                    "BEGIN:VCARD\r\n" +
238                        "VERSION:3.0\r\n" +
239                        "FN:Casper Bonde\r\n" +
240                        "N:Bonde,Casper\r\n" +
241                        "TEL:00498912345678\r\n" +
242                        "TEL:+4587654321\r\n" +
243                        "EMAIL:casper@email.add\r\n" +
244                        "EMAIL:bonde@email.add\r\n" +
245                    "END:VCARD\r\n" +
246                    "BEGIN:BENV\r\n" +
247                        "BEGIN:VCARD\r\n" +
248                            "VERSION:3.0\r\n" +
249                            "FN:Jens Hansen\r\n" +
250                            "N:\r\n" +
251                            "TEL:00498912345678\r\n" +
252                            "TEL:+4587654321\r\n" +
253                            "EMAIL:casper@email.add\r\n" +
254                            "EMAIL:bonde@email.add\r\n" +
255                        "END:VCARD\r\n" +
256                        "BEGIN:BBODY\r\n" +
257                            "ENCODING:G-7BIT\r\n" +
258                            "LENGTH:82\r\n" +
259                            "BEGIN:MSG\r\n" + /*Length 11 */
260                                "0001000E8100949821436587000011CC32FD34079DDF20737A8E4EBBCF21\r\n" + /* Length 62 */
261                            "END:MSG\r\n" + /* Length 9 */
262                        "END:BBODY\r\n" +
263                    "END:BENV\r\n" +
264                 "END:BMSG\r\n";
265
266        String encoded;
267        String[] phone = {"00498912345678", "+4587654321"};
268        String[] email = {"casper@email.add", "bonde@email.add"};
269        msg.addOriginator("Bonde,Casper", "Casper Bonde", phone, email, null, null);
270        msg.addRecipient("", "Jens Hansen", phone, email, null, null);
271        msg.setFolder("outbox");
272        /* TODO: extract current time, and build the expected string */
273        msg.setSmsBodyPdus(BluetoothMapSmsPdu.getSubmitPdus("Let's go fishing!", "00498912345678"));
274        msg.setStatus(false);
275        msg.setType(TYPE.SMS_GSM);
276        try {
277            byte[] encodedBytes = msg.encode();
278            InputStream is = new ByteArrayInputStream(encodedBytes);
279            encoded = new String(encodedBytes);
280            BluetoothMapbMessage newMsg = BluetoothMapbMessage.parse(is, BluetoothMapAppParams.CHARSET_NATIVE);
281            String decoded = ((BluetoothMapbMessageSms) newMsg).getSmsBody();
282            if(D) Log.d(TAG, "\nCalling encoder on decoded message to log its content");
283            newMsg.encode();
284            if(D) Log.d(TAG, "\nExpected: \n" + expected);
285            if(D) Log.d(TAG, "\nEncoded: \n" + encoded);
286            if(D) Log.d(TAG, "\nDecoded: \n" + decoded);
287            assertTrue("The encoded bMessage do not match the expected.", expected.equalsIgnoreCase(encoded));
288            assertTrue("The decoded text is \"" + decoded + "\" - expected \"Let's go fishing!\"", decoded.equalsIgnoreCase("Let's go fishing!"));
289        } catch (UnsupportedEncodingException e) {
290            Log.d(TAG, "Encoding failed.",e);
291            assertTrue("Encoding failed.", true);
292        }
293    }
294
295    /***
296     * Test native Submit PDU encoding and decoding, based on the example in the MAP 1.1 specification.
297     * The difference between this PDU, and the one in the specification:
298     *  - The invalid SC address 0191 is replaced with no address 00
299     *  - The PDU is converted to a submit PDU by adding the TP-MR and removing the service center time stamp.
300     *  - The phone number type is changed from private 91 to international 81
301     */
302    public void testSmsEncodeDecodeNativeSubmitPduWithSc() {
303        BluetoothMapbMessageSms msg = new BluetoothMapbMessageSms();
304        String encoded =
305                 "BEGIN:BMSG\r\n" +
306                    "VERSION:1.0\r\n" +
307                    "STATUS:UNREAD\r\n" +
308                    "TYPE:SMS_GSM\r\n" +
309                    "FOLDER:telecom/msg/outbox\r\n" +
310                    "BEGIN:VCARD\r\n" +
311                        "VERSION:3.0\r\n" +
312                        "FN:Casper Bonde\r\n" +
313                        "N:Bonde,Casper\r\n" +
314                        "TEL:00498912345678\r\n" +
315                        "TEL:+4587654321\r\n" +
316                        "EMAIL:casper@email.add\r\n" +
317                        "EMAIL:bonde@email.add\r\n" +
318                    "END:VCARD\r\n" +
319                    "BEGIN:BENV\r\n" +
320                        "BEGIN:VCARD\r\n" +
321                            "VERSION:3.0\r\n" +
322                            "FN:Jens Hansen\r\n" +
323                            "N:\r\n" +
324                            "TEL:00498912345678\r\n" +
325                            "TEL:+4587654321\r\n" +
326                            "EMAIL:casper@email.add\r\n" +
327                            "EMAIL:bonde@email.add\r\n" +
328                        "END:VCARD\r\n" +
329                        "BEGIN:BBODY\r\n" +
330                            "ENCODING:G-7BIT\r\n" +
331                            "LENGTH:58 \r\n" +
332                            "BEGIN:MSG\r\n" + /*Length 11 */
333                                "018001000B912184254590F500000346F61B\r\n" + /* Length 38 */
334                            "END:MSG\r\n" + /* Length 9 */
335                        "END:BBODY\r\n" +
336                    "END:BENV\r\n" +
337                 "END:BMSG\r\n";
338        try {
339            String expected = "Flo";
340            InputStream is = new ByteArrayInputStream(encoded.getBytes("UTF-8"));
341            BluetoothMapbMessage newMsg = BluetoothMapbMessage.parse(is, BluetoothMapAppParams.CHARSET_NATIVE);
342            String decoded = ((BluetoothMapbMessageSms) newMsg).getSmsBody();
343            if(D) Log.d(TAG, "\nEncoded: \n" + encoded);
344            if(D) Log.d(TAG, "\nDecoded: \n" + decoded);
345            assertTrue("Decoded string (" + decoded + ") did not match expected (" + expected + ")", expected.equals(decoded));
346        } catch (UnsupportedEncodingException e) {
347            Log.d(TAG, "Encoding failed.",e);
348            assertTrue("Encoding failed.", false);
349        }
350    }
351
352    /***
353     * Validate that the folder is correctly truncated to 512 bytes, if a longer folder path
354     * is supplied.
355     */
356    public void testFolderLengthTruncation() {
357        String folder = "";
358        int levelCount = 0;
359        while(folder.length()<640)
360            folder += "/folder" + levelCount++;
361
362        String expected = folder.substring(folder.length()-512, folder.length());
363
364        BluetoothMapbMessageSms msg = new BluetoothMapbMessageSms();
365        msg.setFolder(folder);
366        msg.setStatus(false);
367        msg.setType(TYPE.SMS_GSM);
368
369        try {
370            byte[] encoded = msg.encode();
371            InputStream is = new ByteArrayInputStream(encoded);
372            if(D) Log.d(TAG, new String(encoded));
373            BluetoothMapbMessage newMsg = BluetoothMapbMessage.parse(is, BluetoothMapAppParams.CHARSET_UTF8);
374            assertTrue("Wrong length expected 512, got " + expected.length(), expected.length() == 512);
375            Log.d(TAG, "expected:           " + expected);
376            Log.d(TAG, "newMsg.getFolder(): " + newMsg.getFolder());
377            assertTrue("Folder string did not match", expected.equals(newMsg.getFolder()));
378
379        } catch (UnsupportedEncodingException e) {
380            Log.d(TAG, "Encoding failed.",e);
381            assertTrue("Encoding failed", false);
382        }
383    }
384
385    /***
386     * Test multipart message decoding.
387     */
388    public void testSmsMultipartDecode() {
389        BluetoothMapbMessageSms msg = new BluetoothMapbMessageSms();
390        String encoded =
391                 "BEGIN:BMSG\r\n" +
392                 "VERSION:1.0\r\n" +
393                 "STATUS:READ\r\n" +
394                 "TYPE:SMS_GSM\r\n" +
395                 "FOLDER:/telecom/msg/outbox\r\n" +
396                 "BEGIN:VCARD\r\n" +
397                 "VERSION:2.1\r\n" +
398                 "N:12485254094 \r\n" +
399                 "TEL:12485254094\r\n" +
400                 "END:VCARD\r\n" +
401                 "BEGIN:BENV\r\n" +
402                 "BEGIN:VCARD\r\n" +
403                 "VERSION:2.1\r\n" +
404                 "N:+12485254095 \r\n" +
405                 "TEL:+12485254095\r\n" +
406                 "END:VCARD\r\n" +
407                 "BEGIN:BBODY\r\n" +
408                 "ENCODING:G-7BIT\r\n" +
409                 "LENGTH:762\r\n" +
410                 "BEGIN:MSG\r\n" +
411                 "018041000B912184254590F50000A0050003010301A8E8F41C949E83C220F69B0E7A9B41F7B79C3C07D1DF20F35BDE068541E3B77B1CA697DD617A990C6A97E7F3F0B90C0ABBC9203ABA0C32A7E5733A889E6E9741F437888E2E83E66537B92C07A5DBED32391DA697D97990FB4D4F9BF3A07619B476BFEFA03B3A4C07E5DF7550585E06B9DF74D0BC2E2F83F2EF3A681C7683C86F509A0EBABFEB\r\n" +
412                 "END:MSG\r\n" +
413                 "BEGIN:MSG\r\n" +
414                 "018041000B912184254590F50000A0050003010302C820767A5D06D1D16550DA4D2FBBC96532485E1EA7E1E9B29B0E82B3CBE17919644EBBC9A0779D0EA2A3CB20735A3EA783E8E8B4FB0CA2BF41E437C8FDA683E6757919947FD741E3B01B447E83D274D0FD5D679341ECB7BD0C4AD341F7F01C44479741E6B47C4E0791C379D0DB0C6AE741F2F2BCDE2E83CC6F3928FFAECB41E57638CD06A5E7\r\n" +
415                 "END:MSG\r\n" +
416                 "BEGIN:MSG\r\n" +
417                 "018041000B912184254590F500001A050003010303DC6F3A685E979741F9771D340EBB41E437\r\n" +
418                 "END:MSG\r\n" +
419                 "END:BBODY\r\n" +
420                 "END:BENV\r\n" +
421                 "END:BMSG\r\n";
422        try {
423            InputStream is = new ByteArrayInputStream(encoded.getBytes("UTF-8"));
424            BluetoothMapbMessage newMsg = BluetoothMapbMessage.parse(is, BluetoothMapAppParams.CHARSET_NATIVE);
425            String decoded = ((BluetoothMapbMessageSms) newMsg).getSmsBody();
426            if(D) Log.d(TAG, "\nEncoded: \n" + encoded);
427            if(D) Log.d(TAG, "\nDecoded: \n" + decoded);
428        } catch (UnsupportedEncodingException e) {
429            Log.d(TAG, "Decoding failed.",e);
430            assertTrue("Decoding failed.", false);
431        }
432    }
433
434    /***
435     * Test encoding of a simple MMS text message (UTF8). This validates most parameters.
436     */
437    public void testMmsEncodeText() {
438        BluetoothMapbMessageMime  msg = new BluetoothMapbMessageMime ();
439        String str1 =
440                 "BEGIN:BMSG\r\n" +
441                    "VERSION:1.0\r\n" +
442                    "STATUS:UNREAD\r\n" +
443                    "TYPE:MMS\r\n" +
444                    "FOLDER:telecom/msg/inbox\r\n" +
445                    "BEGIN:VCARD\r\n" +
446                        "VERSION:3.0\r\n" +
447                        "FN:Casper Bonde\r\n" +
448                        "N:Bonde,Casper\r\n" +
449                        "TEL:+4512345678\r\n" +
450                        "TEL:+4587654321\r\n" +
451                        "EMAIL:casper@email.add\r\n" +
452                        "EMAIL:bonde@email.add\r\n" +
453                    "END:VCARD\r\n" +
454                    "BEGIN:BENV\r\n" +
455                        "BEGIN:VCARD\r\n" +
456                            "VERSION:3.0\r\n" +
457                            "FN:Jørn Hansen\r\n" +
458                            "N:\r\n" +
459                            "TEL:+4512345678\r\n" +
460                            "TEL:+4587654321\r\n" +
461                            "EMAIL:casper@email.add\r\n" +
462                            "EMAIL:bonde@email.add\r\n" +
463                        "END:VCARD\r\n" +
464                        "BEGIN:BBODY\r\n" +
465                            "CHARSET:UTF-8\r\n" +
466                            "LENGTH:184\r\n" +
467                            "BEGIN:MSG\r\n" +
468                            "From: \"Jørn Hansen\" <bonde@email.add>;\r\n" +
469                            "To: \"Jørn Hansen\" <bonde@email.add>;\r\n" +
470                            "Cc: Jens Hansen <bonde@email.add>;\r\n" +
471                            "\r\n" +
472                            "This is a short message\r\n" +
473                            "\r\n" +
474                            "<partNameimage>\r\n" +
475                            "\r\n" +
476                            "END:MSG\r\n" +
477                        "END:BBODY\r\n" +
478                    "END:BENV\r\n" +
479                 "END:BMSG\r\n";
480
481        String encoded;
482        String[] phone = {"+4512345678", "+4587654321"};
483        String[] email = {"casper@email.add", "bonde@email.add"};
484        msg.addOriginator("Bonde,Casper", "Casper Bonde", phone, email, null, null);
485        msg.addRecipient("", "Jørn Hansen", phone, email, null, null);
486        msg.setFolder("inbox");
487        msg.setIncludeAttachments(false);
488        msg.addTo("Jørn Hansen", "bonde@email.add");
489        msg.addCc("Jens Hansen", "bonde@email.add");
490        msg.addFrom("Jørn Hansen", "bonde@email.add");
491        BluetoothMapbMessageMime .MimePart part = msg.addMimePart();
492        part.mPartName = "partNameText";
493        part.mContentType ="dsfajfdlk/text/asdfafda";
494        try {
495            part.mData = new String("This is a short message\r\n").getBytes("UTF-8");
496        }
497        catch (UnsupportedEncodingException e) {
498            if(D) Log.e(TAG, "UnsupportedEncodingException should never happen???", e);
499            assertTrue(false);
500        }
501
502        part = msg.addMimePart();
503        part.mPartName = "partNameimage";
504        part.mContentType = "dsfajfdlk/image/asdfafda";
505        part.mData = null;
506
507        msg.setStatus(false);
508        msg.setType(TYPE.MMS);
509        msg.updateCharset();
510
511        try {
512            encoded = new String(msg.encode());
513            if(D) Log.d(TAG, encoded);
514            assertTrue(str1.equals(encoded));
515        } catch (UnsupportedEncodingException e) {
516            Log.d(TAG, "Encoding failed.",e);
517            assertTrue("Encoding failed.", true);
518        }
519    }
520
521    public void testQuotedPrintable() {
522        testQuotedPrintableIso8859_1();
523        testQuotedPrintableUTF_8();
524    }
525
526    public void testQuotedPrintableIso8859_1() {
527        String charset = "iso-8859-1";
528        String input = "Hello, here are some danish letters: =E6=F8=E5.\r\n" +
529                       "Please check that you are able to remove soft " +
530                       "line breaks and handle '=3D' =\r\ncharacters within the text. \r\n" +
531                       "Just a sequence of non optimal characters to make " +
532                       "it complete: !\"#$@[\\]^{|}=\r\n~\r\n\r\n" +
533                       "Thanks\r\n" +
534                       "Casper";
535        String expected = "Hello, here are some danish letters: æøå.\r\n" +
536                "Please check that you are able to remove soft " +
537                "line breaks and handle '=' characters within the text. \r\n" +
538                "Just a sequence of non optimal characters to make " +
539                "it complete: !\"#$@[\\]^{|}~\r\n\r\n" +
540                "Thanks\r\n" +
541                "Casper";
542        String output;
543        output = new String(BluetoothMapUtils.quotedPrintableToUtf8(input, charset));
544        if(D) Log.d(TAG, "\nExpected: \n" + expected);
545        if(D) Log.d(TAG, "\nOutput: \n" + output);
546        assertTrue(output.equals(expected));
547    }
548
549    public void testQuotedPrintableUTF_8() {
550        String charset = "utf-8";
551        String input = "Hello, here are some danish letters: =C3=A6=C3=B8=C3=A5.\r\n" +
552                       "Please check that you are able to remove soft " +
553                       "line breaks and handle '=3D' =\r\ncharacters within the text. \r\n" +
554                       "Just a sequence of non optimal characters to make " +
555                       "it complete: !\"#$@[\\]^{|}=\r\n~\r\n\r\n" +
556                       "Thanks\r\n" +
557                       "Casper";
558        String expected = "Hello, here are some danish letters: æøå.\r\n" +
559                "Please check that you are able to remove soft " +
560                "line breaks and handle '=' characters within the text. \r\n" +
561                "Just a sequence of non optimal characters to make " +
562                "it complete: !\"#$@[\\]^{|}~\r\n\r\n" +
563                "Thanks\r\n" +
564                "Casper";
565        String output;
566        output = new String(BluetoothMapUtils.quotedPrintableToUtf8(input, charset));
567        if(D) Log.d(TAG, "\nExpected: \n" + expected);
568        if(D) Log.d(TAG, "\nOutput: \n" + output);
569        assertTrue(output.equals(expected));
570    }
571
572}
573
574