1 /* 2 * Copyright (C) 2010 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.cts.tradefed.testtype; 17 18 import com.android.cts.tradefed.build.StubCtsBuildHelper; 19 import com.android.ddmlib.testrunner.TestIdentifier; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.result.ITestInvocationListener; 23 24 import org.easymock.EasyMock; 25 26 import java.net.URL; 27 import java.util.ArrayList; 28 import java.util.Collection; 29 import java.util.Collections; 30 31 import junit.framework.TestCase; 32 33 /** 34 * Unit tests for {@link JarHostTest}. 35 */ 36 public class JarHostTestTest extends TestCase { 37 38 private static final String RUN_NAME = "run"; 39 private JarHostTest mJarTest; 40 private StubCtsBuildHelper mStubBuildHelper; 41 42 43 @Override setUp()44 protected void setUp() throws Exception { 45 super.setUp(); 46 mJarTest = new JarHostTest() { 47 // mock out the loading from jar 48 @Override 49 Class<?> loadClass(String className, URL[] urls) throws ClassNotFoundException { 50 return MockTest.class; 51 } 52 }; 53 mStubBuildHelper = new StubCtsBuildHelper(); 54 mJarTest.setBuildHelper(mStubBuildHelper); 55 } 56 57 public static class MockTest extends TestCase { MockTest(String name)58 public MockTest(String name) { 59 super(name); 60 } 61 MockTest()62 public MockTest() { 63 super(); 64 } 65 testFoo()66 public void testFoo() { 67 } 68 } 69 70 /** 71 * Test normal case for 72 * {@link JarHostTest#run(com.android.tradefed.result.ITestInvocationListener)}. 73 */ 74 @SuppressWarnings("unchecked") testRun()75 public void testRun() throws DeviceNotAvailableException { 76 ITestInvocationListener listener = EasyMock.createMock(ITestInvocationListener.class); 77 TestIdentifier expectedTest = new TestIdentifier(MockTest.class.getName(), "testFoo"); 78 79 Collection<TestIdentifier> tests = new ArrayList<TestIdentifier>(1); 80 tests.add(expectedTest); 81 listener.testRunStarted(RUN_NAME, 1); 82 listener.testStarted(expectedTest); 83 listener.testEnded(expectedTest, Collections.EMPTY_MAP); 84 listener.testRunEnded(EasyMock.anyLong(), EasyMock.eq(Collections.EMPTY_MAP)); 85 mJarTest.setTests(tests); 86 mJarTest.setDevice(EasyMock.createMock(ITestDevice.class)); 87 mJarTest.setJarFileName("fakefile"); 88 mJarTest.setRunName(RUN_NAME); 89 90 EasyMock.replay(listener); 91 mJarTest.run(listener); 92 EasyMock.verify(listener); 93 } 94 } 95