1 /*
2  * Copyright 2020 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 android.packageinstaller.install.cts
17 
18 import android.app.Activity
19 import android.content.Intent
20 import android.content.Intent.ACTION_INSTALL_PACKAGE
21 import android.content.pm.PackageInstaller
22 import android.content.pm.PackageManager.MATCH_DEFAULT_ONLY
23 import android.net.Uri
24 import android.platform.test.annotations.AppModeFull
25 import androidx.test.ext.junit.runners.AndroidJUnit4
26 import androidx.test.platform.app.InstrumentationRegistry
27 import com.google.common.truth.Truth.assertThat
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import java.util.concurrent.TimeUnit
31 
32 private const val INSTALL_BUTTON_ID = "button1"
33 
34 @RunWith(AndroidJUnit4::class)
35 @AppModeFull(reason = "Instant apps cannot install packages")
36 class InstallSourceInfoTest : PackageInstallerTestBase() {
37     private val context = InstrumentationRegistry.getInstrumentation().targetContext
38     private val pm = context.packageManager
39     private val ourPackageName = context.packageName
40 
41     @Test
installViaIntentnull42     fun installViaIntent() {
43         assumeNotWatch()
44 
45         val packageInstallerPackageName = getPackageInstallerPackageName()
46 
47         val installation = startInstallationViaIntent()
48         clickInstallerUIButton(INSTALL_BUTTON_ID)
49 
50         // Install should have succeeded
51         assertThat(installation.get(TIMEOUT, TimeUnit.MILLISECONDS)).isEqualTo(Activity.RESULT_OK)
52 
53         val info = pm.getInstallSourceInfo(TEST_APK_PACKAGE_NAME)
54         assertThat(info.getInstallingPackageName()).isEqualTo(packageInstallerPackageName)
55         assertThat(info.getInitiatingPackageName()).isEqualTo(packageInstallerPackageName)
56         assertThat(info.getOriginatingPackageName()).isNull()
57     }
58 
59     @Test
InstallViaSessionnull60     fun InstallViaSession() {
61         assumeNotWatch()
62 
63         startInstallationViaSession()
64         clickInstallerUIButton(INSTALL_BUTTON_ID)
65 
66         // Install should have succeeded
67         assertThat(getInstallSessionResult()).isEqualTo(PackageInstaller.STATUS_SUCCESS)
68 
69         val info = pm.getInstallSourceInfo(TEST_APK_PACKAGE_NAME)
70         assertThat(info.getInstallingPackageName()).isEqualTo(ourPackageName)
71         assertThat(info.getInitiatingPackageName()).isEqualTo(ourPackageName)
72         assertThat(info.getOriginatingPackageName()).isNull()
73     }
74 
getPackageInstallerPackageNamenull75     private fun getPackageInstallerPackageName(): String {
76         val installerIntent = Intent(ACTION_INSTALL_PACKAGE)
77         installerIntent.setDataAndType(Uri.parse("content://com.example/"),
78                 "application/vnd.android.package-archive")
79         return installerIntent.resolveActivityInfo(pm, MATCH_DEFAULT_ONLY).packageName
80     }
81 }
82