1 /*
2  * Copyright (C) 2016 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 com.android.functional.otatests;
17 
18 import static org.easymock.EasyMock.createNiceMock;
19 
20 import android.content.Context;
21 import android.content.ContextWrapper;
22 import android.os.IPowerManager;
23 import android.os.PowerManager;
24 import android.os.RecoverySystem;
25 import android.test.InstrumentationTestCase;
26 
27 import java.io.File;
28 
29 public class PackageProcessTest extends InstrumentationTestCase {
30 
31     private static final String PACKAGE_DATA_PATH =
32             "/data/data/com.google.android.gms/app_download/update.zip";
33     private static final String BLOCK_MAP = "/cache/recovery/block.map";
34     private static final String UNCRYPT_FILE = "/cache/recovery/uncrypt_file";
35 
36     private Context mMockContext;
37     private Context mContext;
38     private PowerManager mMockPowerManager;
39 
40     private class PackageProcessMockContext extends ContextWrapper {
41 
42         private Context mInternal;
43 
PackageProcessMockContext(Context base)44         public PackageProcessMockContext(Context base) {
45             super(base);
46             mInternal = base;
47         }
48 
49         @Override
getSystemService(String name)50         public Object getSystemService(String name) {
51             if (name.equals(Context.POWER_SERVICE)) {
52                 return mMockPowerManager;
53             }
54             return mInternal.getSystemService(name);
55         }
56     }
57 
58     @Override
setUp()59     public void setUp() throws Exception {
60         super.setUp();
61         mContext = getInstrumentation().getContext();
62         mMockContext = new PackageProcessMockContext(mContext);
63         // Set a mocked out power manager into the mocked context, so the device
64         // won't reboot at the end of installPackage
65         IPowerManager mockIPowerManager = createNiceMock(IPowerManager.class);
66         mMockPowerManager = new PowerManager(mContext, mockIPowerManager, null);
67     }
68 
testPackageProcessOnly()69     public void testPackageProcessOnly() throws Exception {
70         File pkg = new File(PACKAGE_DATA_PATH);
71         RecoverySystem.verifyPackage(pkg, null, null);
72         RecoverySystem.processPackage(mMockContext, pkg, null);
73         // uncrypt will push block.map to this location if and only if it finishes successfully
74         assertTrue(new File(BLOCK_MAP).exists());
75     }
76 
testPackageProcessViaInstall()77     public void testPackageProcessViaInstall() throws Exception {
78         File pkg = new File(PACKAGE_DATA_PATH);
79         RecoverySystem.verifyPackage(pkg, null, null);
80         RecoverySystem.installPackage(mMockContext, pkg);
81         // uncrypt will push block.map to this location if and only if it finishes successfully
82         assertTrue(new File(UNCRYPT_FILE).exists());
83     }
84 }
85