1 /* <lambda>null2 * 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 17 package com.android.intentresolver 18 19 import com.android.systemui.flags.BooleanFlag 20 import org.junit.rules.TestRule 21 import org.junit.runner.Description 22 import org.junit.runners.model.Statement 23 24 /** 25 * Ignores tests annotated with [RequireFeatureFlags] which flag requirements does not 26 * meet in the active flag set. 27 * @param flags active flag set 28 */ 29 internal class FeatureFlagRule(flags: Map<BooleanFlag, Boolean>) : TestRule { 30 private val flags = flags.entries.fold(HashMap<String, Boolean>()) { map, (key, value) -> 31 map.apply { 32 put(key.name, value) 33 } 34 } 35 private val skippingStatement = object : Statement() { 36 override fun evaluate() = Unit 37 } 38 39 override fun apply(base: Statement, description: Description): Statement { 40 val annotation = description.annotations.firstOrNull { 41 it is RequireFeatureFlags 42 } as? RequireFeatureFlags 43 ?: return base 44 45 if (annotation.flags.size != annotation.values.size) { 46 error("${description.className}#${description.methodName}: inconsistent number of" + 47 " flags and values in $annotation") 48 } 49 for (i in annotation.flags.indices) { 50 val flag = annotation.flags[i] 51 val value = annotation.values[i] 52 if (flags.getOrDefault(flag, !value) != value) return skippingStatement 53 } 54 return base 55 } 56 } 57