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