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