1 /* 2 * Copyright (C) 2021 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.providers.media; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.media.ApplicationMediaCapabilities; 22 import android.media.MediaFormat; 23 import android.os.Bundle; 24 import android.os.Environment; 25 import android.os.Process; 26 import android.provider.MediaStore; 27 28 import androidx.test.InstrumentationRegistry; 29 import androidx.test.filters.SdkSuppress; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.util.Arrays; 36 import java.util.List; 37 import java.util.Random; 38 39 @RunWith(AndroidJUnit4.class) 40 @SdkSuppress(minSdkVersion = 31, codeName = "S") 41 public class TranscodeHelperTest { 42 private static final String SOME_VALID_FILE_PATH = 43 "/storage/emulated/0/" + Environment.DIRECTORY_DCIM + "/Camera/some_filename.mp4"; 44 45 private final ConfigStore mConfigStore = new TestConfigStore(); 46 private final MediaProvider mDefaultMediaProvider = new MediaProvider() { 47 @Override 48 protected ConfigStore provideConfigStore() { 49 return mConfigStore; 50 } 51 }; 52 53 private final TranscodeHelperImpl mUnderTest = new TranscodeHelperImpl( 54 InstrumentationRegistry.getTargetContext(), mDefaultMediaProvider, mConfigStore); 55 56 @Test testSupportsValidTranscodePath()57 public void testSupportsValidTranscodePath() { 58 List<String> filePaths = Arrays.asList( 59 "/storage/emulated/0/" + Environment.DIRECTORY_DCIM + "/Camera/filename.mp4", 60 "/storage/emulated/1/" + Environment.DIRECTORY_DCIM + "/Camera/filename.mp4", 61 "/storage/emulated/0/dcim/camera/filename.mp4"); 62 63 for (String path : filePaths) { 64 assertThat(mUnderTest.supportsTranscode(path)).isTrue(); 65 } 66 } 67 68 @Test testDoesNotSupportsInvalidTranscodePath()69 public void testDoesNotSupportsInvalidTranscodePath() { 70 List<String> filePaths = Arrays.asList( 71 "/storage/emulated/ab/" + Environment.DIRECTORY_DCIM + "/Camera/filename.mp4", 72 "/storage/emulated/0/" + Environment.DIRECTORY_DCIM + "/Camera/dir/filename.mp4", 73 "/storage/emulate/" + Environment.DIRECTORY_DCIM + "/Camera/filename.mp4", 74 "/storage/emulated/" + Environment.DIRECTORY_DCIM + "/Camera/filename.jpeg", 75 "/storage/emulated/0/dcmi/Camera/dir/filename.mp4"); 76 77 for (String path : filePaths) { 78 assertThat(mUnderTest.supportsTranscode(path)).isFalse(); 79 } 80 } 81 82 @Test testDoesNotTranscodeForMediaProvider()83 public void testDoesNotTranscodeForMediaProvider() { 84 int transcodeReason = mUnderTest.shouldTranscode(SOME_VALID_FILE_PATH, 85 TranscodeHelperImpl.getMyUid(), 86 null); 87 assertThat(transcodeReason).isEqualTo(0); 88 89 Random random = new Random(System.currentTimeMillis()); 90 Bundle bundle = new Bundle(); 91 bundle.putInt(MediaStore.EXTRA_MEDIA_CAPABILITIES_UID, TranscodeHelperImpl.getMyUid()); 92 int randomAppUid = random.nextInt( 93 Process.LAST_APPLICATION_UID - Process.FIRST_APPLICATION_UID + 1) 94 + Process.FIRST_APPLICATION_UID; 95 transcodeReason = mUnderTest.shouldTranscode(SOME_VALID_FILE_PATH, randomAppUid, bundle); 96 assertThat(transcodeReason).isEqualTo(0); 97 } 98 99 @Test testDoesNotTranscodeForSystemProcesses()100 public void testDoesNotTranscodeForSystemProcesses() { 101 Random random = new Random(System.currentTimeMillis()); 102 int randomSystemProcessUid = random.nextInt(Process.FIRST_APPLICATION_UID); 103 int transcodeReason = mUnderTest.shouldTranscode(SOME_VALID_FILE_PATH, 104 randomSystemProcessUid, null); 105 assertThat(transcodeReason).isEqualTo(0); 106 } 107 108 @Test testDoesNotTranscodeIfAppAcceptsOriginalFormat()109 public void testDoesNotTranscodeIfAppAcceptsOriginalFormat() { 110 Random random = new Random(System.currentTimeMillis()); 111 Bundle bundle = new Bundle(); 112 bundle.putBoolean(MediaStore.EXTRA_ACCEPT_ORIGINAL_MEDIA_FORMAT, true); 113 int randomAppUid = random.nextInt( 114 Process.LAST_APPLICATION_UID - Process.FIRST_APPLICATION_UID + 1) 115 + Process.FIRST_APPLICATION_UID; 116 int transcodeReason = mUnderTest.doesAppNeedTranscoding(randomAppUid, bundle, 117 TranscodeHelperImpl.FLAG_HEVC, 0); 118 assertThat(transcodeReason).isEqualTo(0); 119 } 120 121 @Test testDoesNotTranscodeIfAppExtraMediaCapabilitiesHevc_supported()122 public void testDoesNotTranscodeIfAppExtraMediaCapabilitiesHevc_supported() { 123 Random random = new Random(System.currentTimeMillis()); 124 ApplicationMediaCapabilities capabilities = 125 new ApplicationMediaCapabilities.Builder().addSupportedVideoMimeType( 126 MediaFormat.MIMETYPE_VIDEO_HEVC).build(); 127 Bundle bundle = new Bundle(); 128 bundle.putParcelable(MediaStore.EXTRA_MEDIA_CAPABILITIES, capabilities); 129 int randomAppUid = random.nextInt( 130 Process.LAST_APPLICATION_UID - Process.FIRST_APPLICATION_UID + 1) 131 + Process.FIRST_APPLICATION_UID; 132 int transcodeReason = mUnderTest.doesAppNeedTranscoding(randomAppUid, bundle, 133 TranscodeHelperImpl.FLAG_HEVC, 0); 134 assertThat(transcodeReason).isEqualTo(0); 135 } 136 137 @Test testTranscodesIfAppExtraMediaCapabilitiesHevc_unsupported()138 public void testTranscodesIfAppExtraMediaCapabilitiesHevc_unsupported() { 139 Random random = new Random(System.currentTimeMillis()); 140 ApplicationMediaCapabilities capabilities = 141 new ApplicationMediaCapabilities.Builder().addUnsupportedVideoMimeType( 142 MediaFormat.MIMETYPE_VIDEO_HEVC).build(); 143 Bundle bundle = new Bundle(); 144 bundle.putParcelable(MediaStore.EXTRA_MEDIA_CAPABILITIES, capabilities); 145 int randomAppUid = random.nextInt( 146 Process.LAST_APPLICATION_UID - Process.FIRST_APPLICATION_UID + 1) 147 + Process.FIRST_APPLICATION_UID; 148 int transcodeReason = mUnderTest.doesAppNeedTranscoding(randomAppUid, bundle, 149 TranscodeHelperImpl.FLAG_HEVC, 0); 150 assertThat(transcodeReason).isEqualTo( 151 MediaProviderStatsLog.TRANSCODING_DATA__ACCESS_REASON__APP_EXTRA); 152 } 153 } 154