1 /* 2 * Copyright (C) 2012 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.monkey; 18 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.util.ArrayUtil; 21 22 import junit.framework.TestCase; 23 24 import org.easymock.EasyMock; 25 26 import java.util.ArrayList; 27 import java.util.Collection; 28 29 /** 30 * Unit tests for {@link MonkeyBase} 31 */ 32 public class MonkeyBaseTest extends TestCase { 33 34 /** 35 * Test that {@link MonkeyBase#setSubtract(Collection, Collection)} returns same object if 36 * exclude is empty. 37 */ 38 public void testSetSubtract_noExclude() { 39 Collection<String> haystack = ArrayUtil.list("a", "b", "c"); 40 Collection<String> needles = new ArrayList<String>(); 41 // double-checking comparison assumptions 42 assertFalse(haystack == needles); 43 Collection<String> output = MonkeyBase.setSubtract(haystack, needles); 44 assertTrue(haystack == output); 45 } 46 47 /** 48 * Test that {@link MonkeyBase#setSubtract(Collection, Collection)} returns the set subtraction 49 * if exclude is not empty. 50 */ 51 public void testSetSubtract() { 52 Collection<String> haystack = ArrayUtil.list("a", "b", "c"); 53 Collection<String> needles = ArrayUtil.list("b"); 54 Collection<String> output = MonkeyBase.setSubtract(haystack, needles); 55 assertEquals(2, output.size()); 56 assertTrue(output.contains("a")); 57 assertFalse(output.contains("b")); 58 assertTrue(output.contains("c")); 59 } 60 61 /** 62 * Test success case for {@link MonkeyBase#getUptime()}. 63 */ 64 public void testUptime() throws Exception { 65 MonkeyBase monkey = new MonkeyBase(); 66 ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class); 67 monkey.setDevice(mockDevice); 68 EasyMock.expect(mockDevice.executeShellCommand("cat /proc/uptime")).andReturn( 69 "5278.73 1866.80"); 70 EasyMock.replay(mockDevice); 71 assertEquals("5278.73", monkey.getUptime()); 72 } 73 74 /** 75 * Test case for {@link MonkeyBase#getUptime()} where device is initially unresponsive. 76 */ 77 public void testUptime_fail() throws Exception { 78 MonkeyBase monkey = new MonkeyBase(); 79 ITestDevice mockDevice = EasyMock.createMock(ITestDevice.class); 80 monkey.setDevice(mockDevice); 81 EasyMock.expect(mockDevice.getSerialNumber()).andStubReturn("serial"); 82 EasyMock.expect(mockDevice.executeShellCommand("cat /proc/uptime")).andReturn( 83 ""); 84 EasyMock.expect(mockDevice.executeShellCommand("cat /proc/uptime")).andReturn( 85 "5278.73 1866.80"); 86 EasyMock.replay(mockDevice); 87 assertEquals("5278.73", monkey.getUptime()); 88 } 89 } 90 91