Use client context to pass ShortcutManager tests
Uses client context in unit tests to pass the shortcut package name
check.
Before applying this change unit tests were failing with this error:
STACKTRACE:
junit.framework.AssertionFailedError: Expected exception type was
java.lang.IllegalArgumentException but caught
java.lang.SecurityException: Shortcut package name mismatch (message=)
at junit.framework.Assert.fail(Assert.java:50) at
junit.framework.Assert.assertTrue(Assert.java:20) at
com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils
.assertExpectException(ShortcutManagerTestUtils.java:429) at
com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils
.assertExpectException(ShortcutManagerTestUtils.java:410) at
com.android.server.pm.ShortcutManagerTest2
.testShortcutInfoMissingMandatoryFields(ShortcutManagerTest2.java:154)
Test: atest com.android.server.pm.ShortcutManagerTest1
Test: atest com.android.server.pm.ShortcutManagerTest2
Test: atest com.android.server.pm.ShortcutManagerTest3
Test: atest com.android.server.pm.ShortcutManagerTest4
Test: atest com.android.server.pm.ShortcutManagerTest5
Test: atest com.android.server.pm.ShortcutManagerTest6
Test: atest com.android.server.pm.ShortcutManagerTest7
Test: atest com.android.server.pm.ShortcutManagerTest8
Test: atest com.android.server.pm.ShortcutManagerTest9
Test: atest com.android.server.pm.ShortcutManagerTest10
Change-Id: I93e306dbcec206557da3f4c40cb12576e1ec8383
diff --git a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
index a3348c2..32902a7 100644
--- a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
@@ -963,6 +963,10 @@
return getInstrumentation().getContext();
}
+ protected Context getClientContext() {
+ return mClientContext;
+ }
+
protected ShortcutManager getManager() {
return mManager;
}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
index 76d52fd..9b59f91 100644
--- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
@@ -144,8 +144,8 @@
assertExpectException(
IllegalArgumentException.class, "Short label must be provided", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext().getPackageName(), "s"))
.build();
assertTrue(getManager().setDynamicShortcuts(list(si)));
});
@@ -153,15 +153,15 @@
// same for add.
assertExpectException(
IllegalArgumentException.class, "Short label must be provided", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext().getPackageName(), "s"))
.build();
assertTrue(getManager().addDynamicShortcuts(list(si)));
});
assertExpectException(NullPointerException.class, "Intent must be provided", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext().getPackageName(), "s"))
.setShortLabel("x")
.build();
assertTrue(getManager().setDynamicShortcuts(list(si)));
@@ -169,8 +169,8 @@
// same for add.
assertExpectException(NullPointerException.class, "Intent must be provided", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext().getPackageName(), "s"))
.setShortLabel("x")
.build();
assertTrue(getManager().addDynamicShortcuts(list(si)));
@@ -178,7 +178,7 @@
assertExpectException(
IllegalStateException.class, "does not belong to package", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
.setActivity(new ComponentName("xxx", "s"))
.build();
assertTrue(getManager().setDynamicShortcuts(list(si)));
@@ -187,7 +187,7 @@
// same for add.
assertExpectException(
IllegalStateException.class, "does not belong to package", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
.setActivity(new ComponentName("xxx", "s"))
.build();
assertTrue(getManager().addDynamicShortcuts(list(si)));
@@ -198,24 +198,24 @@
assertExpectException(
IllegalStateException.class, "is not main", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext(), "s"))
.build();
assertTrue(getManager().setDynamicShortcuts(list(si)));
});
// For add
assertExpectException(
IllegalStateException.class, "is not main", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext(), "s"))
.build();
assertTrue(getManager().addDynamicShortcuts(list(si)));
});
// For update
assertExpectException(
IllegalStateException.class, "is not main", () -> {
- ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
- .setActivity(new ComponentName(getTestContext(), "s"))
+ ShortcutInfo si = new ShortcutInfo.Builder(getClientContext(), "id")
+ .setActivity(new ComponentName(getClientContext(), "s"))
.build();
assertTrue(getManager().updateShortcuts(list(si)));
});