1 /* 2 * Copyright (C) 2016 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.tradefed.build.IBuildInfo; 19 import com.android.tradefed.config.ConfigurationException; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.config.OptionSetter; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.log.LogUtil.CLog; 24 25 import org.junit.runner.Runner; 26 import org.junit.runner.notification.RunNotifier; 27 import org.junit.runners.Suite; 28 import org.junit.runners.model.InitializationError; 29 import org.junit.runners.model.RunnerBuilder; 30 31 import java.util.HashSet; 32 import java.util.Set; 33 34 /** 35 * Extends the JUnit4 container {@link Suite} in order to provide a {@link ITestDevice} to the tests 36 * that requires it. 37 */ 38 public class DeviceSuite extends Suite 39 implements IDeviceTest, IBuildReceiver, IAbiReceiver, ISetOptionReceiver { 40 private ITestDevice mDevice; 41 private IBuildInfo mBuildInfo; 42 private IAbi mAbi; 43 44 @Option(name = HostTest.SET_OPTION_NAME, description = HostTest.SET_OPTION_DESC) 45 private Set<String> mKeyValueOptions = new HashSet<>(); 46 47 public DeviceSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError { 48 super(klass, builder); 49 } 50 51 @Override 52 public void setDevice(ITestDevice device) { 53 mDevice = device; 54 for (Runner r : getChildren()) { 55 // propagate to runner if it needs a device. 56 if (r instanceof IDeviceTest) { 57 if (mDevice == null) { 58 throw new IllegalArgumentException("Missing device"); 59 } 60 ((IDeviceTest)r).setDevice(mDevice); 61 } 62 } 63 } 64 65 @Override 66 public ITestDevice getDevice() { 67 return mDevice; 68 } 69 70 @Override 71 public void setAbi(IAbi abi) { 72 mAbi = abi; 73 for (Runner r : getChildren()) { 74 // propagate to runner if it needs an abi. 75 if (r instanceof IAbiReceiver) { 76 ((IAbiReceiver)r).setAbi(mAbi); 77 } 78 } 79 } 80 81 @Override 82 public IAbi getAbi() { 83 return mAbi; 84 } 85 86 @Override 87 public void setBuild(IBuildInfo buildInfo) { 88 mBuildInfo = buildInfo; 89 for (Runner r : getChildren()) { 90 // propagate to runner if it needs a buildInfo. 91 if (r instanceof IBuildReceiver) { 92 if (mBuildInfo == null) { 93 throw new IllegalArgumentException("Missing build information"); 94 } 95 ((IBuildReceiver)r).setBuild(mBuildInfo); 96 } 97 } 98 } 99 100 @Override 101 protected void runChild(Runner runner, RunNotifier notifier) { 102 try { 103 OptionSetter setter = new OptionSetter(runner); 104 for (String kv : mKeyValueOptions) { 105 setter.setOptionValue(HostTest.SET_OPTION_NAME, kv); 106 } 107 } catch (ConfigurationException e) { 108 CLog.d("Could not set option set-option on '%s', reason: '%s'", runner, e.getMessage()); 109 } 110 super.runChild(runner, notifier); 111 } 112 } 113