1 /* 2 * Copyright (C) 2018 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.util; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.util.AppVersionFetcher.AppVersionInfo; 22 23 import org.easymock.EasyMock; 24 import org.junit.Assert; 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 AppVersionFetcher} */ 31 @RunWith(JUnit4.class) 32 public class AppVersionFetcherTest { 33 34 private static final String PACKAGE_NAME = "mypackage"; 35 private static final String VERSION_CODE_CMD = "dumpsys package mypackage | grep versionCode="; 36 private static final String VERSION_NAME_CMD = "dumpsys package mypackage | grep versionName="; 37 38 private ITestDevice mMockDevice; 39 40 @Before 41 public void setup() { 42 mMockDevice = EasyMock.createMock(ITestDevice.class); 43 } 44 45 @Test 46 public void testFetchVersionCode_valid() throws DeviceNotAvailableException { 47 EasyMock.expect(mMockDevice.executeShellCommand(VERSION_CODE_CMD)) 48 .andReturn(" versionCode=780616927 minSdk=25 targetSdk=25"); 49 EasyMock.replay(mMockDevice); 50 Assert.assertEquals( 51 "780616927", 52 AppVersionFetcher.fetch(mMockDevice, PACKAGE_NAME, AppVersionInfo.VERSION_CODE)); 53 EasyMock.verify(mMockDevice); 54 } 55 56 @Test 57 public void testFetchVersionName_valid() throws DeviceNotAvailableException { 58 EasyMock.expect(mMockDevice.executeShellCommand(VERSION_NAME_CMD)) 59 .andReturn(" versionName=2.7.0.179109344"); 60 EasyMock.replay(mMockDevice); 61 Assert.assertEquals( 62 "2.7.0.179109344", 63 AppVersionFetcher.fetch(mMockDevice, PACKAGE_NAME, AppVersionInfo.VERSION_NAME)); 64 EasyMock.verify(mMockDevice); 65 } 66 67 @Test 68 public void testFetchVersionCode_validMultipleLines() throws DeviceNotAvailableException { 69 EasyMock.expect(mMockDevice.executeShellCommand(VERSION_CODE_CMD)) 70 .andReturn( 71 " versionCode=456 minSdk=25 targetSdk=25" 72 + "\nversionCode=123 minSdk=25 targetSdk=25"); 73 EasyMock.replay(mMockDevice); 74 Assert.assertEquals( 75 "456", 76 AppVersionFetcher.fetch(mMockDevice, PACKAGE_NAME, AppVersionInfo.VERSION_CODE)); 77 EasyMock.verify(mMockDevice); 78 } 79 80 @Test 81 public void testFetchVersionName_validMultipleLines() throws DeviceNotAvailableException { 82 EasyMock.expect(mMockDevice.executeShellCommand(VERSION_NAME_CMD)) 83 .andReturn(" versionName=2.7.0.1\n versionName=2.7.0.0"); 84 EasyMock.replay(mMockDevice); 85 Assert.assertEquals( 86 "2.7.0.1", 87 AppVersionFetcher.fetch(mMockDevice, PACKAGE_NAME, AppVersionInfo.VERSION_NAME)); 88 EasyMock.verify(mMockDevice); 89 } 90 91 @Test 92 public void testFetchVersionCode_invalidResponse() throws DeviceNotAvailableException { 93 EasyMock.expect(mMockDevice.executeShellCommand(VERSION_CODE_CMD)) 94 .andReturn("invalid response"); 95 EasyMock.replay(mMockDevice); 96 97 try { 98 AppVersionFetcher.fetch(mMockDevice, PACKAGE_NAME, AppVersionInfo.VERSION_CODE); 99 Assert.fail(); 100 } catch (IllegalStateException expectedException) { 101 } 102 103 EasyMock.verify(mMockDevice); 104 } 105 106 @Test 107 public void testFetchVersionName_invalidResponse() throws DeviceNotAvailableException { 108 EasyMock.expect(mMockDevice.executeShellCommand(VERSION_NAME_CMD)) 109 .andReturn("invalid response"); 110 EasyMock.replay(mMockDevice); 111 112 try { 113 AppVersionFetcher.fetch(mMockDevice, PACKAGE_NAME, AppVersionInfo.VERSION_NAME); 114 Assert.fail(); 115 } catch (IllegalStateException expectedException) { 116 } 117 118 EasyMock.verify(mMockDevice); 119 } 120 } 121