OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.notifications; | |
6 | |
7 import static org.hamcrest.MatcherAssert.assertThat; | |
8 import static org.hamcrest.Matchers.containsInAnyOrder; | |
9 import static org.hamcrest.Matchers.greaterThan; | |
10 import static org.hamcrest.Matchers.is; | |
11 | |
12 import android.app.NotificationManager; | |
13 | |
14 import org.junit.Before; | |
15 import org.junit.Test; | |
16 import org.junit.runner.RunWith; | |
17 import org.junit.runners.BlockJUnit4ClassRunner; | |
18 | |
19 import org.chromium.base.test.util.InMemorySharedPreferences; | |
20 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag erProxy; | |
21 | |
22 /** | |
23 * Tests that ChannelsUpdater correctly initializes channels on the notification manager. | |
24 */ | |
25 @RunWith(BlockJUnit4ClassRunner.class) | |
26 public class ChannelsUpdaterTest { | |
27 private MockNotificationManagerProxy mMockNotificationManager; | |
28 private InMemorySharedPreferences mMockSharedPreferences; | |
29 | |
30 @Before | |
31 public void setUp() throws Exception { | |
32 mMockNotificationManager = new MockNotificationManagerProxy(); | |
33 mMockSharedPreferences = new InMemorySharedPreferences(); | |
34 } | |
35 | |
36 @Test | |
37 public void testShouldUpdateChannels_returnsFalsePreO() throws Exception { | |
38 ChannelsUpdater updater = new ChannelsUpdater( | |
39 false /* isAtLeastO */, mMockSharedPreferences, mMockNotificatio nManager, 0); | |
40 assertThat(updater.shouldUpdateChannels(), is(false)); | |
41 } | |
42 | |
43 @Test | |
44 public void testShouldUpdateChannels_returnsTrueIfOAndNoSavedVersionInPrefs( ) throws Exception { | |
45 ChannelsUpdater updater = new ChannelsUpdater( | |
46 true /* isAtLeastO */, mMockSharedPreferences, mMockNotification Manager, 0); | |
47 assertThat(updater.shouldUpdateChannels(), is(true)); | |
48 } | |
49 | |
50 @Test | |
51 public void testShouldUpdateChannels_returnsTrueIfOAndDifferentVersionInPref s() | |
52 throws Exception { | |
53 mMockSharedPreferences.edit().putInt(ChannelsUpdater.CHANNELS_VERSION_KE Y, 4).apply(); | |
54 ChannelsUpdater updater = new ChannelsUpdater( | |
55 true /* isAtLeastO */, mMockSharedPreferences, mMockNotification Manager, 5); | |
56 assertThat(updater.shouldUpdateChannels(), is(true)); | |
57 } | |
58 | |
59 @Test | |
60 public void testShouldUpdateChannels_returnsFalseIfOAndSameVersionInPrefs() throws Exception { | |
61 mMockSharedPreferences.edit().putInt(ChannelsUpdater.CHANNELS_VERSION_KE Y, 3).apply(); | |
62 ChannelsUpdater updater = new ChannelsUpdater( | |
63 true /* isAtLeastO */, mMockSharedPreferences, mMockNotification Manager, 3); | |
64 assertThat(updater.shouldUpdateChannels(), is(false)); | |
65 } | |
66 | |
67 @Test | |
68 public void testUpdateChannels_noopPreO() throws Exception { | |
69 ChannelsUpdater updater = new ChannelsUpdater( | |
70 false /* isAtLeastO */, mMockSharedPreferences, mMockNotificatio nManager, 21); | |
71 updater.updateChannels(); | |
72 | |
73 assertThat(mMockNotificationManager.getChannels().size(), is(0)); | |
74 assertThat(mMockSharedPreferences.getInt(ChannelsUpdater.CHANNELS_VERSIO N_KEY, -1), is(-1)); | |
75 } | |
76 | |
77 @Test | |
78 public void testUpdateChannels_createsExpectedChannelsAndUpdatesPref() throw s Exception { | |
79 ChannelsUpdater updater = new ChannelsUpdater( | |
80 true /* isAtLeastO */, mMockSharedPreferences, mMockNotification Manager, 21); | |
81 updater.updateChannels(); | |
82 | |
83 assertThat(mMockNotificationManager.getChannels().size(), is(greaterThan (0))); | |
84 assertThat(mMockNotificationManager.getNotificationChannelIds(), | |
85 containsInAnyOrder(ChannelsInitializer.CHANNEL_ID_BROWSER, | |
86 ChannelsInitializer.CHANNEL_ID_SITES)); | |
87 assertThat(mMockSharedPreferences.getInt(ChannelsUpdater.CHANNELS_VERSIO N_KEY, -1), is(21)); | |
88 } | |
89 | |
90 @SuppressWarnings("WrongConstant") // for constructing the old channel with invalid parameters | |
nyquist
2017/04/11 19:03:49
Nit: Capitalize For and add period.
awdf
2017/04/12 13:58:15
Went one better and turned it into an actual sente
| |
91 @Test | |
92 public void testUpdateChannels_deletesOldChannelsAndCreatesExpectedOnes() th rows Exception { | |
93 mMockNotificationManager.createNotificationChannel(new ChannelsInitializ er.Channel( | |
94 "OldChannel", 8292304, NotificationManager.IMPORTANCE_HIGH, | |
95 ChannelsInitializer.CHANNEL_GROUP_ID_GENERAL)); | |
96 mMockNotificationManager.createNotificationChannel( | |
97 new ChannelsInitializer.Channel("AnotherOldChannel", 8292304, | |
98 NotificationManager.IMPORTANCE_LOW, "OldChannelGroup")); | |
99 assertThat(mMockNotificationManager.getNotificationChannelIds(), | |
100 containsInAnyOrder("OldChannel", "AnotherOldChannel")); | |
101 | |
102 ChannelsUpdater updater = new ChannelsUpdater( | |
103 true /* isAtLeastO */, mMockSharedPreferences, mMockNotification Manager, 12); | |
104 updater.updateChannels(); | |
105 | |
106 assertThat(mMockNotificationManager.getNotificationChannelIds(), | |
107 containsInAnyOrder(ChannelsInitializer.CHANNEL_ID_BROWSER, | |
108 ChannelsInitializer.CHANNEL_ID_SITES)); | |
109 } | |
110 } | |
OLD | NEW |