1 /*
<lambda>null2  * 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 
17 package com.android.testutils
18 
19 import com.android.testutils.ExceptionUtils.ThrowingRunnable
20 import java.lang.reflect.Modifier
21 import kotlin.system.measureTimeMillis
22 import kotlin.test.assertEquals
23 import kotlin.test.assertFailsWith
24 import kotlin.test.assertFalse
25 import kotlin.test.assertTrue
26 
27 private const val TAG = "Connectivity unit test"
28 
29 fun <T> assertEmpty(ts: Array<T>) = ts.size.let { len ->
30     assertEquals(0, len, "Expected empty array, but length was $len")
31 }
32 
lennull33 fun <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 
37 // Bridge method to help write this in Java. If you're writing Kotlin, consider using
38 // kotlin.test.assertFailsWith instead, as that method is reified and inlined.
assertThrowsnull39 fun <T : Exception> assertThrows(expected: Class<T>, block: ThrowingRunnable): T {
40     return assertFailsWith(expected.kotlin) { block.run() }
41 }
42 
assertThrowsnull43 fun <T : Exception> assertThrows(msg: String, expected: Class<T>, block: ThrowingRunnable): T {
44     return assertFailsWith(expected.kotlin, msg) { block.run() }
45 }
46 
assertEqualBothWaysnull47 fun <T> assertEqualBothWays(o1: T, o2: T) {
48     assertTrue(o1 == o2)
49     assertTrue(o2 == o1)
50 }
51 
assertNotEqualEitherWaynull52 fun <T> assertNotEqualEitherWay(o1: T, o2: T) {
53     assertFalse(o1 == o2)
54     assertFalse(o2 == o1)
55 }
56 
assertStringContainsnull57 fun assertStringContains(got: String, want: String) {
58     assertTrue(got.contains(want), "$got did not contain \"${want}\"")
59 }
60 
assertContainsExactlynull61 fun 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 
assertContainsStringsExactlynull67 fun assertContainsStringsExactly(actual: Array<String>, vararg expected: String) {
68     assertEquals(actual.sorted(), expected.sorted(),
69             "$actual does not contain exactly $expected")
70 }
71 
assertContainsAllnull72 fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) {
73     assertContainsAll(list, elems.asList())
74 }
75 
assertContainsAllnull76 fun <T> assertContainsAll(list: Collection<T>, elems: Collection<T>) {
77     elems.forEach { assertTrue(list.contains(it), "$it not in list") }
78 }
79 
assertRunsInAtMostnull80 fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: Runnable) {
81     assertRunsInAtMost(descr, timeLimit) { fn.run() }
82 }
83 
assertRunsInAtMostnull84 fun 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  */
assertFieldCountEqualsnull99 fun <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 }
104