Home | History | Annotate | Download | only in phone
      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 
     18 package com.android.phone;
     19 
     20 import android.os.Looper;
     21 
     22 /**
     23  * Assertions which will result in program termination.
     24  */
     25 public class Assert {
     26 
     27     private static Boolean sIsMainThreadForTest;
     28 
     29     public static void isTrue(boolean condition) {
     30         if (!condition) {
     31             throw new AssertionError("Expected condition to be true");
     32         }
     33     }
     34 
     35     public static void isMainThread() {
     36         if (sIsMainThreadForTest != null) {
     37             isTrue(sIsMainThreadForTest);
     38             return;
     39         }
     40         isTrue(Looper.getMainLooper().equals(Looper.myLooper()));
     41     }
     42 
     43     public static void isNotMainThread() {
     44         if (sIsMainThreadForTest != null) {
     45             isTrue(!sIsMainThreadForTest);
     46             return;
     47         }
     48         isTrue(!Looper.getMainLooper().equals(Looper.myLooper()));
     49     }
     50 
     51     public static void fail() {
     52         fail("Fail");
     53     }
     54 
     55     public static void fail(String reason) {
     56         throw new AssertionError(reason);
     57     }
     58 
     59     /**
     60      * Override the main thread status for tests. Set to null to revert to normal behavior
     61      */
     62     @NeededForTesting
     63     public static void setIsMainThreadForTesting(Boolean isMainThread) {
     64         sIsMainThreadForTest = isMainThread;
     65     }
     66 }
     67