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 package com.android.cts.backportedfixes; 17 18 import android.os.Build; 19 20 import com.android.cts.backportedfixes.resolver.Status; 21 import com.android.cts.backportedfixes.resolver.StatusResolver; 22 23 import org.junit.AssumptionViolatedException; 24 import org.junit.rules.TestRule; 25 import org.junit.runner.Description; 26 import org.junit.runners.model.Statement; 27 28 import java.util.Locale; 29 30 /** 31 * Validates test annotated with {@link BackportedFixTest}. 32 * 33 * <p>The test will fail if the {@link BackportedFixTest#value()} is not in the list of approved 34 * backported fixes. 35 * 36 * <p>Other test failures will be changed to an assumption failures if {@link 37 * Build#getBackportedFixStatus(long)} returns false. 38 */ 39 public class BackportedFixRule implements TestRule { 40 private final ApprovedBackportedFixes mFixes = ApprovedBackportedFixes.getInstance(); 41 private final StatusResolver mStatusResolver = StatusResolver.create(); 42 43 // TODO: make host version of this. 44 45 @Override apply(Statement statement, Description description)46 public Statement apply(Statement statement, Description description) { 47 BackportedFixTest issue = description.getAnnotation(BackportedFixTest.class); 48 if (issue == null) { 49 return statement; 50 } 51 int alias = mFixes.getAlias(issue.value()); 52 if (!mFixes.getAllIssues().contains(issue.value())) { 53 String msg = 54 String.format( 55 Locale.ROOT, 56 "https://issuetracker.google.com/issues/%d is not an approved" 57 + " backported fix.", 58 issue.value()); 59 throw new IllegalStateException(msg); 60 } 61 return new Statement() { 62 @Override 63 public void evaluate() throws Throwable { 64 try { 65 statement.evaluate(); 66 } catch (AssertionError e) { 67 if (mStatusResolver.getBackportedFixStatus(alias) == Status.Fixed) { 68 throw e; 69 } 70 String msg = 71 String.format( 72 Locale.ROOT, 73 "https://issuetracker.google.com/issues/%d with alias %d is not" 74 + " marked fixed on this device.", 75 issue.value(), 76 alias); 77 throw new AssumptionViolatedException(msg, e); 78 } 79 } 80 }; 81 } 82 } 83