1 /*
2  * Copyright (C) 2019 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.cts.rollback.host.app2;
18 
19 import static com.android.cts.rollback.lib.RollbackInfoSubject.assertThat;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.Manifest;
24 import android.content.rollback.RollbackInfo;
25 
26 import com.android.cts.install.lib.InstallUtils;
27 import com.android.cts.install.lib.TestApp;
28 import com.android.cts.rollback.lib.Rollback;
29 import com.android.cts.rollback.lib.RollbackUtils;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 import java.io.IOException;
38 
39 /**
40  * On-device helper test methods used for host-driven rollback tests.
41  */
42 @RunWith(JUnit4.class)
43 public class HostTestHelper {
44     /**
45      * Adopts common permissions needed to test rollbacks.
46      */
47     @Before
setup()48     public void setup() throws InterruptedException, IOException {
49         InstallUtils.adoptShellPermissionIdentity(
50                     Manifest.permission.INSTALL_PACKAGES,
51                     Manifest.permission.DELETE_PACKAGES,
52                     Manifest.permission.TEST_MANAGE_ROLLBACKS);
53     }
54 
55     /**
56      * Drops adopted shell permissions.
57      */
58     @After
teardown()59     public void teardown() throws InterruptedException, IOException {
60         InstallUtils.dropShellPermissionIdentity();
61     }
62 
63     @Test
testApkRollbackByAnotherInstaller_Phase2_SecondInstaller()64     public void testApkRollbackByAnotherInstaller_Phase2_SecondInstaller() throws Exception {
65         RollbackInfo rollbackA = RollbackUtils.waitForAvailableRollback(TestApp.A);
66         assertThat(rollbackA).packagesContainsExactly(Rollback.from(TestApp.A2).to(TestApp.A1));
67         RollbackUtils.rollback(rollbackA.getRollbackId());
68         // TODO(b/127452064): fix the rollback bug and enable the assertion
69         // Rollback should fail since TestApp.A is installed by another installer.
70         // assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(2);
71     }
72 }
73