1 /*
2  * Copyright (C) 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 
17 package android.incrementalinstall.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assume.assumeTrue;
23 
24 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
25 import com.android.compatibility.common.util.CddTest;
26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.io.File;
34 
35 /**
36  * Test that all devices launched with R have the Incremental feature enabled. Test that the feature
37  * is working properly by installing a package.
38  */
39 @RunWith(DeviceJUnit4ClassRunner.class)
40 public class IncrementalFeatureTest extends BaseHostJUnit4Test {
41     private static final String TEST_REMOTE_DIR = "/data/local/tmp/incremental_feature_test";
42     private static final String TEST_APP_PACKAGE_NAME =
43             "android.incrementalinstall.incrementaltestapp";
44     private static final String TEST_APP_BASE_APK_NAME = "IncrementalTestApp.apk";
45     private static final String IDSIG_SUFFIX = ".idsig";
46     private static final String TEST_APP_BASE_APK_IDSIG_NAME =
47             TEST_APP_BASE_APK_NAME + IDSIG_SUFFIX;
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         int launchApiLevel = getDevice().getLaunchApiLevel();
52 
53         // Devices that launched with R may fail GSI testing if they have incfs built into the
54         // kernel.
55         // Test R devices that report the feature.
56         // Test all devices S and above.
57         assumeTrue((launchApiLevel == 30 && featureAvailable())
58                    || launchApiLevel > 30);
59     }
60 
61     @CddTest(requirement="4/C-1-1")
62     @Test
testFeatureAvailable()63     public void testFeatureAvailable() throws Exception {
64         assertTrue(featureAvailable());
65     }
66 
67     @CddTest(requirement="4/C-1-1,C-3-1")
68     @Test
testFeatureWorkingWithInstallation()69     public void testFeatureWorkingWithInstallation() throws Exception {
70         getDevice().executeShellCommand("mkdir " + TEST_REMOTE_DIR);
71         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild());
72         final File apk = buildHelper.getTestFile(TEST_APP_BASE_APK_NAME);
73         assertNotNull(apk);
74         final File idsig = buildHelper.getTestFile(TEST_APP_BASE_APK_IDSIG_NAME);
75         assertNotNull(idsig);
76         final String remoteApkPath = TEST_REMOTE_DIR + "/" + apk.getName();
77         final String remoteIdsigPath = remoteApkPath + IDSIG_SUFFIX;
78         assertTrue(getDevice().pushFile(apk, remoteApkPath));
79         assertTrue(getDevice().pushFile(idsig, remoteIdsigPath));
80         String installResult = getDevice().executeShellCommand(
81                 "pm install-incremental -t -g " + remoteApkPath);
82         assertEquals("Success\n", installResult);
83         assertTrue(getDevice().isPackageInstalled(TEST_APP_PACKAGE_NAME));
84         getDevice().uninstallPackage(TEST_APP_PACKAGE_NAME);
85     }
86 
featureAvailable()87     private boolean featureAvailable() throws Exception {
88         return "true\n".equals(getDevice().executeShellCommand(
89                 "pm has-feature android.software.incremental_delivery"));
90     }
91 }
92