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.grammaticalinflection.cts; 18 19 import static com.android.os.grammaticalinflection.GrammaticalInflectionExtensionAtoms.APPLICATION_GRAMMATICAL_INFLECTION_CHANGED_FIELD_NUMBER; 20 21 import android.cts.statsdatom.lib.ConfigUtils; 22 import android.cts.statsdatom.lib.DeviceUtils; 23 import android.cts.statsdatom.lib.ReportUtils; 24 25 import com.android.os.AtomsProto; 26 import com.android.os.grammaticalinflection.ApplicationGrammaticalInflectionChanged; 27 import com.android.os.grammaticalinflection.GrammaticalInflectionExtensionAtoms; 28 import com.android.os.StatsLog; 29 import com.android.tradefed.build.IBuildInfo; 30 import com.android.tradefed.testtype.DeviceTestCase; 31 import com.android.tradefed.testtype.IBuildReceiver; 32 33 import com.google.protobuf.ExtensionRegistry; 34 35 import java.util.List; 36 37 public class GrammaticalGenderAtomTests extends DeviceTestCase implements IBuildReceiver { 38 39 public static final int GRAMMATICAL_GENDER_NOT_SPECIFIED = 0; 40 public static final int GRAMMATICAL_GENDER_NEUTRAL = 1; 41 public static final int GRAMMATICAL_GENDER_FEMININE = 2; 42 public static final int GRAMMATICAL_GENDER_MASCULINE = 3; 43 public static final String SETTING_GRAMMATICAL_GENDER_ACTIVITY = 44 "SettingGrammaticalGenderActivity"; 45 private static final String INSTALLED_PACKAGE_NAME_APP = 46 "android.grammaticalinflection.atom.app"; 47 private final static String GENDER_KEY = "gender"; 48 49 @Override setUp()50 protected void setUp() throws Exception { 51 super.setUp(); 52 ConfigUtils.removeConfig(getDevice()); 53 ReportUtils.clearReports(getDevice()); 54 ConfigUtils.uploadConfigForPushedAtom(getDevice(), DeviceUtils.STATSD_ATOM_TEST_PKG, 55 APPLICATION_GRAMMATICAL_INFLECTION_CHANGED_FIELD_NUMBER); 56 } 57 58 @Override tearDown()59 protected void tearDown() throws Exception { 60 resetAppGender(); 61 ConfigUtils.removeConfig(getDevice()); 62 ReportUtils.clearReports(getDevice()); 63 super.tearDown(); 64 } 65 66 @Override setBuild(IBuildInfo buildInfo)67 public void setBuild(IBuildInfo buildInfo) { 68 } 69 testAtomLogging_genderSpecified_logsAtomSuccessfully()70 public void testAtomLogging_genderSpecified_logsAtomSuccessfully() throws Exception { 71 // executing API to change gender of the installed application, this should trigger an 72 // ApplicationGrammaticalInflectionChanged atom entry to be logged. 73 executeSetApplicationGrammaticalGenderCommand(GRAMMATICAL_GENDER_MASCULINE); 74 75 // Retrieving logged metric entries and asserting if they are as expected. 76 ExtensionRegistry registry = ExtensionRegistry.newInstance(); 77 GrammaticalInflectionExtensionAtoms.registerAllExtensions(registry); 78 List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice(), 79 registry); 80 assertEquals(1, data.size()); 81 ApplicationGrammaticalInflectionChanged result = data.get(0).getAtom().getExtension( 82 GrammaticalInflectionExtensionAtoms.applicationGrammaticalInflectionChanged); 83 verifyAtomDetails(result, 84 ApplicationGrammaticalInflectionChanged.SourceId.OTHERS, 85 true); 86 } 87 testAtomLogging_genderNotSpecified_logsAtomSuccessfully()88 public void testAtomLogging_genderNotSpecified_logsAtomSuccessfully() throws Exception { 89 // executing API to change gender of the installed application, this should trigger an 90 // ApplicationGrammaticalInflectionChanged atom entry to be logged. 91 executeSetApplicationGrammaticalGenderCommand(GRAMMATICAL_GENDER_NOT_SPECIFIED); 92 93 // Retrieving logged metric entries and asserting if they are as expected. 94 ExtensionRegistry registry = ExtensionRegistry.newInstance(); 95 GrammaticalInflectionExtensionAtoms.registerAllExtensions(registry); 96 List<StatsLog.EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice(), 97 registry); 98 assertEquals(1, data.size()); 99 ApplicationGrammaticalInflectionChanged result = data.get(0).getAtom().getExtension( 100 GrammaticalInflectionExtensionAtoms.applicationGrammaticalInflectionChanged); 101 verifyAtomDetails(result, 102 ApplicationGrammaticalInflectionChanged.SourceId.OTHERS, 103 false); 104 } 105 verifyAtomDetails(ApplicationGrammaticalInflectionChanged result, ApplicationGrammaticalInflectionChanged.SourceId sourceId, boolean isGrammaticalGenderSpecified)106 private void verifyAtomDetails(ApplicationGrammaticalInflectionChanged result, 107 ApplicationGrammaticalInflectionChanged.SourceId sourceId, 108 boolean isGrammaticalGenderSpecified) { 109 assertEquals(sourceId, result.getSourceId()); 110 assertEquals(isGrammaticalGenderSpecified, result.getIsGrammaticalGenderSpecified()); 111 } 112 resetAppGender()113 private void resetAppGender() throws Exception { 114 executeSetApplicationGrammaticalGenderCommand(GRAMMATICAL_GENDER_NOT_SPECIFIED); 115 } 116 executeSetApplicationGrammaticalGenderCommand(int gender)117 private void executeSetApplicationGrammaticalGenderCommand(int gender) throws Exception { 118 String activity = INSTALLED_PACKAGE_NAME_APP + "/." + SETTING_GRAMMATICAL_GENDER_ACTIVITY; 119 getDevice().executeShellCommand( 120 String.format("am start -W -n %s --ei %s %d", activity, GENDER_KEY, gender)); 121 } 122 } 123