MediaMimeTest.java revision c8c7ca7bd769df9288575b322e10ebf1fb22e4a5
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 // TODO: temporarily remove from medium suite because it hangs whole suite 87 // @MediumTest 88 // Checks the MediaPlaybackActivity handles application/itunes. Some servers 89 // set the Content-type header to application/iTunes (with capital T, but 90 // the download manager downcasts it) for their MP3 podcasts. This is non 91 // standard but we try to support it anyway. 92 // See bug 1401491 93 public void testCheckMediaPlaybackHandlesApplicationItunes() throws Exception { 94 assertMediaPlaybackActivityHandles("application/itunes"); 95 } 96 97 @MediumTest 98 // Checks the activity resolver handling of mime types is case sensitive. 99 // See bug 1710534 100 public void testCheckActivityResolverMimeHandlingIsCaseSensitive() throws Exception { 101 assertNoActivityHandles("AUDIO/MP3"); // <--- look uppercase 102 } 103 104 @MediumTest 105 // Checks the activity resolver does not trims leading whitespaces when 106 // resolving mime types. Trailing whitespaces seems to be non 107 // significant. 108 // See bug 1710534 109 public void testCheckWhiteSpacesInMimeTypeHandling() throws Exception { 110 assertNoActivityHandles(" audio/mp3"); 111 assertNoActivityHandles(" audio/mp3 "); 112 assertMediaPlaybackActivityHandles("audio/mp3 "); 113 } 114 115 // @return a ResolveInfo instance for the mime type or null if the type is 116 // not handled by any activity. 117 private ResolveInfo resolveMime(String mime) { 118 Intent viewIntent = new Intent(Intent.ACTION_VIEW); 119 Uri uri = Uri.fromParts("file", MP3_FILE, null); 120 121 viewIntent.setDataAndType(uri, mime); 122 return mContext.getPackageManager().resolveActivity( 123 viewIntent, PackageManager.MATCH_DEFAULT_ONLY); 124 } 125 126 // Helper method to check the media playback activity handles the given mime type. 127 // @param mime type to test for 128 private void assertMediaPlaybackActivityHandles(String mime) throws Exception { 129 ResolveInfo ri = resolveMime(mime); 130 131 assertNotNull(ri); 132 assertEquals(MEDIA_PLAYBACK_NAME, ri.activityInfo.name.toString()); 133 } 134 135 // Helper method to check that NO activity handles the given mime type. 136 // @param mime type to test for 137 private void assertNoActivityHandles(String mime) throws Exception { 138 assertNull(resolveMime(mime)); 139 } 140} 141