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 17 package com.android.launcher3.shortcuts; 18 19 import android.content.pm.ShortcutInfo; 20 import android.support.test.runner.AndroidJUnit4; 21 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 25 import java.util.ArrayList; 26 import java.util.Collections; 27 import java.util.List; 28 29 import static com.android.launcher3.shortcuts.ShortcutFilter.MAX_SHORTCUTS; 30 import static com.android.launcher3.shortcuts.ShortcutFilter.NUM_DYNAMIC; 31 import static org.junit.Assert.assertEquals; 32 import static org.junit.Assert.assertFalse; 33 import static org.junit.Assert.assertTrue; 34 35 /** 36 * Tests the sorting and filtering of shortcuts in {@link ShortcutFilter}. 37 */ 38 @RunWith(AndroidJUnit4.class) 39 public class ShortcutFilterTest { 40 41 @Test 42 public void testSortAndFilterShortcuts() { 43 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(3, 0), 3, 0); 44 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(0, 3), 0, 3); 45 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(5, 0), MAX_SHORTCUTS, 0); 46 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(0, 5), 0, MAX_SHORTCUTS); 47 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(3, 3), 48 MAX_SHORTCUTS - NUM_DYNAMIC, NUM_DYNAMIC); 49 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(5, 5), 50 MAX_SHORTCUTS - NUM_DYNAMIC, NUM_DYNAMIC); 51 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(5, 1), MAX_SHORTCUTS - 1, 1); 52 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(1, 5), 1, MAX_SHORTCUTS - 1); 53 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(5, 3), 54 MAX_SHORTCUTS - NUM_DYNAMIC, NUM_DYNAMIC); 55 filterShortcutsAndAssertNumStaticAndDynamic(createShortcutsList(3, 5), 56 MAX_SHORTCUTS - NUM_DYNAMIC, NUM_DYNAMIC); 57 } 58 59 private void filterShortcutsAndAssertNumStaticAndDynamic( 60 List<ShortcutInfoCompat> shortcuts, int expectedStatic, int expectedDynamic) { 61 Collections.shuffle(shortcuts); 62 List<ShortcutInfoCompat> filteredShortcuts = ShortcutFilter.sortAndFilterShortcuts(shortcuts); 63 assertIsSorted(filteredShortcuts); 64 65 int numStatic = 0; 66 int numDynamic = 0; 67 for (ShortcutInfoCompat shortcut : filteredShortcuts) { 68 if (shortcut.isDeclaredInManifest()) { 69 numStatic++; 70 } 71 if (shortcut.isDynamic()) { 72 numDynamic++; 73 } 74 } 75 assertEquals(expectedStatic, numStatic); 76 assertEquals(expectedDynamic, numDynamic); 77 } 78 79 private void assertIsSorted(List<ShortcutInfoCompat> shortcuts) { 80 int lastStaticRank = -1; 81 int lastDynamicRank = -1; 82 boolean hasSeenDynamic = false; 83 for (ShortcutInfoCompat shortcut : shortcuts) { 84 int rank = shortcut.getRank(); 85 if (shortcut.isDeclaredInManifest()) { 86 assertFalse("Static shortcuts should come before all dynamic shortcuts.", 87 hasSeenDynamic); 88 assertTrue(rank > lastStaticRank); 89 lastStaticRank = rank; 90 } 91 if (shortcut.isDynamic()) { 92 hasSeenDynamic = true; 93 assertTrue(rank > lastDynamicRank); 94 lastDynamicRank = rank; 95 } 96 } 97 } 98 99 private List<ShortcutInfoCompat> createShortcutsList(int numStatic, int numDynamic) { 100 List<ShortcutInfoCompat> shortcuts = new ArrayList<>(); 101 for (int i = 0; i < numStatic; i++) { 102 shortcuts.add(new Shortcut(true, i)); 103 } 104 for (int i = 0; i < numDynamic; i++) { 105 shortcuts.add(new Shortcut(false, i)); 106 } 107 return shortcuts; 108 } 109 110 private class Shortcut extends ShortcutInfoCompat { 111 private boolean mIsStatic; 112 private int mRank; 113 114 public Shortcut(ShortcutInfo shortcutInfo) { 115 super(shortcutInfo); 116 } 117 118 public Shortcut(boolean isStatic, int rank) { 119 this(null); 120 mIsStatic = isStatic; 121 mRank = rank; 122 } 123 124 @Override 125 public boolean isDeclaredInManifest() { 126 return mIsStatic; 127 } 128 129 @Override 130 public boolean isDynamic() { 131 return !mIsStatic; 132 } 133 134 @Override 135 public int getRank() { 136 return mRank; 137 } 138 } 139 }