blob: 407a1bcaa2e76e9a0990e192607207ded6dc2ce4 [file] [log] [blame]
Bernardo Rufino41349c02018-01-10 21:09:28 +00001/*
2 * Copyright (C) 2018 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
17package com.android.server.backup.testing;
18
Bernardo Rufino3184cc92018-01-20 16:54:58 +000019import static com.google.common.truth.Truth.assertThat;
20
21
Bernardo Rufino41349c02018-01-10 21:09:28 +000022import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
23
Bernardo Rufino3184cc92018-01-20 16:54:58 +000024import org.robolectric.shadows.ShadowLog;
25
Bernardo Rufino41349c02018-01-10 21:09:28 +000026import java.util.concurrent.Callable;
27
28public class TestUtils {
Bernardo Rufino3184cc92018-01-20 16:54:58 +000029 /** Reset logcat with {@link ShadowLog#reset()} before the test case */
30 public static void assertLogcatAtMost(String tag, int level) {
31 assertThat(ShadowLog.getLogsForTag(tag).stream().allMatch(logItem -> logItem.type <= level))
32 .isTrue();
33 }
34
35 /** Reset logcat with {@link ShadowLog#reset()} before the test case */
36 public static void assertLogcatAtLeast(String tag, int level) {
37 assertThat(ShadowLog.getLogsForTag(tag).stream().anyMatch(logItem -> logItem.type >= level))
38 .isTrue();
39 }
40
Bernardo Rufino41349c02018-01-10 21:09:28 +000041 /**
42 * Calls {@link Runnable#run()} and returns if no exception is thrown. Otherwise, if the
43 * exception is unchecked, rethrow it; if it's checked wrap in a {@link RuntimeException} and
44 * throw.
45 *
46 * <p><b>Warning:</b>DON'T use this outside tests. A wrapped checked exception is just a failure
47 * in a test.
48 */
49 public static void uncheck(ThrowingRunnable runnable) {
50 try {
51 runnable.runOrThrow();
52 } catch (Exception e) {
53 throw wrapIfChecked(e);
54 }
55 }
56
57 /**
58 * Calls {@link Callable#call()} and returns the value if no exception is thrown. Otherwise, if
59 * the exception is unchecked, rethrow it; if it's checked wrap in a {@link RuntimeException}
60 * and throw.
61 *
62 * <p><b>Warning:</b>DON'T use this outside tests. A wrapped checked exception is just a failure
63 * in a test.
64 */
65 public static <T> T uncheck(Callable<T> callable) {
66 try {
67 return callable.call();
68 } catch (Exception e) {
69 throw wrapIfChecked(e);
70 }
71 }
72
73 /**
74 * Wrap {@code e} in a {@link RuntimeException} only if it's not one already, in which case it's
75 * returned.
76 */
77 public static RuntimeException wrapIfChecked(Exception e) {
78 if (e instanceof RuntimeException) {
79 return (RuntimeException) e;
80 }
81 return new RuntimeException(e);
82 }
83
84 private TestUtils() {}
85}