1 /* 2 * Copyright (C) 2021 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.server.wm.flicker.service.assertors 18 19 import com.android.server.wm.flicker.service.FlickerService 20 import com.android.server.wm.traces.common.tags.Transition 21 import java.io.FileNotFoundException 22 23 /** 24 * Stores data for FASS assertions. 25 */ 26 data class AssertionData( 27 val transitionType: Transition, 28 val assertion: BaseAssertion, 29 val category: String 30 ) { 31 companion object { 32 /** 33 * Returns the name of the assertors configuration file. 34 */ 35 private const val CONFIG_FILE_NAME = "config.json" 36 37 /** 38 * Creates an assertion data based on it's fully-qualified class path [cls] and set 39 * its category to [category] 40 */ fromStringnull41 fun fromString( 42 transitionType: Transition, 43 category: String, 44 cls: String, 45 args: List<String> 46 ): AssertionData { 47 val clsDescriptor = Class.forName(cls) 48 49 val assertionObj = if (args.isEmpty()) { 50 clsDescriptor.newInstance() as BaseAssertion 51 } else { 52 val ctor = clsDescriptor.constructors 53 .firstOrNull { it.parameterCount == args.size } 54 ?: error("Constructor not found") 55 ctor.newInstance(*args.toTypedArray()) as BaseAssertion 56 } 57 58 return AssertionData(transitionType, assertionObj, category) 59 } 60 61 /** 62 * Reads the assertions configuration for the configuration file. 63 * 64 * @param fileName the location of the configuration file 65 * @return a list with assertors configuration 66 * 67 * @throws FileNotFoundException when there is no config file 68 */ 69 @JvmOverloads readConfigurationnull70 fun readConfiguration(fileName: String = CONFIG_FILE_NAME): List<AssertionData> { 71 val fileContent = FlickerService::class.java.classLoader.getResource(fileName) 72 ?.readText(Charsets.UTF_8) 73 ?: throw FileNotFoundException("A configuration file must exist!") 74 return AssertionConfigParser.parseConfigFile(fileContent) 75 } 76 } 77 } 78