1 /* 2 * Copyright (C) 2024 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 android.content.cts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.platform.test.annotations.AppModeFull; 26 27 import androidx.test.ext.junit.runners.AndroidJUnit4; 28 import androidx.test.platform.app.InstrumentationRegistry; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.util.List; 34 35 @RunWith(AndroidJUnit4.class) 36 @AppModeFull // TODO(Instant) Figure out which intents should be visible 37 public class AvailableDocumentIntentsTest { 38 39 /** 40 * Assert target intent can be handled by at least one Activity. 41 * 42 * @param intent - the Intent will be handled. 43 */ assertCanBeHandled(final Intent intent)44 private void assertCanBeHandled(final Intent intent) { 45 final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); 46 final PackageManager packageManager = context.getPackageManager(); 47 List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0); 48 assertThat(resolveInfoList).isNotNull(); 49 // one or more activity can handle this intent. 50 assertThat(resolveInfoList.size()).isGreaterThan(0); 51 } 52 53 @Test testOpenDocumentAny()54 public void testOpenDocumentAny() { 55 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 56 intent.addCategory(Intent.CATEGORY_OPENABLE); 57 intent.setType("*/*"); 58 assertCanBeHandled(intent); 59 } 60 61 @Test testOpenDocumentImage()62 public void testOpenDocumentImage() { 63 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 64 intent.addCategory(Intent.CATEGORY_OPENABLE); 65 intent.setType("image/*"); 66 assertCanBeHandled(intent); 67 } 68 69 @Test testCreateDocument()70 public void testCreateDocument() { 71 Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); 72 intent.addCategory(Intent.CATEGORY_OPENABLE); 73 intent.setType("text/plain"); 74 assertCanBeHandled(intent); 75 } 76 } 77