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

Commit 94c32eaf authored by Matías Hernández's avatar Matías Hernández
Browse files

Add integration (but non-CTS) tests for some NM Zen behaviors

Fixes: 360862012
Test: atest FrameworksUiServicesTests:android.app.NotificationManagerZenTest
Flag: TEST_ONLY
Change-Id: Ic43a351459029db3123573709e050cbf668b4ebf
parent 78d84698
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ android_test {
    ],

    static_libs: [
        "compatibility-device-util-axt-minus-dexmaker",
        "frameworks-base-testutils",
        "services.accessibility",
        "services.core",
+2 −0
Original line number Diff line number Diff line
@@ -45,6 +45,8 @@
        <provider android:name=".DummyProvider"
            android:authorities="com.android.services.uitests" />

        <activity android:name="android.app.ExampleActivity" />

    </application>

    <instrumentation
+20 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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 android.app;

public class ExampleActivity extends Activity {
}
+284 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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 android.app;

import static android.app.NotificationManager.INTERRUPTION_FILTER_ALL;
import static android.app.NotificationSystemUtil.runAsSystemUi;
import static android.app.NotificationSystemUtil.toggleNotificationPolicyAccess;
import static android.service.notification.Condition.STATE_FALSE;
import static android.service.notification.Condition.STATE_TRUE;

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

import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.service.notification.Condition;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Map;

@RunWith(AndroidJUnit4.class)
public class NotificationManagerZenTest {

    private Context mContext;
    private NotificationManager mNotificationManager;

    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    @Before
    public void setUp() throws Exception {
        mContext = ApplicationProvider.getApplicationContext();
        mNotificationManager = mContext.getSystemService(NotificationManager.class);

        toggleNotificationPolicyAccess(mContext, mContext.getPackageName(), true);
        runAsSystemUi(() -> mNotificationManager.setInterruptionFilter(INTERRUPTION_FILTER_ALL));
        removeAutomaticZenRules();
    }

    @After
    public void tearDown() {
        runAsSystemUi(() -> mNotificationManager.setInterruptionFilter(INTERRUPTION_FILTER_ALL));
        removeAutomaticZenRules();
    }

    private void removeAutomaticZenRules() {
        // Delete AZRs created by this test (query "as app", then delete "as system" so they are
        // not preserved to be restored later).
        Map<String, AutomaticZenRule> rules = mNotificationManager.getAutomaticZenRules();
        runAsSystemUi(() -> {
            for (String ruleId : rules.keySet()) {
                mNotificationManager.removeAutomaticZenRule(ruleId);
            }
        });
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
    public void setAutomaticZenRuleState_manualActivation() {
        AutomaticZenRule ruleToCreate = createZenRule("rule");
        String ruleId = mNotificationManager.addAutomaticZenRule(ruleToCreate);
        Condition manualActivate = new Condition(ruleToCreate.getConditionId(), "manual-on",
                STATE_TRUE, Condition.SOURCE_USER_ACTION);
        Condition manualDeactivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_FALSE, Condition.SOURCE_USER_ACTION);
        Condition autoActivate = new Condition(ruleToCreate.getConditionId(), "auto-on",
                STATE_TRUE);
        Condition autoDeactivate = new Condition(ruleToCreate.getConditionId(), "auto-off",
                STATE_FALSE);

        // User manually activates -> it's active.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualActivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // User manually deactivates -> it's inactive.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualDeactivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // And app can activate and deactivate.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
    public void setAutomaticZenRuleState_manualDeactivation() {
        AutomaticZenRule ruleToCreate = createZenRule("rule");
        String ruleId = mNotificationManager.addAutomaticZenRule(ruleToCreate);
        Condition manualActivate = new Condition(ruleToCreate.getConditionId(), "manual-on",
                STATE_TRUE, Condition.SOURCE_USER_ACTION);
        Condition manualDeactivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_FALSE, Condition.SOURCE_USER_ACTION);
        Condition autoActivate = new Condition(ruleToCreate.getConditionId(), "auto-on",
                STATE_TRUE);
        Condition autoDeactivate = new Condition(ruleToCreate.getConditionId(), "auto-off",
                STATE_FALSE);

        // App activates rule.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // User manually deactivates -> it's inactive.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualDeactivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // User manually reactivates -> it's active.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualActivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // That manual activation removed the override-deactivate, but didn't put an
        // override-activate, so app can deactivate when its natural schedule ends.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
    public void setAutomaticZenRuleState_respectsManuallyActivated() {
        AutomaticZenRule ruleToCreate = createZenRule("rule");
        String ruleId = mNotificationManager.addAutomaticZenRule(ruleToCreate);
        Condition manualActivate = new Condition(ruleToCreate.getConditionId(), "manual-on",
                STATE_TRUE, Condition.SOURCE_USER_ACTION);
        Condition autoActivate = new Condition(ruleToCreate.getConditionId(), "auto-on",
                STATE_TRUE);
        Condition autoDeactivate = new Condition(ruleToCreate.getConditionId(), "auto-off",
                STATE_FALSE);

        // App thinks rule should be inactive.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // Manually activate -> it's active.
        runAsSystemUi(() -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualActivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // App says it should be inactive, but it's ignored.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // App says it should be active. No change now...
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // ... but when the app wants to deactivate next time, it works.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
    public void setAutomaticZenRuleState_respectsManuallyDeactivated() {
        AutomaticZenRule ruleToCreate = createZenRule("rule");
        String ruleId = mNotificationManager.addAutomaticZenRule(ruleToCreate);
        Condition manualDeactivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_FALSE, Condition.SOURCE_USER_ACTION);
        Condition autoActivate = new Condition(ruleToCreate.getConditionId(), "auto-on",
                STATE_TRUE);
        Condition autoDeactivate = new Condition(ruleToCreate.getConditionId(), "auto-off",
                STATE_FALSE);

        // App activates rule.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // User manually deactivates -> it's inactive.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualDeactivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // App says it should be active, but it's ignored.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // App says it should be inactive. No change now...
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // ... but when the app wants to activate next time, it works.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
    public void setAutomaticZenRuleState_manualActivationFromApp() {
        AutomaticZenRule ruleToCreate = createZenRule("rule");
        String ruleId = mNotificationManager.addAutomaticZenRule(ruleToCreate);
        Condition manualActivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_TRUE, Condition.SOURCE_USER_ACTION);
        Condition manualDeactivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_FALSE, Condition.SOURCE_USER_ACTION);
        Condition autoActivate = new Condition(ruleToCreate.getConditionId(), "auto-on",
                STATE_TRUE);
        Condition autoDeactivate = new Condition(ruleToCreate.getConditionId(), "auto-off",
                STATE_FALSE);

        // App activates rule.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // User manually deactivates from SysUI -> it's inactive.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualDeactivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // User manually activates from App -> it's active.
        mNotificationManager.setAutomaticZenRuleState(ruleId, manualActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // And app can automatically deactivate it later.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);
    }

    @Test
    @RequiresFlagsEnabled({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
    public void setAutomaticZenRuleState_manualDeactivationFromApp() {
        AutomaticZenRule ruleToCreate = createZenRule("rule");
        String ruleId = mNotificationManager.addAutomaticZenRule(ruleToCreate);
        Condition manualActivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_TRUE, Condition.SOURCE_USER_ACTION);
        Condition manualDeactivate = new Condition(ruleToCreate.getConditionId(), "manual-off",
                STATE_FALSE, Condition.SOURCE_USER_ACTION);
        Condition autoActivate = new Condition(ruleToCreate.getConditionId(), "auto-on",
                STATE_TRUE);

        // User manually activates from SysUI -> it's active.
        runAsSystemUi(
                () -> mNotificationManager.setAutomaticZenRuleState(ruleId, manualActivate));
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);

        // User manually deactivates from App -> it's inactive.
        mNotificationManager.setAutomaticZenRuleState(ruleId, manualDeactivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE);

        // And app can automatically activate it later.
        mNotificationManager.setAutomaticZenRuleState(ruleId, autoActivate);
        assertThat(mNotificationManager.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE);
    }

    private AutomaticZenRule createZenRule(String name) {
        return createZenRule(name, NotificationManager.INTERRUPTION_FILTER_PRIORITY);
    }

    private AutomaticZenRule createZenRule(String name, int filter) {
        return new AutomaticZenRule(name, null,
                new ComponentName(mContext, ExampleActivity.class),
                new Uri.Builder().scheme("scheme")
                        .appendPath("path")
                        .appendQueryParameter("fake_rule", "fake_value")
                        .build(), null, filter, true);
    }
}
+71 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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 android.app;

import static org.junit.Assert.assertEquals;

import android.Manifest;
import android.content.Context;
import android.os.ParcelFileDescriptor;

import androidx.annotation.NonNull;
import androidx.test.platform.app.InstrumentationRegistry;

import com.android.compatibility.common.util.AmUtils;
import com.android.compatibility.common.util.FileUtils;
import com.android.compatibility.common.util.SystemUtil;
import com.android.compatibility.common.util.ThrowingRunnable;

import java.io.FileInputStream;
import java.io.IOException;

public class NotificationSystemUtil {

    /**
     * Runs a {@link ThrowingRunnable} as the Shell, while adopting SystemUI's permission (as
     * checked by {@code NotificationManagerService#isCallerSystemOrSystemUi}).
     */
    protected static void runAsSystemUi(@NonNull ThrowingRunnable runnable) {
        SystemUtil.runWithShellPermissionIdentity(
                InstrumentationRegistry.getInstrumentation().getUiAutomation(),
                runnable, Manifest.permission.STATUS_BAR_SERVICE);
    }

    static void toggleNotificationPolicyAccess(Context context, String packageName,
            boolean on) throws IOException {

        String command = " cmd notification " + (on ? "allow_dnd " : "disallow_dnd ") + packageName
                + " " + context.getUserId();

        runCommand(command, InstrumentationRegistry.getInstrumentation());
        AmUtils.waitForBroadcastBarrier();

        NotificationManager nm = context.getSystemService(NotificationManager.class);
        assertEquals("Notification Policy Access Grant is "
                + nm.isNotificationPolicyAccessGranted() + " not " + on + " for "
                + packageName, on, nm.isNotificationPolicyAccessGranted());
    }

    private static void runCommand(String command, Instrumentation instrumentation)
            throws IOException {
        UiAutomation uiAutomation = instrumentation.getUiAutomation();
        try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(
                uiAutomation.executeShellCommand(command))) {
            FileUtils.readInputStreamFully(fis);
        }
    }
}