blob: 5634f4f55bb1674d312fc2520fd2fb88c903fafd [file] [log] [blame]
/*
* 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_0305;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.SystemClock;
import android.util.Log;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import androidx.test.filters.SdkSuppress;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;
import androidx.test.uiautomator.BySelector;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertFalse;
/**
* Basic sample for unbundled UiAutomator.
*/
@RunWith(AndroidJUnit4.class)
public class DeviceTest {
private static final String BASIC_SAMPLE_PACKAGE
= "android.security.cts.CVE_2021_0305";
private static final int LAUNCH_TIMEOUT_MS = 20000;
private static final String STRING_TO_BE_TYPED = "UiAutomator";
private UiDevice mDevice;
@Before
public void startMainActivityFromHomeScreen() {
Log.d("CVE", "startMainActivityFromHomeScreen()");
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Launch the blueprint app
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); // Clear out any previous instances
context.startActivity(intent);
// Wait for the app to appear
Log.d("CVE", "wait for the app to appear");
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT_MS);
Log.d("CVE", "app appeared");
}
@After
public void lastOperation(){
Log.d("CVE", "lastOperation() wait 20s");
SystemClock.sleep(20000);
Log.d("CVE", "lastOperation() completed");
}
@Test
public void testClick() {
Log.d("CVE", "testClick()");
boolean clicked = false;
java.util.List<UiObject2> objects;
BySelector selector = By.clickable(true);
String button;
//Detect "Test Button".
//"Test Button" appears after onResume().
//mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT_MS);
//waits for onEnterAnimationComplete() to finish.
//So we have LAUNCH_TIMEOUT_MS for the button to appear.
//If the button it still not available then
//we assume the button is obscured and the test passes.
Log.d("CVE", "looking for clickable");
objects = mDevice.findObjects(selector);
for (UiObject2 o : objects) {
button = o.getText();
Log.d("CVE", "button:" + button);
if(button==null){
//check the next button
continue;
}
switch(button){
case "Test Button" :
o.click();
clicked=true;
Log.i("CVE", "clicked: Test Button");
break;
default :
//check the next button
continue;
}
//A button this test is looking for just got pressed.
//Ignore remaining buttons
break;
}
Log.d("CVE", "testClick() end");
assertFalse(clicked);
Log.d("CVE", "testClick() passed");
}
}