• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.systemui
18 
19 import androidx.test.filters.SmallTest
20 import com.google.common.truth.Truth.assertThat
21 import org.junit.Assert.fail
22 import org.junit.Before
23 import org.junit.Rule
24 import org.junit.Test
25 import org.junit.runner.JUnitCore
26 
27 @Suppress("JUnitMalformedDeclaration")
28 @SmallTest
29 class OnTeardownRuleTest : SysuiTestCase() {
30     // None of these inner classes should be run except as part of this utilities-testing test
31     class HasTeardown {
32         @get:Rule val teardownRule = OnTeardownRule()
33 
34         @Before
setUpnull35         fun setUp() {
36             teardownWasRun = false
37             teardownRule.onTeardown { teardownWasRun = true }
38         }
39 
doTestnull40         @Test fun doTest() {}
41 
42         companion object {
43             var teardownWasRun = false
44         }
45     }
46 
47     @Test
teardownRunsnull48     fun teardownRuns() {
49         val result = JUnitCore().run(HasTeardown::class.java)
50         assertThat(result.failures).isEmpty()
51         assertThat(HasTeardown.teardownWasRun).isTrue()
52     }
53 
54     class FirstTeardownFails {
55         @get:Rule val teardownRule = OnTeardownRule()
56 
57         @Before
setUpnull58         fun setUp() {
59             teardownWasRun = false
60             teardownRule.onTeardown { fail("One fails") }
61             teardownRule.onTeardown { teardownWasRun = true }
62         }
63 
doTestnull64         @Test fun doTest() {}
65 
66         companion object {
67             var teardownWasRun = false
68         }
69     }
70 
71     @Test
allTeardownsRunnull72     fun allTeardownsRun() {
73         val result = JUnitCore().run(FirstTeardownFails::class.java)
74         assertThat(result.failures.map { it.message }).isEqualTo(listOf("One fails"))
75         assertThat(FirstTeardownFails.teardownWasRun).isTrue()
76     }
77 
78     class ThreeTeardowns {
79         @get:Rule val teardownRule = OnTeardownRule()
80 
81         @Before
setUpnull82         fun setUp() {
83             messages.clear()
84         }
85 
86         @Test
doTestnull87         fun doTest() {
88             teardownRule.onTeardown { messages.add("A") }
89             teardownRule.onTeardown { messages.add("B") }
90             teardownRule.onTeardown { messages.add("C") }
91         }
92 
93         companion object {
94             val messages = mutableListOf<String>()
95         }
96     }
97 
98     @Test
reverseOrdernull99     fun reverseOrder() {
100         val result = JUnitCore().run(ThreeTeardowns::class.java)
101         assertThat(result.failures).isEmpty()
102         assertThat(ThreeTeardowns.messages).isEqualTo(listOf("C", "B", "A"))
103     }
104 
105     class TryToDoABadThing {
106         @get:Rule val teardownRule = OnTeardownRule()
107 
108         @Test
doTestnull109         fun doTest() {
110             teardownRule.onTeardown {
111                 teardownRule.onTeardown {
112                     // do nothing
113                 }
114             }
115         }
116     }
117 
118     @Test
prohibitTeardownDuringTeardownnull119     fun prohibitTeardownDuringTeardown() {
120         val result = JUnitCore().run(TryToDoABadThing::class.java)
121         assertThat(result.failures.map { it.message })
122             .isEqualTo(listOf("Cannot add new teardown routines after test complete."))
123     }
124 }
125