1 /* 2 * Copyright (C) 2017 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.tradefed.device; 17 18 import static org.junit.Assert.*; 19 20 import com.android.tradefed.util.CommandResult; 21 import com.android.tradefed.util.CommandStatus; 22 import com.android.tradefed.util.IRunUtil; 23 24 import org.easymock.EasyMock; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** Unit tests for {@link AndroidDebugBridgeWrapper}. */ 31 @RunWith(JUnit4.class) 32 public class AndroidDebugBridgeWrapperTest { 33 34 private AndroidDebugBridgeWrapper mBridge; 35 private IRunUtil mMockRunUtil; 36 37 @Before 38 public void setUp() { 39 mMockRunUtil = EasyMock.createMock(IRunUtil.class); 40 mBridge = 41 new AndroidDebugBridgeWrapper() { 42 @Override 43 IRunUtil getRunUtil() { 44 return mMockRunUtil; 45 } 46 }; 47 } 48 49 /** Test parsing the adb version when available. */ 50 @Test 51 public void testAdbVersionParsing() { 52 CommandResult res = new CommandResult(); 53 res.setStatus(CommandStatus.SUCCESS); 54 res.setStdout("Android Debug Bridge version 1.0.36\nRevision 0e7324e9095a-android\n"); 55 EasyMock.expect( 56 mMockRunUtil.runTimedCmd( 57 EasyMock.anyLong(), EasyMock.eq("fakeadb"), EasyMock.eq("version"))) 58 .andReturn(res); 59 EasyMock.replay(mMockRunUtil); 60 String version = mBridge.getAdbVersion("fakeadb"); 61 assertEquals("1.0.36-0e7324e9095a-android", version); 62 EasyMock.verify(mMockRunUtil); 63 } 64 65 /** Test parsing the adb version when available with alternative format. */ 66 @Test 67 public void testAdbVersionParsing_altFormat() { 68 CommandResult res = new CommandResult(); 69 res.setStatus(CommandStatus.SUCCESS); 70 res.setStdout( 71 "Android Debug Bridge version 1.0.36\n" 72 + "Version 0.0.0-4407735\n" 73 + "Installed as /usr/local/adb\n"); 74 EasyMock.expect( 75 mMockRunUtil.runTimedCmd( 76 EasyMock.anyLong(), EasyMock.eq("fakeadb"), EasyMock.eq("version"))) 77 .andReturn(res); 78 EasyMock.replay(mMockRunUtil); 79 String version = mBridge.getAdbVersion("fakeadb"); 80 assertEquals("1.0.36 subVersion: 0.0.0-4407735 install path: /usr/local/adb", version); 81 EasyMock.verify(mMockRunUtil); 82 } 83 84 /** Test when the version process fails. */ 85 @Test 86 public void testAdbVersionParse_runFail() { 87 CommandResult res = new CommandResult(); 88 res.setStatus(CommandStatus.FAILED); 89 res.setStdout(""); 90 res.setStderr(""); 91 EasyMock.expect( 92 mMockRunUtil.runTimedCmd( 93 EasyMock.anyLong(), EasyMock.eq("fakeadb"), EasyMock.eq("version"))) 94 .andReturn(res); 95 EasyMock.replay(mMockRunUtil); 96 assertNull(mBridge.getAdbVersion("fakeadb")); 97 EasyMock.verify(mMockRunUtil); 98 } 99 100 /** Test when the revision is not present, in that case we output a partial version. */ 101 @Test 102 public void testAdbVersionParsing_partial() { 103 CommandResult res = new CommandResult(); 104 res.setStatus(CommandStatus.SUCCESS); 105 res.setStdout("Android Debug Bridge version 1.0.36\n"); 106 EasyMock.expect( 107 mMockRunUtil.runTimedCmd( 108 EasyMock.anyLong(), EasyMock.eq("fakeadb"), EasyMock.eq("version"))) 109 .andReturn(res); 110 EasyMock.replay(mMockRunUtil); 111 String version = mBridge.getAdbVersion("fakeadb"); 112 assertEquals("1.0.36", version); 113 EasyMock.verify(mMockRunUtil); 114 } 115 116 /** Test when the output from 'adb version' is not as expected at all. */ 117 @Test 118 public void testAdbVersionParsing_parseFail() { 119 CommandResult res = new CommandResult(); 120 res.setStatus(CommandStatus.SUCCESS); 121 res.setStdout("This is probably not the right output\n"); 122 EasyMock.expect( 123 mMockRunUtil.runTimedCmd( 124 EasyMock.anyLong(), EasyMock.eq("fakeadb"), EasyMock.eq("version"))) 125 .andReturn(res); 126 EasyMock.replay(mMockRunUtil); 127 assertNull(mBridge.getAdbVersion("fakeadb")); 128 EasyMock.verify(mMockRunUtil); 129 } 130 } 131