Home | History | Annotate | Download | only in utility
      1 /*
      2  * Copyright (C) 2011 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.emailcommon.utility;
     18 
     19 import com.android.emailcommon.provider.EmailContent;
     20 
     21 import android.net.Uri;
     22 import android.test.AndroidTestCase;
     23 import android.test.suitebuilder.annotation.SmallTest;
     24 
     25 import java.io.File;
     26 
     27 @SmallTest
     28 public class AttachmentUtilitiesTests extends AndroidTestCase {
     29     /**
     30      * Test static inferMimeType()
     31      * From the method doc:
     32      *
     33      * <pre>
     34      *                   |---------------------------------------------------------|
     35      *                   |                  E X T E N S I O N                      |
     36      *                   |---------------------------------------------------------|
     37      *                   | .eml        | known(.png) | unknown(.abc) | none        |
     38      * | M |-----------------------------------------------------------------------|
     39      * | I | none        | msg/rfc822  | image/png   | app/abc       | app/oct-str |
     40      * | M |-------------| (always     |             |               |             |
     41      * | E | app/oct-str |  overrides  |             |               |             |
     42      * | T |-------------|             |             |-----------------------------|
     43      * | Y | text/plain  |             |             | text/plain                  |
     44      * | P |-------------|             |-------------------------------------------|
     45      * | E | any/type    |             | any/type                                  |
     46      * |---|-----------------------------------------------------------------------|
     47      * </pre>
     48      *
     49      * Also, all results should be in lowercase.
     50      */
     51     public void testInferMimeType() {
     52         final String DEFAULT_MIX = "Application/Octet-stream";
     53         final String DEFAULT_LOWER = DEFAULT_MIX.toLowerCase();
     54         final String TEXT_PLAIN = "text/plain";
     55         final String TYPE_IMG_PNG = "image/png";
     56         final String FILE_PNG = "myfile.false.pNg";
     57         final String FILE_ABC = "myfile.false.aBc";
     58         final String FILE_NO_EXT = "myfile";
     59 
     60         // .eml files always override mime type
     61         assertEquals("message/rfc822", AttachmentUtilities.inferMimeType("a.eml", null));
     62         assertEquals("message/rfc822", AttachmentUtilities.inferMimeType("a.eml", ""));
     63         assertEquals("message/rfc822",
     64                 AttachmentUtilities.inferMimeType("a.eml", DEFAULT_LOWER));
     65         assertEquals("message/rfc822",
     66                 AttachmentUtilities.inferMimeType("a.eMl", TEXT_PLAIN));
     67         assertEquals("message/rfc822",
     68                 AttachmentUtilities.inferMimeType("a.eml", TYPE_IMG_PNG));
     69 
     70         // Non-generic, non-empty mime type; return it
     71         assertEquals("mime/type", AttachmentUtilities.inferMimeType(FILE_PNG, "Mime/TyPe"));
     72         assertEquals("mime/type", AttachmentUtilities.inferMimeType(FILE_ABC, "Mime/TyPe"));
     73         assertEquals("mime/type",
     74                 AttachmentUtilities.inferMimeType(FILE_NO_EXT, "Mime/TyPe"));
     75         assertEquals("mime/type", AttachmentUtilities.inferMimeType(null, "Mime/TyPe"));
     76         assertEquals("mime/type", AttachmentUtilities.inferMimeType("", "Mime/TyPe"));
     77 
     78         // Recognizable file extension; return known type
     79         assertEquals("image/png", AttachmentUtilities.inferMimeType(FILE_PNG, null));
     80         assertEquals("image/png", AttachmentUtilities.inferMimeType(FILE_PNG, ""));
     81         assertEquals("image/png", AttachmentUtilities.inferMimeType(FILE_PNG, DEFAULT_MIX));
     82         assertEquals("image/png", AttachmentUtilities.inferMimeType(FILE_PNG, TEXT_PLAIN));
     83 
     84         // Unrecognized and non-empty file extension, non-"text/plain" type; generate mime type
     85         assertEquals("application/abc", AttachmentUtilities.inferMimeType(FILE_ABC, null));
     86         assertEquals("application/abc", AttachmentUtilities.inferMimeType(FILE_ABC, ""));
     87         assertEquals("application/abc",
     88                 AttachmentUtilities.inferMimeType(FILE_ABC, DEFAULT_MIX));
     89 
     90         // Unrecognized and empty file extension, non-"text/plain" type; return "app/octet-stream"
     91         assertEquals(DEFAULT_LOWER, AttachmentUtilities.inferMimeType(FILE_NO_EXT, null));
     92         assertEquals(DEFAULT_LOWER, AttachmentUtilities.inferMimeType(FILE_NO_EXT, ""));
     93         assertEquals(DEFAULT_LOWER,
     94                 AttachmentUtilities.inferMimeType(FILE_NO_EXT, DEFAULT_MIX));
     95         assertEquals(DEFAULT_LOWER, AttachmentUtilities.inferMimeType(null, null));
     96         assertEquals(DEFAULT_LOWER, AttachmentUtilities.inferMimeType("", ""));
     97 
     98         // Unrecognized or empty file extension, "text/plain" type; return "text/plain"
     99         assertEquals(TEXT_PLAIN, AttachmentUtilities.inferMimeType(FILE_ABC, TEXT_PLAIN));
    100         assertEquals(TEXT_PLAIN,
    101                 AttachmentUtilities.inferMimeType(FILE_NO_EXT, TEXT_PLAIN));
    102         assertEquals(TEXT_PLAIN, AttachmentUtilities.inferMimeType(null, TEXT_PLAIN));
    103         assertEquals(TEXT_PLAIN, AttachmentUtilities.inferMimeType("", TEXT_PLAIN));
    104     }
    105 
    106     /**
    107      * Text extension extractor
    108      */
    109     public void testGetFilenameExtension() {
    110         final String FILE_NO_EXTENSION = "myfile";
    111         final String FILE_EXTENSION = "myfile.pDf";
    112         final String FILE_TWO_EXTENSIONS = "myfile.false.AbC";
    113 
    114         assertNull(AttachmentUtilities.getFilenameExtension(null));
    115         assertNull(AttachmentUtilities.getFilenameExtension(""));
    116         assertNull(AttachmentUtilities.getFilenameExtension(FILE_NO_EXTENSION));
    117 
    118         assertEquals("pdf", AttachmentUtilities.getFilenameExtension(FILE_EXTENSION));
    119         assertEquals("abc", AttachmentUtilities.getFilenameExtension(FILE_TWO_EXTENSIONS));
    120 
    121         // The API makes no claim as to how these are handled (it probably should),
    122         // but make sure that they don't crash.
    123         AttachmentUtilities.getFilenameExtension("filename.");
    124         AttachmentUtilities.getFilenameExtension(".extension");
    125     }
    126 }
    127