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