Home | History | Annotate | Download | only in applications
      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.settingslib.applications;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.content.pm.ApplicationInfo;
     25 
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.junit.runners.JUnit4;
     30 
     31 @RunWith(JUnit4.class)
     32 public class ApplicationsStateTest {
     33     private ApplicationsState.AppEntry mEntry;
     34 
     35     @Before
     36     public void setUp() {
     37         mEntry = mock(ApplicationsState.AppEntry.class);
     38         mEntry.info = mock(ApplicationInfo.class);
     39     }
     40 
     41     @Test
     42     public void testGamesFilterAcceptsGameDeprecated() {
     43         mEntry.info.flags = ApplicationInfo.FLAG_IS_GAME;
     44 
     45         assertThat(ApplicationsState.FILTER_GAMES.filterApp(mEntry)).isTrue();
     46     }
     47 
     48     @Test
     49     public void testGameFilterAcceptsCategorizedGame() {
     50         mEntry.info.category = ApplicationInfo.CATEGORY_GAME;
     51 
     52         assertThat(ApplicationsState.FILTER_GAMES.filterApp(mEntry)).isTrue();
     53     }
     54 
     55     @Test
     56     public void testGameFilterAcceptsCategorizedGameAndDeprecatedIsGame() {
     57         mEntry.info.flags = ApplicationInfo.FLAG_IS_GAME;
     58         mEntry.info.category = ApplicationInfo.CATEGORY_GAME;
     59 
     60         assertThat(ApplicationsState.FILTER_GAMES.filterApp(mEntry)).isTrue();
     61     }
     62 
     63     @Test
     64     public void testGamesFilterRejectsNotGame() {
     65         mEntry.info.category = ApplicationInfo.CATEGORY_UNDEFINED;
     66 
     67         assertThat(ApplicationsState.FILTER_GAMES.filterApp(mEntry)).isFalse();
     68     }
     69 
     70     @Test
     71     public void testAudioFilterAcceptsCategorizedAudio() {
     72         mEntry.info.category = ApplicationInfo.CATEGORY_AUDIO;
     73 
     74         assertThat(ApplicationsState.FILTER_AUDIO.filterApp(mEntry)).isTrue();
     75     }
     76 
     77     @Test
     78     public void testAudiosFilterRejectsNotAudio() {
     79         mEntry.info.category = ApplicationInfo.CATEGORY_GAME;
     80 
     81         assertThat(ApplicationsState.FILTER_AUDIO.filterApp(mEntry)).isFalse();
     82     }
     83 
     84     @Test
     85     public void testAudiosFilterRejectsDefaultCategory() {
     86         mEntry.info.category = ApplicationInfo.CATEGORY_UNDEFINED;
     87 
     88         assertThat(ApplicationsState.FILTER_AUDIO.filterApp(mEntry)).isFalse();
     89     }
     90 
     91     @Test
     92     public void testOtherAppsRejectsAudio() {
     93         mEntry.info.category = ApplicationInfo.CATEGORY_AUDIO;
     94 
     95         assertThat(ApplicationsState.FILTER_OTHER_APPS.filterApp(mEntry)).isFalse();
     96     }
     97 
     98     @Test
     99     public void testOtherAppsRejectsGame() {
    100         mEntry.info.category = ApplicationInfo.CATEGORY_GAME;
    101 
    102         assertThat(ApplicationsState.FILTER_OTHER_APPS.filterApp(mEntry)).isFalse();
    103     }
    104 
    105     @Test
    106     public void testOtherAppsRejectsImageApp() {
    107         mEntry.info.category = ApplicationInfo.CATEGORY_IMAGE;
    108 
    109         assertThat(ApplicationsState.FILTER_OTHER_APPS.filterApp(mEntry)).isFalse();
    110     }
    111 
    112     @Test
    113     public void testOtherAppsAcceptsDefaultCategory() {
    114         mEntry.info.category = ApplicationInfo.CATEGORY_UNDEFINED;
    115 
    116         assertThat(ApplicationsState.FILTER_OTHER_APPS.filterApp(mEntry)).isTrue();
    117     }
    118 
    119     @Test
    120     public void testPhotosFilterAcceptsFilter() {
    121         mEntry.info.category = ApplicationInfo.CATEGORY_IMAGE;
    122 
    123         assertThat(ApplicationsState.FILTER_PHOTOS.filterApp(mEntry)).isTrue();
    124     }
    125 
    126     @Test
    127     public void testPhotosFilterRejectsNotPhotos() {
    128         mEntry.info.category = ApplicationInfo.CATEGORY_VIDEO;
    129 
    130         assertThat(ApplicationsState.FILTER_PHOTOS.filterApp(mEntry)).isFalse();
    131     }
    132 
    133     @Test
    134     public void testPhotosFilterRejectsDefaultCategory() {
    135         mEntry.info.category = ApplicationInfo.CATEGORY_UNDEFINED;
    136 
    137         assertThat(ApplicationsState.FILTER_PHOTOS.filterApp(mEntry)).isFalse();
    138     }
    139 
    140     @Test
    141     public void testDownloadAndLauncherAndInstantAcceptsCorrectApps() {
    142         // should include instant apps
    143         mEntry.isHomeApp = false;
    144         mEntry.hasLauncherEntry = false;
    145         when(mEntry.info.isInstantApp()).thenReturn(true);
    146         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(mEntry))
    147                 .isTrue();
    148 
    149         // should included updated system apps
    150         when(mEntry.info.isInstantApp()).thenReturn(false);
    151         mEntry.info.flags = ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
    152         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(mEntry))
    153                 .isTrue();
    154 
    155         // should not include system apps other than the home app
    156         mEntry.info.flags = ApplicationInfo.FLAG_SYSTEM;
    157         mEntry.isHomeApp = false;
    158         mEntry.hasLauncherEntry = false;
    159         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(mEntry))
    160                 .isFalse();
    161 
    162         // should include the home app
    163         mEntry.isHomeApp = true;
    164         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(mEntry))
    165                 .isTrue();
    166 
    167         // should include any System app with a launcher entry
    168         mEntry.isHomeApp = false;
    169         mEntry.hasLauncherEntry = true;
    170         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(mEntry))
    171                 .isTrue();
    172     }
    173 
    174     @Test
    175     public void testDownloadAndLauncherAcceptsCorrectApps() {
    176         mEntry.isHomeApp = false;
    177         mEntry.hasLauncherEntry = false;
    178 
    179         // should included updated system apps
    180         when(mEntry.info.isInstantApp()).thenReturn(false);
    181         mEntry.info.flags = ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
    182         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER.filterApp(mEntry))
    183                 .isTrue();
    184 
    185         // should not include system apps other than the home app
    186         mEntry.info.flags = ApplicationInfo.FLAG_SYSTEM;
    187         mEntry.isHomeApp = false;
    188         mEntry.hasLauncherEntry = false;
    189         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER.filterApp(mEntry))
    190                 .isFalse();
    191 
    192         // should include the home app
    193         mEntry.isHomeApp = true;
    194         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER.filterApp(mEntry))
    195                 .isTrue();
    196 
    197         // should include any System app with a launcher entry
    198         mEntry.isHomeApp = false;
    199         mEntry.hasLauncherEntry = true;
    200         assertThat(ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER.filterApp(mEntry))
    201                 .isTrue();
    202     }
    203 
    204     @Test
    205     public void testOtherAppsRejectsLegacyGame() {
    206         mEntry.info.flags = ApplicationInfo.FLAG_IS_GAME;
    207 
    208         assertThat(ApplicationsState.FILTER_OTHER_APPS.filterApp(mEntry)).isFalse();
    209     }
    210 
    211     @Test
    212     public void testInstantFilterAcceptsInstantApp() {
    213         when(mEntry.info.isInstantApp()).thenReturn(true);
    214         assertThat(ApplicationsState.FILTER_INSTANT.filterApp(mEntry)).isTrue();
    215     }
    216 
    217     @Test
    218     public void testInstantFilterRejectsNonInstantApp() {
    219         when(mEntry.info.isInstantApp()).thenReturn(false);
    220         assertThat(ApplicationsState.FILTER_INSTANT.filterApp(mEntry)).isFalse();
    221     }
    222 
    223     @Test
    224     public void testEnabledFilterRejectsInstantApp() {
    225         mEntry.info.enabled = true;
    226         assertThat(ApplicationsState.FILTER_ALL_ENABLED.filterApp(mEntry)).isTrue();
    227         when(mEntry.info.isInstantApp()).thenReturn(true);
    228         assertThat(ApplicationsState.FILTER_ALL_ENABLED.filterApp(mEntry)).isFalse();
    229     }
    230 
    231     @Test
    232     public void testFilterWithDomainUrls() {
    233         mEntry.info.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS;
    234         // should included updated system apps
    235         when(mEntry.info.isInstantApp()).thenReturn(false);
    236         assertThat(ApplicationsState.FILTER_WITH_DOMAIN_URLS.filterApp(mEntry))
    237                 .isTrue();
    238         mEntry.info.privateFlags &= ~ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS;
    239         assertThat(ApplicationsState.FILTER_WITH_DOMAIN_URLS.filterApp(mEntry))
    240                 .isFalse();
    241         mEntry.info.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS;
    242         when(mEntry.info.isInstantApp()).thenReturn(true);
    243         assertThat(ApplicationsState.FILTER_WITH_DOMAIN_URLS.filterApp(mEntry))
    244                 .isFalse();
    245     }
    246 
    247     @Test
    248     public void testDisabledFilterRejectsInstantApp() {
    249         mEntry.info.enabled = false;
    250         assertThat(ApplicationsState.FILTER_DISABLED.filterApp(mEntry)).isTrue();
    251         when(mEntry.info.isInstantApp()).thenReturn(true);
    252         assertThat(ApplicationsState.FILTER_DISABLED.filterApp(mEntry)).isFalse();
    253     }
    254 
    255     @Test
    256     public void testVideoFilterAcceptsCategorizedVideo() {
    257         mEntry.info.category = ApplicationInfo.CATEGORY_VIDEO;
    258 
    259         assertThat(ApplicationsState.FILTER_MOVIES.filterApp(mEntry)).isTrue();
    260     }
    261 
    262     @Test
    263     public void testVideosFilterRejectsNotVideo() {
    264         mEntry.info.category = ApplicationInfo.CATEGORY_GAME;
    265 
    266         assertThat(ApplicationsState.FILTER_MOVIES.filterApp(mEntry)).isFalse();
    267     }
    268 }
    269