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.systemui.util; 18 19 import android.os.Looper; 20 21 import androidx.annotation.VisibleForTesting; 22 23 /** 24 * Helper providing common assertions. 25 */ 26 public class Assert { 27 private static final Looper sMainLooper = Looper.getMainLooper(); 28 private static Thread sTestThread = null; 29 30 @VisibleForTesting setTestableLooper(Looper testLooper)31 public static void setTestableLooper(Looper testLooper) { 32 setTestThread(testLooper == null ? null : testLooper.getThread()); 33 } 34 35 @VisibleForTesting setTestThread(Thread thread)36 public static void setTestThread(Thread thread) { 37 sTestThread = thread; 38 } 39 isMainThread()40 public static void isMainThread() { 41 if (!sMainLooper.isCurrentThread() 42 && (sTestThread == null || sTestThread != Thread.currentThread())) { 43 throw new IllegalStateException("should be called from the main thread." 44 + " sMainLooper.threadName=" + sMainLooper.getThread().getName() 45 + " Thread.currentThread()=" + Thread.currentThread().getName()); 46 } 47 } 48 isNotMainThread()49 public static void isNotMainThread() { 50 if (sMainLooper.isCurrentThread() 51 && (sTestThread == null || sTestThread == Thread.currentThread())) { 52 throw new IllegalStateException("should not be called from the main thread."); 53 } 54 } 55 } 56