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