Merge "Add settings page for notification channel groups"

This commit is contained in:
Julia Reynolds
2017-09-01 12:49:39 +00:00
committed by Android (Google) Code Review
14 changed files with 751 additions and 127 deletions

View File

@@ -16,7 +16,29 @@
package com.android.settings.notification;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import android.support.test.espresso.intent.Intents;
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasExtra;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.fail;
import android.app.Instrumentation;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
@@ -29,12 +51,6 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.allOf;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class AppNotificationSettingsTest {
@@ -42,10 +58,29 @@ public class AppNotificationSettingsTest {
private Context mTargetContext;
private Instrumentation mInstrumentation;
NotificationManager mNm;
private NotificationChannelGroup mGroup1;
private NotificationChannel mGroup1Channel1;
private NotificationChannel mGroup1Channel2;
private NotificationChannelGroup mGroup2;
private NotificationChannel mGroup2Channel1;
private NotificationChannel mUngroupedChannel;
@Before
public void setUp() {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mTargetContext = mInstrumentation.getTargetContext();
mNm = (NotificationManager) mTargetContext.getSystemService(Context.NOTIFICATION_SERVICE);
mGroup1 = new NotificationChannelGroup(this.getClass().getName() + "1", "group1");
mGroup2 = new NotificationChannelGroup(this.getClass().getName() + "2", "group2");
mNm.createNotificationChannelGroup(mGroup1);
mNm.createNotificationChannelGroup(mGroup2);
mGroup1Channel1 = createChannel(mGroup1, this.getClass().getName()+ "c1-1");
mGroup1Channel2 = createChannel(mGroup1, this.getClass().getName()+ "c1-2");
mGroup2Channel1 = createChannel(mGroup2, this.getClass().getName()+ "c2-1");
mUngroupedChannel = createChannel(null, this.getClass().getName()+ "c");
}
@Test
@@ -60,4 +95,72 @@ public class AppNotificationSettingsTest {
.check(doesNotExist());
}
@Test
public void launchNotificationSetting_showGroupsWithMultipleChannels() {
final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText(mGroup1.getName().toString()))).check(
matches(isDisplayed()));
try {
onView(allOf(withText(mGroup1Channel1.getName().toString())))
.check(matches(isDisplayed()));
fail("Channel erroneously appearing");
} catch (Exception e) {
// expected
}
// links to group page
Intents.init();
onView(allOf(withText(mGroup1.getName().toString()))).perform(click());
intended(allOf(hasExtra(EXTRA_SHOW_FRAGMENT,
ChannelGroupNotificationSettings.class.getName())));
Intents.release();
}
@Test
public void launchNotificationSetting_showUngroupedChannels() {
final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText(mUngroupedChannel.getName().toString())))
.check(matches(isDisplayed()));
// links directly to channel page
Intents.init();
onView(allOf(withText(mUngroupedChannel.getName().toString()))).perform(click());
intended(allOf(hasExtra(EXTRA_SHOW_FRAGMENT, ChannelNotificationSettings.class.getName())));
Intents.release();
}
@Test
public void launchNotificationSetting_showGroupsWithOneChannel() {
final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText(mGroup2Channel1.getName().toString())))
.check(matches(isDisplayed()));
try {
onView(allOf(withText(mGroup2.getName().toString()))).check(
matches(isDisplayed()));
fail("Group erroneously appearing");
} catch (Exception e) {
// expected
}
// links directly to channel page
Intents.init();
onView(allOf(withText(mGroup2Channel1.getName().toString()))).perform(click());
intended(allOf(hasExtra(EXTRA_SHOW_FRAGMENT, ChannelNotificationSettings.class.getName())));
Intents.release();
}
private NotificationChannel createChannel(NotificationChannelGroup group,
String id) {
NotificationChannel channel = new NotificationChannel(id, id, IMPORTANCE_DEFAULT);
if (group != null) {
channel.setGroup(group.getId());
}
mNm.createNotificationChannel(channel);
return channel;
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import static android.app.NotificationManager.IMPORTANCE_HIGH;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.fail;
import android.app.INotificationManager;
import android.app.Instrumentation;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.os.ServiceManager;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ChannelGroupNotificationSettingsTest {
private Context mTargetContext;
private Instrumentation mInstrumentation;
private NotificationManager mNm;
@Before
public void setUp() {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mTargetContext = mInstrumentation.getTargetContext();
mNm = (NotificationManager) mTargetContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
@Test
public void launchNotificationSetting_displaysChannels() {
NotificationChannelGroup group =
new NotificationChannelGroup(this.getClass().getName(), this.getClass().getName());
group.setDescription("description");
NotificationChannel channel = new NotificationChannel(this.getClass().getName(),
"channel" + this.getClass().getName(), IMPORTANCE_MIN);
channel.setGroup(this.getClass().getName());
NotificationChannel channel2 = new NotificationChannel("2"+this.getClass().getName(),
"2channel" + this.getClass().getName(), IMPORTANCE_MIN);
channel2.setGroup(this.getClass().getName());
mNm.createNotificationChannelGroup(group);
mNm.createNotificationChannel(channel);
mNm.createNotificationChannel(channel2);
final Intent intent = new Intent(Settings.ACTION_CHANNEL_GROUP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_GROUP_ID, group.getId());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText(group.getName().toString()))).check(matches(isDisplayed()));
onView(allOf(withText(channel.getName().toString()))).check(
matches(isDisplayed()));
onView(allOf(withText(group.getDescription().toString()))).check(
matches(isDisplayed()));
onView(allOf(withText(channel2.getName().toString()))).check(
matches(isDisplayed()));
try {
onView(allOf(withText("Android is blocking this group of notifications from"
+ " appearing on this device"))).check(matches(isDisplayed()));
fail("Blocking footer erroneously appearing");
} catch (Exception e) {
// expected
}
}
@Test
public void launchNotificationSettings_blockedGroup() throws Exception {
NotificationChannelGroup blocked =
new NotificationChannelGroup("blocked", "blocked");
NotificationChannel channel =
new NotificationChannel("channel", "channel", IMPORTANCE_HIGH);
channel.setGroup(blocked.getId());
mNm.createNotificationChannelGroup(blocked);
mNm.createNotificationChannel(channel);
INotificationManager sINM = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
blocked.setBlocked(true);
sINM.updateNotificationChannelGroupForPackage(
mTargetContext.getPackageName(), Process.myUid(), blocked);
final Intent intent = new Intent(Settings.ACTION_CHANNEL_GROUP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_GROUP_ID, blocked.getId());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText("Off"), isDisplayed())).check(matches(isDisplayed()));
onView(allOf(withText("Android is blocking this group of notifications from"
+ " appearing on this device"))).check(matches(isDisplayed()));
try {
onView(allOf(withText(channel.getName().toString()))).check(matches(isDisplayed()));
fail("settings appearing for blocked group");
} catch (Exception e) {
// expected
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.fail;
import android.app.INotificationManager;
import android.app.Instrumentation;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.os.ServiceManager;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class ChannelNotificationSettingsTest {
private Context mTargetContext;
private Instrumentation mInstrumentation;
private NotificationChannel mNotificationChannel;
private NotificationManager mNm;
@Before
public void setUp() {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mTargetContext = mInstrumentation.getTargetContext();
mNm = (NotificationManager) mTargetContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationChannel = new NotificationChannel(this.getClass().getName(),
this.getClass().getName(), IMPORTANCE_MIN);
mNm.createNotificationChannel(mNotificationChannel);
}
@Test
public void launchNotificationSetting_shouldNotCrash() {
final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, mNotificationChannel.getId());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText(mNotificationChannel.getName().toString()))).check(
matches(isDisplayed()));
}
@Test
public void launchNotificationSettings_blockedChannel() throws Exception {
NotificationChannel blocked =
new NotificationChannel("blocked", "blocked", IMPORTANCE_NONE);
mNm.createNotificationChannel(blocked);
INotificationManager sINM = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
blocked.setImportance(IMPORTANCE_NONE);
sINM.updateNotificationChannelForPackage(
mTargetContext.getPackageName(), Process.myUid(), blocked);
final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, mTargetContext.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, blocked.getId());
mInstrumentation.startActivitySync(intent);
onView(allOf(withText("Off"), isDisplayed())).check(matches(isDisplayed()));
onView(allOf(withText("Android is blocking this category of notifications from"
+ " appearing on this device"))).check(matches(isDisplayed()));
try {
onView(allOf(withText("On the lock screen"))).check(matches(isDisplayed()));
fail("settings appearing for blocked channel");
} catch (Exception e) {
// expected
}
}
}