• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.annotation;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.annotation.RestrictedForEnvironment.Environment;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 @RunWith(JUnit4.class)
28 public class RestrictedForEnvironmentTests {
29 
30     @Test
testAnnotationAvailableInRuntime()31     public void testAnnotationAvailableInRuntime() throws Exception {
32         ClassWithAnnotation clz = new ClassWithAnnotation();
33         RestrictedForEnvironment annotation = clz.getClass().getAnnotation(
34                 RestrictedForEnvironment.class);
35 
36         assertThat(annotation).isNotNull();
37     }
38 
39     @Test
testAnnotationIsRepeatable()40     public void testAnnotationIsRepeatable() throws Exception {
41         ClassWithRepeatedAnnotation clz = new ClassWithRepeatedAnnotation();
42         RestrictedForEnvironment[] annotations = clz.getClass().getAnnotationsByType(
43                 RestrictedForEnvironment.class);
44 
45         assertThat(annotations).hasLength(2);
46     }
47 
48     @Test
testAnnotationParameters()49     public void testAnnotationParameters() throws Exception {
50         ClassWithAnnotation clz = new ClassWithAnnotation();
51         RestrictedForEnvironment annotation = clz.getClass().getAnnotation(
52                 RestrictedForEnvironment.class);
53 
54         Environment[] e = annotation.environments();
55         assertThat(e).asList().containsExactly(Environment.SDK_SANDBOX);
56         int from = annotation.from();
57         assertThat(from).isEqualTo(33);
58     }
59 
60     @Test
testAnnotationParameters_environmentToString()61     public void testAnnotationParameters_environmentToString() throws Exception {
62         ClassWithAnnotation clz = new ClassWithAnnotation();
63         RestrictedForEnvironment annotation = clz.getClass().getAnnotation(
64                 RestrictedForEnvironment.class);
65 
66         Environment e = annotation.environments()[0];
67         assertThat(e).isEqualTo(Environment.SDK_SANDBOX);
68         assertThat(e.toString()).isEqualTo("SDK Runtime");
69     }
70 
71     @Test
testAnnotationParameters_environment_multipleEnvironments()72     public void testAnnotationParameters_environment_multipleEnvironments() throws Exception {
73         ClassWithMultipleEnvironment clz = new ClassWithMultipleEnvironment();
74         RestrictedForEnvironment annotation = clz.getClass().getAnnotation(
75                 RestrictedForEnvironment.class);
76 
77         Environment[] e = annotation.environments();
78         assertThat(e).asList().containsExactly(Environment.SDK_SANDBOX, Environment.SDK_SANDBOX);
79     }
80 
81     @RestrictedForEnvironment(environments=Environment.SDK_SANDBOX, from=33)
82     private static class ClassWithAnnotation {
83     }
84 
85     @RestrictedForEnvironment(environments=Environment.SDK_SANDBOX, from=0)
86     @RestrictedForEnvironment(environments=Environment.SDK_SANDBOX, from=0)
87     private static class ClassWithRepeatedAnnotation {
88     }
89 
90     @RestrictedForEnvironment(
91         environments={Environment.SDK_SANDBOX, Environment.SDK_SANDBOX},
92         from=0)
93     private static class ClassWithMultipleEnvironment {
94     }
95 }
96