1 /*
2  * Copyright (C) 2023 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.platform.test.flag.junit;
18 
19 import static org.junit.Assert.assertThrows;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.platform.test.flag.util.FlagReadException;
23 
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 /** Unit tests for {@code DeviceFlagsValueProvider}. */
31 @RunWith(JUnit4.class)
32 public final class DeviceFlagsValueProviderTest {
33     private final DeviceFlagsValueProvider mFlagsValueProvider = new DeviceFlagsValueProvider();
34 
35     @Before
setUp()36     public void setUp() {
37         mFlagsValueProvider.setUp();
38     }
39 
40     @After
tearDown()41     public void tearDown() {
42         mFlagsValueProvider.tearDownBeforeTest();
43     }
44 
45     @Test
getBoolean_classNotFound_throwException()46     public void getBoolean_classNotFound_throwException() {
47         assertThrows(
48                 FlagReadException.class,
49                 () -> mFlagsValueProvider.getBoolean("android.platform.test.flagName1"));
50     }
51 
52     @Test
getBoolean_noSuchMethod_throwException()53     public void getBoolean_noSuchMethod_throwException() {
54         assertThrows(
55                 FlagReadException.class,
56                 () -> mFlagsValueProvider.getBoolean("android.platfrm.test.flag.junit.flag1"));
57     }
58 
59     @Test
getBoolean_validFlag()60     public void getBoolean_validFlag() throws Exception {
61         assertTrue(mFlagsValueProvider.getBoolean("android.platform.test.flag.junit.flag_name_1"));
62     }
63 
64     @Test
getBoolean_notBooleanFlag_throwException()65     public void getBoolean_notBooleanFlag_throwException() {
66         assertThrows(
67                 FlagReadException.class,
68                 () ->
69                         mFlagsValueProvider.getBoolean(
70                                 "android.platform.test.flag.junit.flag_name_2"));
71     }
72 
73     @Test
getBoolean_aconfigFlagWithNameSpace_throwException()74     public void getBoolean_aconfigFlagWithNameSpace_throwException() throws Exception {
75         assertThrows(
76                 FlagReadException.class,
77                 () ->
78                         mFlagsValueProvider.getBoolean(
79                                 "namespace/android.platform.test.flag.junit.flag_name_1"));
80     }
81 
82     @Test
getBoolean_fromNotExistLegacyFlag_throwException()83     public void getBoolean_fromNotExistLegacyFlag_throwException() {
84         assertThrows(
85                 FlagReadException.class,
86                 () -> mFlagsValueProvider.getBoolean("does_not_exist/flag"));
87     }
88 
89     @Test
getBoolean_fromLegacyNonBooleanFlag_throwException()90     public void getBoolean_fromLegacyNonBooleanFlag_throwException() {
91         assertThrows(
92                 FlagReadException.class,
93                 () -> mFlagsValueProvider.getBoolean("my_namespace/flag2"));
94     }
95 
96     @Test
getBoolean_fromLegacyBooleanFlag()97     public void getBoolean_fromLegacyBooleanFlag() throws Exception {
98         assertTrue(mFlagsValueProvider.getBoolean("my_namespace/flag1"));
99     }
100 }
101