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.example;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.invoker.TestInformation;
22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
23 import com.android.tradefed.result.ITestInvocationListener;
24 import com.android.tradefed.result.TestDescription;
25 import com.android.tradefed.testtype.IRemoteTest;
26 
27 import java.util.HashMap;
28 
29 /**
30  * Reboots the device and verifies it comes back online. This simple reboot tests acts as an example
31  * integration test.
32  */
33 public class RebootTest implements IRemoteTest {
34 
35     @Option(name = "num-of-reboots", description = "Number of times to reboot the device.")
36     private int mNumDeviceReboots = 1;
37 
38     /** {@inheritDoc} */
39     @Override
run(TestInformation testInfo, ITestInvocationListener listener)40     public void run(TestInformation testInfo, ITestInvocationListener listener)
41             throws DeviceNotAvailableException {
42         long start;
43         HashMap<String, Metric> emptyMap = new HashMap<>();
44         TestDescription testId;
45         start = System.currentTimeMillis();
46         listener.testRunStarted(String.format("#%d device reboots", mNumDeviceReboots),
47                                 mNumDeviceReboots);
48         try {
49             for (int testCount = 0; testCount < mNumDeviceReboots; testCount++) {
50                 testId = new TestDescription("RebootTest",
51                                             String.format("RebootLoop #%d", testCount));
52                 listener.testStarted(testId);
53                 try {
54                     testInfo.getDevice().reboot();
55                 } catch (DeviceNotAvailableException e) {
56                     listener.testFailed(testId, "Failed to reboot.");
57                 } finally {
58                     listener.testEnded(testId, emptyMap);
59                 }
60             }
61         }
62         finally {
63             listener.testRunEnded(System.currentTimeMillis() - start, emptyMap);
64         }
65     }
66 }
67