diff --git a/core/java/android/content/pm/ShortcutInfo.java b/core/java/android/content/pm/ShortcutInfo.java index da58717a46efe..094b89d4a4467 100644 --- a/core/java/android/content/pm/ShortcutInfo.java +++ b/core/java/android/content/pm/ShortcutInfo.java @@ -254,9 +254,9 @@ public final class ShortcutInfo implements Parcelable { */ public void enforceMandatoryFields() { Preconditions.checkStringNotEmpty(mId, "Shortcut ID must be provided"); - Preconditions.checkNotNull(mActivity, "activity must be provided"); + Preconditions.checkNotNull(mActivity, "Activity must be provided"); if (mTitle == null && mTitleResId == 0) { - throw new IllegalArgumentException("Shortcut title must be provided"); + throw new IllegalArgumentException("Short label must be provided"); } Preconditions.checkNotNull(mIntent, "Shortcut Intent must be provided"); } diff --git a/services/core/java/com/android/server/pm/ShortcutPackage.java b/services/core/java/com/android/server/pm/ShortcutPackage.java index f336ff32c7da1..934545ad24618 100644 --- a/services/core/java/com/android/server/pm/ShortcutPackage.java +++ b/services/core/java/com/android/server/pm/ShortcutPackage.java @@ -908,7 +908,7 @@ class ShortcutPackage extends ShortcutPackageItem { if (operation != ShortcutService.OPERATION_UPDATE) { // This method may be called before validating shortcuts, so this may happen, // and is a caller side error. - throw new NullPointerException("activity must be provided"); + throw new NullPointerException("Activity must be provided"); } continue; // Activity can be null for update. } 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 8ce7304b52ffa..2f11967cdb020 100644 --- a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java @@ -697,6 +697,10 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase { return getInstrumentation().getContext(); } + protected ShortcutManager getManager() { + return mManager; + } + protected void deleteAllSavedFiles() { // Empty the data directory. if (mInjectedFilePathRoot.exists()) { 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 399fddfcfb872..ea44462c94f34 100644 --- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java +++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java @@ -56,35 +56,78 @@ public class ShortcutManagerTest2 extends BaseShortcutManagerTest { // ShortcutInfo tests public void testShortcutInfoMissingMandatoryFields() { + // Disable throttling. + mService.updateConfigurationLocked( + ShortcutService.ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999," + + ShortcutService.ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999" + ); + assertExpectException( IllegalArgumentException.class, "ID must be provided", () -> new ShortcutInfo.Builder(getTestContext()).build()); - assertExpectException( - NullPointerException.class, - "Intent action must be set", + + assertExpectException(NullPointerException.class, "Intent action must be set", () -> new ShortcutInfo.Builder(getTestContext()).setIntent(new Intent())); + + assertExpectException(NullPointerException.class, "Activity must be provided", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()).setId("id").build(); + assertTrue(getManager().setDynamicShortcuts(list(si))); + }); + assertExpectException( - NullPointerException.class, - "activity must be provided", - () -> new ShortcutInfo.Builder(getTestContext()).setId("id").build() - .enforceMandatoryFields()); + IllegalArgumentException.class, "Short label must be provided", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()) + .setId("id") + .setActivity(new ComponentName(getTestContext().getPackageName(), "s")) + .build(); + assertTrue(getManager().setDynamicShortcuts(list(si))); + }); + assertExpectException( - IllegalArgumentException.class, - "title must be provided", - () -> new ShortcutInfo.Builder(getTestContext()).setId("id") - .setActivity( - new ComponentName(getTestContext().getPackageName(), "s")) - .build() - .enforceMandatoryFields()); + IllegalArgumentException.class, "Short label must be provided", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()) + .setId("id") + .setActivity(new ComponentName(getTestContext().getPackageName(), "s")) + .build(); + assertTrue(getManager().addDynamicShortcuts(list(si))); + }); + + assertExpectException(NullPointerException.class, "Intent must be provided", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()) + .setId("id") + .setActivity(new ComponentName(getTestContext().getPackageName(), "s")) + .setShortLabel("x") + .build(); + assertTrue(getManager().setDynamicShortcuts(list(si))); + }); + + assertExpectException(NullPointerException.class, "Intent must be provided", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()) + .setId("id") + .setActivity(new ComponentName(getTestContext().getPackageName(), "s")) + .setShortLabel("x") + .build(); + assertTrue(getManager().addDynamicShortcuts(list(si))); + }); + assertExpectException( - NullPointerException.class, - "Intent must be provided", - () -> new ShortcutInfo.Builder(getTestContext()).setId("id") - .setActivity( - new ComponentName(getTestContext().getPackageName(), "s")) - .setTitle("x").build() - .enforceMandatoryFields()); + IllegalStateException.class, "package name mismatch", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()) + .setId("id") + .setActivity(new ComponentName("xxx", "s")) + .build(); + assertTrue(getManager().setDynamicShortcuts(list(si))); + }); + + assertExpectException( + IllegalStateException.class, "package name mismatch", () -> { + ShortcutInfo si = new ShortcutInfo.Builder(getTestContext()) + .setId("id") + .setActivity(new ComponentName("xxx", "s")) + .build(); + assertTrue(getManager().addDynamicShortcuts(list(si))); + }); } public void testShortcutInfoParcel() { diff --git a/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java b/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java index 71878fdb10c47..04c7a042ef386 100644 --- a/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java +++ b/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java @@ -289,8 +289,6 @@ public class ShortcutManagerTestUtils { String expectedExceptionMessageRegex, Runnable r) { try { r.run(); - Assert.fail("Expected exception type " + expectedExceptionType.getName() - + " was not thrown (message=" + message + ")"); } catch (Throwable e) { Assert.assertTrue( "Expected exception type was " + expectedExceptionType.getName() @@ -299,7 +297,10 @@ public class ShortcutManagerTestUtils { if (expectedExceptionMessageRegex != null) { MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage()); } + return; // Pass } + Assert.fail("Expected exception type " + expectedExceptionType.getName() + + " was not thrown"); } public static List assertShortcutIds(List actualShortcuts,