Home | History | Annotate | Download | only in shortcutmanager
      1 /*
      2  * Copyright (C) 2016 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 package android.content.pm.cts.shortcutmanager;
     17 
     18 import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY;
     19 import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER;
     20 import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC;
     21 import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_MANIFEST;
     22 import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED;
     23 
     24 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
     25 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
     26 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.retryUntil;
     27 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.setDefaultLauncher;
     28 
     29 import android.test.suitebuilder.annotation.SmallTest;
     30 
     31 import com.android.compatibility.common.util.CddTest;
     32 
     33 @CddTest(requirement="3.8.1/C-2-3")
     34 @SmallTest
     35 public class ShortcutManagerMultiLauncherTest extends ShortcutManagerCtsTestsBase {
     36     /**
     37      * Make sure diffrerent launchers will have different set of pinned shortcuts.
     38      */
     39     public void testPinShortcuts() {
     40         runWithCaller(mPackageContext1, () -> {
     41             enableManifestActivity("Launcher_manifest_1", true);
     42             enableManifestActivity("Launcher_manifest_2", true);
     43 
     44             retryUntil(() -> getManager().getManifestShortcuts().size() == 3,
     45                     "Manifest shortcuts didn't show up");
     46 
     47             assertTrue(getManager().setDynamicShortcuts(list(
     48                     makeShortcut("s1"),
     49                     makeShortcut("s2"),
     50                     makeShortcut("s3"),
     51                     makeShortcut("s4"),
     52                     makeShortcut("s5")
     53             )));
     54             assertWith(getManager().getDynamicShortcuts())
     55                     .haveIds("s1", "s2", "s3", "s4", "s5")
     56                     .areAllDynamic()
     57                     .areAllEnabled();
     58             assertWith(getManager().getManifestShortcuts())
     59                     .haveIds("ms1", "ms21", "ms22")
     60                     .areAllManifest()
     61                     .areAllEnabled();
     62         });
     63         runWithCaller(mPackageContext2, () -> {
     64             enableManifestActivity("Launcher_manifest_1", true);
     65             enableManifestActivity("Launcher_manifest_3", true);
     66 
     67             retryUntil(() -> getManager().getManifestShortcuts().size() == 3,
     68                     "Manifest shortcuts didn't show up");
     69 
     70             assertTrue(getManager().setDynamicShortcuts(list(
     71                     makeShortcut("s1"),
     72                     makeShortcut("s2"),
     73                     makeShortcut("s3"),
     74                     makeShortcut("s4"),
     75                     makeShortcut("s5")
     76             )));
     77             assertWith(getManager().getDynamicShortcuts())
     78                     .haveIds("s1", "s2", "s3", "s4", "s5")
     79                     .areAllDynamic()
     80                     .areAllEnabled();
     81             assertWith(getManager().getManifestShortcuts())
     82                     .haveIds("ms1", "ms31", "ms32")
     83                     .areAllManifest()
     84                     .areAllEnabled();
     85         });
     86         setDefaultLauncher(getInstrumentation(), mLauncherContext1);
     87 
     88         runWithCaller(mLauncherContext1, () -> {
     89             getLauncherApps().pinShortcuts(
     90                     mPackageContext1.getPackageName(),
     91                     list("s1", "s2", "s3", "ms1", "ms21"), getUserHandle());
     92             getLauncherApps().pinShortcuts(
     93                     mPackageContext2.getPackageName(),
     94                     list("s2", "s3", "ms31"), getUserHandle());
     95         });
     96 
     97         setDefaultLauncher(getInstrumentation(), mLauncherContext2);
     98 
     99         runWithCaller(mLauncherContext2, () -> {
    100             getLauncherApps().pinShortcuts(
    101                     mPackageContext1.getPackageName(),
    102                     list("s3", "s4", "ms22"), getUserHandle());
    103             getLauncherApps().pinShortcuts(
    104                     mPackageContext2.getPackageName(),
    105                     list("s1", "s2", "s3", "ms32"), getUserHandle());
    106         });
    107 
    108         runWithCaller(mPackageContext1, () -> {
    109             assertWith(getManager().getDynamicShortcuts())
    110                     .haveIds("s1", "s2", "s3", "s4", "s5")
    111                     .areAllDynamic()
    112                     .areAllEnabled();
    113             assertWith(getManager().getManifestShortcuts())
    114                     .haveIds("ms1", "ms21", "ms22")
    115                     .areAllManifest()
    116                     .areAllEnabled();
    117             assertWith(getManager().getPinnedShortcuts())
    118                     .haveIds("s1", "s2", "s3", "s4", "ms1", "ms21", "ms22")
    119                     .areAllEnabled();
    120 
    121             // Then remove some
    122             getManager().removeDynamicShortcuts(list("s1", "s2", "s5"));
    123             getManager().disableShortcuts(list("s4"));
    124 
    125             enableManifestActivity("Launcher_manifest_1", false);
    126             retryUntil(() -> getManager().getManifestShortcuts().size() == 2,
    127                     "Manifest shortcuts didn't show up");
    128 
    129             assertWith(getManager().getDynamicShortcuts())
    130                     .haveIds("s3")
    131                     .areAllDynamic()
    132                     .areAllEnabled();
    133             assertWith(getManager().getManifestShortcuts())
    134                     .haveIds("ms21", "ms22")
    135                     .areAllManifest()
    136                     .areAllEnabled();
    137             assertWith(getManager().getPinnedShortcuts())
    138                     .haveIds("s1", "s2", "s3", "s4", "ms1", "ms21", "ms22")
    139                     ;
    140 
    141         });
    142         runWithCaller(mPackageContext2, () -> {
    143             assertWith(getManager().getDynamicShortcuts())
    144                     .haveIds("s1", "s2", "s3", "s4", "s5")
    145                     .areAllDynamic()
    146                     .areAllEnabled();
    147             assertWith(getManager().getManifestShortcuts())
    148                     .haveIds("ms1", "ms31", "ms32")
    149                     .areAllManifest()
    150                     .areAllEnabled();
    151             assertWith(getManager().getPinnedShortcuts())
    152                     .haveIds("s1", "s2", "s3", "ms31", "ms32")
    153                     .areAllEnabled();
    154 
    155             // Then remove some
    156             getManager().removeDynamicShortcuts(list("s1", "s2", "s3", "s4"));
    157 
    158             enableManifestActivity("Launcher_manifest_3", false);
    159             retryUntil(() -> getManager().getManifestShortcuts().size() == 1,
    160                     "Manifest shortcuts didn't show up");
    161 
    162             assertWith(getManager().getDynamicShortcuts())
    163                     .haveIds("s5")
    164                     .areAllDynamic()
    165                     .areAllEnabled();
    166             assertWith(getManager().getManifestShortcuts())
    167                     .haveIds("ms1")
    168                     .areAllManifest()
    169                     .areAllEnabled();
    170             assertWith(getManager().getPinnedShortcuts())
    171                     .haveIds("s1", "s2", "s3", "ms31", "ms32")
    172                     ;
    173         });
    174 
    175         setDefaultLauncher(getInstrumentation(), mLauncherContext1);
    176 
    177         runWithCaller(mLauncherContext1, () -> {
    178             // For package 1.
    179             assertWith(getShortcutsAsLauncher(
    180                     FLAG_MATCH_DYNAMIC | FLAG_GET_KEY_FIELDS_ONLY,
    181                     mPackageContext1.getPackageName()))
    182                     .haveIds("s3")
    183                     .areAllEnabled();
    184             assertWith(getShortcutsAsLauncher(
    185                     FLAG_MATCH_MANIFEST | FLAG_GET_KEY_FIELDS_ONLY,
    186                     mPackageContext1.getPackageName()))
    187                     .haveIds("ms21", "ms22")
    188                     .areAllEnabled();
    189             assertWith(getShortcutsAsLauncher(
    190                     FLAG_MATCH_PINNED | FLAG_GET_KEY_FIELDS_ONLY,
    191                     mPackageContext1.getPackageName()))
    192                     .haveIds("s1", "s2", "s3", "ms1", "ms21")
    193 
    194                     .selectByIds("s1", "s2", "s3", "ms21")
    195                     .areAllEnabled()
    196 
    197                     .revertToOriginalList()
    198                     .selectByIds("ms1")
    199                     .areAllDisabled();
    200 
    201             // For package 2.
    202             assertWith(getShortcutsAsLauncher(
    203                     FLAG_MATCH_DYNAMIC | FLAG_GET_KEY_FIELDS_ONLY,
    204                     mPackageContext2.getPackageName()))
    205                     .haveIds("s5")
    206                     .areAllEnabled();
    207             assertWith(getShortcutsAsLauncher(
    208                     FLAG_MATCH_MANIFEST | FLAG_GET_KEY_FIELDS_ONLY,
    209                     mPackageContext2.getPackageName()))
    210                     .haveIds("ms1")
    211                     .areAllEnabled();
    212             assertWith(getShortcutsAsLauncher(
    213                     FLAG_MATCH_PINNED | FLAG_GET_KEY_FIELDS_ONLY,
    214                     mPackageContext2.getPackageName()))
    215                     .haveIds("s2", "s3", "ms31")
    216 
    217                     .selectByIds("s2", "s3")
    218                     .areAllEnabled()
    219 
    220                     .revertToOriginalList()
    221                     .selectByIds("ms31")
    222                     .areAllDisabled();
    223         });
    224 
    225         setDefaultLauncher(getInstrumentation(), mLauncherContext2);
    226 
    227         runWithCaller(mLauncherContext2, () -> {
    228             // For package 1.
    229             assertWith(getShortcutsAsLauncher(
    230                     FLAG_MATCH_DYNAMIC | FLAG_GET_KEY_FIELDS_ONLY,
    231                     mPackageContext1.getPackageName()))
    232                     .haveIds("s3")
    233                     .areAllEnabled();
    234             assertWith(getShortcutsAsLauncher(
    235                     FLAG_MATCH_MANIFEST | FLAG_GET_KEY_FIELDS_ONLY,
    236                     mPackageContext1.getPackageName()))
    237                     .haveIds("ms21", "ms22")
    238                     .areAllEnabled();
    239             assertWith(getShortcutsAsLauncher(
    240                     FLAG_MATCH_PINNED | FLAG_GET_KEY_FIELDS_ONLY,
    241                     mPackageContext1.getPackageName()))
    242                     .haveIds("s3", "s4", "ms22")
    243 
    244                     .selectByIds("s3", "ms22")
    245                     .areAllEnabled()
    246 
    247                     .revertToOriginalList()
    248                     .selectByIds("s4")
    249                     .areAllDisabled();
    250 
    251             // For package 2.
    252             assertWith(getShortcutsAsLauncher(
    253                     FLAG_MATCH_DYNAMIC | FLAG_GET_KEY_FIELDS_ONLY,
    254                     mPackageContext2.getPackageName()))
    255                     .haveIds("s5")
    256                     .areAllEnabled();
    257             assertWith(getShortcutsAsLauncher(
    258                     FLAG_MATCH_MANIFEST | FLAG_GET_KEY_FIELDS_ONLY,
    259                     mPackageContext2.getPackageName()))
    260                     .haveIds("ms1")
    261                     .areAllEnabled();
    262             assertWith(getShortcutsAsLauncher(
    263                     FLAG_MATCH_PINNED | FLAG_GET_KEY_FIELDS_ONLY,
    264                     mPackageContext2.getPackageName()))
    265                     .haveIds("s1", "s2", "s3", "ms32")
    266 
    267                     .selectByIds("s1", "s2", "s3")
    268                     .areAllEnabled()
    269 
    270                     .revertToOriginalList()
    271                     .selectByIds("ms32")
    272                     .areAllDisabled();
    273 
    274             // Make sure "ALL_PINNED" doesn't work without the permission.
    275             assertWith(getShortcutsAsLauncher(
    276                     FLAG_MATCH_PINNED | FLAG_GET_KEY_FIELDS_ONLY
    277                             | FLAG_MATCH_PINNED_BY_ANY_LAUNCHER,
    278                     mPackageContext1.getPackageName()))
    279                     .haveIds("s3", "s4", "ms22");
    280             assertWith(getShortcutsAsLauncher(
    281                     FLAG_MATCH_PINNED | FLAG_GET_KEY_FIELDS_ONLY
    282                             | FLAG_MATCH_PINNED_BY_ANY_LAUNCHER,
    283                     mPackageContext2.getPackageName()))
    284                     .haveIds("s1", "s2", "s3", "ms32");
    285         });
    286     }
    287 }
    288