blob: 4af4c24d6aa5c72120b5fc3a3d91103bcf38f0c3 [file] [log] [blame]
/*
* 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_2021_39701;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;
import android.app.UiAutomation;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.RemoteException;
import android.view.KeyEvent;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
@RunWith(AndroidJUnit4.class)
public class DeviceTest {
@Test
public void testService() {
final int timeoutMs = 10000;
boolean buttonClicked = false;
UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
UiDevice device = UiDevice.getInstance(getInstrumentation());
Context context = getApplicationContext();
Resources resources = context.getResources();
assumeNotNull(context);
PackageManager packageManager = context.getPackageManager();
assumeNotNull(packageManager);
Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
assumeNotNull(intent);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
assumeNoException(e);
}
String applicationName = resources.getString(R.string.systemUiPackage);
assumeTrue(resources.getString(R.string.failMessage) + applicationName,
device.wait(Until.hasObject(By.pkg(applicationName).depth(0)), timeoutMs));
// Click on Add Button
buttonClicked = clickButton(resources.getString(R.string.addButton));
assumeTrue(resources.getString(R.string.addBtnNotFound), buttonClicked);
try {
if (packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)) {
device.pressKeyCode(KeyEvent.KEYCODE_SLEEP);
} else {
device.sleep();
}
} catch (RemoteException e) {
assumeNoException(e);
}
uiAutomation.executeShellCommand(resources.getString(R.string.wakeup));
assumeTrue(resources.getString(R.string.failMessage) + applicationName,
device.wait(Until.hasObject(By.pkg(applicationName).depth(0)), timeoutMs));
// Click on the icon on locked screen
buttonClicked = clickButton(resources.getString(R.string.controlButton));
assumeTrue(resources.getString(R.string.iconNotFound), buttonClicked);
assumeTrue(device.pressKeyCode(KeyEvent.KEYCODE_MENU));
assumeTrue(resources.getString(R.string.failMessage) + applicationName,
device.wait(Until.hasObject(By.pkg(applicationName).depth(0)), timeoutMs));
int result = resources.getInteger(R.integer.pocServNotStart);
String message = "";
long startTime = System.currentTimeMillis();
while ((System.currentTimeMillis() - startTime) < timeoutMs) {
SharedPreferences sharedpref = getApplicationContext().getSharedPreferences(
resources.getString(R.string.pocSharedPref), Context.MODE_APPEND);
result = sharedpref.getInt(resources.getString(R.string.flag),
resources.getInteger(R.integer.pocServNotStart));
message = sharedpref.getString(resources.getString(R.string.message), "");
if (result < resources.getInteger(R.integer.pocServNotStart)) {
break;
}
}
assumeTrue(device.pressHome());
assumeTrue(resources.getString(R.string.pocSerNotStarted),
result != resources.getInteger(R.integer.pocServNotStart));
assertEquals(message, resources.getInteger(R.integer.pass), result);
}
private boolean clickButton(String id) {
UiDevice device = UiDevice.getInstance(getInstrumentation());
boolean buttonClicked = false;
BySelector selector = By.clickable(true);
List<UiObject2> objects = device.findObjects(selector);
for (UiObject2 object : objects) {
String objectId = object.getResourceName();
if (objectId != null && objectId.equalsIgnoreCase(id)) {
object.click();
buttonClicked = true;
break;
}
}
return buttonClicked;
}
}