Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.backup.testing; |
| 18 | |
Bernardo Rufino | 3184cc9 | 2018-01-20 16:54:58 +0000 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 22 | import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
| 23 | |
Bernardo Rufino | 3184cc9 | 2018-01-20 16:54:58 +0000 | [diff] [blame] | 24 | import org.robolectric.shadows.ShadowLog; |
| 25 | |
Bernardo Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 26 | import java.util.concurrent.Callable; |
| 27 | |
| 28 | public class TestUtils { |
Bernardo Rufino | 3184cc9 | 2018-01-20 16:54:58 +0000 | [diff] [blame] | 29 | /** 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 Rufino | 41349c0 | 2018-01-10 21:09:28 +0000 | [diff] [blame] | 41 | /** |
| 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 | } |