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.settings.spa.development.compat
18 
19 import android.content.Context
20 import android.content.pm.ApplicationInfo
21 import android.content.pm.PackageManager
22 import android.content.pm.PackageManager.PackageInfoFlags
23 import androidx.compose.ui.test.junit4.createComposeRule
24 import androidx.test.core.app.ApplicationProvider
25 import androidx.test.ext.junit.runners.AndroidJUnit4
26 import com.google.common.truth.Truth.assertThat
27 import kotlinx.coroutines.flow.first
28 import kotlinx.coroutines.flow.flowOf
29 import kotlinx.coroutines.test.runTest
30 import org.junit.Before
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.Mock
35 import org.mockito.Mockito.any
36 import org.mockito.Mockito.anyInt
37 import org.mockito.Spy
38 import org.mockito.junit.MockitoJUnit
39 import org.mockito.junit.MockitoRule
40 import org.mockito.Mockito.`when` as whenever
41 
42 @RunWith(AndroidJUnit4::class)
43 class PlatformCompatAppListModelTest {
44     @get:Rule
45     val composeTestRule = createComposeRule()
46 
47     @get:Rule
48     val mockito: MockitoRule = MockitoJUnit.rule()
49 
50     @Spy
51     private val context: Context = ApplicationProvider.getApplicationContext()
52 
53     @Mock
54     private lateinit var packageManager: PackageManager
55 
56     private lateinit var listModel: PlatformCompatAppListModel
57 
58     @Before
setUpnull59     fun setUp() {
60         whenever(context.packageManager).thenReturn(packageManager)
61         whenever(packageManager.getInstalledPackagesAsUser(any<PackageInfoFlags>(), anyInt()))
62             .thenReturn(emptyList())
63         listModel = PlatformCompatAppListModel(context)
64     }
65 
66     @Test
<lambda>null67     fun transform() = runTest {
68         val recordListFlow = listModel.transform(
69             userIdFlow = flowOf(USER_ID),
70             appListFlow = flowOf(listOf(APP)),
71         )
72 
73         val recordList = recordListFlow.first()
74         assertThat(recordList).hasSize(1)
75         val record = recordList[0]
76         assertThat(record.app).isSameInstanceAs(APP)
77     }
78 
79     @Test
<lambda>null80     fun getSummary() = runTest {
81         val summary = getSummary(APP)
82 
83         assertThat(summary).isEqualTo(PACKAGE_NAME)
84     }
85 
getSummarynull86     private fun getSummary(app: ApplicationInfo): String {
87         lateinit var summary: () -> String
88         composeTestRule.setContent {
89             summary = listModel.getSummary(
90                 option = 0,
91                 record = PlatformCompatAppRecord(app),
92             )
93         }
94         return summary()
95     }
96 
97     private companion object {
98         const val USER_ID = 0
99         const val PACKAGE_NAME = "package.name"
<lambda>null100         val APP = ApplicationInfo().apply {
101             packageName = PACKAGE_NAME
102         }
103     }
104 }