Commit 8aa7ec22 authored by Xing Liu's avatar Xing Liu Committed by Commit Bot

Android notification: Use ChromeNotification in NotificationManagerProxy.

This CL adds an API in NotificationManagerProxy to take
ChromeNotification struct when sending notification, to avoid duplicate
variable used in ChromeNotificationBuilder. Also this will enfore
NotificationManagerProxy code path.

Bug: 898269
Change-Id: I4dac598a56da3bb3b2a647445e1b47fe4ead42ce
Reviewed-on: https://chromium-review.googlesource.com/c/1437774
Commit-Queue: Xing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#630588}
parent 33a4035f
......@@ -103,5 +103,8 @@ public interface ChromeNotificationBuilder {
Notification buildWithBigTextStyle(String bigText);
@Deprecated
Notification build();
ChromeNotification buildChromeNotification();
}
......@@ -349,4 +349,10 @@ public class NotificationBuilder implements ChromeNotificationBuilder {
public Notification build() {
return mBuilder.build();
}
@Override
public ChromeNotification buildChromeNotification() {
assert mMetadata != null;
return new ChromeNotification(build(), mMetadata);
}
}
......@@ -289,4 +289,10 @@ public class NotificationCompatBuilder implements ChromeNotificationBuilder {
public Notification build() {
return mBuilder.build();
}
@Override
public ChromeNotification buildChromeNotification() {
assert mMetadata != null;
return new ChromeNotification(build(), mMetadata);
}
}
......@@ -40,9 +40,13 @@ public interface NotificationManagerProxy {
@TargetApi(Build.VERSION_CODES.O)
void deleteNotificationChannel(String id);
@Deprecated
void notify(int id, Notification notification);
@Deprecated
void notify(String tag, int id, Notification notification);
void notify(ChromeNotification notification);
@TargetApi(Build.VERSION_CODES.O)
NotificationChannel getNotificationChannel(String channelId);
......
......@@ -100,6 +100,13 @@ public class NotificationManagerProxyImpl implements NotificationManagerProxy {
mNotificationManager.notify(tag, id, notification);
}
@Override
public void notify(ChromeNotification notification) {
assert notification.getMetadata() != null;
mNotificationManager.notify(notification.getMetadata().tag, notification.getMetadata().id,
notification.getNotification());
}
@TargetApi(Build.VERSION_CODES.O)
@Override
public NotificationChannel getNotificationChannel(String channelId) {
......
......@@ -6,7 +6,6 @@ package org.chromium.chrome.browser.notifications;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.test.InstrumentationRegistry;
......@@ -33,14 +32,13 @@ import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
public class ChromeNotificationBuilderTest {
private static final int TEST_NOTIFICATION_ID = 101;
private NotificationManager mNotificationManager;
private NotificationManagerProxy mNotificationManager;
@Before
public void setUp() {
Context context = InstrumentationRegistry.getTargetContext();
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager = new NotificationManagerProxyImpl(context);
// Don't rely on channels already being registered.
clearNotificationChannels(mNotificationManager);
......@@ -52,7 +50,7 @@ public class ChromeNotificationBuilderTest {
mNotificationManager.cancelAll();
}
private static void clearNotificationChannels(NotificationManager notificationManager) {
private static void clearNotificationChannels(NotificationManagerProxy notificationManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
for (NotificationChannel channel : notificationManager.getNotificationChannels()) {
if (!channel.getId().equals(NotificationChannel.DEFAULT_CHANNEL_ID)) {
......@@ -72,7 +70,6 @@ public class ChromeNotificationBuilderTest {
Notification notification = notificationBuilder.setContentTitle("Title")
.setSmallIcon(R.drawable.ic_chrome)
.build();
mNotificationManager.notify(TEST_NOTIFICATION_ID, notification);
}
......@@ -89,4 +86,21 @@ public class ChromeNotificationBuilderTest {
mNotificationManager.notify(TEST_NOTIFICATION_ID, notification);
}
}
\ No newline at end of file
@MediumTest
@Test
public void buildChromeNotification() {
ChromeNotificationBuilder builder =
NotificationBuilderFactory.createChromeNotificationBuilder(true,
ChannelDefinitions.ChannelId.BROWSER, null,
new NotificationMetadata(
NotificationUmaTracker.SystemNotificationType.BROWSER_ACTIONS, null,
TEST_NOTIFICATION_ID));
ChromeNotification notification = builder.setContentTitle("Title")
.setSmallIcon(R.drawable.ic_chrome)
.buildChromeNotification();
mNotificationManager.notify(notification);
}
}
......@@ -11,6 +11,7 @@ import android.app.NotificationChannelGroup;
import android.os.Build;
import android.support.annotation.Nullable;
import org.chromium.chrome.browser.notifications.ChromeNotification;
import org.chromium.chrome.browser.notifications.NotificationManagerProxy;
import java.util.ArrayList;
......@@ -115,6 +116,12 @@ public class MockNotificationManagerProxy implements NotificationManagerProxy {
mMutationCount++;
}
@Override
public void notify(ChromeNotification notification) {
notify(notification.getMetadata().tag, notification.getMetadata().id,
notification.getNotification());
}
private static String makeKey(int id, @Nullable String tag) {
String key = Integer.toString(id);
if (tag != null) key += KEY_SEPARATOR + tag;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment