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.suite.checker; 17 18 import static org.junit.Assert.assertEquals; 19 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus; 22 23 import org.easymock.EasyMock; 24 import org.junit.Before; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Unit tests for {@link SystemServerStatusChecker} */ 30 @RunWith(JUnit4.class) 31 public class SystemServerStatusCheckerTest { 32 33 private SystemServerStatusChecker mChecker; 34 private ITestDevice mMockDevice; 35 36 @Before 37 public void setUp() { 38 mMockDevice = EasyMock.createMock(ITestDevice.class); 39 mChecker = new SystemServerStatusChecker(); 40 } 41 42 /** Test that system checker pass if the pid of system checker does not change. */ 43 @Test 44 public void testPidRemainUnchanged() throws Exception { 45 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq("pidof system_server"))) 46 .andReturn("914") 47 .times(2); 48 EasyMock.replay(mMockDevice); 49 assertEquals(CheckStatus.SUCCESS, mChecker.preExecutionCheck(mMockDevice).getStatus()); 50 assertEquals(CheckStatus.SUCCESS, mChecker.postExecutionCheck(mMockDevice).getStatus()); 51 EasyMock.verify(mMockDevice); 52 } 53 54 /** Test that system checker fail if the pid of system checker does change. */ 55 @Test 56 public void testPidChanged() throws Exception { 57 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq("pidof system_server"))) 58 .andReturn("914\n"); 59 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq("pidof system_server"))) 60 .andReturn("1024\n"); 61 EasyMock.replay(mMockDevice); 62 assertEquals(CheckStatus.SUCCESS, mChecker.preExecutionCheck(mMockDevice).getStatus()); 63 assertEquals(CheckStatus.FAILED, mChecker.postExecutionCheck(mMockDevice).getStatus()); 64 EasyMock.verify(mMockDevice); 65 } 66 67 /** Test that if the format of the pid is unexpected, we skip the system checker. */ 68 @Test 69 public void testFailToGetPid() throws Exception { 70 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq("pidof system_server"))) 71 .andReturn("not found\n"); 72 EasyMock.replay(mMockDevice); 73 assertEquals(CheckStatus.SUCCESS, mChecker.preExecutionCheck(mMockDevice).getStatus()); 74 assertEquals(CheckStatus.SUCCESS, mChecker.postExecutionCheck(mMockDevice).getStatus()); 75 EasyMock.verify(mMockDevice); 76 } 77 78 /** 79 * Test that if the pid output is null, we fail the current preExecution but skip post 80 * execution. 81 */ 82 @Test 83 public void testPid_null() throws Exception { 84 EasyMock.expect(mMockDevice.executeShellCommand(EasyMock.eq("pidof system_server"))) 85 .andReturn(null); 86 EasyMock.replay(mMockDevice); 87 assertEquals(CheckStatus.FAILED, mChecker.preExecutionCheck(mMockDevice).getStatus()); 88 assertEquals(CheckStatus.SUCCESS, mChecker.postExecutionCheck(mMockDevice).getStatus()); 89 EasyMock.verify(mMockDevice); 90 } 91 } 92