| /* |
| * Copyright (C) 2021 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_2021_0591; |
| |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.pm.PackageManager; |
| import android.os.SystemClock; |
| import android.support.test.uiautomator.By; |
| import android.support.test.uiautomator.BySelector; |
| import android.support.test.uiautomator.UiDevice; |
| import android.support.test.uiautomator.UiObject2; |
| import android.support.test.uiautomator.Until; |
| import androidx.test.runner.AndroidJUnit4; |
| import java.util.List; |
| import org.junit.Before; |
| import org.junit.After; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| |
| import static androidx.test.core.app.ApplicationProvider.getApplicationContext; |
| import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| import static org.hamcrest.CoreMatchers.notNullValue; |
| import static org.junit.Assert.assertThat; |
| |
| @RunWith(AndroidJUnit4.class) |
| public class DeviceTest { |
| |
| private static final String BASIC_SAMPLE_PACKAGE = |
| "android.security.cts.CVE_2021_0591"; |
| private static final int LAUNCH_TIMEOUT_MS = 20000; |
| private UiDevice mDevice; |
| |
| @Before |
| public void startMainActivityFromHomeScreen() { |
| mDevice = UiDevice.getInstance(getInstrumentation()); |
| try { |
| mDevice.wakeUp(); |
| mDevice.pressMenu(); |
| mDevice.pressHome(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| Context context = getApplicationContext(); |
| assertThat(context, notNullValue()); |
| PackageManager packageManager = context.getPackageManager(); |
| assertThat(packageManager, notNullValue()); |
| final Intent intent = packageManager.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE); |
| assertThat(intent, notNullValue()); |
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| context.startActivity(intent); |
| mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT_MS); |
| } |
| |
| @After |
| public void lastOperation() { |
| SystemClock.sleep(20000); |
| } |
| |
| @Test |
| public void testClick() { |
| List<UiObject2> objects; |
| BySelector selector = By.clickable(true); |
| String button; |
| objects = mDevice.findObjects(selector); |
| for (UiObject2 o : objects) { |
| button = o.getText(); |
| if (button == null) { |
| continue; |
| } |
| if (button.matches("ALLOW|YES|Allow|Yes")) { |
| o.click(); |
| return; |
| } |
| } |
| } |
| } |