Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2416)

Unified Diff: chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridgeTest.java

Issue 2316263002: Notifications in sensitive contexts now display origin + small icon (Closed)
Patch Set: Moved public notification creation and icon generation to NotificationBuilderBase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridgeTest.java
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridgeTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridgeTest.java
index 668f27c6ce0e46a157d6b88270aafffbf92394aa..a762a69512b68105c1d8982f30acba28df2b295e 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridgeTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationPlatformBridgeTest.java
@@ -180,14 +180,16 @@ public class NotificationPlatformBridgeTest extends NotificationTestBase {
@Feature({"Browser", "Notifications"})
public void testDefaultNotificationProperties() throws Exception {
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
+ Context context = getInstrumentation().getTargetContext();
Notification notification = showAndGetNotification("MyNotification", "{ body: 'Hello' }");
+ String expectedOrigin =
+ UrlFormatter.formatUrlForSecurityDisplay(getOrigin(), false /* showScheme */);
// Validate the contents of the notification.
assertEquals("MyNotification", NotificationTestUtil.getExtraTitle(notification));
assertEquals("Hello", NotificationTestUtil.getExtraText(notification));
- assertEquals(UrlFormatter.formatUrlForSecurityDisplay(getOrigin(), false /* showScheme */),
- NotificationTestUtil.getExtraSubText(notification));
+ assertEquals(expectedOrigin, NotificationTestUtil.getExtraSubText(notification));
// Verify that the ticker text contains the notification's title and body.
String tickerText = notification.tickerText.toString();
@@ -195,12 +197,28 @@ public class NotificationPlatformBridgeTest extends NotificationTestBase {
assertTrue(tickerText.contains("MyNotification"));
assertTrue(tickerText.contains("Hello"));
+ // On L+, verify the public version of the notification contains the notification's origin,
+ // and that the body text has been replaced.
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ assertNotNull(notification.publicVersion);
+ assertEquals(context.getString(R.string.notification_hidden_text),
+ NotificationTestUtil.getExtraText(notification.publicVersion));
+ }
+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
+ // On N+, origin should be set as the subtext of the public notification.
+ assertEquals(expectedOrigin,
+ NotificationTestUtil.getExtraSubText(notification.publicVersion));
+ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ // On L/M, origin should be the title of the public notification.
+ assertEquals(
+ expectedOrigin, NotificationTestUtil.getExtraTitle(notification.publicVersion));
+ }
+
// Verify that the notification's timestamp is set in the past 60 seconds. This number has
// no significance, but needs to be high enough to not cause flakiness as it's set by the
// renderer process on notification creation.
assertTrue(Math.abs(System.currentTimeMillis() - notification.when) < 60 * 1000);
- Context context = getInstrumentation().getTargetContext();
assertNotNull(NotificationTestUtil.getLargeIconFromNotification(context, notification));
// Validate the notification's behavior.
@@ -311,8 +329,9 @@ public class NotificationPlatformBridgeTest extends NotificationTestBase {
/**
* Verifies that on Android M+, notifications which specify a badge will have that icon
- * fetched and included as the small icon in the notification.
- * If the test target is L or below, verifies the small icon is the expected chrome logo.
+ * fetched and included as the small icon in the notification and public version.
+ * If the test target is L or below, verifies the small icon (and public small icon on L) is
+ * the expected chrome logo.
*/
@MediumTest
@Feature({"Browser", "Notifications"})
@@ -329,16 +348,35 @@ public class NotificationPlatformBridgeTest extends NotificationTestBase {
assertNotNull(smallIcon);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ // Custom badges are only supported on M+.
+ // 1. Check the notification badge.
URL badgeUrl =
new URL(getTestServer().getURL("/chrome/test/data/notifications/badge.png"));
Bitmap bitmap = BitmapFactory.decodeStream(badgeUrl.openStream());
Bitmap expected = bitmap.copy(bitmap.getConfig(), true);
NotificationBuilderBase.applyWhiteOverlayToBitmap(expected);
assertTrue(expected.sameAs(smallIcon));
+
+ // 2. Check the public notification badge.
+ assertNotNull(notification.publicVersion);
+ Bitmap publicSmallIcon = NotificationTestUtil.getSmallIconFromNotification(
+ context, notification.publicVersion);
+ assertNotNull(publicSmallIcon);
+ assertEquals(expected.getWidth(), publicSmallIcon.getWidth());
+ assertEquals(expected.getHeight(), publicSmallIcon.getHeight());
+ assertTrue(expected.sameAs(publicSmallIcon));
} else {
Bitmap expected =
BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_chrome);
assertTrue(expected.sameAs(smallIcon));
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ // Public versions of notifications are only supported on L+.
+ assertNotNull(notification.publicVersion);
+ Bitmap publicSmallIcon = NotificationTestUtil.getSmallIconFromNotification(
+ context, notification.publicVersion);
+ assertTrue(expected.sameAs(publicSmallIcon));
+ }
}
}
@@ -384,8 +422,7 @@ public class NotificationPlatformBridgeTest extends NotificationTestBase {
// Create a second rounded icon for the test's origin, and compare its dimensions against
// those of the icon associated to the notification itself.
- RoundedIconGenerator generator = notificationBridge.mIconGenerator;
- assertNotNull(generator);
+ RoundedIconGenerator generator = getRoundedIconGenerator();
Bitmap generatedIcon = generator.generateIconForUrl(getOrigin());
assertNotNull(generatedIcon);
@@ -393,54 +430,20 @@ public class NotificationPlatformBridgeTest extends NotificationTestBase {
NotificationTestUtil.getLargeIconFromNotification(context, notification)));
}
- /**
- * Tests the three paths for ensuring that a notification will be shown with a normalized icon:
- * (1) NULL bitmaps should have an auto-generated image.
- * (2) Large bitmaps should be resized to the device's intended size.
- * (3) Smaller bitmaps should be left alone.
- */
- @MediumTest
- @Feature({"Browser", "Notifications"})
- public void testEnsureNormalizedIconBehavior() throws Exception {
- setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
-
- // Create a notification to ensure that the NotificationPlatformBridge is initialized.
- showAndGetNotification("MyNotification", "{}");
-
- // Get the dimensions of the notification icon that will be presented to the user.
- Context appContext = getInstrumentation().getTargetContext().getApplicationContext();
- Resources resources = appContext.getResources();
-
+ private RoundedIconGenerator getRoundedIconGenerator() {
Peter Beverloo 2016/09/12 17:52:03 Something to consider: could reduce duplication by
awdf 2016/09/14 12:30:29 Done.
+ Resources resources = getInstrumentation().getTargetContext().getResources();
int largeIconWidthPx =
resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
int largeIconHeightPx =
resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
-
- String origin = "https://example.com";
-
- NotificationPlatformBridge notificationBridge =
- NotificationPlatformBridge.getInstanceForTests();
- assertNotNull(notificationBridge);
-
- Bitmap fromNullIcon = notificationBridge.ensureNormalizedIcon(null, origin);
- assertNotNull(fromNullIcon);
- assertEquals(largeIconWidthPx, fromNullIcon.getWidth());
- assertEquals(largeIconHeightPx, fromNullIcon.getHeight());
-
- Bitmap largeIcon = Bitmap.createBitmap(largeIconWidthPx * 2, largeIconHeightPx * 2,
- Bitmap.Config.ALPHA_8);
-
- Bitmap fromLargeIcon = notificationBridge.ensureNormalizedIcon(largeIcon, origin);
- assertNotNull(fromLargeIcon);
- assertEquals(largeIconWidthPx, fromLargeIcon.getWidth());
- assertEquals(largeIconHeightPx, fromLargeIcon.getHeight());
-
- Bitmap smallIcon = Bitmap.createBitmap(largeIconWidthPx / 2, largeIconHeightPx / 2,
- Bitmap.Config.ALPHA_8);
-
- Bitmap fromSmallIcon = notificationBridge.ensureNormalizedIcon(smallIcon, origin);
- assertNotNull(fromSmallIcon);
- assertEquals(smallIcon, fromSmallIcon);
+ float density = resources.getDisplayMetrics().density;
+ int cornerRadiusPx = Math.min(largeIconWidthPx, largeIconHeightPx) / 2;
+ RoundedIconGenerator generator =
+ new RoundedIconGenerator(largeIconWidthPx, largeIconHeightPx, cornerRadiusPx,
+ NotificationBuilderBase.NOTIFICATION_ICON_BG_COLOR,
+ NotificationBuilderBase.NOTIFICATION_TEXT_SIZE_DP * density);
+ assertNotNull(generator);
+ return generator;
}
/*

Powered by Google App Engine
This is Rietveld 408576698