1 /* 2 * Copyright (C) 2024 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.adservices.common; 17 18 import com.android.adservices.shared.testing.TestDeviceHelper; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.invoker.TestInformation; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.targetprep.BuildError; 24 import com.android.tradefed.targetprep.ITargetPreparer; 25 import com.android.tradefed.targetprep.TargetSetupError; 26 27 // TODO(b/347083260): add unit test (there's no AdServicesSharedLibrariesHostTests currently) 28 /* 29 * TODO(b/281868718): currently this is the only custom TargetPreparer we're providing, so it's 30 * quite simple. But ideally we should split it in a few classes: 31 * 32 * AbstractTargetPreparer - (side-less?) class hosted on shared directory, it would automatically 33 * take care of plumbing tasks such as: 34 * - extracting ITestDevice from TestInformation (and checking it's not null) 35 * - throw UnsupportedOperationException on deprecated methods from ITargetPreparer 36 * - throw IllegalStateException on wrong calls (like setUp() / tearDown() more than once or 37 * tearDown() before setUp() 38 * - adding constructor that would make sure only one instance is running at any time 39 * - provide a protected / @VisibleForTesting isRunning() method 40 * 41 * AbstractTestDeviceHelperSetterTargetPreparer - abstract class that extends 42 * AbstractTargetPreparer and has the logic to set TestDeviceHelperSetter, as currently doing here 43 * (so this class would initially just extend it) 44 * 45 * TestDeviceHelperSetterTargetPreparer - final class that extends 46 * AbstractTestDeviceHelperSetterTargetPreparer, could be used in projects that don't want / need 47 * to extend AbstractTestDeviceHelperSetterTargetPreparer. 48 */ 49 /** Target preparer that should be used by all host-side tests. */ 50 public final class AdServicesHostTestsTargetPreparer implements ITargetPreparer { 51 52 @Override setUp(TestInformation testInformation)53 public void setUp(TestInformation testInformation) 54 throws TargetSetupError, BuildError, DeviceNotAvailableException { 55 CLog.v("setup(%s)", testInformation); 56 ITestDevice device = testInformation.getDevice(); 57 TestDeviceHelper.setTestDevice(device); 58 } 59 60 @Override tearDown(TestInformation testInformation, Throwable e)61 public void tearDown(TestInformation testInformation, Throwable e) 62 throws DeviceNotAvailableException { 63 CLog.v("tearDown(%s, %s)", testInformation, e); 64 } 65 } 66