1 /* 2 * Copyright (C) 2024 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.car.hiddenapitest; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertThrows; 22 23 import android.Manifest; 24 import android.app.GrammaticalInflectionManager; 25 import android.app.Instrumentation; 26 import android.app.UiAutomation; 27 import android.car.extendedapitest.testbase.CarApiTestBase; 28 import android.content.Context; 29 import android.content.res.Configuration; 30 31 import androidx.test.InstrumentationRegistry; 32 33 import com.android.bedstead.harrier.DeviceState; 34 import com.android.bedstead.multiuser.annotations.RequireRunOnVisibleBackgroundNonProfileUser; 35 import com.android.bedstead.multiuser.annotations.RequireVisibleBackgroundUsers; 36 37 import org.junit.After; 38 import org.junit.Before; 39 import org.junit.ClassRule; 40 import org.junit.Rule; 41 import org.junit.Test; 42 43 // TODO(b/353837838): Move to CTS once GrammaticalInflection APIs are test APIs. 44 public class GrammaticalInflectionManagerTest extends CarApiTestBase { 45 46 private static final String TAG = GrammaticalInflectionManagerTest.class.getSimpleName(); 47 48 private final Instrumentation mInstrumentation = 49 InstrumentationRegistry.getInstrumentation(); 50 private final Context mContext = mInstrumentation.getContext(); 51 private final UiAutomation mUiAutomation = mInstrumentation.getUiAutomation(); 52 private final GrammaticalInflectionManager mGrammaticalInflectionManager = 53 mContext.getSystemService(GrammaticalInflectionManager.class); 54 int mPreviousGrammaticalGender; 55 56 @Rule 57 @ClassRule 58 public static final DeviceState sDeviceState = new DeviceState(); 59 60 @Before setUp()61 public void setUp() { 62 mUiAutomation.adoptShellPermissionIdentity( 63 Manifest.permission.READ_SYSTEM_GRAMMATICAL_GENDER); 64 mPreviousGrammaticalGender = mGrammaticalInflectionManager.getSystemGrammaticalGender(); 65 } 66 67 @After cleanUp()68 public void cleanUp() { 69 // Reset the System Grammatical Gender 70 try { 71 mGrammaticalInflectionManager.setSystemWideGrammaticalGender( 72 mPreviousGrammaticalGender); 73 } catch (Exception e) { 74 // Ignore 75 } 76 mUiAutomation.dropShellPermissionIdentity(); 77 } 78 79 @Test 80 @RequireVisibleBackgroundUsers(reason = "Passengers are not allowed to change " 81 + "GrammaticalInflection.") 82 @RequireRunOnVisibleBackgroundNonProfileUser testPassengerCantChangeGrammaticalInflection()83 public void testPassengerCantChangeGrammaticalInflection() { 84 Exception e = assertThrows(SecurityException.class, 85 () -> mGrammaticalInflectionManager.setSystemWideGrammaticalGender( 86 Configuration.GRAMMATICAL_GENDER_MASCULINE)); 87 88 assertThat(e).hasMessageThat().contains( 89 "Only current user is allowed to update GrammaticalInflection."); 90 } 91 } 92