Chalard Jean | 48c6c7d | 2020-06-25 23:39:15 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Chalard Jean | b1856fe | 2020-06-26 00:42:01 +0900 | [diff] [blame^] | 17 | @file:JvmName("MiscAsserts") |
| 18 | |
Chalard Jean | 48c6c7d | 2020-06-25 23:39:15 +0900 | [diff] [blame] | 19 | package com.android.testutils |
| 20 | |
| 21 | import com.android.testutils.ExceptionUtils.ThrowingRunnable |
| 22 | import java.lang.reflect.Modifier |
| 23 | import kotlin.system.measureTimeMillis |
| 24 | import kotlin.test.assertEquals |
| 25 | import kotlin.test.assertFailsWith |
| 26 | import kotlin.test.assertFalse |
| 27 | import kotlin.test.assertTrue |
| 28 | |
| 29 | private const val TAG = "Connectivity unit test" |
| 30 | |
| 31 | fun <T> assertEmpty(ts: Array<T>) = ts.size.let { len -> |
| 32 | assertEquals(0, len, "Expected empty array, but length was $len") |
| 33 | } |
| 34 | |
| 35 | fun <T> assertLength(expected: Int, got: Array<T>) = got.size.let { len -> |
| 36 | assertEquals(expected, len, "Expected array of length $expected, but was $len for $got") |
| 37 | } |
| 38 | |
Chalard Jean | e06c5c8 | 2020-06-26 00:16:27 +0900 | [diff] [blame] | 39 | // Bridge method to help write this in Java. If you're writing Kotlin, consider using |
Chalard Jean | 48c6c7d | 2020-06-25 23:39:15 +0900 | [diff] [blame] | 40 | // kotlin.test.assertFailsWith instead, as that method is reified and inlined. |
| 41 | fun <T : Exception> assertThrows(expected: Class<T>, block: ThrowingRunnable): T { |
| 42 | return assertFailsWith(expected.kotlin) { block.run() } |
| 43 | } |
| 44 | |
| 45 | fun <T : Exception> assertThrows(msg: String, expected: Class<T>, block: ThrowingRunnable): T { |
| 46 | return assertFailsWith(expected.kotlin, msg) { block.run() } |
| 47 | } |
| 48 | |
| 49 | fun <T> assertEqualBothWays(o1: T, o2: T) { |
| 50 | assertTrue(o1 == o2) |
| 51 | assertTrue(o2 == o1) |
| 52 | } |
| 53 | |
| 54 | fun <T> assertNotEqualEitherWay(o1: T, o2: T) { |
| 55 | assertFalse(o1 == o2) |
| 56 | assertFalse(o2 == o1) |
| 57 | } |
| 58 | |
| 59 | fun assertStringContains(got: String, want: String) { |
| 60 | assertTrue(got.contains(want), "$got did not contain \"${want}\"") |
| 61 | } |
| 62 | |
| 63 | fun assertContainsExactly(actual: IntArray, vararg expected: Int) { |
| 64 | // IntArray#sorted() returns a list, so it's fine to test with equals() |
| 65 | assertEquals(actual.sorted(), expected.sorted(), |
| 66 | "$actual does not contain exactly $expected") |
| 67 | } |
| 68 | |
| 69 | fun assertContainsStringsExactly(actual: Array<String>, vararg expected: String) { |
| 70 | assertEquals(actual.sorted(), expected.sorted(), |
| 71 | "$actual does not contain exactly $expected") |
| 72 | } |
| 73 | |
| 74 | fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) { |
| 75 | assertContainsAll(list, elems.asList()) |
| 76 | } |
| 77 | |
| 78 | fun <T> assertContainsAll(list: Collection<T>, elems: Collection<T>) { |
| 79 | elems.forEach { assertTrue(list.contains(it), "$it not in list") } |
| 80 | } |
| 81 | |
| 82 | fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: Runnable) { |
| 83 | assertRunsInAtMost(descr, timeLimit) { fn.run() } |
| 84 | } |
| 85 | |
| 86 | fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: () -> Unit) { |
| 87 | val timeTaken = measureTimeMillis(fn) |
| 88 | val msg = String.format("%s: took %dms, limit was %dms", descr, timeTaken, timeLimit) |
| 89 | assertTrue(timeTaken <= timeLimit, msg) |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Verifies that the number of nonstatic fields in a java class equals a given count. |
| 94 | * Note: this is essentially not useful for Kotlin code where fields are not really a thing. |
| 95 | * |
| 96 | * This assertion serves as a reminder to update test code around it if fields are added |
| 97 | * after the test is written. |
| 98 | * @param count Expected number of nonstatic fields in the class. |
| 99 | * @param clazz Class to test. |
| 100 | */ |
| 101 | fun <T> assertFieldCountEquals(count: Int, clazz: Class<T>) { |
| 102 | assertEquals(count, clazz.declaredFields.filter { |
| 103 | !Modifier.isStatic(it.modifiers) && !Modifier.isTransient(it.modifiers) |
| 104 | }.size) |
| 105 | } |