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 package com.android.intentresolver.ext 17 18 import android.content.ComponentName 19 import android.content.Intent 20 import com.google.common.truth.Truth.assertThat 21 import com.google.common.truth.Truth.assertWithMessage 22 import java.util.function.Predicate 23 import org.junit.Test 24 25 class IntentExtTest { 26 27 private val hasSendAction = <lambda>null28 Predicate<Intent> { 29 it?.action == Intent.ACTION_SEND || it?.action == Intent.ACTION_SEND_MULTIPLE 30 } 31 32 @Test hasActionnull33 fun hasAction() { 34 val sendIntent = Intent(Intent.ACTION_SEND) 35 assertThat(sendIntent.hasAction(Intent.ACTION_SEND)).isTrue() 36 assertThat(sendIntent.hasAction(Intent.ACTION_VIEW)).isFalse() 37 } 38 39 @Test hasComponentnull40 fun hasComponent() { 41 assertThat(Intent().hasComponent()).isFalse() 42 assertThat(Intent().setComponent(ComponentName("A", "B")).hasComponent()).isTrue() 43 } 44 45 @Test hasSendActionnull46 fun hasSendAction() { 47 assertThat(Intent(Intent.ACTION_SEND).hasSendAction()).isTrue() 48 assertThat(Intent(Intent.ACTION_SEND_MULTIPLE).hasSendAction()).isTrue() 49 assertThat(Intent(Intent.ACTION_SENDTO).hasSendAction()).isFalse() 50 assertThat(Intent(Intent.ACTION_VIEW).hasSendAction()).isFalse() 51 } 52 53 @Test hasSingleCategorynull54 fun hasSingleCategory() { 55 val intent = Intent().addCategory(Intent.CATEGORY_HOME) 56 assertThat(intent.hasSingleCategory(Intent.CATEGORY_HOME)).isTrue() 57 assertThat(intent.hasSingleCategory(Intent.CATEGORY_DEFAULT)).isFalse() 58 59 intent.addCategory(Intent.CATEGORY_TEST) 60 assertThat(intent.hasSingleCategory(Intent.CATEGORY_TEST)).isFalse() 61 } 62 63 @Test ifMatch_matchednull64 fun ifMatch_matched() { 65 val sendIntent = Intent(Intent.ACTION_SEND) 66 val sendMultipleIntent = Intent(Intent.ACTION_SEND_MULTIPLE) 67 68 sendIntent.ifMatch(hasSendAction) { addFlags(Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) } 69 sendMultipleIntent.ifMatch(hasSendAction) { addFlags(Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) } 70 assertWithMessage("sendIntent flags") 71 .that(sendIntent.flags) 72 .isEqualTo(Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) 73 assertWithMessage("sendMultipleIntent flags") 74 .that(sendMultipleIntent.flags) 75 .isEqualTo(Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) 76 } 77 78 @Test ifMatch_notMatchednull79 fun ifMatch_notMatched() { 80 val viewIntent = Intent(Intent.ACTION_VIEW) 81 82 viewIntent.ifMatch(hasSendAction) { addFlags(Intent.FLAG_ACTIVITY_MATCH_EXTERNAL) } 83 assertWithMessage("viewIntent flags").that(viewIntent.flags).isEqualTo(0) 84 } 85 } 86