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