1 /*
2  * Copyright (C) 2012 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.targetprep;
17 
18 import com.android.tradefed.config.Option;
19 import com.android.tradefed.config.OptionClass;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.invoker.TestInformation;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.util.IRunUtil;
25 import com.android.tradefed.util.RunUtil;
26 
27 /** A simple target preparer to waste time and potentially restart the device. */
28 @OptionClass(alias = "time-waster")
29 public class TimeWaster extends BaseTargetPreparer {
30     @Option(name = "delay", description = "Time to delay, in msecs", mandatory = true)
31     private long mDelayMsecs = 0;
32 
33     @Option(name = "reboot-before-sleep", description = "Whether to reboot the device before " +
34             "sleeping")
35     private boolean mRebootBeforeSleep = false;
36 
37     @Option(name = "reboot-after-sleep", description = "Whether to reboot the device after " +
38             "sleeping")
39     private boolean mRebootAfterSleep = false;
40 
41     /** {@inheritDoc} */
42     @Override
setUp(TestInformation testInfo)43     public void setUp(TestInformation testInfo)
44             throws TargetSetupError, DeviceNotAvailableException {
45         ITestDevice device = testInfo.getDevice();
46         if (mRebootBeforeSleep) {
47             CLog.d("Pre-sleep device reboot on %s", device.getSerialNumber());
48             device.reboot();
49         }
50 
51         CLog.d("Sleeping %d msecs on device %s", mDelayMsecs, device.getSerialNumber());
52         getRunUtil().sleep(mDelayMsecs);
53 
54         if (mRebootAfterSleep) {
55             CLog.d("Post-sleep device reboot on %s", device.getSerialNumber());
56             device.reboot();
57         }
58     }
59 
getRunUtil()60     IRunUtil getRunUtil() {
61         return RunUtil.getDefault();
62     }
63 }
64