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.bedstead.harrier;
17 
18 import androidx.annotation.NonNull;
19 
20 import com.android.bedstead.nene.utils.FailureDumper;
21 
22 import java.lang.annotation.Annotation;
23 
24 /**
25  * Interface used to register a new class which can execute Harrier annotations.
26  * <p>
27  * This can be used to add additional harrier-compatible annotations without modifying harrier
28  * <p>
29  * Ideally instances of this interface shouldn't contain state but should reference to
30  * extension functions or use objects of {@link DeviceStateComponent}
31  * with {@link BedsteadServiceLocator} as the provider
32  */
33 // This is written in Java because Kotlin interfaces can't expose default methods to Java
34 public interface AnnotationExecutor extends FailureDumper {
35     /**
36      * Called when an annotation should be applied.
37      *
38      * <p>This should take care of recording any state necessary to correctly restore state after
39      * the test.
40      * Annotations that don't need the context of device state components
41      * could be handled by extension functions like: {@link AnnotationLogicExtensionsKt}
42      */
applyAnnotation(@onNull Annotation annotation)43     void applyAnnotation(@NonNull Annotation annotation);
44 
45     /**
46      * Called when a test has failed which used this annotation executor.
47      */
onTestFailed(@onNull Throwable exception)48     default void onTestFailed(@NonNull Throwable exception) {}
49 }
50