Home | History | Annotate | Download | only in usb
      1 /*
      2  * Copyright (C) 2016 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.cts.verifier.usb;
     18 
     19 import androidx.annotation.NonNull;
     20 import android.util.Log;
     21 
     22 /**
     23  * Utilities for the USB CTS verifier tests.
     24  */
     25 public class Util {
     26     private static final String LOG_TAG = Util.class.getSimpleName();
     27 
     28     /**
     29      * Run a {@link Invokable} and expect a {@link Throwable}.
     30      *
     31      * @param r             The {@link Invokable} to run
     32      * @param expectedClass The expected {@link Throwable} type
     33      */
     34     public static void runAndAssertException(@NonNull Invokable r,
     35             @NonNull Class<? extends Throwable> expectedClass) throws Throwable {
     36         try {
     37             r.run();
     38         } catch (Throwable e) {
     39             if (e.getClass().isAssignableFrom(expectedClass)) {
     40                 return;
     41             } else {
     42                 Log.e(LOG_TAG, "Expected: " + expectedClass.getName() + ", got: "
     43                         + e.getClass().getName());
     44                 throw e;
     45             }
     46         }
     47 
     48         throw new AssertionError("No throwable thrown");
     49     }
     50 
     51 
     52     /**
     53      * A {@link Runnable} that can throw an {@link Throwable}.
     54      */
     55     public interface Invokable {
     56         /**
     57          * Run the code that might cause an exception.
     58          */
     59         void run() throws Throwable;
     60     }
     61 }
     62