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