• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static android.support.test.espresso.Espresso.onView;
22 import static android.support.test.espresso.assertion.ViewAssertions.matches;
23 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
24 import static android.support.test.espresso.matcher.ViewMatchers.withId;
25 import static android.support.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.InstrumentationRegistry;
40 import android.support.test.filters.SmallTest;
41 import android.support.test.runner.AndroidJUnit4;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 
47 @RunWith(AndroidJUnit4.class)
48 @SmallTest
49 public class ChannelNotificationSettingsTest {
50 
51     private Context mTargetContext;
52     private Instrumentation mInstrumentation;
53     private NotificationChannel mNotificationChannel;
54     private NotificationManager mNm;
55 
56     @Before
setUp()57     public void setUp() {
58         mInstrumentation = InstrumentationRegistry.getInstrumentation();
59         mTargetContext = mInstrumentation.getTargetContext();
60 
61         mNm  = (NotificationManager) mTargetContext.getSystemService(Context.NOTIFICATION_SERVICE);
62         mNotificationChannel = new NotificationChannel(this.getClass().getName(),
63                 this.getClass().getName(), IMPORTANCE_MIN);
64         mNm.createNotificationChannel(mNotificationChannel);
65     }
66 
67     @Test
launchNotificationSetting_shouldNotCrash()68     public void launchNotificationSetting_shouldNotCrash() {
69         final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
70                 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName())
71                 .putExtra(Settings.EXTRA_CHANNEL_ID, mNotificationChannel.getId());
72         mInstrumentation.startActivitySync(intent);
73 
74         onView(allOf(withText(mNotificationChannel.getName().toString()))).check(
75                 matches(isDisplayed()));
76     }
77 
78     @Test
launchNotificationSettings_blockedChannel()79     public void launchNotificationSettings_blockedChannel() throws Exception {
80         NotificationChannel blocked =
81                 new NotificationChannel("blocked", "blocked", IMPORTANCE_NONE);
82         mNm.createNotificationChannel(blocked);
83 
84         INotificationManager sINM = INotificationManager.Stub.asInterface(
85                 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
86         blocked.setImportance(IMPORTANCE_NONE);
87         sINM.updateNotificationChannelForPackage(
88                 mTargetContext.getPackageName(), Process.myUid(), blocked);
89 
90         final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
91                 .putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName())
92                 .putExtra(Settings.EXTRA_CHANNEL_ID, blocked.getId());
93         mInstrumentation.startActivitySync(intent);
94 
95         onView(allOf(withText("Off"), isDisplayed())).check(matches(isDisplayed()));
96         onView(allOf(withText("Android is blocking this category of notifications from"
97                 + " appearing on this device"))).check(matches(isDisplayed()));
98 
99         try {
100             onView(allOf(withText("On the lock screen"))).check(matches(isDisplayed()));
101             fail("settings appearing for blocked channel");
102         } catch (Exception e) {
103             // expected
104         }
105     }
106 }
107