1 /* 2 * Copyright 2022 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 androidx.test.ext.junitgtesttest 18 19 import androidx.test.ext.junitgtest.GtestRunner 20 import androidx.test.ext.junitgtest.TargetLibrary 21 import com.google.common.truth.Truth.assertThat 22 import org.junit.Test 23 import org.junit.runner.Description 24 import org.junit.runner.RunWith 25 import org.junit.runner.notification.Failure 26 import org.junit.runner.notification.RunListener 27 import org.junit.runner.notification.RunNotifier 28 29 /** 30 * Tests the GtestRunner 31 * 32 * These specific tests would be more appropriate to put in junit-gtest itself, and have an 33 * integration test app run the tests more like an actual app consuming the library would (basically 34 * the [NativeTests] class), but due to b/236913987 it is currently difficult or impossible to make 35 * the test libraries ('apptest') available to the tests without including them in the release AAR. 36 */ 37 class GtestRunnerTest { 38 private val runListener = CountingRunListener() <lambda>null39 private val runNotifier = RunNotifier().apply { addListener(runListener) } 40 41 companion object { 42 private val runner = GtestRunner(NativeTests::class.java) 43 } 44 45 @Test runsTheTestnull46 fun runsTheTest() { 47 runner.run(runNotifier) 48 assertThat(runListener.failures).hasSize(2) 49 val adderFailure = runListener.failures[0] 50 assertThat(adderFailure.message.normalizeWhitespace()) 51 .contains( 52 """ 53 Expected equality of these values: 54 42 55 add(42, 1) 56 Which is: 43 57 """ 58 .normalizeWhitespace() 59 ) 60 61 val uncaughtExceptionFailure = runListener.failures[1] 62 assertThat(uncaughtExceptionFailure.message.normalizeWhitespace()) 63 .contains( 64 """ 65 unknown file:-1 66 Unknown C++ exception thrown in the test body. 67 """ 68 .normalizeWhitespace() 69 ) 70 } 71 72 @Test reportsAllResultsnull73 fun reportsAllResults() { 74 runner.run(runNotifier) 75 assertThat(runListener.descriptions.map { it.displayName }) 76 .isEqualTo( 77 listOf( 78 "adder_pass(androidx.test.ext.junitgtesttest.GtestRunnerTest\$NativeTests)", 79 "foo_fail(androidx.test.ext.junitgtesttest.GtestRunnerTest\$NativeTests)", 80 "JUnitNotifyingListener_handles_null_file_names(androidx.test.ext.junitgtesttest." + 81 "GtestRunnerTest\$NativeTests)" 82 ) 83 ) 84 } 85 normalizeWhitespacenull86 fun String.normalizeWhitespace(): String { 87 return replace("\\s+".toRegex(), " ").trim() 88 } 89 90 class CountingRunListener : RunListener() { 91 val failures = mutableListOf<Failure>() 92 val descriptions = mutableListOf<Description>() 93 testFailurenull94 override fun testFailure(failure: Failure) { 95 failures.add(failure) 96 } 97 testFinishednull98 override fun testFinished(description: Description) { 99 descriptions.add(description) 100 } 101 } 102 103 @RunWith(GtestRunner::class) @TargetLibrary(libraryName = "apptest") class NativeTests 104 } 105