1 /*
2  * Copyright (C) 2011 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.tradefed.targetprep;
18 
19 import com.android.tradefed.build.IDeviceBuildInfo;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 
23 import junit.framework.TestCase;
24 
25 import org.easymock.EasyMock;
26 import org.easymock.IMocksControl;
27 
28 import java.io.File;
29 
30 public class SystemUpdaterDeviceFlasherTest extends TestCase {
31 
32     private static final String A_BUILD_ID = "1";
33 
34     private static final String TEST_STRING = "foo";
35 
36     private SystemUpdaterDeviceFlasher mFlasher;
37 
38     private IMocksControl mControl;
39 
40     private IDeviceBuildInfo mMockDeviceBuild;
41 
42     private ITestDevice mMockDevice;
43 
44     @Override
setUp()45     protected void setUp() throws Exception {
46         super.setUp();
47         ITestsZipInstaller mockZipInstaller = EasyMock.createMock(ITestsZipInstaller.class);
48         mFlasher = new SystemUpdaterDeviceFlasher();
49         mFlasher.setTestsZipInstaller(mockZipInstaller);
50         mControl = EasyMock.createStrictControl();
51         mControl.checkOrder(false);
52         mMockDevice = mControl.createMock(ITestDevice.class);
53         mMockDeviceBuild = mControl.createMock(IDeviceBuildInfo.class);
54         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(TEST_STRING);
55         EasyMock.expect(mMockDevice.getProductType()).andStubReturn(TEST_STRING);
56         EasyMock.expect(mMockDevice.getDeviceDescriptor()).andStubReturn(null);
57     }
58 
testFlash()59     public void testFlash() throws DeviceNotAvailableException, TargetSetupError {
60         yieldDifferentBuilds(true);
61         File fakeImage = new File("fakeImageFile");
62         mControl.checkOrder(true);
63         EasyMock.expect(mMockDeviceBuild.getOtaPackageFile()).andReturn(fakeImage);
64         EasyMock.expect(mMockDevice.pushFile(fakeImage, "/cache/update.zip")).andReturn(true);
65         String commandsRegex = "echo +--update_package +> +/cache/recovery/command +&& *"
66                 + "echo +/cache/update.zip +>> +/cache/recovery/command";
67         EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.matches(commandsRegex))).andReturn(
68                 "foo");
69         mMockDevice.rebootIntoRecovery();
70         mMockDevice.waitForDeviceAvailable();
71         mMockDevice.reboot();
72 
73         mControl.replay();
74         mFlasher.flash(mMockDevice, mMockDeviceBuild);
75         mControl.verify();
76     }
77 
testFlash_noOta()78     public void testFlash_noOta() throws DeviceNotAvailableException {
79         yieldDifferentBuilds(true);
80         EasyMock.expect(mMockDeviceBuild.getOtaPackageFile()).andReturn(null);
81 
82         mControl.replay();
83         try {
84             mFlasher.flash(mMockDevice, mMockDeviceBuild);
85             fail("didn't throw expected exception when OTA is missing: "
86                     + TargetSetupError.class.getSimpleName());
87         } catch (TargetSetupError e) {
88             assertTrue(true);
89         } finally {
90             mControl.verify();
91         }
92     }
93 
testFlashSameBuild()94     public void testFlashSameBuild() throws DeviceNotAvailableException, TargetSetupError {
95         yieldDifferentBuilds(false);
96         mControl.replay();
97         mFlasher.flash(mMockDevice, mMockDeviceBuild);
98         mControl.verify();
99     }
100 
yieldDifferentBuilds(boolean different)101     private void yieldDifferentBuilds(boolean different) throws DeviceNotAvailableException {
102         EasyMock.expect(mMockDevice.getBuildId()).andReturn(A_BUILD_ID).anyTimes();
103         EasyMock.expect(mMockDeviceBuild.getDeviceBuildId()).andReturn(
104                 (different ? A_BUILD_ID + 1 : A_BUILD_ID)).anyTimes();
105     }
106 }
107