1 /*
2  * Copyright (C) 2022 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.cts.packagemanager.stats.host;
18 
19 import android.cts.statsdatom.lib.AtomTestUtils;
20 import android.cts.statsdatom.lib.ConfigUtils;
21 import android.cts.statsdatom.lib.DeviceUtils;
22 import android.cts.statsdatom.lib.ReportUtils;
23 
24 import com.android.os.AtomsProto;
25 import com.android.os.StatsLog;
26 import com.android.tradefed.device.DeviceNotAvailableException;
27 import com.android.tradefed.device.ITestDevice;
28 import com.android.tradefed.util.RunUtil;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 public class SettingsProviderSettingChangedStatsTests extends PackageManagerStatsTestsBase {
34 
testSettingsChanged()35     public void testSettingsChanged() throws Throwable {
36         ConfigUtils.uploadConfigForPushedAtom(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG,
37                 AtomsProto.Atom.SETTINGS_PROVIDER_SETTING_CHANGED_FIELD_NUMBER);
38         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
39         putSetting(getDevice(), "secure", "test_setting1", "100");
40         putSetting(getDevice(), "system", "test_setting2", "200");
41         putSetting(getDevice(), "global", "test_setting3", "300");
42         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
43         deleteSetting(getDevice(), "secure", "test_setting1");
44         deleteSetting(getDevice(), "system", "test_setting2");
45         deleteSetting(getDevice(), "global", "test_setting3");
46         RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
47 
48         List<AtomsProto.SettingsProviderSettingChanged> reports = new ArrayList<>();
49         for (StatsLog.EventMetricData data : ReportUtils.getEventMetricDataList(getDevice())) {
50             if (data.getAtom().hasSettingsProviderSettingChanged()) {
51                 AtomsProto.SettingsProviderSettingChanged setting =
52                         data.getAtom().getSettingsProviderSettingChanged();
53                 String name = setting.getName();
54                 if (name.equals("test_setting1") || name.equals("test_setting2")
55                         || name.equals("test_setting3")) {
56                     reports.add(setting);
57                 }
58             }
59         }
60         assertEquals(6, reports.size());
61         assertEquals("test_setting1", reports.get(0).getName());
62         assertEquals(2, reports.get(0).getType());
63         assertEquals(0, reports.get(0).getChangeType());
64         assertEquals("test_setting2", reports.get(1).getName());
65         assertEquals(1, reports.get(1).getType());
66         assertEquals(0, reports.get(1).getChangeType());
67         assertEquals("test_setting3", reports.get(2).getName());
68         assertEquals(0, reports.get(2).getType());
69         assertEquals(0, reports.get(2).getChangeType());
70         assertEquals("test_setting1", reports.get(3).getName());
71         assertEquals(2, reports.get(3).getType());
72         assertEquals(1, reports.get(3).getChangeType());
73         assertEquals("test_setting2", reports.get(4).getName());
74         assertEquals(1, reports.get(4).getType());
75         assertEquals(1, reports.get(4).getChangeType());
76         assertEquals("test_setting3", reports.get(5).getName());
77         assertEquals(0, reports.get(5).getType());
78         assertEquals(1, reports.get(5).getChangeType());
79     }
80 
putSetting(ITestDevice device, String type, String name, String value)81     private static void putSetting(ITestDevice device, String type, String name, String value)
82             throws DeviceNotAvailableException {
83         device.executeShellCommand(String.format("settings put %s %s %s", type, name, value));
84     }
85 
deleteSetting(ITestDevice device, String type, String name)86     private static void deleteSetting(ITestDevice device, String type, String name)
87             throws DeviceNotAvailableException {
88         device.executeShellCommand(String.format("settings delete %s %s", type, name));
89     }
90 }
91