1 /* 2 * Copyright (C) 2021 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.devicepolicy.cts; 18 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.junit.Assume.assumeTrue; 23 import static org.testng.Assert.assertThrows; 24 25 import android.app.admin.RemoteDevicePolicyManager; 26 import android.app.role.RoleManager; 27 import android.content.ComponentName; 28 import android.content.Context; 29 import android.provider.Telephony; 30 import android.telephony.TelephonyManager; 31 32 import com.android.bedstead.harrier.BedsteadJUnit4; 33 import com.android.bedstead.harrier.DeviceState; 34 import com.android.bedstead.harrier.annotations.Postsubmit; 35 import com.android.bedstead.harrier.annotations.enterprise.CanSetPolicyTest; 36 import com.android.bedstead.harrier.annotations.enterprise.CannotSetPolicyTest; 37 import com.android.bedstead.harrier.annotations.enterprise.PolicyAppliesTest; 38 import com.android.bedstead.harrier.annotations.enterprise.PolicyDoesNotApplyTest; 39 import com.android.bedstead.harrier.policies.DefaultSmsApplication; 40 import com.android.bedstead.nene.TestApis; 41 import com.android.bedstead.remotedpc.RemotePolicyManager; 42 import com.android.bedstead.testapp.TestApp; 43 import com.android.bedstead.testapp.TestAppInstance; 44 45 import org.junit.Before; 46 import org.junit.ClassRule; 47 import org.junit.Rule; 48 import org.junit.runner.RunWith; 49 50 // TODO(b/198442101): Add tests for the COPE case when we can sideload system apps 51 @RunWith(BedsteadJUnit4.class) 52 public final class DefaultSmsApplicationTest { 53 @ClassRule 54 @Rule 55 public static DeviceState sDeviceState = new DeviceState(); 56 57 private static final Context sContext = TestApis.context().instrumentedContext(); 58 private static final TestApp sSmsApp = sDeviceState.testApps() 59 .query() 60 .wherePackageName() 61 // TODO(b/198420874): Query for the intent filters relevant to the SMS tests 62 .isEqualTo("com.android.bedstead.testapp.SmsApp") 63 .get(); 64 private static final String FAKE_SMS_APP_NAME = "FakeSmsAppName"; 65 66 private ComponentName mAdmin; 67 private RemoteDevicePolicyManager mDpm; 68 private TelephonyManager mTelephonyManager; 69 private RoleManager mRoleManager; 70 71 @Before setUp()72 public void setUp() { 73 RemotePolicyManager dpc = sDeviceState.dpc(); 74 mAdmin = dpc.componentName(); 75 mDpm = dpc.devicePolicyManager(); 76 mTelephonyManager = sContext.getSystemService(TelephonyManager.class); 77 mRoleManager = sContext.getSystemService(RoleManager.class); 78 } 79 80 // TODO(b/198588696): Add support is @RequireSmsCapable and @RequireNotSmsCapable 81 @Postsubmit(reason = "new test") 82 @PolicyAppliesTest(policy = DefaultSmsApplication.class) setDefaultSmsApplication_works()83 public void setDefaultSmsApplication_works() { 84 assumeTrue(mTelephonyManager.isSmsCapable() 85 || (mRoleManager != null && mRoleManager.isRoleAvailable(RoleManager.ROLE_SMS))); 86 String previousSmsAppName = getDefaultSmsPackage(); 87 try (TestAppInstance smsApp = sSmsApp.install()) { 88 mDpm.setDefaultSmsApplication(mAdmin, smsApp.packageName()); 89 90 assertThat(getDefaultSmsPackage()).isEqualTo(smsApp.packageName()); 91 } finally { 92 mDpm.setDefaultSmsApplication(mAdmin, previousSmsAppName); 93 } 94 } 95 96 // TODO(b/198588696): Add support is @RequireSmsCapable and @RequireNotSmsCapable 97 @Postsubmit(reason = "new test") 98 @PolicyDoesNotApplyTest(policy = DefaultSmsApplication.class) setDefaultSmsApplication_unchanged()99 public void setDefaultSmsApplication_unchanged() { 100 assumeTrue(mTelephonyManager.isSmsCapable() 101 || (mRoleManager != null && mRoleManager.isRoleAvailable(RoleManager.ROLE_SMS))); 102 String previousSmsAppName = getDefaultSmsPackage(); 103 try (TestAppInstance smsApp = sSmsApp.install()) { 104 mDpm.setDefaultSmsApplication(mAdmin, smsApp.packageName()); 105 106 assertThat(getDefaultSmsPackage()).isEqualTo(previousSmsAppName); 107 } finally { 108 mDpm.setDefaultSmsApplication(mAdmin, previousSmsAppName); 109 } 110 } 111 112 // TODO(b/198588696): Add support is @RequireSmsCapable and @RequireNotSmsCapable 113 @Postsubmit(reason = "new test") 114 @CanSetPolicyTest(policy = DefaultSmsApplication.class) setDefaultSmsApplication_smsPackageDoesNotExist_unchanged()115 public void setDefaultSmsApplication_smsPackageDoesNotExist_unchanged() { 116 assumeTrue(mTelephonyManager.isSmsCapable() 117 || (mRoleManager != null && mRoleManager.isRoleAvailable(RoleManager.ROLE_SMS))); 118 String previousSmsAppName = getDefaultSmsPackage(); 119 120 mDpm.setDefaultSmsApplication(mAdmin, FAKE_SMS_APP_NAME); 121 122 try { 123 assertThat(getDefaultSmsPackage()).isEqualTo(previousSmsAppName); 124 } finally { 125 mDpm.setDefaultSmsApplication(mAdmin, previousSmsAppName); 126 } 127 } 128 129 @Postsubmit(reason = "new test") 130 @CanSetPolicyTest(policy = DefaultSmsApplication.class) setDefaultSmsApplication_nullAdmin_throwsException()131 public void setDefaultSmsApplication_nullAdmin_throwsException() { 132 try (TestAppInstance smsApp = sSmsApp.install()) { 133 134 assertThrows(NullPointerException.class, () -> 135 mDpm.setDefaultSmsApplication( 136 /* admin= */ null, smsApp.packageName())); 137 } 138 } 139 140 // TODO(b/198588696): Add support is @RequireSmsCapable and @RequireNotSmsCapable 141 @Postsubmit(reason = "new test") 142 @CanSetPolicyTest(policy = DefaultSmsApplication.class) setDefaultSmsApplication_notSmsCapable_unchanged()143 public void setDefaultSmsApplication_notSmsCapable_unchanged() { 144 assumeTrue(!mTelephonyManager.isSmsCapable() 145 && (mRoleManager == null || !mRoleManager.isRoleAvailable(RoleManager.ROLE_SMS))); 146 String previousSmsAppName = getDefaultSmsPackage(); 147 try (TestAppInstance smsApp = sSmsApp.install()) { 148 mDpm.setDefaultSmsApplication(mAdmin, smsApp.packageName()); 149 150 assertThat(getDefaultSmsPackage()).isEqualTo(previousSmsAppName); 151 } finally { 152 mDpm.setDefaultSmsApplication(mAdmin, previousSmsAppName); 153 } 154 } 155 156 @Postsubmit(reason = "new test") 157 // We don't include non device admin states as passing a null admin is a NullPointerException 158 @CannotSetPolicyTest(policy = DefaultSmsApplication.class, includeNonDeviceAdminStates = false) setDefaultSmsApplication_invalidAdmin_throwsException()159 public void setDefaultSmsApplication_invalidAdmin_throwsException() { 160 try (TestAppInstance smsApp = sSmsApp.install()) { 161 162 assertThrows(SecurityException.class, () -> 163 mDpm.setDefaultSmsApplication(mAdmin, smsApp.packageName())); 164 } 165 } 166 getDefaultSmsPackage()167 private String getDefaultSmsPackage() { 168 return Telephony.Sms.getDefaultSmsPackage(sContext); 169 } 170 } 171