1 /*
2  * Copyright (C) 2023 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 package com.android.safetycenter.testing
17 
18 import android.content.Context
19 import org.junit.Assume.assumeFalse
20 import org.junit.Assume.assumeTrue
21 import org.junit.rules.TestRule
22 import org.junit.runner.Description
23 import org.junit.runners.model.Statement
24 
25 /**
26  * JUnit [TestRule] for on-device tests that requires Safety Center to be supported. This rule does
27  * not require Safety Center to be enabled.
28  *
29  * For tests which should only run on devices where Safety Center is not supported, instantiate with
30  * [requireSupportIs] set to `false` to invert the condition.
31  */
32 class SupportsSafetyCenterRule(private val context: Context, requireSupportIs: Boolean = true) :
33     TestRule {
34 
35     private val shouldSupportSafetyCenter: Boolean = requireSupportIs
36 
applynull37     override fun apply(base: Statement, description: Description): Statement {
38         return object : Statement() {
39             override fun evaluate() {
40                 val support = context.deviceSupportsSafetyCenter()
41                 if (shouldSupportSafetyCenter) {
42                     assumeTrue("Test device does not support Safety Center", support)
43                 } else {
44                     assumeFalse("Test device supports Safety Center", support)
45                 }
46                 base.evaluate()
47             }
48         }
49     }
50 }
51