Gentle notifications screen

With lockscreen and status bar settings

Test: robo tests
Bug: 128445911
Change-Id: I46c5943d4d8dc1ee74b5b920239f259f5880a2d3
This commit is contained in:
Julia Reynolds
2019-05-02 22:16:05 -04:00
parent 1c3d7aa1f9
commit 643eb7572f
17 changed files with 790 additions and 12 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2019 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
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/drawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>

View File

@@ -7872,6 +7872,33 @@
<!-- Configure notifications: value for swipe direction [CHAR LIMIT=100] -->
<string name="swipe_direction_rtl">Swipe left to dismiss, right to show menu</string>
<!-- Configure notifications: link to education/gentle notification settings [CHAR LIMIT=100] -->
<string name="gentle_notifications_title">Gentle notifications</string>
<!-- Gentle notifications settings : header for selecting where gentle notifications should appear [CHAR LIMIT=100] -->
<string name="gentle_notifications_also_display">Also display in</string>
<!-- Gentle notifications settings: display in status bar [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_status">Status bar</string>
<!-- Gentle notifications settings: display in lock screen [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_lock">Lock screen</string>
<!-- Gentle notifications footer: what are gentle notification [CHAR LIMIT=NONE] -->
<string name="gentle_notifications_education">Gentle notifications are always silent and always appear in the pull-down shade</string>
<!-- Gentle notifications settings: display only in shade [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_summary_shade">Display in pull-down shade only</string>
<!-- Gentle notifications settings: display in shade and on the lock screen [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_summary_shade_lock">Display in pull-down shade &amp; on lock screen</string>
<!-- Gentle notifications settings: display in shade and on status bar [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_summary_shade_status">Display in pull-down shade &amp; status bar</string>
<!-- Gentle notifications settings: display on all surfaces [CHAR LIMIT=100] -->
<string name="gentle_notifications_display_summary_shade_status_lock">Display in pull-down shade, status bar &amp; on lock screen</string>
<!-- Configure Notifications: Title for the pulse notification light option. [CHAR LIMIT=30] -->
<string name="notification_pulse_title">Blink light</string>

View File

@@ -26,11 +26,11 @@
settings:controller="com.android.settings.widget.VideoPreferenceController"
android:persistent="false" />
<SwitchPreference
android:key="hide_silent_icons"
android:title="@string/hide_silent_icons_title"
android:summary="@string/hide_silent_icons_summary"
settings:controller="com.android.settings.notification.SilentStatusBarPreferenceController" />
<Preference
android:key="gentle_notifications"
android:title="@string/gentle_notifications_title"
settings:controller="com.android.settings.notification.GentleNotificationsPreferenceController"
android:fragment="com.android.settings.notification.GentleNotificationSettings"/>
<PreferenceCategory
android:key="configure_notifications_smart"

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/gentle_notifications_title"
android:key="gentle_notification_settings">
<com.android.settingslib.widget.LayoutPreference
android:key="gentle_notifications_drawable"
android:title="@string/summary_placeholder"
android:layout="@layout/drawable_layout"
settings:controller="com.android.settings.notification.GentleDrawablePreferenceController"
android:order="1"
settings:allowDividerBelow="true"
android:persistent="false" />
<Preference
android:key="gentle_notifications_footer"
android:title="@string/gentle_notifications_education"
android:order="2"
android:icon="@drawable/ic_info_outline_24"
style="?attr/footerPreferenceStyle"
settings:allowDividerAbove="true"
android:clickable="false"
android:selectable="false" />
<PreferenceCategory
android:key="gentle_notifications_settings"
android:title="@string/gentle_notifications_also_display"
android:order="3">
<SwitchPreference
android:key="silent_icons"
android:title="@string/gentle_notifications_display_status"
settings:controller="com.android.settings.notification.SilentStatusBarPreferenceController" />
<SwitchPreference
android:key="lock_screen"
android:title="@string/gentle_notifications_display_lock"
settings:controller="com.android.settings.notification.SilentLockscreenPreferenceController" />
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2019 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 android.content.Context;
import android.provider.Settings;
import android.widget.ImageView;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.LayoutPreference;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
public class GentleDrawablePreferenceController extends BasePreferenceController {
@VisibleForTesting
static final int ON = 1;
private NotificationBackend mBackend;
public GentleDrawablePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mBackend = new NotificationBackend();
}
@VisibleForTesting
void setBackend(NotificationBackend backend) {
mBackend = backend;
}
@Override
public void updateState(Preference preference) {
LayoutPreference pref = (LayoutPreference) preference;
boolean showOnLockscreen = showOnLockscreen();
boolean showOnStatusBar = showOnStatusBar();
ImageView view = pref.findViewById(R.id.drawable);
if (showOnLockscreen) {
if (showOnStatusBar) {
view.setImageResource(R.drawable.gentle_notifications_shade_status_lock);
} else {
view.setImageResource(R.drawable.gentle_notifications_shade_lock);
}
} else if (showOnStatusBar) {
view.setImageResource(R.drawable.gentle_notifications_shade_status);
} else {
view.setImageResource(R.drawable.gentle_notifications_shade);
}
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
private boolean showOnLockscreen() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0) == ON;
}
private boolean showOnStatusBar() {
return !mBackend.shouldHideSilentStatusBarIcons(mContext);
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2019 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 android.app.settings.SettingsEnums;
import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import java.util.Arrays;
import java.util.List;
@SearchIndexable
public class GentleNotificationSettings extends DashboardFragment {
private static final String TAG = "GentleNotiSettings";
@Override
public int getMetricsCategory() {
return SettingsEnums.GENTLE_NOTIFICATIONS_SCREEN;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.gentle_notification_settings;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(SilentStatusBarPreferenceController.class).setListener(
shown -> updatePreferenceStates());
use(SilentLockscreenPreferenceController.class).setListener(
shown -> updatePreferenceStates());
}
/**
* For Search.
*/
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(
Context context, boolean enabled) {
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.gentle_notification_settings;
return Arrays.asList(sir);
}
};
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2019 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.provider.Settings.Secure.NOTIFICATION_BUBBLES;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import androidx.annotation.VisibleForTesting;
public class GentleNotificationsPreferenceController extends BasePreferenceController {
@VisibleForTesting
static final int ON = 1;
private NotificationBackend mBackend;
public GentleNotificationsPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mBackend = new NotificationBackend();
}
@VisibleForTesting
void setBackend(NotificationBackend backend) {
mBackend = backend;
}
@Override
public CharSequence getSummary() {
boolean showOnLockscreen = showOnLockscreen();
boolean showOnStatusBar = showOnStatusBar();
if (showOnLockscreen) {
if (showOnStatusBar) {
return mContext.getString(
R.string.gentle_notifications_display_summary_shade_status_lock);
} else {
return mContext.getString(R.string.gentle_notifications_display_summary_shade_lock);
}
} else if (showOnStatusBar) {
return mContext.getString(R.string.gentle_notifications_display_summary_shade_status);
} else {
return mContext.getString(R.string.gentle_notifications_display_summary_shade);
}
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
private boolean showOnLockscreen() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0) == ON;
}
private boolean showOnStatusBar() {
return !mBackend.shouldHideSilentStatusBarIcons(mContext);
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2019 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.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import com.android.settings.core.TogglePreferenceController;
import com.google.common.annotations.VisibleForTesting;
public class SilentLockscreenPreferenceController extends TogglePreferenceController {
private static final String KEY = "lock_screen";
private Listener mListener;
public SilentLockscreenPreferenceController(Context context) {
super(context, KEY);
}
@Override
public boolean isChecked() {
return Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0) == 1;
}
@Override
public boolean setChecked(boolean isChecked) {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, isChecked ? 1 : 0);
if (mListener != null) {
mListener.onChange(isChecked);
}
return true;
}
@Override
public int getAvailabilityStatus() {
return Settings.Secure.getInt(
mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 1) != 0
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
public void setListener(Listener listener) {
mListener = listener;
}
interface Listener {
void onChange(boolean shown);
}
}

View File

@@ -28,9 +28,10 @@ import com.google.common.annotations.VisibleForTesting;
public class SilentStatusBarPreferenceController extends TogglePreferenceController {
private static final String KEY = "hide_silent_icons";
private static final String KEY = "silent_icons";
private static final int MY_USER_ID = UserHandle.myUserId();
private NotificationBackend mBackend;
private Listener mListener;
public SilentStatusBarPreferenceController(Context context) {
super(context, KEY);
@@ -44,12 +45,15 @@ public class SilentStatusBarPreferenceController extends TogglePreferenceControl
@Override
public boolean isChecked() {
return mBackend.shouldHideSilentStatusBarIcons(mContext);
return !mBackend.shouldHideSilentStatusBarIcons(mContext);
}
@Override
public boolean setChecked(boolean isChecked) {
mBackend.setHideSilentStatusIcons(isChecked);
mBackend.setHideSilentStatusIcons(!isChecked);
if (mListener != null) {
mListener.onChange(isChecked);
}
return true;
}
@@ -60,6 +64,14 @@ public class SilentStatusBarPreferenceController extends TogglePreferenceControl
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
public void setListener(Listener listener) {
mListener = listener;
}
interface Listener {
void onChange(boolean shown);
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright (C) 2019 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import android.widget.ImageView;
import com.android.settings.R;
import com.android.settingslib.widget.LayoutPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import androidx.preference.Preference;
@RunWith(RobolectricTestRunner.class)
public class GentleDrawablePreferenceControllerTest {
private Context mContext;
private GentleDrawablePreferenceController mController;
@Mock
private LayoutPreference mPreference;
@Mock
NotificationBackend mBackend;
@Mock
ImageView mView;
private static final String KEY = "gentle_notifications";
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new GentleDrawablePreferenceController(mContext, KEY);
mController.setBackend(mBackend);
when(mPreference.findViewById(R.id.drawable)).thenReturn(mView);
}
@Test
public void display_shouldDisplay() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_lock() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(true);
mController.updateState(mPreference);
verify(mView).setImageResource(R.drawable.gentle_notifications_shade_lock);
}
@Test
public void updateState_status() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(false);
mController.updateState(mPreference);
verify(mView).setImageResource(R.drawable.gentle_notifications_shade_status);
}
@Test
public void updateState_both() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(false);
mController.updateState(mPreference);
verify(mView).setImageResource(R.drawable.gentle_notifications_shade_status_lock);
}
@Test
public void updateState_neither() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(true);
mController.updateState(mPreference);
verify(mView).setImageResource(R.drawable.gentle_notifications_shade);
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2019 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.provider.Settings.Secure.NOTIFICATION_BUBBLES;
import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF;
import static com.android.settings.notification.BadgingNotificationPreferenceController.ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import androidx.preference.Preference;
@RunWith(RobolectricTestRunner.class)
public class GentleNotificationsPreferenceControllerTest {
private Context mContext;
@Mock
NotificationBackend mBackend;
private GentleNotificationsPreferenceController mController;
private Preference mPreference;
private static final String KEY = "gentle_notifications";
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new GentleNotificationsPreferenceController(mContext, KEY);
mController.setBackend(mBackend);
mPreference = new Preference(mContext);
}
@Test
public void display_shouldDisplay() {
assertThat(mPreference.isVisible()).isTrue();
}
@Test
public void getSummary_lock() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(true);
assertThat(mController.getSummary()).isEqualTo(
mContext.getString(R.string.gentle_notifications_display_summary_shade_lock));
}
@Test
public void getSummary_status() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(false);
assertThat(mController.getSummary()).isEqualTo(
mContext.getString(R.string.gentle_notifications_display_summary_shade_status));
}
@Test
public void getSummary_both() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(false);
assertThat(mController.getSummary()).isEqualTo(mContext.getString(
R.string.gentle_notifications_display_summary_shade_status_lock));
}
@Test
public void getSummary_neither() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
when(mBackend.shouldHideSilentStatusBarIcons(mContext)).thenReturn(true);
assertThat(mController.getSummary()).isEqualTo(
mContext.getString(R.string.gentle_notifications_display_summary_shade));
}
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (C) 2019 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.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
public class SilentLockscreenPreferenceControllerTest {
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private SilentLockscreenPreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new SilentLockscreenPreferenceController(mContext);
mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void isAvailable_featureEnabled() {
Settings.Secure.putInt(
mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 1);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_featureDisabled() {
Settings.Secure.putInt(
mContext.getContentResolver(), NOTIFICATION_NEW_INTERRUPTION_MODEL, 0);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isChecked_settingIsOff_false() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void isChecked_settingIsOn_true() {
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void onPreferenceChange_on() {
mController.onPreferenceChange(mPreference, true);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0)).isEqualTo(1);
}
@Test
public void onPreferenceChange_off() {
mController.onPreferenceChange(mPreference, false);
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 1)).isEqualTo(0);
}
@Test
public void listenerTriggered() {
SilentLockscreenPreferenceController.Listener listener = mock(
SilentLockscreenPreferenceController.Listener.class);
mController.setListener(listener);
mController.setChecked(false);
verify(listener).onChange(false);
mController.setChecked(true);
verify(listener).onChange(true);
}
}

View File

@@ -21,6 +21,7 @@ import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MOD
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -79,26 +80,39 @@ public class SilentStatusBarPreferenceControllerTest {
@Test
public void isChecked_settingIsOff_false() {
when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false);
when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void isChecked_settingIsOn_true() {
when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(true);
when(mBackend.shouldHideSilentStatusBarIcons(any())).thenReturn(false);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void onPreferenceChange_on() {
mController.onPreferenceChange(mPreference, true);
verify(mBackend).setHideSilentStatusIcons(true);
verify(mBackend).setHideSilentStatusIcons(false);
}
@Test
public void onPreferenceChange_off() {
mController.onPreferenceChange(mPreference, false);
verify(mBackend).setHideSilentStatusIcons(false);
verify(mBackend).setHideSilentStatusIcons(true);
}
@Test
public void listenerTriggered() {
SilentStatusBarPreferenceController.Listener listener = mock(
SilentStatusBarPreferenceController.Listener.class);
mController.setListener(listener);
mController.setChecked(false);
verify(listener).onChange(false);
mController.setChecked(true);
verify(listener).onChange(true);
}
}