blob: 143e66f896d37cc8e01846e652ffa196712e3412 [file] [log] [blame]
Ta-wei Yenb250ce82016-06-07 15:16:30 -07001/*
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
18package com.android.phone;
19
20import android.os.Looper;
21
22/**
23 * Assertions which will result in program termination.
24 */
25public class Assert {
26
Ta-wei Yenccb523d2016-07-12 18:18:39 -070027 private static Boolean sIsMainThreadForTest;
28
Ta-wei Yenb250ce82016-06-07 15:16:30 -070029 public static void isTrue(boolean condition) {
30 if (!condition) {
31 throw new AssertionError("Expected condition to be true");
32 }
33 }
34
Ta-wei Yenccb523d2016-07-12 18:18:39 -070035 public static void isMainThread() {
36 if (sIsMainThreadForTest != null) {
37 isTrue(sIsMainThreadForTest);
38 return;
39 }
40 isTrue(Looper.getMainLooper().equals(Looper.myLooper()));
41 }
42
Ta-wei Yenb250ce82016-06-07 15:16:30 -070043 public static void isNotMainThread() {
Ta-wei Yenccb523d2016-07-12 18:18:39 -070044 if (sIsMainThreadForTest != null) {
45 isTrue(!sIsMainThreadForTest);
46 return;
47 }
Ta-wei Yenb250ce82016-06-07 15:16:30 -070048 isTrue(!Looper.getMainLooper().equals(Looper.myLooper()));
49 }
50
51 public static void fail() {
52 throw new AssertionError("Fail");
53 }
Ta-wei Yenccb523d2016-07-12 18:18:39 -070054
55 /**
56 * Override the main thread status for tests. Set to null to revert to normal behavior
57 */
58 @NeededForTesting
59 public static void setIsMainThreadForTesting(Boolean isMainThread) {
60 sIsMainThreadForTest = isMainThread;
61 }
Ta-wei Yenb250ce82016-06-07 15:16:30 -070062}