1 /*
2  * 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.emptystate
18 
19 import android.content.ContentResolver
20 import android.content.Intent
21 import android.content.pm.IPackageManager
22 import com.google.common.truth.Truth.assertThat
23 import org.junit.Test
24 import org.mockito.Mockito.any
25 import org.mockito.Mockito.anyInt
26 import org.mockito.Mockito.eq
27 import org.mockito.Mockito.nullable
28 import org.mockito.kotlin.doReturn
29 import org.mockito.kotlin.mock
30 
31 class CrossProfileIntentsCheckerTest {
32     private val PERSONAL_USER_ID = 10
33     private val WORK_USER_ID = 20
34 
35     private val contentResolver = mock<ContentResolver>()
36 
37     @Test
testChecker_hasCrossProfileIntentsnull38     fun testChecker_hasCrossProfileIntents() {
39         val packageManager =
40             mock<IPackageManager> {
41                 on {
42                     canForwardTo(
43                         any(Intent::class.java),
44                         nullable(String::class.java),
45                         eq(PERSONAL_USER_ID),
46                         eq(WORK_USER_ID)
47                     )
48                 } doReturn (true)
49             }
50         val checker = CrossProfileIntentsChecker(contentResolver, packageManager)
51         val intents = listOf(Intent())
52         assertThat(checker.hasCrossProfileIntents(intents, PERSONAL_USER_ID, WORK_USER_ID)).isTrue()
53     }
54 
55     @Test
testChecker_noCrossProfileIntentsnull56     fun testChecker_noCrossProfileIntents() {
57         val packageManager =
58             mock<IPackageManager> {
59                 on {
60                     canForwardTo(
61                         any(Intent::class.java),
62                         nullable(String::class.java),
63                         anyInt(),
64                         anyInt()
65                     )
66                 } doReturn (false)
67             }
68         val checker = CrossProfileIntentsChecker(contentResolver, packageManager)
69         val intents = listOf(Intent())
70         assertThat(checker.hasCrossProfileIntents(intents, PERSONAL_USER_ID, WORK_USER_ID))
71             .isFalse()
72     }
73 
74     @Test
testChecker_noIntentsnull75     fun testChecker_noIntents() {
76         val packageManager = mock<IPackageManager>()
77         val checker = CrossProfileIntentsChecker(contentResolver, packageManager)
78         val intents = listOf<Intent>()
79         assertThat(checker.hasCrossProfileIntents(intents, PERSONAL_USER_ID, WORK_USER_ID))
80             .isFalse()
81     }
82 }
83