1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.launcher3.compat;
17 
18 import android.content.pm.PackageInstaller.SessionParams;
19 import android.content.pm.PackageManager;
20 import android.graphics.Bitmap;
21 import android.text.TextUtils;
22 
23 import androidx.test.filters.LargeTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import com.android.launcher3.Launcher;
27 import com.android.launcher3.LauncherState;
28 import com.android.launcher3.Workspace;
29 import com.android.launcher3.ui.AbstractLauncherUiTest;
30 
31 import org.junit.After;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.util.UUID;
36 
37 
38 /**
39  * Test to verify promise icon flow.
40  */
41 @LargeTest
42 @RunWith(AndroidJUnit4.class)
43 public class PromiseIconUiTest extends AbstractLauncherUiTest {
44 
45     private int mSessionId = -1;
46 
47     @Override
setUp()48     public void setUp() throws Exception {
49         super.setUp();
50         mDevice.pressHome();
51         waitForLauncherCondition("Launcher didn't start", launcher -> launcher != null);
52         waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
53         mSessionId = -1;
54     }
55 
56     @After
tearDown()57     public void tearDown() {
58         if (mSessionId > -1) {
59             mTargetContext.getPackageManager().getPackageInstaller().abandonSession(mSessionId);
60         }
61     }
62 
63     /**
64      * Create a session and return the id.
65      */
createSession(String label, Bitmap icon)66     private int createSession(String label, Bitmap icon) throws Throwable {
67         SessionParams params = new SessionParams(SessionParams.MODE_FULL_INSTALL);
68         params.setAppPackageName("test.promise.app");
69         params.setAppLabel(label);
70         params.setAppIcon(icon);
71         params.setInstallReason(PackageManager.INSTALL_REASON_USER);
72         return mTargetContext.getPackageManager().getPackageInstaller().createSession(params);
73     }
74 
75     @Test
testPromiseIcon_addedFromEligibleSession()76     public void testPromiseIcon_addedFromEligibleSession() throws Throwable {
77         final String appLabel = "Test Promise App " + UUID.randomUUID().toString();
78         final Workspace.ItemOperator findPromiseApp = (info, view) ->
79                 info != null && TextUtils.equals(info.title, appLabel);
80 
81         // Create and add test session
82         mSessionId = createSession(appLabel, Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8));
83 
84         // Verify promise icon is added
85         waitForLauncherCondition("Test Promise App not found on workspace", launcher ->
86                 launcher.getWorkspace().getFirstMatch(findPromiseApp) != null);
87 
88         // Remove session
89         mTargetContext.getPackageManager().getPackageInstaller().abandonSession(mSessionId);
90         mSessionId = -1;
91 
92         // Verify promise icon is removed
93         waitForLauncherCondition("Test Promise App not removed from workspace", launcher ->
94                 launcher.getWorkspace().getFirstMatch(findPromiseApp) == null);
95     }
96 
97     @Test
testPromiseIcon_notAddedFromIneligibleSession()98     public void testPromiseIcon_notAddedFromIneligibleSession() throws Throwable {
99         final String appLabel = "Test Promise App " + UUID.randomUUID().toString();
100         final Workspace.ItemOperator findPromiseApp = (info, view) ->
101                 info != null && TextUtils.equals(info.title, appLabel);
102 
103         // Create and add test session without icon or label
104         mSessionId = createSession(null, null);
105 
106         // Sleep for duration of animation if a view was to be added + some buffer time.
107         Thread.sleep(Launcher.NEW_APPS_PAGE_MOVE_DELAY + Launcher.NEW_APPS_ANIMATION_DELAY + 500);
108 
109         // Verify promise icon is not added
110         waitForLauncherCondition("Test Promise App not found on workspace", launcher ->
111                 launcher.getWorkspace().getFirstMatch(findPromiseApp) == null);
112     }
113 }
114