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.bedstead.enterprise.annotations;
18 
19 import static com.android.bedstead.enterprise.annotations.EnsureHasDeviceOwner.DO_PO_PRIORITY;
20 
21 import com.android.bedstead.harrier.annotations.AnnotationPriorityRunPrecedence;
22 import com.android.bedstead.harrier.annotations.RequireNotInstantApp;
23 import com.android.bedstead.harrier.annotations.UsesAnnotationExecutor;
24 
25 import java.lang.annotation.ElementType;
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.lang.annotation.Target;
29 
30 /**
31  * Mark that a test requires that the given admin delegates the given scope to a test app.
32  *
33  * <p>You should use {@code DeviceState} to ensure that the device enters
34  * the correct state for the method. You can use {@code Devicestate#delegate()} to interact with
35  * the delegate.
36  */
37 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE})
38 @Retention(RetentionPolicy.RUNTIME)
39 // TODO(b/206441366): Add instant app support
40 @RequireNotInstantApp(reason = "Instant Apps cannot run Enterprise Tests")
41 // TODO(b/219750042): If we leave over appops and permissions then the delegate will have them
42 @UsesAnnotationExecutor(UsesAnnotationExecutor.ENTERPRISE)
43 public @interface EnsureHasDelegate {
44 
45     /** The default key used for the testapp installed as delegate */
46     String DELEGATE_KEY = "delegate";
47 
48     // TODO(276740719): Add support for customisable delegates
49 //    /**
50 //     * The key used to identify this delegate.
51 //     *
52 //     * <p>This can be used with {@link AdditionalQueryParameters} to modify the requirements for
53 //     * the delegate. */
54 //    String key() default DELEGATE_KEY;
55 
56     int ENSURE_HAS_DELEGATE_PRIORITY = DO_PO_PRIORITY + 1; // Should run after setting DO/PO
57 
58     enum AdminType {
59         DEVICE_OWNER,
60         PROFILE_OWNER,
61         PRIMARY
62     }
63 
64     // TODO(276740719): Add support for querying for the delegate
65 
66     /**
67      * The admin that should delegate this scope.
68      *
69      * <p>If this is set to {@link AdminType#PRIMARY} and {@link #isPrimary()} is true, then the
70      * delegate will replace the primary dpc as primary without error.
71      */
admin()72     AdminType admin();
73 
74     /** The scope being delegated. */
scopes()75     String[] scopes();
76 
77     /**
78      * Whether this delegate should be returned by calls to {@code DeviceState#dpc()}.
79      *
80      * <p>Only one policy manager per test should be marked as primary.
81      */
isPrimary()82     boolean isPrimary() default false;
83 
84      /**
85      * Priority sets the order that annotations will be resolved.
86      *
87      * <p>Annotations with a lower priority will be resolved before annotations with a higher
88      * priority.
89      *
90      * <p>If there is an order requirement between annotations, ensure that the priority of the
91      * annotation which must be resolved first is lower than the one which must be resolved later.
92      *
93      * <p>Priority can be set to a {@link AnnotationPriorityRunPrecedence} constant, or to any {@link int}.
94      */
priority()95     int priority() default ENSURE_HAS_DELEGATE_PRIORITY;
96 }
97