Merge "Revert "OMS: add tests for the OMS transactional API""

This commit is contained in:
Treehugger Robot
2021-01-13 00:00:42 +00:00
committed by Gerrit Code Review
5 changed files with 50 additions and 190 deletions

View File

@@ -18,76 +18,60 @@ package com.android.overlaytest;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import android.annotation.NonNull; import android.app.UiAutomation;
import android.content.Context; import android.content.res.Resources;
import android.content.om.OverlayManager; import android.os.ParcelFileDescriptor;
import android.content.om.OverlayManagerTransaction;
import android.os.UserHandle;
import androidx.test.InstrumentationRegistry; import androidx.test.InstrumentationRegistry;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
class LocalOverlayManager { class LocalOverlayManager {
private static final long TIMEOUT = 30; private static final long TIMEOUT = 30;
public static void toggleOverlaysAndWait(@NonNull final String[] overlaysToEnable, public static void setEnabledAndWait(Executor executor, final String packageName,
@NonNull final String[] overlaysToDisable) throws Exception { boolean enable) throws Exception {
final int userId = UserHandle.myUserId(); final String pattern = (enable ? "[x]" : "[ ]") + " " + packageName;
OverlayManagerTransaction.Builder builder = new OverlayManagerTransaction.Builder(); if (executeShellCommand("cmd overlay list").contains(pattern)) {
for (String pkg : overlaysToEnable) { // nothing to do, overlay already in the requested state
builder.setEnabled(pkg, true, userId); return;
} }
for (String pkg : overlaysToDisable) {
builder.setEnabled(pkg, false, userId);
}
OverlayManagerTransaction transaction = builder.build();
final Context ctx = InstrumentationRegistry.getTargetContext(); final Resources res = InstrumentationRegistry.getContext().getResources();
final String[] oldApkPaths = res.getAssets().getApkPaths();
FutureTask<Boolean> task = new FutureTask<>(() -> { FutureTask<Boolean> task = new FutureTask<>(() -> {
while (true) { while (true) {
final String[] paths = ctx.getResources().getAssets().getApkPaths(); if (!Arrays.equals(oldApkPaths, res.getAssets().getApkPaths())) {
if (arrayTailContains(paths, overlaysToEnable)
&& arrayDoesNotContain(paths, overlaysToDisable)) {
return true; return true;
} }
Thread.sleep(10); Thread.sleep(10);
} }
}); });
OverlayManager om = ctx.getSystemService(OverlayManager.class);
om.commit(transaction);
Executor executor = (cmd) -> new Thread(cmd).start();
executor.execute(task); executor.execute(task);
executeShellCommand("cmd overlay " + (enable ? "enable " : "disable ") + packageName);
task.get(TIMEOUT, SECONDS); task.get(TIMEOUT, SECONDS);
} }
private static boolean arrayTailContains(@NonNull final String[] array, private static String executeShellCommand(final String command)
@NonNull final String[] substrings) { throws Exception {
if (array.length < substrings.length) { final UiAutomation uiAutomation =
return false; InstrumentationRegistry.getInstrumentation().getUiAutomation();
} final ParcelFileDescriptor pfd = uiAutomation.executeShellCommand(command);
for (int i = 0; i < substrings.length; i++) { try (InputStream in = new ParcelFileDescriptor.AutoCloseInputStream(pfd)) {
String a = array[array.length - substrings.length + i]; final BufferedReader reader = new BufferedReader(
String s = substrings[i]; new InputStreamReader(in, StandardCharsets.UTF_8));
if (!a.contains(s)) { StringBuilder str = new StringBuilder();
return false; String line;
while ((line = reader.readLine()) != null) {
str.append(line);
} }
return str.toString();
} }
return true;
}
private static boolean arrayDoesNotContain(@NonNull final String[] array,
@NonNull final String[] substrings) {
for (String s : substrings) {
for (String a : array) {
if (a.contains(s)) {
return false;
}
}
}
return true;
} }
} }

View File

@@ -1,133 +0,0 @@
/*
* Copyright (C) 2019 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 com.android.overlaytest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.testng.Assert.assertThrows;
import android.content.Context;
import android.content.om.OverlayInfo;
import android.content.om.OverlayManager;
import android.content.om.OverlayManagerTransaction;
import android.content.res.Resources;
import android.os.UserHandle;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.List;
@RunWith(JUnit4.class)
@MediumTest
public class TransactionTest {
static final String APP_OVERLAY_ONE_PKG = "com.android.overlaytest.app_overlay_one";
static final String APP_OVERLAY_TWO_PKG = "com.android.overlaytest.app_overlay_two";
private Context mContext;
private Resources mResources;
private OverlayManager mOverlayManager;
private int mUserId;
private UserHandle mUserHandle;
@Before
public void setUp() throws Exception {
mContext = InstrumentationRegistry.getContext();
mResources = mContext.getResources();
mOverlayManager = mContext.getSystemService(OverlayManager.class);
mUserId = UserHandle.myUserId();
mUserHandle = UserHandle.of(mUserId);
LocalOverlayManager.toggleOverlaysAndWait(
new String[]{},
new String[]{APP_OVERLAY_ONE_PKG, APP_OVERLAY_TWO_PKG});
}
@Test
public void testValidTransaction() throws Exception {
assertOverlayIsEnabled(APP_OVERLAY_ONE_PKG, false, mUserId);
assertOverlayIsEnabled(APP_OVERLAY_TWO_PKG, false, mUserId);
OverlayManagerTransaction t = new OverlayManagerTransaction.Builder()
.setEnabled(APP_OVERLAY_ONE_PKG, true)
.setEnabled(APP_OVERLAY_TWO_PKG, true)
.build();
mOverlayManager.commit(t);
assertOverlayIsEnabled(APP_OVERLAY_ONE_PKG, true, mUserId);
assertOverlayIsEnabled(APP_OVERLAY_TWO_PKG, true, mUserId);
List<OverlayInfo> ois =
mOverlayManager.getOverlayInfosForTarget("com.android.overlaytest", mUserHandle);
assertEquals(ois.size(), 2);
assertEquals(ois.get(0).packageName, APP_OVERLAY_ONE_PKG);
assertEquals(ois.get(1).packageName, APP_OVERLAY_TWO_PKG);
OverlayManagerTransaction t2 = new OverlayManagerTransaction.Builder()
.setEnabled(APP_OVERLAY_TWO_PKG, true)
.setEnabled(APP_OVERLAY_ONE_PKG, true)
.build();
mOverlayManager.commit(t2);
assertOverlayIsEnabled(APP_OVERLAY_ONE_PKG, true, mUserId);
assertOverlayIsEnabled(APP_OVERLAY_TWO_PKG, true, mUserId);
List<OverlayInfo> ois2 =
mOverlayManager.getOverlayInfosForTarget("com.android.overlaytest", mUserHandle);
assertEquals(ois2.size(), 2);
assertEquals(ois2.get(0).packageName, APP_OVERLAY_TWO_PKG);
assertEquals(ois2.get(1).packageName, APP_OVERLAY_ONE_PKG);
OverlayManagerTransaction t3 = new OverlayManagerTransaction.Builder()
.setEnabled(APP_OVERLAY_TWO_PKG, false)
.build();
mOverlayManager.commit(t3);
assertOverlayIsEnabled(APP_OVERLAY_ONE_PKG, true, mUserId);
assertOverlayIsEnabled(APP_OVERLAY_TWO_PKG, false, mUserId);
List<OverlayInfo> ois3 =
mOverlayManager.getOverlayInfosForTarget("com.android.overlaytest", mUserHandle);
assertEquals(ois3.size(), 2);
assertEquals(ois3.get(0).packageName, APP_OVERLAY_TWO_PKG);
assertEquals(ois3.get(1).packageName, APP_OVERLAY_ONE_PKG);
}
@Test
public void testInvalidRequestHasNoEffect() {
assertOverlayIsEnabled(APP_OVERLAY_ONE_PKG, false, mUserId);
assertOverlayIsEnabled(APP_OVERLAY_TWO_PKG, false, mUserId);
OverlayManagerTransaction t = new OverlayManagerTransaction.Builder()
.setEnabled(APP_OVERLAY_ONE_PKG, true)
.setEnabled("does-not-exist", true)
.setEnabled(APP_OVERLAY_TWO_PKG, true)
.build();
assertThrows(SecurityException.class, () -> mOverlayManager.commit(t));
assertOverlayIsEnabled(APP_OVERLAY_ONE_PKG, false, mUserId);
assertOverlayIsEnabled(APP_OVERLAY_TWO_PKG, false, mUserId);
}
private void assertOverlayIsEnabled(final String packageName, boolean enabled, int userId) {
final OverlayInfo oi = mOverlayManager.getOverlayInfo(packageName, UserHandle.of(userId));
assertNotNull(oi);
assertEquals(oi.isEnabled(), enabled);
}
}

View File

@@ -22,6 +22,8 @@ import org.junit.BeforeClass;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import java.util.concurrent.Executor;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
@MediumTest @MediumTest
public class WithMultipleOverlaysTest extends OverlayBaseTest { public class WithMultipleOverlaysTest extends OverlayBaseTest {
@@ -31,8 +33,9 @@ public class WithMultipleOverlaysTest extends OverlayBaseTest {
@BeforeClass @BeforeClass
public static void enableOverlay() throws Exception { public static void enableOverlay() throws Exception {
LocalOverlayManager.toggleOverlaysAndWait( Executor executor = (cmd) -> new Thread(cmd).start();
new String[]{FRAMEWORK_OVERLAY_PKG, APP_OVERLAY_ONE_PKG, APP_OVERLAY_TWO_PKG}, LocalOverlayManager.setEnabledAndWait(executor, APP_OVERLAY_ONE_PKG, true);
new String[]{}); LocalOverlayManager.setEnabledAndWait(executor, APP_OVERLAY_TWO_PKG, true);
LocalOverlayManager.setEnabledAndWait(executor, FRAMEWORK_OVERLAY_PKG, true);
} }
} }

View File

@@ -22,6 +22,8 @@ import org.junit.BeforeClass;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import java.util.concurrent.Executor;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
@MediumTest @MediumTest
public class WithOverlayTest extends OverlayBaseTest { public class WithOverlayTest extends OverlayBaseTest {
@@ -30,9 +32,10 @@ public class WithOverlayTest extends OverlayBaseTest {
} }
@BeforeClass @BeforeClass
public static void enableOverlays() throws Exception { public static void enableOverlay() throws Exception {
LocalOverlayManager.toggleOverlaysAndWait( Executor executor = (cmd) -> new Thread(cmd).start();
new String[]{FRAMEWORK_OVERLAY_PKG, APP_OVERLAY_ONE_PKG}, LocalOverlayManager.setEnabledAndWait(executor, APP_OVERLAY_ONE_PKG, true);
new String[]{APP_OVERLAY_TWO_PKG}); LocalOverlayManager.setEnabledAndWait(executor, APP_OVERLAY_TWO_PKG, false);
LocalOverlayManager.setEnabledAndWait(executor, FRAMEWORK_OVERLAY_PKG, true);
} }
} }

View File

@@ -22,6 +22,8 @@ import org.junit.BeforeClass;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import java.util.concurrent.Executor;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
@MediumTest @MediumTest
public class WithoutOverlayTest extends OverlayBaseTest { public class WithoutOverlayTest extends OverlayBaseTest {
@@ -31,8 +33,9 @@ public class WithoutOverlayTest extends OverlayBaseTest {
@BeforeClass @BeforeClass
public static void disableOverlays() throws Exception { public static void disableOverlays() throws Exception {
LocalOverlayManager.toggleOverlaysAndWait( Executor executor = (cmd) -> new Thread(cmd).start();
new String[]{}, LocalOverlayManager.setEnabledAndWait(executor, APP_OVERLAY_ONE_PKG, false);
new String[]{FRAMEWORK_OVERLAY_PKG, APP_OVERLAY_ONE_PKG, APP_OVERLAY_TWO_PKG}); LocalOverlayManager.setEnabledAndWait(executor, APP_OVERLAY_TWO_PKG, false);
LocalOverlayManager.setEnabledAndWait(executor, FRAMEWORK_OVERLAY_PKG, false);
} }
} }