1 /* 2 * Copyright (C) 2011 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.testtype; 17 18 import com.android.ddmlib.testrunner.TestIdentifier; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.result.ITestInvocationListener; 21 22 import junit.framework.TestCase; 23 24 import org.easymock.EasyMock; 25 26 import java.util.Collections; 27 import java.util.HashMap; 28 import java.util.Map; 29 30 /** 31 * Unit tests for {@link DeviceTestSuite}. 32 */ 33 public class DeviceTestSuiteTest extends TestCase { 34 35 public static class MockTest extends DeviceTestCase { 36 37 public void test1() { 38 // Metrics are also available for test within Suite 39 addTestMetric("key1", "metric1"); 40 } 41 public void test2() {} 42 } 43 44 public static class MockAbortTest extends DeviceTestCase { 45 46 private static final String EXCEP_MSG = "failed"; 47 private static final String FAKE_SERIAL = "fakeserial"; 48 49 public void test1() throws DeviceNotAvailableException { 50 throw new DeviceNotAvailableException(EXCEP_MSG, FAKE_SERIAL); 51 } 52 } 53 54 /** 55 * Verify that calling run on a DeviceTestSuite will run all test methods. 56 */ 57 @SuppressWarnings("unchecked") 58 public void testRun_suite() throws Exception { 59 DeviceTestSuite suite = new DeviceTestSuite(); 60 suite.addTestSuite(MockTest.class); 61 62 ITestInvocationListener listener = EasyMock.createMock(ITestInvocationListener.class); 63 listener.testRunStarted(DeviceTestSuite.class.getName(), 2); 64 final TestIdentifier test1 = new TestIdentifier(MockTest.class.getName(), "test1"); 65 final TestIdentifier test2 = new TestIdentifier(MockTest.class.getName(), "test2"); 66 listener.testStarted(test1); 67 Map<String, String> metrics = new HashMap<>(); 68 metrics.put("key1", "metric1"); 69 listener.testEnded(test1, metrics); 70 listener.testStarted(test2); 71 listener.testEnded(test2, Collections.EMPTY_MAP); 72 listener.testRunEnded(EasyMock.anyLong(), (Map<String, String>) EasyMock.anyObject()); 73 EasyMock.replay(listener); 74 75 suite.run(listener); 76 EasyMock.verify(listener); 77 } 78 79 /** 80 * Verify that a device not available exception is thrown up. 81 */ 82 @SuppressWarnings("unchecked") 83 public void testRun_deviceNotAvail() { 84 DeviceTestSuite suite = new DeviceTestSuite(); 85 suite.addTestSuite(MockAbortTest.class); 86 87 // create a mock ITestInvocationListener, because results are easier to verify 88 ITestInvocationListener listener = EasyMock.createMock(ITestInvocationListener.class); 89 90 final TestIdentifier test1 = new TestIdentifier(MockAbortTest.class.getName(), "test1"); 91 listener.testRunStarted(DeviceTestSuite.class.getName(), 1); 92 listener.testStarted(test1); 93 listener.testFailed(EasyMock.eq(test1), 94 EasyMock.contains(MockAbortTest.EXCEP_MSG)); 95 listener.testEnded(test1, Collections.EMPTY_MAP); 96 listener.testRunFailed(EasyMock.contains(MockAbortTest.EXCEP_MSG)); 97 listener.testRunEnded(EasyMock.anyLong(), (Map<String, String>) EasyMock.anyObject()); 98 EasyMock.replay(listener); 99 try { 100 suite.run(listener); 101 fail("DeviceNotAvailableException not thrown"); 102 } catch (DeviceNotAvailableException e) { 103 // expected 104 } 105 EasyMock.verify(listener); 106 } 107 } 108