Home | History | Annotate | Download | only in example
      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.device.IManagedTestDevice;
     22 import com.android.tradefed.device.ITestDevice;
     23 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     24 import com.android.tradefed.result.ITestInvocationListener;
     25 import com.android.tradefed.result.TestDescription;
     26 import com.android.tradefed.testtype.IDeviceTest;
     27 import com.android.tradefed.testtype.IRemoteTest;
     28 
     29 import java.util.HashMap;
     30 
     31 /**
     32  * Reboots the device and verifies it comes back online.
     33  * This simple reboot tests acts as an example integration test.
     34  */
     35 public class RebootTest implements IRemoteTest, IDeviceTest {
     36     private ITestDevice mDevice = null;
     37 
     38     @Option(name = "num-of-reboots", description = "Number of times to reboot the device.")
     39     private int mNumDeviceReboots = 1;
     40 
     41     /**
     42      * {@inheritDoc}
     43      */
     44     @Override
     45     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
     46         long start;
     47         HashMap<String, Metric> emptyMap = new HashMap<>();
     48         TestDescription testId;
     49         start = System.currentTimeMillis();
     50         listener.testRunStarted(String.format("#%d device reboots", mNumDeviceReboots),
     51                                 mNumDeviceReboots);
     52         try {
     53             for (int testCount = 0; testCount < mNumDeviceReboots; testCount++) {
     54                 testId = new TestDescription("RebootTest",
     55                                             String.format("RebootLoop #%d", testCount));
     56                 listener.testStarted(testId);
     57                 try {
     58                     getDevice().nonBlockingReboot();
     59                     if (((IManagedTestDevice) getDevice()).getMonitor().waitForDeviceOnline()
     60                             == null) {
     61                         listener.testFailed(testId, "Reboot failed");
     62                         ((IManagedTestDevice) getDevice()).recoverDevice();
     63                     }
     64                 }
     65                 finally {
     66                     listener.testEnded(testId, emptyMap);
     67                 }
     68             }
     69         }
     70         finally {
     71             listener.testRunEnded(System.currentTimeMillis() - start, emptyMap);
     72         }
     73     }
     74 
     75     /**
     76      * {@inheritDoc}
     77      */
     78     @Override
     79     public void setDevice(ITestDevice device) {
     80         mDevice = device;
     81     }
     82 
     83     /**
     84      * {@inheritDoc}
     85      */
     86     @Override
     87     public ITestDevice getDevice() {
     88         return mDevice;
     89     }
     90 }
     91