1 /*
2  * Copyright (C) 2020 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.stats.pull;
18 
19 import static android.os.UserHandle.USER_SYSTEM;
20 
21 import static com.android.internal.util.FrameworkStatsLog.SETTING_SNAPSHOT;
22 
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 
27 import android.content.Context;
28 import android.provider.DeviceConfig;
29 
30 import androidx.test.platform.app.InstrumentationRegistry;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 /**
38  * Build/Install/Run:
39  * atest FrameworksServicesTests:SettingsStatsUtilTest
40  */
41 @RunWith(AndroidJUnit4.class)
42 public class SettingsStatsUtilTest {
43     private static final String[] KEYS = new String[]{
44             "screen_auto_brightness_adj",
45             "font_scale"
46     };
47     private static final String ENCODED = "ChpzY3JlZW5fYXV0b19icmlnaHRuZXNzX2FkagoKZm9udF9zY2FsZQ";
48     private static final String FLAG = "testflag";
49     private Context mContext;
50 
51     @Before
setUp()52     public void setUp() {
53         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
54                 FLAG,
55                 "",
56                 false /* makeDefault*/);
57         mContext = InstrumentationRegistry.getInstrumentation().getContext();
58     }
59 
60     @Test
getList_emptyString_nullValue()61     public void getList_emptyString_nullValue() {
62         assertNull(SettingsStatsUtil.getList(FLAG));
63     }
64 
65     @Test
getList_notValidString_nullValue()66     public void getList_notValidString_nullValue() {
67         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS, FLAG, "abcd", false);
68 
69         assertNull(SettingsStatsUtil.getList(FLAG));
70     }
71 
72     @Test
getList_validString_correctValue()73     public void getList_validString_correctValue() {
74         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS, FLAG, ENCODED, false);
75 
76         assertArrayEquals(KEYS, SettingsStatsUtil.getList(FLAG).element);
77     }
78 
79     @Test
logGlobalSettings_noWhitelist_correctSize()80     public void logGlobalSettings_noWhitelist_correctSize() {
81         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
82                 "GlobalFeature__boolean_whitelist", "", false);
83         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
84                 "GlobalFeature__integer_whitelist", "", false);
85         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
86                 "GlobalFeature__float_whitelist", "", false);
87         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
88                 "GlobalFeature__string_whitelist", "", false);
89 
90         assertEquals(0, SettingsStatsUtil.logGlobalSettings(mContext, SETTING_SNAPSHOT,
91                 USER_SYSTEM).size());
92     }
93 
94     @Test
logGlobalSettings_correctSize()95     public void logGlobalSettings_correctSize() {
96         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
97                 "GlobalFeature__boolean_whitelist", ENCODED, false);
98         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
99                 "GlobalFeature__integer_whitelist", ENCODED, false);
100         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
101                 "GlobalFeature__float_whitelist", ENCODED, false);
102         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SETTINGS_STATS,
103                 "GlobalFeature__string_whitelist", ENCODED, false);
104 
105         assertEquals(KEYS.length * 4,
106                 SettingsStatsUtil.logGlobalSettings(mContext, SETTING_SNAPSHOT,
107                         USER_SYSTEM).size());
108     }
109 }
110