1 /* <lambda>null2 * Copyright (C) 2023 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.intentresolver 18 19 import android.app.PendingIntent 20 import android.content.Intent 21 import android.graphics.drawable.Icon 22 import android.net.Uri 23 import android.service.chooser.ChooserAction 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import androidx.test.platform.app.InstrumentationRegistry 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 30 @RunWith(AndroidJUnit4::class) 31 class ChooserRequestParametersTest { 32 val flags = TestFeatureFlagRepository(mapOf()) 33 34 @Test 35 fun testChooserActions() { 36 val actionCount = 3 37 val intent = Intent(Intent.ACTION_SEND) 38 val actions = createChooserActions(actionCount) 39 val chooserIntent = 40 Intent(Intent.ACTION_CHOOSER).apply { 41 putExtra(Intent.EXTRA_INTENT, intent) 42 putExtra(Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS, actions) 43 } 44 val request = ChooserRequestParameters(chooserIntent, "", Uri.EMPTY, flags) 45 assertThat(request.chooserActions).containsExactlyElementsIn(actions).inOrder() 46 } 47 48 @Test 49 fun testChooserActions_empty() { 50 val intent = Intent(Intent.ACTION_SEND) 51 val chooserIntent = 52 Intent(Intent.ACTION_CHOOSER).apply { putExtra(Intent.EXTRA_INTENT, intent) } 53 val request = ChooserRequestParameters(chooserIntent, "", Uri.EMPTY, flags) 54 assertThat(request.chooserActions).isEmpty() 55 } 56 57 @Test 58 fun testChooserActions_tooMany() { 59 val intent = Intent(Intent.ACTION_SEND) 60 val chooserActions = createChooserActions(10) 61 val chooserIntent = 62 Intent(Intent.ACTION_CHOOSER).apply { 63 putExtra(Intent.EXTRA_INTENT, intent) 64 putExtra(Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS, chooserActions) 65 } 66 67 val request = ChooserRequestParameters(chooserIntent, "", Uri.EMPTY, flags) 68 69 val expectedActions = chooserActions.sliceArray(0 until 5) 70 assertThat(request.chooserActions).containsExactlyElementsIn(expectedActions).inOrder() 71 } 72 73 private fun createChooserActions(count: Int): Array<ChooserAction> { 74 return Array(count) { i -> createChooserAction("$i") } 75 } 76 77 private fun createChooserAction(label: CharSequence): ChooserAction { 78 val icon = Icon.createWithContentUri("content://org.package.app/image") 79 val pendingIntent = 80 PendingIntent.getBroadcast( 81 InstrumentationRegistry.getInstrumentation().getTargetContext(), 82 0, 83 Intent("TESTACTION"), 84 PendingIntent.FLAG_IMMUTABLE 85 ) 86 return ChooserAction.Builder(icon, label, pendingIntent).build() 87 } 88 } 89