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 static org.junit.Assert.assertTrue; 20 import static org.junit.Assert.fail; 21 import static org.mockito.Mockito.inOrder; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.times; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import com.android.tradefed.build.IDeviceBuildInfo; 28 import com.android.tradefed.device.DeviceNotAvailableException; 29 import com.android.tradefed.device.ITestDevice; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 import org.mockito.InOrder; 36 import org.mockito.Mock; 37 import org.mockito.Mockito; 38 import org.mockito.MockitoAnnotations; 39 40 import java.io.File; 41 42 @RunWith(JUnit4.class) 43 public class SystemUpdaterDeviceFlasherTest { 44 45 private static final String A_BUILD_ID = "1"; 46 47 private static final String TEST_STRING = "foo"; 48 49 private SystemUpdaterDeviceFlasher mFlasher; 50 51 @Mock ITestDevice mMockDevice; 52 @Mock IDeviceBuildInfo mMockDeviceBuild; 53 54 private InOrder mInOrder; 55 56 @Before setUp()57 public void setUp() throws Exception { 58 MockitoAnnotations.initMocks(this); 59 mInOrder = inOrder(mMockDevice, mMockDeviceBuild); 60 61 ITestsZipInstaller mockZipInstaller = mock(ITestsZipInstaller.class); 62 mFlasher = new SystemUpdaterDeviceFlasher(); 63 mFlasher.setTestsZipInstaller(mockZipInstaller); 64 when(mMockDevice.getSerialNumber()).thenReturn(TEST_STRING); 65 when(mMockDevice.getProductType()).thenReturn(TEST_STRING); 66 when(mMockDevice.getDeviceDescriptor()).thenReturn(null); 67 } 68 69 @Test testFlash()70 public void testFlash() throws DeviceNotAvailableException, TargetSetupError { 71 yieldDifferentBuilds(true); 72 File fakeImage = new File("fakeImageFile"); 73 when(mMockDeviceBuild.getOtaPackageFile()).thenReturn(fakeImage); 74 when(mMockDevice.pushFile(fakeImage, "/cache/update.zip", true)).thenReturn(true); 75 String commandsRegex = 76 "echo +--update_package +> +/cache/recovery/command +&& *" 77 + "echo +/cache/update.zip +>> +/cache/recovery/command"; 78 when(mMockDevice.executeShellCommand(Mockito.matches(commandsRegex))).thenReturn("foo"); 79 80 mFlasher.flash(mMockDevice, mMockDeviceBuild); 81 82 mInOrder.verify(mMockDeviceBuild).getOtaPackageFile(); 83 mInOrder.verify(mMockDevice) 84 .pushFile(Mockito.eq(fakeImage), Mockito.eq("/cache/update.zip"), Mockito.eq(true)); 85 mInOrder.verify(mMockDevice).executeShellCommand(Mockito.matches(commandsRegex)); 86 mInOrder.verify(mMockDevice).rebootIntoRecovery(); 87 mInOrder.verify(mMockDevice).waitForDeviceAvailable(); 88 mInOrder.verify(mMockDevice).reboot(); 89 } 90 91 @Test testFlash_noOta()92 public void testFlash_noOta() throws DeviceNotAvailableException { 93 yieldDifferentBuilds(true); 94 when(mMockDeviceBuild.getOtaPackageFile()).thenReturn(null); 95 96 try { 97 mFlasher.flash(mMockDevice, mMockDeviceBuild); 98 fail( 99 "didn't throw expected exception when OTA is missing: " 100 + TargetSetupError.class.getSimpleName()); 101 } catch (TargetSetupError e) { 102 assertTrue(true); 103 } finally { 104 verify(mMockDeviceBuild).getOtaPackageFile(); 105 } 106 } 107 108 @Test testFlashSameBuild()109 public void testFlashSameBuild() throws DeviceNotAvailableException, TargetSetupError { 110 yieldDifferentBuilds(false); 111 mFlasher.flash(mMockDevice, mMockDeviceBuild); 112 113 mInOrder.verify(mMockDeviceBuild, times(0)).getOtaPackageFile(); 114 mInOrder.verify(mMockDevice, times(0)) 115 .pushFile(Mockito.any(), Mockito.eq("/cache/update.zip")); 116 mInOrder.verify(mMockDevice, times(0)).executeShellCommand(Mockito.any()); 117 mInOrder.verify(mMockDevice, times(0)).rebootIntoRecovery(); 118 mInOrder.verify(mMockDevice, times(0)).waitForDeviceAvailable(); 119 mInOrder.verify(mMockDevice, times(0)).reboot(); 120 } 121 yieldDifferentBuilds(boolean different)122 private void yieldDifferentBuilds(boolean different) throws DeviceNotAvailableException { 123 when(mMockDevice.getBuildId()).thenReturn(A_BUILD_ID); 124 when(mMockDeviceBuild.getDeviceBuildId()) 125 .thenReturn((different ? A_BUILD_ID + 1 : A_BUILD_ID)); 126 } 127 } 128