diff options
author | Laura Lu <lauralu@google.com> | 2019-05-09 01:48:05 -0700 |
---|---|---|
committer | Harry Zhang <harrytczhang@google.com> | 2019-05-10 17:45:55 +0000 |
commit | 1315fad510b76425263a53b64eb360ed1ec25d1c (patch) | |
tree | 5ce4516542a8f04a60b4f112c847f12a0f56acb3 | |
parent | 06fc0a21e5327f2beda22eeb979b149d3234c9bd (diff) | |
download | platform_testing-1315fad510b76425263a53b64eb360ed1ec25d1c.tar.gz |
Post notification with a package to launch and open notification by title
Bug: 131339798
Change-Id: I1597bc19fe1a897df3815ad9eeeb8aef1922b839
(cherry picked from commit 14fdae56e3de46650c8e0c1686aa2f7140406427)
3 files changed, 88 insertions, 15 deletions
diff --git a/libraries/app-helpers/interfaces/handheld/src/android/platform/helpers/INotificationHelper.java b/libraries/app-helpers/interfaces/handheld/src/android/platform/helpers/INotificationHelper.java index bf3ccab9a..cada5da5a 100644 --- a/libraries/app-helpers/interfaces/handheld/src/android/platform/helpers/INotificationHelper.java +++ b/libraries/app-helpers/interfaces/handheld/src/android/platform/helpers/INotificationHelper.java @@ -45,9 +45,37 @@ public interface INotificationHelper extends IAppHelper { /** * Setup Expectations: None * + * <p>Posts a number of notifications to the device with a package to launch. Successive calls + * to this should post new notifications in addition to those previously posted. Note that this + * may fail if the helper has surpassed the system-defined limit for per-package notifications. + * + * @param count The number of notifications to post. + * @param pkg The application that will be launched by notifications. + */ + public default void postNotifications(int count, String pkg) { + throw new UnsupportedOperationException("Not yet implemented."); + } + + /** + * Setup Expectations: None + * * <p>Cancel any notifications posted by this helper. */ default public void cancelNotifications() { throw new UnsupportedOperationException("Not yet implemented."); } + + /** + * Setup expectations: Notification shade opened. + * + * <p>Opens the first notification by the specified title and checks if the expected application + * is in foreground or not + * + * @param title The title of the notification to open. + * @param expectedPkg The foreground application after opening a notification. Won't check the + * foreground application if the application is null + */ + public default void openNotificationByTitle(String title, String expectedPkg) { + throw new UnsupportedOperationException("Not yet implemented."); + } } diff --git a/libraries/rule/src/android/platform/test/rule/NotificationPressureRule.java b/libraries/rule/src/android/platform/test/rule/NotificationPressureRule.java index c566551dd..1b32ca8c9 100644 --- a/libraries/rule/src/android/platform/test/rule/NotificationPressureRule.java +++ b/libraries/rule/src/android/platform/test/rule/NotificationPressureRule.java @@ -33,6 +33,7 @@ public class NotificationPressureRule extends TestWatcher { private INotificationHelper mNotificationHelper = initNotificationHelper(); private final int mNotificationCount; + private final String mPackage; public NotificationPressureRule() { this(DEFAULT_NOTIFICATION_COUNT); @@ -44,8 +45,22 @@ public class NotificationPressureRule extends TestWatcher { "Notifications are limited to %d per package.", MAX_NOTIFICATION_COUNT)); } mNotificationCount = notificationCount; + mPackage = null; } + public NotificationPressureRule(int notificationCount, String pkg) + throws IllegalArgumentException { + if (notificationCount > MAX_NOTIFICATION_COUNT) { + throw new IllegalArgumentException( + String.format( + "Notifications are limited to %d per package.", + MAX_NOTIFICATION_COUNT)); + } + mNotificationCount = notificationCount; + mPackage = pkg; + } + + @VisibleForTesting INotificationHelper initNotificationHelper() { HelperAccessor<INotificationHelper> helperAccessor = @@ -55,7 +70,7 @@ public class NotificationPressureRule extends TestWatcher { @Override protected void starting(Description description) { - mNotificationHelper.postNotifications(mNotificationCount); + mNotificationHelper.postNotifications(mNotificationCount, mPackage); } @Override diff --git a/libraries/rule/tests/src/android/platform/test/rule/NotificationPressureRuleTest.java b/libraries/rule/tests/src/android/platform/test/rule/NotificationPressureRuleTest.java index a6649e417..2c2598d06 100644 --- a/libraries/rule/tests/src/android/platform/test/rule/NotificationPressureRuleTest.java +++ b/libraries/rule/tests/src/android/platform/test/rule/NotificationPressureRuleTest.java @@ -34,20 +34,22 @@ import org.mockito.Mockito; public class NotificationPressureRuleTest { private static final int TEST_NOTIFICATION_COUNT = 50; + private static final String TEST_PACKAGE = "package.name"; - /** - * Tests that notifications are posted before the test method and cancelled at the end. - */ + /** Tests that notifications are posted before the test method and cancelled at the end. */ @Test - public void testPostsNotifications() throws Throwable { - TestableNotificationPressureRule rule = new TestableNotificationPressureRule(); - Statement testStatement = new Statement() { - @Override - public void evaluate() throws Throwable { - // Assert that device posted new notifications. - verify(rule.initNotificationHelper()).postNotifications(TEST_NOTIFICATION_COUNT); - } - }; + public void testPostNotifications_withoutPackage() throws Throwable { + TestableNotificationPressureRule rule = + new TestableNotificationPressureRule(TEST_NOTIFICATION_COUNT); + Statement testStatement = + new Statement() { + @Override + public void evaluate() throws Throwable { + // Assert that device posted new notifications. + verify(rule.initNotificationHelper()) + .postNotifications(TEST_NOTIFICATION_COUNT, null); + } + }; rule.apply(testStatement, Description.createTestDescription("clzz", "mthd")) .evaluate(); @@ -56,11 +58,39 @@ public class NotificationPressureRuleTest { verify(rule.initNotificationHelper()).cancelNotifications(); } + /** + * Tests that notifications are posted with package before the test method and cancelled at the + * end. + */ + @Test + public void testPostNotifications_withPackage() throws Throwable { + TestableNotificationPressureRule rule = + new TestableNotificationPressureRule(TEST_NOTIFICATION_COUNT, TEST_PACKAGE); + Statement testStatement = + new Statement() { + @Override + public void evaluate() throws Throwable { + // Assert that device posted new notifications. + verify(rule.initNotificationHelper()) + .postNotifications(TEST_NOTIFICATION_COUNT, TEST_PACKAGE); + } + }; + + rule.apply(testStatement, Description.createTestDescription("clzz", "mthd")).evaluate(); + + // Assert that all notifications are cancelled at the end of the test. + verify(rule.initNotificationHelper()).cancelNotifications(); + } + private static final class TestableNotificationPressureRule extends NotificationPressureRule { private INotificationHelper mNotificationHelper; - TestableNotificationPressureRule() { - super(TEST_NOTIFICATION_COUNT); + TestableNotificationPressureRule(int notificationCount) { + super(notificationCount); + } + + TestableNotificationPressureRule(int notificationCount, String pkg) { + super(notificationCount, pkg); } @Override |