Home | History | Annotate | Download | only in notification
      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 junit.framework.Assert.assertEquals;
     20 import static junit.framework.Assert.assertFalse;
     21 import static junit.framework.Assert.assertNull;
     22 import static junit.framework.Assert.assertTrue;
     23 
     24 import com.android.settings.notification.NotificationBackend.AppRow;
     25 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     26 
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 
     30 @RunWith(SettingsRobolectricTestRunner.class)
     31 public class NotificationBackendTest {
     32 
     33     @Test
     34     public void testMarkAppRow_unblockablePackage() {
     35         AppRow appRow = new AppRow();
     36         String packageName = "foo.bar.unblockable";
     37         appRow.pkg = packageName;
     38         String[] nonBlockablePkgs = new String[2];
     39         nonBlockablePkgs[0] = packageName;
     40         nonBlockablePkgs[1] = "some.other.package";
     41         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
     42 
     43         // This package has a package lock but no locked channels
     44         assertTrue(appRow.lockedImportance);
     45         assertNull(appRow.lockedChannelId);
     46     }
     47 
     48     @Test
     49     public void testMarkAppRow_unblockableChannelOrPkg() {
     50         String channelBlockName = "foo.bar.pkgWithChannel";
     51         String pkgBlockName = "foo.bar.pkgBlock";
     52         String[] nonBlockablePkgs = new String[2];
     53         nonBlockablePkgs[0] = pkgBlockName;
     54         nonBlockablePkgs[1] = channelBlockName + ":SpecificChannel";
     55 
     56         // This package has a channel level lock but no full package lock
     57         AppRow channelBlockApp = new AppRow();
     58         channelBlockApp.pkg = channelBlockName;
     59         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, channelBlockApp,
     60                 channelBlockName);
     61         assertFalse(channelBlockApp.lockedImportance);
     62         assertEquals("SpecificChannel", channelBlockApp.lockedChannelId);
     63 
     64         // This other package has the reverse
     65         AppRow pkgBlock = new AppRow();
     66         pkgBlock.pkg = pkgBlockName;
     67         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, pkgBlock, pkgBlockName);
     68         assertTrue(pkgBlock.lockedImportance);
     69         assertNull(pkgBlock.lockedChannelId);
     70 
     71         // This third package has no locks at all
     72         AppRow otherAppRow = new AppRow();
     73         otherAppRow.pkg ="foo.bar.nothingBlocked";
     74         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, otherAppRow,
     75                 "foo.bar.nothingBlocked");
     76         assertFalse(otherAppRow.lockedImportance);
     77         assertNull(otherAppRow.lockedChannelId);
     78     }
     79 
     80     @Test
     81     public void testMarkAppRow_unblockableChannelAndPkg() {
     82         AppRow appRow = new AppRow();
     83         String packageName = "foo.bar.unblockable";
     84         appRow.pkg = packageName;
     85         String[] nonBlockablePkgs = new String[2];
     86         nonBlockablePkgs[0] = "foo.bar.unblockable";
     87         nonBlockablePkgs[1] = "foo.bar.unblockable:SpecificChannel";
     88         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
     89 
     90         // This package has both a channel lock and a package lock
     91         assertTrue(appRow.lockedImportance);
     92         assertEquals("SpecificChannel", appRow.lockedChannelId);
     93     }
     94 
     95     @Test
     96     public void testMarkAppRow_channelNameWithColons() {
     97         AppRow appRow = new AppRow();
     98         String packageName = "foo.bar.unblockable";
     99         String channelName = "SpecificChannel:1234:abc:defg";
    100         appRow.pkg = packageName;
    101         String[] nonBlockablePkgs = new String[1];
    102         nonBlockablePkgs[0] = packageName + ":" + channelName;
    103         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
    104 
    105         assertEquals(channelName, appRow.lockedChannelId);
    106     }
    107 
    108     @Test
    109     public void testMarkAppRow_blocklistWithNullEntries() {
    110         AppRow appRow = new AppRow();
    111         String packageName = "foo.bar.unblockable";
    112         appRow.pkg = packageName;
    113         String[] nonBlockablePkgs = new String[6]; // extra long list with some entries left null
    114         nonBlockablePkgs[2] = "foo.bar.unblockable";
    115         nonBlockablePkgs[4] = "foo.bar.unblockable:SpecificChannel";
    116         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
    117 
    118         assertTrue(appRow.lockedImportance);
    119         assertEquals("SpecificChannel", appRow.lockedChannelId);
    120     }
    121 }
    122