1 /* 2 * Copyright (C) 2020 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 com.android.launcher3.secondarydisplay; 17 18 import static com.android.launcher3.util.LauncherModelHelper.TEST_PACKAGE; 19 import static com.android.launcher3.util.LauncherUIHelper.doLayout; 20 import static com.android.launcher3.util.Preconditions.assertNotNull; 21 22 import static org.junit.Assert.assertNotSame; 23 import static org.junit.Assert.assertTrue; 24 25 import android.content.Context; 26 import android.os.UserManager; 27 import android.provider.Settings; 28 29 import com.android.launcher3.InvariantDeviceProfile; 30 import com.android.launcher3.allapps.AllAppsPagedView; 31 import com.android.launcher3.allapps.AllAppsRecyclerView; 32 import com.android.launcher3.util.LauncherLayoutBuilder; 33 import com.android.launcher3.util.LauncherModelHelper; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.robolectric.Robolectric; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.LooperMode; 42 import org.robolectric.annotation.LooperMode.Mode; 43 import org.robolectric.shadow.api.Shadow; 44 import org.robolectric.shadows.ShadowUserManager; 45 46 /** 47 * Tests for {@link SecondaryDisplayLauncher} with work profile 48 */ 49 @RunWith(RobolectricTestRunner.class) 50 @LooperMode(Mode.PAUSED) 51 public class SDWorkModeTest { 52 53 private static final int SYSTEM_USER = 0; 54 private static final int FLAG_SYSTEM = 0x00000800; 55 private static final int WORK_PROFILE_ID = 10; 56 private static final int FLAG_PROFILE = 0x00001000; 57 58 private Context mTargetContext; 59 private InvariantDeviceProfile mIdp; 60 private LauncherModelHelper mModelHelper; 61 62 @Before setup()63 public void setup() throws Exception { 64 mModelHelper = new LauncherModelHelper(); 65 mTargetContext = RuntimeEnvironment.application; 66 mIdp = InvariantDeviceProfile.INSTANCE.get(mTargetContext); 67 Settings.Global.putFloat(mTargetContext.getContentResolver(), 68 Settings.Global.WINDOW_ANIMATION_SCALE, 0); 69 70 mModelHelper.installApp(TEST_PACKAGE); 71 } 72 73 @Test testAllAppsList_noWorkProfile()74 public void testAllAppsList_noWorkProfile() throws Exception { 75 SecondaryDisplayLauncher launcher = loadLauncher(); 76 launcher.showAppDrawer(true); 77 doLayout(launcher); 78 79 verifyRecyclerViewCount(launcher.getAppsView().getActiveRecyclerView()); 80 } 81 82 @Test testAllAppsList_workProfile()83 public void testAllAppsList_workProfile() throws Exception { 84 ShadowUserManager sum = Shadow.extract(mTargetContext.getSystemService(UserManager.class)); 85 sum.addUser(SYSTEM_USER, "me", FLAG_SYSTEM); 86 sum.addProfile(SYSTEM_USER, WORK_PROFILE_ID, "work", FLAG_PROFILE); 87 88 SecondaryDisplayLauncher launcher = loadLauncher(); 89 launcher.showAppDrawer(true); 90 doLayout(launcher); 91 92 AllAppsRecyclerView rv1 = launcher.getAppsView().getActiveRecyclerView(); 93 verifyRecyclerViewCount(rv1); 94 95 assertNotNull(launcher.getAppsView().getWorkModeSwitch()); 96 assertTrue(launcher.getAppsView().getRecyclerViewContainer() instanceof AllAppsPagedView); 97 98 AllAppsPagedView pagedView = 99 (AllAppsPagedView) launcher.getAppsView().getRecyclerViewContainer(); 100 pagedView.snapToPageImmediately(1); 101 doLayout(launcher); 102 103 AllAppsRecyclerView rv2 = launcher.getAppsView().getActiveRecyclerView(); 104 verifyRecyclerViewCount(rv2); 105 assertNotSame(rv1, rv2); 106 } 107 loadLauncher()108 private SecondaryDisplayLauncher loadLauncher() throws Exception { 109 // Install 100 apps 110 for (int i = 0; i < 100; i++) { 111 mModelHelper.installApp(TEST_PACKAGE + i); 112 } 113 mModelHelper.setupDefaultLayoutProvider(new LauncherLayoutBuilder()).loadModelSync(); 114 SecondaryDisplayLauncher launcher = 115 Robolectric.buildActivity(SecondaryDisplayLauncher.class).setup().get(); 116 doLayout(launcher); 117 return launcher; 118 } 119 verifyRecyclerViewCount(AllAppsRecyclerView rv)120 private void verifyRecyclerViewCount(AllAppsRecyclerView rv) { 121 int childCount = rv.getChildCount(); 122 assertTrue(childCount > 0); 123 assertTrue(childCount < 100); 124 } 125 } 126