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_MIN; 20 import static android.app.NotificationManager.IMPORTANCE_NONE; 21 22 import static androidx.test.espresso.Espresso.onView; 23 import static androidx.test.espresso.assertion.ViewAssertions.matches; 24 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; 25 import static androidx.test.espresso.matcher.ViewMatchers.withText; 26 27 import static org.hamcrest.Matchers.allOf; 28 import static org.junit.Assert.fail; 29 30 import android.app.INotificationManager; 31 import android.app.Instrumentation; 32 import android.app.NotificationChannel; 33 import android.app.NotificationManager; 34 import android.content.Context; 35 import android.content.Intent; 36 import android.os.Process; 37 import android.os.ServiceManager; 38 import android.provider.Settings; 39 import android.support.test.uiautomator.UiDevice; 40 41 import androidx.test.InstrumentationRegistry; 42 import androidx.test.filters.SmallTest; 43 import androidx.test.runner.AndroidJUnit4; 44 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 49 @RunWith(AndroidJUnit4.class) 50 @SmallTest 51 public class ChannelNotificationSettingsTest { 52 private static final String WM_DISMISS_KEYGUARD_COMMAND = "wm dismiss-keyguard"; 53 54 private UiDevice mUiDevice; 55 private Context mTargetContext; 56 private Instrumentation mInstrumentation; 57 private NotificationChannel mNotificationChannel; 58 private NotificationManager mNm; 59 60 @Before setUp()61 public void setUp() throws Exception { 62 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 63 mTargetContext = mInstrumentation.getTargetContext(); 64 65 mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 66 mUiDevice.wakeUp(); 67 mUiDevice.executeShellCommand(WM_DISMISS_KEYGUARD_COMMAND); 68 69 mNm = (NotificationManager) mTargetContext.getSystemService(Context.NOTIFICATION_SERVICE); 70 mNotificationChannel = new NotificationChannel(this.getClass().getName(), 71 this.getClass().getName(), IMPORTANCE_MIN); 72 mNm.createNotificationChannel(mNotificationChannel); 73 } 74 75 @Test launchNotificationSetting_shouldNotCrash()76 public void launchNotificationSetting_shouldNotCrash() { 77 final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) 78 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName()) 79 .putExtra(Settings.EXTRA_CHANNEL_ID, mNotificationChannel.getId()) 80 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 81 mInstrumentation.startActivitySync(intent); 82 83 onView(allOf(withText(mNotificationChannel.getName().toString()))).check( 84 matches(isDisplayed())); 85 } 86 87 @Test launchNotificationSettings_blockedChannel()88 public void launchNotificationSettings_blockedChannel() throws Exception { 89 NotificationChannel blocked = 90 new NotificationChannel("blocked", "blocked", IMPORTANCE_NONE); 91 mNm.createNotificationChannel(blocked); 92 93 INotificationManager sINM = INotificationManager.Stub.asInterface( 94 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 95 blocked.setImportance(IMPORTANCE_NONE); 96 sINM.updateNotificationChannelForPackage( 97 mTargetContext.getPackageName(), Process.myUid(), blocked); 98 99 final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) 100 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName()) 101 .putExtra(Settings.EXTRA_CHANNEL_ID, blocked.getId()) 102 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 103 mInstrumentation.startActivitySync(intent); 104 105 onView(allOf(withText("At your request, Android is blocking this category of notifications" 106 + " from appearing on this device"))).check(matches(isDisplayed())); 107 108 try { 109 onView(allOf(withText("On the lock screen"))).check(matches(isDisplayed())); 110 fail("settings appearing for blocked channel"); 111 } catch (Exception e) { 112 // expected 113 } 114 } 115 } 116