Chromium Code Reviews| Index: chrome/android/junit/src/org/chromium/chrome/browser/notifications/channels/ChannelsUpdaterTest.java |
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/notifications/channels/ChannelsUpdaterTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/notifications/channels/ChannelsUpdaterTest.java |
| index 43f7050066be464e6f8f09c6abbdecfdc19dae39..f381e4703ad564745a939ecc2a444fee6d6617eb 100644 |
| --- a/chrome/android/junit/src/org/chromium/chrome/browser/notifications/channels/ChannelsUpdaterTest.java |
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/notifications/channels/ChannelsUpdaterTest.java |
| @@ -7,9 +7,14 @@ package org.chromium.chrome.browser.notifications.channels; |
| import static org.hamcrest.MatcherAssert.assertThat; |
| import static org.hamcrest.Matchers.containsInAnyOrder; |
| import static org.hamcrest.Matchers.greaterThan; |
| +import static org.hamcrest.Matchers.hasSize; |
| import static org.hamcrest.Matchers.is; |
| +import static org.mockito.ArgumentMatchers.any; |
| +import static org.mockito.Mockito.mock; |
| +import static org.mockito.Mockito.when; |
| import android.app.NotificationManager; |
| +import android.content.res.Resources; |
| import org.junit.Before; |
| import org.junit.Test; |
| @@ -17,22 +22,32 @@ import org.junit.runner.RunWith; |
| import org.junit.runners.BlockJUnit4ClassRunner; |
| import org.chromium.base.test.util.InMemorySharedPreferences; |
| +import org.chromium.chrome.browser.notifications.NotificationManagerProxy; |
| import org.chromium.chrome.test.util.browser.notifications.MockNotificationManagerProxy; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| /** |
| * Tests that ChannelsUpdater correctly initializes channels on the notification manager. |
| */ |
| @RunWith(BlockJUnit4ClassRunner.class) |
| public class ChannelsUpdaterTest { |
| - private MockNotificationManagerProxy mMockNotificationManager; |
| + private NotificationManagerProxy mMockNotificationManager; |
| private InMemorySharedPreferences mMockSharedPreferences; |
| private ChannelsInitializer mChannelsInitializer; |
| + private Resources mMockResources; |
| @Before |
| public void setUp() throws Exception { |
| mMockNotificationManager = new MockNotificationManagerProxy(); |
| - mChannelsInitializer = |
| - new ChannelsInitializer(mMockNotificationManager, new ChannelDefinitions()); |
| + |
| + // Mock the resources to prevent nullpointers on string resource lookups (none of these |
| + // tests need the real strings, if they did this would need to be an instrumentation test). |
| + mMockResources = mock(Resources.class); |
| + when(mMockResources.getString(any(Integer.class))).thenReturn(""); |
| + |
| + mChannelsInitializer = new ChannelsInitializer(mMockNotificationManager, mMockResources); |
| mMockSharedPreferences = new InMemorySharedPreferences(); |
| } |
| @@ -73,7 +88,7 @@ public class ChannelsUpdaterTest { |
| false /* isAtLeastO */, mMockSharedPreferences, mChannelsInitializer, 21); |
| updater.updateChannels(); |
| - assertThat(mMockNotificationManager.getChannels().size(), is(0)); |
| + assertThat(mMockNotificationManager.getNotificationChannels(), hasSize(0)); |
| assertThat(mMockSharedPreferences.getInt(ChannelsUpdater.CHANNELS_VERSION_KEY, -1), is(-1)); |
| } |
| @@ -83,8 +98,8 @@ public class ChannelsUpdaterTest { |
| true /* isAtLeastO */, mMockSharedPreferences, mChannelsInitializer, 21); |
| updater.updateChannels(); |
| - assertThat(mMockNotificationManager.getChannels().size(), is(greaterThan(0))); |
| - assertThat(mMockNotificationManager.getNotificationChannelIds(), |
| + assertThat(mMockNotificationManager.getNotificationChannels(), hasSize((greaterThan(0)))); |
| + assertThat(getChannelIds(mMockNotificationManager.getNotificationChannels()), |
| containsInAnyOrder(ChannelDefinitions.CHANNEL_ID_BROWSER, |
| ChannelDefinitions.CHANNEL_ID_DOWNLOADS, |
| ChannelDefinitions.CHANNEL_ID_INCOGNITO, |
| @@ -92,34 +107,31 @@ public class ChannelsUpdaterTest { |
| assertThat(mMockSharedPreferences.getInt(ChannelsUpdater.CHANNELS_VERSION_KEY, -1), is(21)); |
| } |
| - // Warnings suppressed in order to construct the legacy channels with invalid channel ids. |
| - @SuppressWarnings("WrongConstant") |
| @Test |
| public void testUpdateChannels_deletesLegacyChannelsAndCreatesExpectedOnes() throws Exception { |
| - // Fake some legacy channels (since we don't have any yet). |
| - ChannelDefinitions channelDefinitions = new ChannelDefinitions() { |
| - @Override |
| - public String[] getLegacyChannelIds() { |
| - return new String[] {"OldChannel", "AnotherOldChannel"}; |
| - } |
| - }; |
| - mMockNotificationManager.createNotificationChannel(new ChannelDefinitions.Channel( |
| - "OldChannel", 8292304, NotificationManager.IMPORTANCE_HIGH, |
| - ChannelDefinitions.CHANNEL_GROUP_ID_GENERAL)); |
| - mMockNotificationManager.createNotificationChannel( |
| - new ChannelDefinitions.Channel("AnotherOldChannel", 8292304, |
| - NotificationManager.IMPORTANCE_LOW, "OldChannelGroup")); |
| - assertThat(mMockNotificationManager.getNotificationChannelIds(), |
| - containsInAnyOrder("OldChannel", "AnotherOldChannel")); |
| + // Set up any legacy channels. |
| + for (String id : ChannelDefinitions.getLegacyChannelIds()) { |
| + mMockNotificationManager.createNotificationChannel( |
| + new Channel(id, id, NotificationManager.IMPORTANCE_LOW, |
| + ChannelDefinitions.CHANNEL_GROUP_ID_GENERAL)); |
| + } |
| ChannelsUpdater updater = new ChannelsUpdater(true /* isAtLeastO */, mMockSharedPreferences, |
| - new ChannelsInitializer(mMockNotificationManager, channelDefinitions), 12); |
| + new ChannelsInitializer(mMockNotificationManager, mMockResources), 12); |
| updater.updateChannels(); |
| - assertThat(mMockNotificationManager.getNotificationChannelIds(), |
| + assertThat(getChannelIds(mMockNotificationManager.getNotificationChannels()), |
| containsInAnyOrder(ChannelDefinitions.CHANNEL_ID_BROWSER, |
| ChannelDefinitions.CHANNEL_ID_DOWNLOADS, |
| ChannelDefinitions.CHANNEL_ID_INCOGNITO, |
| ChannelDefinitions.CHANNEL_ID_SITES, ChannelDefinitions.CHANNEL_ID_MEDIA)); |
| } |
| + |
| + private static List<String> getChannelIds(List<Channel> channels) { |
| + List<String> ids = new ArrayList<>(); |
| + for (Channel ch : channels) { |
|
Peter Beverloo
2017/05/15 16:41:46
micro nit: channels, fine to fix in another CL :)
|
| + ids.add(ch.getId()); |
| + } |
| + return ids; |
| + } |
| } |