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.android.server.flags;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.JUnit4;
24 
25 @RunWith(JUnit4.class)
26 public class FlagCacheTest {
27     private static final String NS = "ns";
28     private static final String NAME = "name";
29 
30     FlagCache mFlagCache = new FlagCache();
31 
32     @Test
testGetOrNull_unset()33     public void testGetOrNull_unset() {
34         assertThat(mFlagCache.getOrNull(NS, NAME)).isNull();
35     }
36 
37     @Test
testGetOrSet_unset()38     public void testGetOrSet_unset() {
39         assertThat(mFlagCache.getOrSet(NS, NAME, "value")).isEqualTo("value");
40     }
41 
42     @Test
testGetOrSet_alreadySet()43     public void testGetOrSet_alreadySet() {
44         mFlagCache.setIfChanged(NS, NAME, "value");
45         assertThat(mFlagCache.getOrSet(NS, NAME, "newvalue")).isEqualTo("value");
46     }
47 
48     @Test
testSetIfChanged_unset()49     public void testSetIfChanged_unset() {
50         assertThat(mFlagCache.setIfChanged(NS, NAME, "value")).isTrue();
51     }
52 
53     @Test
testSetIfChanged_noChange()54     public void testSetIfChanged_noChange() {
55         mFlagCache.setIfChanged(NS, NAME, "value");
56         assertThat(mFlagCache.setIfChanged(NS, NAME, "value")).isFalse();
57     }
58 
59     @Test
testSetIfChanged_changing()60     public void testSetIfChanged_changing() {
61         mFlagCache.setIfChanged(NS, NAME, "value");
62         assertThat(mFlagCache.setIfChanged(NS, NAME, "newvalue")).isTrue();
63     }
64 
65     @Test
testContainsNamespace_unset()66     public void testContainsNamespace_unset() {
67         assertThat(mFlagCache.containsNamespace(NS)).isFalse();
68     }
69 
70     @Test
testContainsNamespace_set()71     public void testContainsNamespace_set() {
72         mFlagCache.setIfChanged(NS, NAME, "value");
73         assertThat(mFlagCache.containsNamespace(NS)).isTrue();
74     }
75 
76     @Test
testContains_unset()77     public void testContains_unset() {
78         assertThat(mFlagCache.contains(NS, NAME)).isFalse();
79     }
80 
81     @Test
testContains_set()82     public void testContains_set() {
83         mFlagCache.setIfChanged(NS, NAME, "value");
84         assertThat(mFlagCache.contains(NS, NAME)).isTrue();
85     }
86 }
87