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 com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicShortcutCountExceeded; 19 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith; 20 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list; 21 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.retryUntil; 22 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.setDefaultLauncher; 23 24 import android.test.suitebuilder.annotation.SmallTest; 25 26 import com.android.compatibility.common.util.CddTest; 27 28 @CddTest(requirement="3.8.1/C-4-1") 29 @SmallTest 30 public class ShortcutManagerMaxCountTest extends ShortcutManagerCtsTestsBase { 31 /** 32 * Basic tests: single app, single activity, no manifest shortcuts. 33 */ 34 public void testNumDynamicShortcuts() { 35 runWithCallerWithStrictMode(mPackageContext1, () -> { 36 assertTrue(getManager().setDynamicShortcuts(list(makeShortcut("s1")))); 37 assertTrue(getManager().setDynamicShortcuts(list( 38 makeShortcut("s1"), 39 makeShortcut("s2"), 40 makeShortcut("s3"), 41 makeShortcut("s4"), 42 makeShortcut("s5") 43 ))); 44 assertWith(getManager().getDynamicShortcuts()) 45 .haveIds("s1", "s2", "s3", "s4", "s5") 46 .areAllDynamic() 47 .areAllEnabled(); 48 assertTrue(getManager().setDynamicShortcuts(list( 49 makeShortcut("s1x"), 50 makeShortcut("s2x"), 51 makeShortcut("s3x"), 52 makeShortcut("s4x"), 53 makeShortcut("s5x") 54 ))); 55 56 assertDynamicShortcutCountExceeded(() -> { 57 getManager().setDynamicShortcuts(list( 58 makeShortcut("s1y"), 59 makeShortcut("s2y"), 60 makeShortcut("s3y"), 61 makeShortcut("s4y"), 62 makeShortcut("s5y"), 63 makeShortcut("s6y"))); 64 }); 65 assertWith(getManager().getDynamicShortcuts()) 66 .haveIds("s1x", "s2x", "s3x", "s4x", "s5x") 67 .areAllDynamic() 68 .areAllEnabled(); 69 70 assertDynamicShortcutCountExceeded(() -> { 71 getManager().addDynamicShortcuts(list( 72 makeShortcut("s1y"))); 73 }); 74 assertWith(getManager().getDynamicShortcuts()) 75 .haveIds("s1x", "s2x", "s3x", "s4x", "s5x") 76 .areAllDynamic() 77 .areAllEnabled(); 78 getManager().removeDynamicShortcuts(list("s5x")); 79 assertTrue(getManager().addDynamicShortcuts(list( 80 makeShortcut("s1y")))); 81 82 assertWith(getManager().getDynamicShortcuts()) 83 .haveIds("s1x", "s2x", "s3x", "s4x", "s1y") 84 .areAllDynamic() 85 .areAllEnabled(); 86 87 getManager().removeAllDynamicShortcuts(); 88 89 assertTrue(getManager().addDynamicShortcuts(list( 90 makeShortcut("s1"), 91 makeShortcut("s2"), 92 makeShortcut("s3"), 93 makeShortcut("s4"), 94 makeShortcut("s5") 95 ))); 96 assertWith(getManager().getDynamicShortcuts()) 97 .haveIds("s1", "s2", "s3", "s4", "s5") 98 .areAllDynamic() 99 .areAllEnabled(); 100 }); 101 } 102 103 /** 104 * Manifest shortcuts are included in the count too. 105 */ 106 public void testWithManifest() throws Exception { 107 runWithCallerWithStrictMode(mPackageContext1, () -> { 108 enableManifestActivity("Launcher_manifest_1", true); 109 enableManifestActivity("Launcher_manifest_2", true); 110 111 retryUntil(() -> getManager().getManifestShortcuts().size() == 3, 112 "Manifest shortcuts didn't show up"); 113 114 }); 115 116 runWithCallerWithStrictMode(mPackageContext1, () -> { 117 assertWith(getManager().getManifestShortcuts()) 118 .haveIds("ms1", "ms21", "ms22") 119 .areAllManifest() 120 .areAllEnabled() 121 .areAllNotPinned() 122 123 .selectByIds("ms1") 124 .forAllShortcuts(sa -> { 125 assertEquals(getActivity("Launcher_manifest_1"), sa.getActivity()); 126 }) 127 128 .revertToOriginalList() 129 .selectByIds("ms21", "ms22") 130 .forAllShortcuts(sa -> { 131 assertEquals(getActivity("Launcher_manifest_2"), sa.getActivity()); 132 }); 133 134 }); 135 136 // Note since max counts is per activity, testNumDynamicShortcuts_single should just pass. 137 testNumDynamicShortcuts(); 138 139 // Launcher_manifest_1 has one manifest, so can only add 4 dynamic shortcuts. 140 runWithCallerWithStrictMode(mPackageContext1, () -> { 141 setTargetActivityOverride("Launcher_manifest_1"); 142 143 assertTrue(getManager().setDynamicShortcuts(list( 144 makeShortcut("s1"), 145 makeShortcut("s2"), 146 makeShortcut("s3"), 147 makeShortcut("s4") 148 ))); 149 assertWith(getManager().getDynamicShortcuts()) 150 .selectByActivity(getActivity("Launcher_manifest_1")) 151 .haveIds("s1", "s2", "s3", "s4") 152 .areAllEnabled(); 153 154 assertDynamicShortcutCountExceeded(() -> getManager().setDynamicShortcuts(list( 155 makeShortcut("s1x"), 156 makeShortcut("s2x"), 157 makeShortcut("s3x"), 158 makeShortcut("s4x"), 159 makeShortcut("s5x") 160 ))); 161 // Not changed. 162 assertWith(getManager().getDynamicShortcuts()) 163 .selectByActivity(getActivity("Launcher_manifest_1")) 164 .haveIds("s1", "s2", "s3", "s4") 165 .areAllEnabled(); 166 }); 167 168 // Launcher_manifest_2 has two manifests, so can only add 3. 169 runWithCallerWithStrictMode(mPackageContext1, () -> { 170 setTargetActivityOverride("Launcher_manifest_2"); 171 172 assertTrue(getManager().addDynamicShortcuts(list( 173 makeShortcut("s1"), 174 makeShortcut("s2"), 175 makeShortcut("s3") 176 ))); 177 assertWith(getManager().getDynamicShortcuts()) 178 .selectByActivity(getActivity("Launcher_manifest_2")) 179 .haveIds("s1", "s2", "s3") 180 .areAllEnabled(); 181 182 assertDynamicShortcutCountExceeded(() -> getManager().addDynamicShortcuts(list( 183 makeShortcut("s1x") 184 ))); 185 // Not added. 186 assertWith(getManager().getDynamicShortcuts()) 187 .selectByActivity(getActivity("Launcher_manifest_2")) 188 .haveIds("s1", "s2", "s3") 189 .areAllEnabled(); 190 }); 191 } 192 193 public void testChangeActivity() { 194 runWithCallerWithStrictMode(mPackageContext1, () -> { 195 setTargetActivityOverride("Launcher"); 196 assertTrue(getManager().setDynamicShortcuts(list( 197 makeShortcut("s1"), 198 makeShortcut("s2"), 199 makeShortcut("s3"), 200 makeShortcut("s4"), 201 makeShortcut("s5") 202 ))); 203 assertWith(getManager().getDynamicShortcuts()) 204 .selectByActivity(getActivity("Launcher")) 205 .haveIds("s1", "s2", "s3", "s4", "s5") 206 .areAllDynamic() 207 .areAllEnabled(); 208 209 setTargetActivityOverride("Launcher2"); 210 211 assertTrue(getManager().addDynamicShortcuts(list( 212 makeShortcut("s1b"), 213 makeShortcut("s2b"), 214 makeShortcut("s3b"), 215 makeShortcut("s4b"), 216 makeShortcut("s5b") 217 ))); 218 assertWith(getManager().getDynamicShortcuts()) 219 .selectByActivity(getActivity("Launcher")) 220 .haveIds("s1", "s2", "s3", "s4", "s5") 221 .areAllDynamic() 222 .areAllEnabled() 223 224 .revertToOriginalList() 225 .selectByActivity(getActivity("Launcher2")) 226 .haveIds("s1b", "s2b", "s3b", "s4b", "s5b") 227 .areAllDynamic() 228 .areAllEnabled(); 229 230 // Moving one from L1 to L2 is not allowed. 231 assertDynamicShortcutCountExceeded(() -> getManager().updateShortcuts(list( 232 makeShortcut("s1", getActivity("Launcher2")) 233 ))); 234 235 assertWith(getManager().getDynamicShortcuts()) 236 .selectByActivity(getActivity("Launcher")) 237 .haveIds("s1", "s2", "s3", "s4", "s5") 238 .areAllDynamic() 239 .areAllEnabled() 240 241 .revertToOriginalList() 242 .selectByActivity(getActivity("Launcher2")) 243 .haveIds("s1b", "s2b", "s3b", "s4b", "s5b") 244 .areAllDynamic() 245 .areAllEnabled(); 246 247 // But swapping shortcuts will work. 248 assertTrue(getManager().updateShortcuts(list( 249 makeShortcut("s1", getActivity("Launcher2")), 250 makeShortcut("s1b", getActivity("Launcher")) 251 ))); 252 253 assertWith(getManager().getDynamicShortcuts()) 254 .selectByActivity(getActivity("Launcher")) 255 .haveIds("s1b", "s2", "s3", "s4", "s5") 256 .areAllDynamic() 257 .areAllEnabled() 258 259 .revertToOriginalList() 260 .selectByActivity(getActivity("Launcher2")) 261 .haveIds("s1", "s2b", "s3b", "s4b", "s5b") 262 .areAllDynamic() 263 .areAllEnabled(); 264 }); 265 } 266 267 public void testWithPinned() { 268 runWithCallerWithStrictMode(mPackageContext1, () -> { 269 assertTrue(getManager().setDynamicShortcuts(list( 270 makeShortcut("s1"), 271 makeShortcut("s2"), 272 makeShortcut("s3"), 273 makeShortcut("s4"), 274 makeShortcut("s5") 275 ))); 276 }); 277 278 setDefaultLauncher(getInstrumentation(), mLauncherContext1); 279 280 runWithCallerWithStrictMode(mLauncherContext1, () -> { 281 getLauncherApps().pinShortcuts(mPackageContext1.getPackageName(), 282 list("s1", "s2", "s3", "s4", "s5"), getUserHandle()); 283 }); 284 285 runWithCallerWithStrictMode(mPackageContext1, () -> { 286 assertTrue(getManager().setDynamicShortcuts(list( 287 makeShortcut("s6"), 288 makeShortcut("s7"), 289 makeShortcut("s8"), 290 makeShortcut("s9"), 291 makeShortcut("s10") 292 ))); 293 294 assertWith(getManager().getDynamicShortcuts()) 295 .haveIds("s6", "s7", "s8", "s9", "s10") 296 .areAllEnabled() 297 .areAllNotPinned(); 298 299 assertWith(getManager().getPinnedShortcuts()) 300 .haveIds("s1", "s2", "s3", "s4", "s5") 301 .areAllEnabled() 302 .areAllNotDynamic(); 303 }); 304 } 305 } 306