| /* |
| * Copyright (C) 2022 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package android.security.cts.CVE_2022_20223; |
| |
| import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| import static org.junit.Assert.assertFalse; |
| import static org.junit.Assume.assumeNoException; |
| import static org.junit.Assume.assumeTrue; |
| |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.SharedPreferences; |
| import android.content.res.Resources; |
| import android.os.Bundle; |
| import android.provider.Settings; |
| import android.telecom.TelecomManager; |
| |
| import androidx.test.runner.AndroidJUnit4; |
| import androidx.test.uiautomator.By; |
| import androidx.test.uiautomator.BySelector; |
| import androidx.test.uiautomator.UiDevice; |
| import androidx.test.uiautomator.Until; |
| |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| |
| @RunWith(AndroidJUnit4.class) |
| public class DeviceTest { |
| private static final int TIMEOUT_MS = 20000; |
| private UiDevice mDevice; |
| private Context mContext; |
| |
| private String getDefaultDialerPackage() { |
| TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class); |
| return telecomManager.getSystemDialerPackage(); |
| } |
| |
| // Wait for UiObject to appear and click on the UiObject if it is visible |
| private boolean clickUiObject(BySelector selector) { |
| boolean objectFound = mDevice.wait(Until.hasObject(selector), TIMEOUT_MS); |
| if (objectFound) { |
| mDevice.findObject(selector).click(); |
| } |
| return objectFound; |
| } |
| |
| @Test |
| public void testAppRestrictionsFragment() { |
| try { |
| mDevice = UiDevice.getInstance(getInstrumentation()); |
| mContext = getInstrumentation().getContext(); |
| |
| Intent intent = new Intent(Settings.ACTION_USER_SETTINGS); |
| intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| mContext.startActivity(intent); |
| |
| BySelector selector = By.text(mContext.getString(R.string.userName)); |
| assumeTrue( |
| mContext.getString(R.string.timedOutMsg, mContext.getString(R.string.userName)), |
| clickUiObject(selector)); |
| |
| String settingsPackageName = |
| intent.resolveActivity(mContext.getPackageManager()).getPackageName(); |
| Context settingsContext = mContext.createPackageContext(settingsPackageName, |
| Context.CONTEXT_IGNORE_SECURITY); |
| Resources res = settingsContext.getPackageManager() |
| .getResourcesForApplication(settingsPackageName); |
| String text = settingsContext |
| .getString(res.getIdentifier(mContext.getString(R.string.textResId), |
| mContext.getString(R.string.resType), settingsPackageName)); |
| selector = By.text(text); |
| assumeTrue(mContext.getString(R.string.timedOutMsg, text), clickUiObject(selector)); |
| |
| selector = By.res(mContext.getString(R.string.appSettingsIconResId)); |
| assumeTrue( |
| mContext.getString(R.string.timedOutMsg, |
| mContext.getString(R.string.appSettingsIconResId)), |
| clickUiObject(selector)); |
| |
| assertFalse(mContext.getString(R.string.testFailMsg), |
| mDevice.wait(Until.hasObject(By.pkg(getDefaultDialerPackage())), TIMEOUT_MS)); |
| } catch (Exception e) { |
| assumeNoException(e); |
| } finally { |
| try { |
| SharedPreferences sharedPrefs = mContext.getSharedPreferences( |
| mContext.getString(R.string.sharedPreferences), Context.MODE_APPEND); |
| String assumptionFailure = |
| sharedPrefs.getString(mContext.getString(R.string.messageKey), null); |
| assumeTrue(assumptionFailure, assumptionFailure == null); |
| } catch (Exception e) { |
| assumeNoException(e); |
| } |
| } |
| } |
| } |