Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fae05be2 authored by Yining Liu's avatar Yining Liu
Browse files

Add tests for updating Settings Page for Avalanche Suppression

Add unit tests for the updating of the Settings
Page for the Avalanche
visual and auditory Suppression.

Bug: 330606963
Test: PoliteNotificationGlobalPreferenceControllerTest, PoliteNotifWorkProfileToggleControllerTest
Flag: com.android.server.notification.polite_notifications
Change-Id: I8a84f688eb0b3981fbb0fdb699755b4601583702
parent bf6d9580
Loading
Loading
Loading
Loading
+69 −6
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ package com.android.settings.notification;

import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@@ -27,6 +29,8 @@ import android.os.UserHandle;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;

import androidx.preference.Preference;

import com.android.server.notification.Flags;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.shadow.ShadowSystemSettings;
@@ -50,6 +54,7 @@ public class PoliteNotifWorkProfileToggleControllerTest {

    private Context mContext;
    PoliteNotifWorkProfileToggleController mController;
    private Preference mPreference;
    @Mock
    private AudioHelper mAudioHelper;

@@ -58,12 +63,16 @@ public class PoliteNotifWorkProfileToggleControllerTest {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        when(mAudioHelper.getManagedProfileId(any())).thenReturn(UserHandle.MIN_SECONDARY_USER_ID);
        setCoolDownEnabled(true);
        assertThat(isCoolDownEnabled()).isTrue();
        mController = new PoliteNotifWorkProfileToggleController(mContext, PREFERENCE_KEY,
                mAudioHelper);
        mPreference = new Preference(RuntimeEnvironment.application);
        mPreference.setKey(mController.getPreferenceKey());
    }

    @Test
    public void isAvailable_flagEnabled_workProfileExists_shouldReturnTrue() {
    public void isAvailable_flagEnabled_coolDownEnabled_workProfileExists_shouldReturnTrue() {
        // TODO: b/291907312 - remove feature flags
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        assertThat(mController.isAvailable()).isTrue();
@@ -71,6 +80,17 @@ public class PoliteNotifWorkProfileToggleControllerTest {
                BasePreferenceController.AVAILABLE);
    }

    @Test
    public void isAvailable_flagEnabled_coolDownDisabled_workProfileExists_shouldReturnFalse() {
        // TODO: b/291907312 - remove feature flags
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        setCoolDownEnabled(false);
        assertThat(isCoolDownEnabled()).isFalse();
        assertThat(mController.isAvailable()).isFalse();
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void isAvailable_flagEnabled_workProfileMissing_shouldReturnFalse() {
        // TODO: b/291907312 - remove feature flags
@@ -84,7 +104,7 @@ public class PoliteNotifWorkProfileToggleControllerTest {
    }

    @Test
    public void isAvailable_flagDisabled_shouldReturnFalse() {
    public void isAvailable_coolDownEnabled_flagDisabled_shouldReturnFalse() {
        // TODO: b/291907312 - remove feature flags
        mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        assertThat(mController.isAvailable()).isFalse();
@@ -94,7 +114,7 @@ public class PoliteNotifWorkProfileToggleControllerTest {

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void isChecked_enabledForWorkProfile_shouldReturnTrue() {
    public void isChecked_coolDownEnabled_enabledForWorkProfile_shouldReturnTrue() {
        Settings.System.putIntForUser(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON,
                UserHandle.MIN_SECONDARY_USER_ID);
@@ -103,7 +123,18 @@ public class PoliteNotifWorkProfileToggleControllerTest {

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void isChecked_disabledForWorkProfile_shouldReturnFalse() {
    public void isChecked_coolDownDisabled_enabledForWorkProfile_shouldReturnFalse() {
        setCoolDownEnabled(false);
        assertThat(isCoolDownEnabled()).isFalse();
        Settings.System.putIntForUser(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON,
                UserHandle.MIN_SECONDARY_USER_ID);
        assertThat(mController.isChecked()).isFalse();
    }

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void isChecked_coolDownEnabled_disabledForWorkProfile_shouldReturnFalse() {
        Settings.System.putIntForUser(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF,
                UserHandle.MIN_SECONDARY_USER_ID);
@@ -112,7 +143,7 @@ public class PoliteNotifWorkProfileToggleControllerTest {

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void setChecked_setTrue_shouldEnablePoliteNotifForWorkProfile() {
    public void setChecked_coolDownEnabled_setTrue_shouldEnablePoliteNotifForWorkProfile() {
        Settings.System.putIntForUser(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, OFF,
                UserHandle.MIN_SECONDARY_USER_ID);
@@ -124,7 +155,7 @@ public class PoliteNotifWorkProfileToggleControllerTest {

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void setChecked_setFalse_shouldDisablePoliteNotifForWorkProfile() {
    public void setChecked_coolDownEnabled_setFalse_shouldDisablePoliteNotifForWorkProfile() {
        Settings.System.putIntForUser(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON,
                UserHandle.MIN_SECONDARY_USER_ID);
@@ -133,4 +164,36 @@ public class PoliteNotifWorkProfileToggleControllerTest {
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON,
                UserHandle.MIN_SECONDARY_USER_ID)).isEqualTo(OFF);
    }

    @Test
    public void isVisible_coolDownSetToBeDisabled_shouldReturnFalse() {
        assertThat(mPreference.isVisible()).isTrue();
        setCoolDownEnabled(false);
        assertThat(isCoolDownEnabled()).isFalse();
        mController.updateState(mPreference);
        assertThat(mPreference.isVisible()).isFalse();
    }
    @Test
    public void isVisible_coolDownSetToBeEnabled_shouldReturnTrue() {
        setCoolDownEnabled(false);
        assertThat(isCoolDownEnabled()).isFalse();
        mController.updateState(mPreference);
        assertThat(mPreference.isVisible()).isFalse();

        setCoolDownEnabled(true);
        assertThat(isCoolDownEnabled()).isTrue();
        mController.updateState(mPreference);
        assertThat(mPreference.isVisible()).isTrue();
    }


    private void setCoolDownEnabled(boolean enabled) {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF));
    }

    private boolean isCoolDownEnabled() {
        return Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == ON;
    }
}
+120 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;

import com.android.server.notification.Flags;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.shadow.ShadowSystemSettings;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
public class PoliteNotificationGlobalPreferenceControllerTest {
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private static final String PREFERENCE_KEY = "preference_key";

    private Context mContext;
    PoliteNotificationGlobalPreferenceController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        setCoolDownEnabled(true);
        assertThat(isCoolDownEnabled()).isTrue();
        // TODO: b/291907312 - remove feature flags
        mSetFlagsRule.enableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        mController = new PoliteNotificationGlobalPreferenceController(mContext, PREFERENCE_KEY);
    }

    @Test
    public void isAvailable_flagEnabled_shouldReturnTrue() {
        assertThat(mController.isAvailable()).isTrue();
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.AVAILABLE);
    }

    @Test
    public void isAvailable_flagDisabled_shouldReturnFalse() {
        // TODO: b/291907312 - remove feature flags
        mSetFlagsRule.disableFlags(Flags.FLAG_POLITE_NOTIFICATIONS);
        assertThat(mController.isAvailable()).isFalse();
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void isChecked_coolDownEnabled_shouldReturnTrue() {
        assertThat(mController.isChecked()).isTrue();
    }

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void isChecked_coolDownDisabled_shouldReturnFalse() {
        setCoolDownEnabled(false);
        assertThat(isCoolDownEnabled()).isFalse();
        assertThat(mController.isChecked()).isFalse();
    }

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void setChecked_setTrue_shouldEnableCoolDown() {
        setCoolDownEnabled(false);
        assertThat(isCoolDownEnabled()).isFalse();

        mController.setChecked(true);
        assertThat(isCoolDownEnabled()).isTrue();
    }

    @Test
    @Config(shadows = ShadowSystemSettings.class)
    public void setChecked_setFalse_shouldDisableCoolDown() {
        assertThat(isCoolDownEnabled()).isTrue();

        mController.setChecked(false);
        assertThat(isCoolDownEnabled()).isFalse();
    }

    private void setCoolDownEnabled(boolean enabled) {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF));
    }

    private boolean isCoolDownEnabled() {
        return Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.NOTIFICATION_COOLDOWN_ENABLED, ON) == ON;
    }
}