1 /*
2  * Copyright (C) 2018 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.RESULT_CANCELED
19 import android.app.Activity.RESULT_OK
20 import android.content.Intent
21 import android.net.Uri
22 import android.platform.test.annotations.AppModeFull
23 
24 import androidx.test.InstrumentationRegistry
25 import androidx.test.runner.AndroidJUnit4
26 
27 import org.junit.After
28 import org.junit.Assert.assertEquals
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 
32 import java.util.concurrent.TimeUnit
33 
34 private const val INSTALL_BUTTON_ID = "button1"
35 private const val CANCEL_BUTTON_ID = "button2"
36 
37 @RunWith(AndroidJUnit4::class)
38 @AppModeFull(reason = "Instant apps cannot install packages")
39 class IntentTest : PackageInstallerTestBase() {
40     private val context = InstrumentationRegistry.getTargetContext()
41 
42     @After
disableSecureFrpnull43     fun disableSecureFrp() {
44         setSecureFrp(false)
45     }
46 
47     /**
48      * Check that we can install an app via a package-installer intent
49      */
50     @Test
confirmInstallationnull51     fun confirmInstallation() {
52         assumeNotWatch()
53 
54         val installation = startInstallationViaIntent()
55         clickInstallerUIButton(INSTALL_BUTTON_ID)
56 
57         // Install should have succeeded
58         assertEquals(RESULT_OK, installation.get(TIMEOUT, TimeUnit.MILLISECONDS))
59         assertInstalled()
60     }
61 
62     /**
63      * Install an app via a package-installer intent, but then cancel it when the package installer
64      * pops open.
65      */
66     @Test
cancelInstallationnull67     fun cancelInstallation() {
68         assumeNotWatch()
69 
70         val installation = startInstallationViaIntent()
71         clickInstallerUIButton(CANCEL_BUTTON_ID)
72 
73         // Install should have been aborted
74         assertEquals(RESULT_CANCELED, installation.get(TIMEOUT, TimeUnit.MILLISECONDS))
75         assertNotInstalled()
76     }
77 
78     /**
79      * Make sure that an already installed app can be reinstalled via a "package" uri
80      */
81     @Test
reinstallViaPackageUrinull82     fun reinstallViaPackageUri() {
83         assumeNotWatch()
84 
85         // Regular install
86         confirmInstallation()
87 
88         // Reinstall
89         val intent = Intent(Intent.ACTION_INSTALL_PACKAGE)
90         intent.data = Uri.fromParts("package", TEST_APK_PACKAGE_NAME, null)
91         intent.putExtra(Intent.EXTRA_RETURN_RESULT, true)
92         intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
93 
94         val reinstall = installDialogStarter.activity.startActivityForResult(intent)
95 
96         clickInstallerUIButton(INSTALL_BUTTON_ID)
97 
98         // Install should have succeeded
99         assertEquals(RESULT_OK, reinstall.get(TIMEOUT, TimeUnit.MILLISECONDS))
100         assertInstalled()
101     }
102 
103     /**
104      * Check that we can't install an app via a package-installer intent if Secure FRP is enabled
105      */
106     @Test
packageNotInstalledSecureFrpnull107     fun packageNotInstalledSecureFrp() {
108         setSecureFrp(true)
109         try {
110             val installation = startInstallationViaIntent()
111             clickInstallerUIButton(INSTALL_BUTTON_ID)
112 
113             // Install should not have succeeded
114             assertEquals(RESULT_CANCELED, installation.get(TIMEOUT, TimeUnit.MILLISECONDS))
115             assertNotInstalled()
116         } finally {
117             setSecureFrp(false)
118         }
119     }
120 }
121