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 53 public MediaMimeTest() { 54 super("com.android.mediaframeworktest", MediaFrameworkTest.class); 55 } 56 57 @Override 58 protected void setUp() throws Exception { 59 super.setUp(); 60 mContext = getActivity(); 61 // Checks you have all the test files on your SDCARD. 62 assertTrue(new File(MP3_FILE).exists()); 63 } 64 65 @Override 66 protected void tearDown() throws Exception { 67 super.tearDown(); 68 } 69 70 // ---------------------------------------------------------------------- 71 // AUDIO mime type resolution tests. 72 73 @MediumTest 74 // Checks the MediaPlaybackActivity handles audio/mp3. 75 public void testCheckMediaPlaybackHandlesAudioMp3() throws Exception { 76 assertMediaPlaybackActivityHandles("audio/mp3"); 77 } 78 79 @Suppress 80 // Checks the MediaPlaybackActivity handles audio/*. 81 public void testCheckMediaPlaybackHandlesAudio() throws Exception { 82 assertMediaPlaybackActivityHandles("audio/*"); 83 } 84 85 // TODO: temporarily remove from medium suite because it hangs whole suite 86 // @MediumTest 87 // Checks the MediaPlaybackActivity handles application/itunes. Some servers 88 // set the Content-type hadb ieader 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 } 132 133 // Helper method to check that NO activity handles the given mime type. 134 // @param mime type to test for 135 private void assertNoActivityHandles(String mime) throws Exception { 136 assertNull(resolveMime(mime)); 137 } 138} 139