MediaMimeTest.java revision 105925376f8d0f6b318c9938c7b83ef7fef094da
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 17package com.android.mediaframeworktest.functional; 18 19import java.io.File; 20 21import android.content.Context; 22import android.content.Intent; 23import android.content.pm.ActivityInfo; 24import android.content.pm.PackageManager; 25import android.content.pm.ResolveInfo; 26import android.net.Uri; 27import android.test.ActivityInstrumentationTestCase2; 28import android.test.suitebuilder.annotation.LargeTest; 29import android.test.suitebuilder.annotation.MediumTest; 30import android.test.suitebuilder.annotation.Suppress; 31import android.util.Log; 32import com.android.mediaframeworktest.MediaFrameworkTest; 33 34/* 35 * System tests for the handling of mime type in the media framework. 36 * 37 * To run this test suite: 38 make frameworks/base/media/tests/MediaFrameworkTest 39 make mediaframeworktest 40 41 adb install -r out/target/product/dream/data/app/mediaframeworktest.apk 42 43 adb shell am instrument -e class \ 44 com.android.mediaframeworktest.functional.MediaMimeTest \ 45 -w com.android.mediaframeworktest/.MediaFrameworkTestRunner 46 * 47 */ 48public class MediaMimeTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> { 49 private final String TAG = "MediaMimeTest"; 50 private Context mContext; 51 private final String MP3_FILE = "/sdcard/media_api/music/SHORTMP3.mp3"; 52 private final String MEDIA_PLAYBACK_NAME = "com.android.music.MediaPlaybackActivity"; 53 54 public MediaMimeTest() { 55 super("com.android.mediaframeworktest", MediaFrameworkTest.class); 56 } 57 58 @Override 59 protected void setUp() throws Exception { 60 super.setUp(); 61 mContext = getActivity(); 62 // Checks you have all the test files on your SDCARD. 63 assertTrue(new File(MP3_FILE).exists()); 64 } 65 66 @Override 67 protected void tearDown() throws Exception { 68 super.tearDown(); 69 } 70 71 // ---------------------------------------------------------------------- 72 // AUDIO mime type resolution tests. 73 74 @MediumTest 75 // Checks the MediaPlaybackActivity handles audio/mp3. 76 public void testCheckMediaPlaybackHandlesAudioMp3() throws Exception { 77 assertMediaPlaybackActivityHandles("audio/mp3"); 78 } 79 80 @MediumTest 81 // Checks the MediaPlaybackActivity handles audio/*. 82 public void testCheckMediaPlaybackHandlesAudio() throws Exception { 83 assertMediaPlaybackActivityHandles("audio/*"); 84 } 85 86 @MediumTest 87 // Checks the MediaPlaybackActivity handles application/itunes. Some servers 88 // set the Content-type header to application/iTunes (with capital T, but 89 // the download manager downcasts it) for their MP3 podcasts. This is non 90 // standard but we try to support it anyway. 91 // See bug 1401491 92 public void testCheckMediaPlaybackHandlesApplicationItunes() throws Exception { 93 assertMediaPlaybackActivityHandles("application/itunes"); 94 } 95 96 @MediumTest 97 // Checks the activity resolver handling of mime types is case sensitive. 98 // See bug 1710534 99 public void testCheckActivityResolverMimeHandlingIsCaseSensitive() throws Exception { 100 assertNoActivityHandles("AUDIO/MP3"); // <--- look uppercase 101 } 102 103 @MediumTest 104 // Checks the activity resolver does not trims leading whitespaces when 105 // resolving mime types. Trailing whitespaces seems to be non 106 // significant. 107 // See bug 1710534 108 public void testCheckWhiteSpacesInMimeTypeHandling() throws Exception { 109 assertNoActivityHandles(" audio/mp3"); 110 assertNoActivityHandles(" audio/mp3 "); 111 assertMediaPlaybackActivityHandles("audio/mp3 "); 112 } 113 114 // @return a ResolveInfo instance for the mime type or null if the type is 115 // not handled by any activity. 116 private ResolveInfo resolveMime(String mime) { 117 Intent viewIntent = new Intent(Intent.ACTION_VIEW); 118 Uri uri = Uri.fromParts("file", MP3_FILE, null); 119 120 viewIntent.setDataAndType(uri, mime); 121 return mContext.getPackageManager().resolveActivity( 122 viewIntent, PackageManager.MATCH_DEFAULT_ONLY); 123 } 124 125 // Helper method to check the media playback activity handles the given mime type. 126 // @param mime type to test for 127 private void assertMediaPlaybackActivityHandles(String mime) throws Exception { 128 ResolveInfo ri = resolveMime(mime); 129 130 assertNotNull(ri); 131 assertEquals(MEDIA_PLAYBACK_NAME, ri.activityInfo.name.toString()); 132 } 133 134 // Helper method to check that NO activity handles the given mime type. 135 // @param mime type to test for 136 private void assertNoActivityHandles(String mime) throws Exception { 137 assertNull(resolveMime(mime)); 138 } 139} 140