blob: 91fb1cd43cb1e800f42b13432411bd9343963b4b [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
junyulai148bdcb2021-03-03 20:21:08 +080035fun <T> assertEmpty(ts: List<T>) = ts.size.let { len ->
36 assertEquals(0, len, "Expected empty list, but length was $len")
37}
38
Chalard Jean48c6c7d2020-06-25 23:39:15 +090039fun <T> assertLength(expected: Int, got: Array<T>) = got.size.let { len ->
40 assertEquals(expected, len, "Expected array of length $expected, but was $len for $got")
41}
42
junyulai148bdcb2021-03-03 20:21:08 +080043fun <T> assertLength(expected: Int, got: List<T>) = got.size.let { len ->
44 assertEquals(expected, len, "Expected list of length $expected, but was $len for $got")
45}
46
Chalard Jeane06c5c82020-06-26 00:16:27 +090047// Bridge method to help write this in Java. If you're writing Kotlin, consider using
Chalard Jean48c6c7d2020-06-25 23:39:15 +090048// kotlin.test.assertFailsWith instead, as that method is reified and inlined.
49fun <T : Exception> assertThrows(expected: Class<T>, block: ThrowingRunnable): T {
50 return assertFailsWith(expected.kotlin) { block.run() }
51}
52
53fun <T : Exception> assertThrows(msg: String, expected: Class<T>, block: ThrowingRunnable): T {
54 return assertFailsWith(expected.kotlin, msg) { block.run() }
55}
56
57fun <T> assertEqualBothWays(o1: T, o2: T) {
58 assertTrue(o1 == o2)
59 assertTrue(o2 == o1)
60}
61
62fun <T> assertNotEqualEitherWay(o1: T, o2: T) {
63 assertFalse(o1 == o2)
64 assertFalse(o2 == o1)
65}
66
67fun assertStringContains(got: String, want: String) {
68 assertTrue(got.contains(want), "$got did not contain \"${want}\"")
69}
70
71fun assertContainsExactly(actual: IntArray, vararg expected: Int) {
72 // IntArray#sorted() returns a list, so it's fine to test with equals()
73 assertEquals(actual.sorted(), expected.sorted(),
74 "$actual does not contain exactly $expected")
75}
76
77fun assertContainsStringsExactly(actual: Array<String>, vararg expected: String) {
78 assertEquals(actual.sorted(), expected.sorted(),
79 "$actual does not contain exactly $expected")
80}
81
82fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) {
83 assertContainsAll(list, elems.asList())
84}
85
86fun <T> assertContainsAll(list: Collection<T>, elems: Collection<T>) {
87 elems.forEach { assertTrue(list.contains(it), "$it not in list") }
88}
89
90fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: Runnable) {
91 assertRunsInAtMost(descr, timeLimit) { fn.run() }
92}
93
94fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: () -> Unit) {
95 val timeTaken = measureTimeMillis(fn)
96 val msg = String.format("%s: took %dms, limit was %dms", descr, timeTaken, timeLimit)
97 assertTrue(timeTaken <= timeLimit, msg)
98}
99
100/**
101 * Verifies that the number of nonstatic fields in a java class equals a given count.
102 * Note: this is essentially not useful for Kotlin code where fields are not really a thing.
103 *
104 * This assertion serves as a reminder to update test code around it if fields are added
105 * after the test is written.
106 * @param count Expected number of nonstatic fields in the class.
107 * @param clazz Class to test.
108 */
109fun <T> assertFieldCountEquals(count: Int, clazz: Class<T>) {
110 assertEquals(count, clazz.declaredFields.filter {
111 !Modifier.isStatic(it.modifiers) && !Modifier.isTransient(it.modifiers)
112 }.size)
113}
junyulai148bdcb2021-03-03 20:21:08 +0800114
115fun assertSameElements(expected: List<String?>, actual: List<String?>) {
116 val expectedSet: HashSet<String?> = HashSet<String?>(expected)
117 assertEquals(expectedSet.size.toLong(), expected.size.toLong(),
118 "expected list contains duplicates")
119 val actualSet: HashSet<String?> = HashSet<String?>(actual)
120 assertEquals(actualSet.size.toLong(), actual.size.toLong(), "actual list contains duplicates")
121 assertEquals(expectedSet, actualSet)
122}