1 /*
2  * Copyright (C) 2020 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.tradefed.targetprep;
18 
19 import com.android.tradefed.config.GlobalConfiguration;
20 import com.android.tradefed.config.Option;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.IDeviceManager;
23 import com.android.tradefed.device.ITestDevice;
24 import com.android.tradefed.invoker.TestInformation;
25 import com.android.tradefed.util.RunUtil;
26 
27 import java.time.Duration;
28 import java.util.List;
29 
30 /**
31  * A TargetPreparer that launches an emulator locally from an android build environment.
32  *
33  * <p>Its highly recommended to prepare an emulator snapshot (eg tradefed run
34  * emulator/generate-snapshot) before running any configs that includes this.
35  */
36 public class LocalEmulatorLaunch extends BaseEmulatorPreparer {
37 
38     @Option(name = "boot-timeout", description = "maximum duration to wait for emulator to boot")
39     private Duration mBootTimeout = Duration.ofSeconds(30);
40 
41     @Override
setUp(TestInformation testInformation)42     public void setUp(TestInformation testInformation)
43             throws TargetSetupError, BuildError, DeviceNotAvailableException {
44         ITestDevice allocatedDevice = testInformation.getDevice();
45         IDeviceManager manager = GlobalConfiguration.getDeviceManagerInstance();
46         List<String> args = buildEmulatorLaunchArgs();
47         args.add("-read-only");
48         RunUtil runUtil = buildRunUtilForEmulatorLaunch();
49 
50         manager.launchEmulator(allocatedDevice, mBootTimeout.toMillis(), runUtil, args);
51     }
52 
53     @Override
tearDown(TestInformation testInformation, Throwable e)54     public void tearDown(TestInformation testInformation, Throwable e)
55             throws DeviceNotAvailableException {
56         // device manager should automatically free/kill the launched emulator
57     }
58 }
59