xref: /aosp_15_r20/development/samples/AconfigDemo/tests/unittests/StaticContentUnitTests.java (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
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 com.example.android.aconfig.demo;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.platform.test.flag.junit.SetFlagsRule;
22 import com.example.android.aconfig.demo.flags.Flags;
23 
24 import org.junit.Assume;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 
30 @RunWith(Parameterized.class)
31 public final class StaticContentUnitTests {
32     @Rule public final SetFlagsRule mSetFlagsRule;
33 
34     @Parameterized.Parameters(name = "isInitWithDefault={0}")
data()35     public static Object[] data() {
36         return new Boolean[] {false, true};
37     }
38 
StaticContentUnitTests(boolean isInitWithDefault)39     public StaticContentUnitTests(boolean isInitWithDefault) {
40         mIsInitWithDefault = isInitWithDefault;
41         mSetFlagsRule = new SetFlagsRule(isInitWithDefault
42                 ? SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT
43                 : SetFlagsRule.DefaultInitValueType.NULL_DEFAULT);
44     }
45 
46     private boolean mIsInitWithDefault;
47 
48     @Test
staticContent_enable_staticFlag_disable_thirdFlag()49     public void staticContent_enable_staticFlag_disable_thirdFlag() {
50         Assume.assumeTrue(!mIsInitWithDefault);
51         mSetFlagsRule.enableFlags(Flags.FLAG_APPEND_STATIC_CONTENT);
52         mSetFlagsRule.disableFlags(Flags.FLAG_THIRD_FLAG);
53         mSetFlagsRule.disableFlags(Flags.FLAG_READ_ONLY_FLAG);
54         StaticContent statiContent = new StaticContent();
55         String ret = statiContent.getContent();
56         StringBuilder expected = new StringBuilder();
57         expected.append("The flag: appendStaticContent is ON!!\n\n");
58         expected.append("The flag: thirdFlag is OFF!!\n\n");
59         expected.append("The flag: read only flag static is OFF!!\n\n");
60         assertEquals("Expected message", expected.toString(), ret);
61     }
62 
63     @Test
staticContent_enable_thirdFlag_with_default()64     public void staticContent_enable_thirdFlag_with_default() {
65         Assume.assumeTrue(mIsInitWithDefault);
66         mSetFlagsRule.enableFlags(Flags.FLAG_THIRD_FLAG);
67         StaticContent statiContent = new StaticContent();
68         String ret = statiContent.getContent();
69         StringBuilder expected = new StringBuilder();
70         expected.append("The flag: appendStaticContent is OFF!!\n\n");
71         expected.append("The flag: thirdFlag is ON!!\n\n");
72         expected.append("The flag: read only flag static is OFF!!\n\n");
73         assertEquals("Expected message", expected.toString(), ret);
74     }
75 }
76 
77