blob: 09126d7992ebe74807a2326bc907f885e4322130 [file] [log] [blame]
Chalard Jean48c6c7d2020-06-25 23:39:15 +09001/*
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 Jeanb1856fe2020-06-26 00:42:01 +090017@file:JvmName("MiscAsserts")
18
Chalard Jean48c6c7d2020-06-25 23:39:15 +090019package com.android.testutils
20
21import com.android.testutils.ExceptionUtils.ThrowingRunnable
22import java.lang.reflect.Modifier
23import kotlin.system.measureTimeMillis
24import kotlin.test.assertEquals
25import kotlin.test.assertFailsWith
26import kotlin.test.assertFalse
27import kotlin.test.assertTrue
28
29private const val TAG = "Connectivity unit test"
30
31fun <T> assertEmpty(ts: Array<T>) = ts.size.let { len ->
32 assertEquals(0, len, "Expected empty array, but length was $len")
33}
34
35fun <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 Jeane06c5c82020-06-26 00:16:27 +090039// Bridge method to help write this in Java. If you're writing Kotlin, consider using
Chalard Jean48c6c7d2020-06-25 23:39:15 +090040// kotlin.test.assertFailsWith instead, as that method is reified and inlined.
41fun <T : Exception> assertThrows(expected: Class<T>, block: ThrowingRunnable): T {
42 return assertFailsWith(expected.kotlin) { block.run() }
43}
44
45fun <T : Exception> assertThrows(msg: String, expected: Class<T>, block: ThrowingRunnable): T {
46 return assertFailsWith(expected.kotlin, msg) { block.run() }
47}
48
49fun <T> assertEqualBothWays(o1: T, o2: T) {
50 assertTrue(o1 == o2)
51 assertTrue(o2 == o1)
52}
53
54fun <T> assertNotEqualEitherWay(o1: T, o2: T) {
55 assertFalse(o1 == o2)
56 assertFalse(o2 == o1)
57}
58
59fun assertStringContains(got: String, want: String) {
60 assertTrue(got.contains(want), "$got did not contain \"${want}\"")
61}
62
63fun 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
69fun assertContainsStringsExactly(actual: Array<String>, vararg expected: String) {
70 assertEquals(actual.sorted(), expected.sorted(),
71 "$actual does not contain exactly $expected")
72}
73
74fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) {
75 assertContainsAll(list, elems.asList())
76}
77
78fun <T> assertContainsAll(list: Collection<T>, elems: Collection<T>) {
79 elems.forEach { assertTrue(list.contains(it), "$it not in list") }
80}
81
82fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: Runnable) {
83 assertRunsInAtMost(descr, timeLimit) { fn.run() }
84}
85
86fun 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 */
101fun <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}