• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.exchange;
18 
19 import com.android.email.provider.EmailContent.Account;
20 import com.android.email.provider.EmailContent.Mailbox;
21 import com.android.exchange.adapter.EmailSyncAdapter;
22 import com.android.exchange.adapter.EmailSyncAdapter.EasEmailSyncParser;
23 
24 import android.test.AndroidTestCase;
25 
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.GregorianCalendar;
30 import java.util.TimeZone;
31 
32 public class EasEmailSyncAdapterTests extends AndroidTestCase {
33 
34     /**
35      * Create and return a short, simple InputStream that has at least four bytes, which is all
36      * that's required to initialize an EasParser (the parent class of EasEmailSyncParser)
37      * @return the InputStream
38      */
getTestInputStream()39     public InputStream getTestInputStream() {
40         return new ByteArrayInputStream(new byte[] {0, 0, 0, 0, 0});
41     }
42 
getTestService()43     EasSyncService getTestService() {
44         Account account = new Account();
45         account.mId = -1;
46         Mailbox mailbox = new Mailbox();
47         mailbox.mId = -1;
48         EasSyncService service = new EasSyncService();
49         service.mContext = getContext();
50         service.mMailbox = mailbox;
51         service.mAccount = account;
52         return service;
53     }
54 
getTestSyncAdapter()55     EmailSyncAdapter getTestSyncAdapter() {
56         EasSyncService service = getTestService();
57         EmailSyncAdapter adapter = new EmailSyncAdapter(service.mMailbox, service);
58         return adapter;
59     }
60 
61     /**
62      * Check functionality for getting mime type from a file name (using its extension)
63      * The default for all unknown files is application/octet-stream
64      */
testGetMimeTypeFromFileName()65     public void testGetMimeTypeFromFileName() throws IOException {
66         EasSyncService service = getTestService();
67         EmailSyncAdapter adapter = new EmailSyncAdapter(service.mMailbox, service);
68         EasEmailSyncParser p = adapter.new EasEmailSyncParser(getTestInputStream(), adapter);
69         // Test a few known types
70         String mimeType = p.getMimeTypeFromFileName("foo.jpg");
71         assertEquals("image/jpeg", mimeType);
72         // Make sure this is case insensitive
73         mimeType = p.getMimeTypeFromFileName("foo.JPG");
74         assertEquals("image/jpeg", mimeType);
75         mimeType = p.getMimeTypeFromFileName("this_is_a_weird_filename.gif");
76         assertEquals("image/gif", mimeType);
77         // Test an illegal file name ending with the extension prefix
78         mimeType = p.getMimeTypeFromFileName("foo.");
79         assertEquals("application/octet-stream", mimeType);
80         // Test a really awful name
81         mimeType = p.getMimeTypeFromFileName(".....");
82         assertEquals("application/octet-stream", mimeType);
83         // Test a bare file name (no extension)
84         mimeType = p.getMimeTypeFromFileName("foo");
85         assertEquals("application/octet-stream", mimeType);
86         // And no name at all (null isn't a valid input)
87         mimeType = p.getMimeTypeFromFileName("");
88         assertEquals("application/octet-stream", mimeType);
89     }
90 
testFormatDateTime()91     public void testFormatDateTime() throws IOException {
92         EmailSyncAdapter adapter = getTestSyncAdapter();
93         GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
94         // Calendar is odd, months are zero based, so the first 11 below is December...
95         calendar.set(2008, 11, 11, 18, 19, 20);
96         String date = adapter.formatDateTime(calendar);
97         assertEquals("2008-12-11T18:19:20.000Z", date);
98         calendar.clear();
99         calendar.set(2012, 0, 2, 23, 0, 1);
100         date = adapter.formatDateTime(calendar);
101         assertEquals("2012-01-02T23:00:01.000Z", date);
102     }
103 }
104