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 17 @file:JvmName("MiscAsserts") 18 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> assertEmpty(ts: Collection<T>) = ts.size.let { len -> 36 assertEquals(0, len, "Expected empty collection, but length was $len") 37 } 38 39 fun <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 43 fun <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 47 // Bridge method to help write this in Java. If you're writing Kotlin, consider using 48 // kotlin.test.assertFailsWith instead, as that method is reified and inlined. 49 fun <T : Exception> assertThrows(expected: Class<T>, block: ThrowingRunnable): T { 50 return assertFailsWith(expected.kotlin) { block.run() } 51 } 52 53 fun <T : Exception> assertThrows(msg: String, expected: Class<T>, block: ThrowingRunnable): T { 54 return assertFailsWith(expected.kotlin, msg) { block.run() } 55 } 56 57 fun <T> assertEqualBothWays(o1: T, o2: T) { 58 assertTrue(o1 == o2) 59 assertTrue(o2 == o1) 60 } 61 62 fun <T> assertNotEqualEitherWay(o1: T, o2: T) { 63 assertFalse(o1 == o2) 64 assertFalse(o2 == o1) 65 } 66 67 fun assertStringContains(got: String, want: String) { 68 assertTrue(got.contains(want), "$got did not contain \"${want}\"") 69 } 70 71 fun 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 77 fun assertContainsStringsExactly(actual: Array<String>, vararg expected: String) { 78 assertEquals(actual.sorted(), expected.sorted(), 79 "$actual does not contain exactly $expected") 80 } 81 82 fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) { 83 assertContainsAll(list, elems.asList()) 84 } 85 86 fun <T> assertContainsAll(list: Collection<T>, elems: Collection<T>) { 87 elems.forEach { assertTrue(list.contains(it), "$it not in list") } 88 } 89 90 fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: Runnable) { 91 assertRunsInAtMost(descr, timeLimit) { fn.run() } 92 } 93 94 fun 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 */ 109 fun <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 } 114 115 fun <T> assertSameElements(expected: List<T>, actual: List<T>) { 116 val expectedSet: HashSet<T> = HashSet(expected) 117 assertEquals(expectedSet.size, expected.size, "expected list contains duplicates") 118 val actualSet: HashSet<T> = HashSet(actual) 119 assertEquals(actualSet.size, actual.size, "actual list contains duplicates") 120 assertEquals(expectedSet, actualSet) 121 }