1 /* 2 * Copyright (C) 2019 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.model; 18 19 import static com.android.launcher3.util.LauncherModelHelper.TEST_ACTIVITY; 20 import static com.android.launcher3.util.LauncherModelHelper.TEST_PACKAGE; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assume.assumeTrue; 24 25 import android.content.Context; 26 import android.content.pm.LauncherApps; 27 import android.content.pm.PackageInstaller; 28 import android.content.pm.PackageInstaller.SessionParams; 29 30 import androidx.test.ext.junit.runners.AndroidJUnit4; 31 import androidx.test.filters.SmallTest; 32 33 import com.android.launcher3.LauncherSettings; 34 import com.android.launcher3.icons.BitmapInfo; 35 import com.android.launcher3.model.data.FolderInfo; 36 import com.android.launcher3.model.data.ItemInfo; 37 import com.android.launcher3.util.LauncherLayoutBuilder; 38 import com.android.launcher3.util.LauncherModelHelper; 39 40 import org.junit.After; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 45 /** 46 * Tests for layout parser for remote layout 47 */ 48 @SmallTest 49 @RunWith(AndroidJUnit4.class) 50 public class DefaultLayoutProviderTest { 51 52 private LauncherModelHelper mModelHelper; 53 private Context mTargetContext; 54 55 @Before setUp()56 public void setUp() { 57 mModelHelper = new LauncherModelHelper(); 58 mTargetContext = mModelHelper.sandboxContext; 59 } 60 61 @After tearDown()62 public void tearDown() { 63 mModelHelper.destroy(); 64 } 65 66 @Test testCustomProfileLoaded_with_icon_on_hotseat()67 public void testCustomProfileLoaded_with_icon_on_hotseat() throws Exception { 68 writeLayoutAndLoad(new LauncherLayoutBuilder().atHotseat(0) 69 .putApp(TEST_PACKAGE, TEST_ACTIVITY)); 70 71 // Verify one item in hotseat 72 assertEquals(1, mModelHelper.getBgDataModel().workspaceItems.size()); 73 ItemInfo info = mModelHelper.getBgDataModel().workspaceItems.get(0); 74 assertEquals(LauncherSettings.Favorites.CONTAINER_HOTSEAT, info.container); 75 assertEquals(LauncherSettings.Favorites.ITEM_TYPE_APPLICATION, info.itemType); 76 } 77 78 @Test testCustomProfileLoaded_with_folder()79 public void testCustomProfileLoaded_with_folder() throws Exception { 80 writeLayoutAndLoad(new LauncherLayoutBuilder().atHotseat(0).putFolder(android.R.string.copy) 81 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 82 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 83 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 84 .build()); 85 86 // Verify folder 87 assertEquals(1, mModelHelper.getBgDataModel().workspaceItems.size()); 88 ItemInfo info = mModelHelper.getBgDataModel().workspaceItems.get(0); 89 assertEquals(LauncherSettings.Favorites.ITEM_TYPE_FOLDER, info.itemType); 90 assertEquals(3, ((FolderInfo) info).getContents().size()); 91 } 92 93 @Test testCustomProfileLoaded_with_folder_custom_title()94 public void testCustomProfileLoaded_with_folder_custom_title() throws Exception { 95 writeLayoutAndLoad(new LauncherLayoutBuilder().atHotseat(0).putFolder("CustomFolder") 96 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 97 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 98 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 99 .build()); 100 101 // Verify folder 102 assertEquals(1, mModelHelper.getBgDataModel().workspaceItems.size()); 103 ItemInfo info = mModelHelper.getBgDataModel().workspaceItems.get(0); 104 assertEquals(LauncherSettings.Favorites.ITEM_TYPE_FOLDER, info.itemType); 105 assertEquals(3, ((FolderInfo) info).getContents().size()); 106 assertEquals("CustomFolder", info.title.toString()); 107 } 108 109 @Test testCustomProfileLoaded_with_widget()110 public void testCustomProfileLoaded_with_widget() throws Exception { 111 String pendingAppPkg = "com.test.pending"; 112 113 // Add a placeholder session info so that the widget exists 114 SessionParams params = new SessionParams(SessionParams.MODE_FULL_INSTALL); 115 params.setAppPackageName(pendingAppPkg); 116 params.setAppIcon(BitmapInfo.LOW_RES_ICON); 117 118 PackageInstaller installer = mTargetContext.getPackageManager().getPackageInstaller(); 119 installer.createSession(params); 120 121 writeLayoutAndLoad(new LauncherLayoutBuilder().atWorkspace(0, 1, 0) 122 .putWidget(pendingAppPkg, "PlaceholderWidget", 2, 2)); 123 124 // Verify widget 125 assertEquals(1, mModelHelper.getBgDataModel().appWidgets.size()); 126 ItemInfo info = mModelHelper.getBgDataModel().appWidgets.get(0); 127 assertEquals(LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET, info.itemType); 128 assertEquals(2, info.spanX); 129 assertEquals(2, info.spanY); 130 } 131 132 @Test testCustomProfileLoaded_with_shortcut_on_hotseat()133 public void testCustomProfileLoaded_with_shortcut_on_hotseat() throws Exception { 134 assumeTrue(mTargetContext.getSystemService(LauncherApps.class).hasShortcutHostPermission()); 135 writeLayoutAndLoad(new LauncherLayoutBuilder().atHotseat(0) 136 .putShortcut(TEST_PACKAGE, "shortcut2")); 137 138 // Verify one item in hotseat 139 assertEquals(1, mModelHelper.getBgDataModel().workspaceItems.size()); 140 ItemInfo info = mModelHelper.getBgDataModel().workspaceItems.get(0); 141 assertEquals(LauncherSettings.Favorites.CONTAINER_HOTSEAT, info.container); 142 assertEquals(LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT, info.itemType); 143 } 144 145 @Test testCustomProfileLoaded_with_shortcut_in_folder()146 public void testCustomProfileLoaded_with_shortcut_in_folder() throws Exception { 147 assumeTrue(mTargetContext.getSystemService(LauncherApps.class).hasShortcutHostPermission()); 148 writeLayoutAndLoad(new LauncherLayoutBuilder().atHotseat(0).putFolder(android.R.string.copy) 149 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 150 .addApp(TEST_PACKAGE, TEST_ACTIVITY) 151 .addShortcut(TEST_PACKAGE, "shortcut2") 152 .build()); 153 154 // Verify folder 155 assertEquals(1, mModelHelper.getBgDataModel().workspaceItems.size()); 156 FolderInfo info = (FolderInfo) mModelHelper.getBgDataModel().workspaceItems.get(0); 157 assertEquals(3, info.getContents().size()); 158 159 // Verify last icon 160 assertEquals(LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT, 161 info.getContents().get(info.getContents().size() - 1).itemType); 162 } 163 writeLayoutAndLoad(LauncherLayoutBuilder builder)164 private void writeLayoutAndLoad(LauncherLayoutBuilder builder) throws Exception { 165 mModelHelper.setupDefaultLayoutProvider(builder).loadModelSync(); 166 } 167 } 168