1 /*
2  * Copyright (C) 2017 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.Option;
20 import com.android.tradefed.config.OptionClass;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.invoker.TestInformation;
24 
25 /** Target preparer that reboots the device. */
26 @OptionClass(alias = "reboot-preparer")
27 public final class RebootTargetPreparer extends BaseTargetPreparer {
28 
29     @Option(name = "pre-reboot", description = "Reboot the device during setUp.")
30     private boolean mPreReboot = true;
31 
32     @Option(name = "post-reboot", description = "Reboot the device during tearDown.")
33     private boolean mPostReboot = false;
34 
35     @Override
setUp(TestInformation testInfo)36     public void setUp(TestInformation testInfo)
37             throws TargetSetupError, BuildError, DeviceNotAvailableException {
38         if (mPreReboot) {
39             ITestDevice device = testInfo.getDevice();
40             device.reboot();
41         }
42     }
43 
44     @Override
tearDown(TestInformation testInfo, Throwable e)45     public void tearDown(TestInformation testInfo, Throwable e) throws DeviceNotAvailableException {
46         if (mPostReboot) {
47             ITestDevice device = testInfo.getDevice();
48             device.reboot();
49         }
50     }
51 }
52