1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.notification; 18 19 import static android.app.NotificationManager.IMPORTANCE_DEFAULT; 20 import static android.support.test.espresso.Espresso.onView; 21 import static android.support.test.espresso.action.ViewActions.click; 22 import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; 23 import static android.support.test.espresso.assertion.ViewAssertions.matches; 24 import android.support.test.espresso.intent.Intents; 25 26 import static android.support.test.espresso.intent.Intents.intended; 27 import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtra; 28 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 29 import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; 30 import static android.support.test.espresso.matcher.ViewMatchers.withId; 31 import static android.support.test.espresso.matcher.ViewMatchers.withText; 32 33 import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT; 34 35 import static org.hamcrest.Matchers.allOf; 36 import static org.junit.Assert.fail; 37 38 import android.app.Instrumentation; 39 import android.app.NotificationChannel; 40 import android.app.NotificationChannelGroup; 41 import android.app.NotificationManager; 42 import android.content.Context; 43 import android.content.Intent; 44 import android.provider.Settings; 45 import android.support.test.InstrumentationRegistry; 46 import android.support.test.espresso.matcher.ViewMatchers; 47 import android.support.test.filters.SmallTest; 48 import android.support.test.runner.AndroidJUnit4; 49 50 import org.junit.Before; 51 import org.junit.Test; 52 import org.junit.runner.RunWith; 53 54 @RunWith(AndroidJUnit4.class) 55 @SmallTest 56 public class AppNotificationSettingsTest { 57 58 private Context mTargetContext; 59 private Instrumentation mInstrumentation; 60 61 NotificationManager mNm; 62 private NotificationChannelGroup mGroup1; 63 private NotificationChannel mGroup1Channel1; 64 private NotificationChannel mGroup1Channel2; 65 private NotificationChannelGroup mGroup2; 66 private NotificationChannel mGroup2Channel1; 67 private NotificationChannel mUngroupedChannel; 68 69 @Before setUp()70 public void setUp() { 71 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 72 mTargetContext = mInstrumentation.getTargetContext(); 73 mNm = (NotificationManager) mTargetContext.getSystemService(Context.NOTIFICATION_SERVICE); 74 75 mGroup1 = new NotificationChannelGroup(this.getClass().getName() + "1", "group1"); 76 mGroup2 = new NotificationChannelGroup(this.getClass().getName() + "2", "group2"); 77 mNm.createNotificationChannelGroup(mGroup1); 78 mNm.createNotificationChannelGroup(mGroup2); 79 80 mGroup1Channel1 = createChannel(mGroup1, this.getClass().getName()+ "c1-1"); 81 mGroup1Channel2 = createChannel(mGroup1, this.getClass().getName()+ "c1-2"); 82 mGroup2Channel1 = createChannel(mGroup2, this.getClass().getName()+ "c2-1"); 83 mUngroupedChannel = createChannel(null, this.getClass().getName()+ "c"); 84 } 85 86 @Test launchNotificationSetting_shouldNotHaveAppInfoLink()87 public void launchNotificationSetting_shouldNotHaveAppInfoLink() { 88 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) 89 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName()); 90 91 mInstrumentation.startActivitySync(intent); 92 93 onView(allOf(withId(android.R.id.button1), 94 withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))) 95 .check(doesNotExist()); 96 } 97 98 @Test launchNotificationSetting_showGroupsWithMultipleChannels()99 public void launchNotificationSetting_showGroupsWithMultipleChannels() { 100 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) 101 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName()); 102 mInstrumentation.startActivitySync(intent); 103 onView(allOf(withText(mGroup1.getName().toString()))).check( 104 matches(isDisplayed())); 105 try { 106 onView(allOf(withText(mGroup1Channel1.getName().toString()))) 107 .check(matches(isDisplayed())); 108 fail("Channel erroneously appearing"); 109 } catch (Exception e) { 110 // expected 111 } 112 // links to group page 113 Intents.init(); 114 onView(allOf(withText(mGroup1.getName().toString()))).perform(click()); 115 intended(allOf(hasExtra(EXTRA_SHOW_FRAGMENT, 116 ChannelGroupNotificationSettings.class.getName()))); 117 Intents.release(); 118 } 119 120 @Test launchNotificationSetting_showUngroupedChannels()121 public void launchNotificationSetting_showUngroupedChannels() { 122 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) 123 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName()); 124 mInstrumentation.startActivitySync(intent); 125 onView(allOf(withText(mUngroupedChannel.getName().toString()))) 126 .check(matches(isDisplayed())); 127 // links directly to channel page 128 Intents.init(); 129 onView(allOf(withText(mUngroupedChannel.getName().toString()))).perform(click()); 130 intended(allOf(hasExtra(EXTRA_SHOW_FRAGMENT, ChannelNotificationSettings.class.getName()))); 131 Intents.release(); 132 } 133 134 @Test launchNotificationSetting_showGroupsWithOneChannel()135 public void launchNotificationSetting_showGroupsWithOneChannel() { 136 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) 137 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName()); 138 mInstrumentation.startActivitySync(intent); 139 140 onView(allOf(withText(mGroup2Channel1.getName().toString()))) 141 .check(matches(isDisplayed())); 142 try { 143 onView(allOf(withText(mGroup2.getName().toString()))).check( 144 matches(isDisplayed())); 145 fail("Group erroneously appearing"); 146 } catch (Exception e) { 147 // expected 148 } 149 150 // links directly to channel page 151 Intents.init(); 152 onView(allOf(withText(mGroup2Channel1.getName().toString()))).perform(click()); 153 intended(allOf(hasExtra(EXTRA_SHOW_FRAGMENT, ChannelNotificationSettings.class.getName()))); 154 Intents.release(); 155 } 156 createChannel(NotificationChannelGroup group, String id)157 private NotificationChannel createChannel(NotificationChannelGroup group, 158 String id) { 159 NotificationChannel channel = new NotificationChannel(id, id, IMPORTANCE_DEFAULT); 160 if (group != null) { 161 channel.setGroup(group.getId()); 162 } 163 mNm.createNotificationChannel(channel); 164 return channel; 165 } 166 } 167