• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.quickstep;
2 
3 import static androidx.test.InstrumentationRegistry.getInstrumentation;
4 
5 import static com.android.launcher3.LauncherState.OVERVIEW;
6 
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertTrue;
10 
11 import android.app.PendingIntent;
12 import android.app.usage.UsageStatsManager;
13 import android.content.Intent;
14 import android.os.Build;
15 
16 import androidx.test.filters.LargeTest;
17 import androidx.test.runner.AndroidJUnit4;
18 
19 import com.android.launcher3.Launcher;
20 import com.android.quickstep.views.DigitalWellBeingToast;
21 import com.android.quickstep.views.RecentsView;
22 import com.android.quickstep.views.TaskView;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 
27 import java.time.Duration;
28 
29 @LargeTest
30 @RunWith(AndroidJUnit4.class)
31 public class DigitalWellBeingToastTest extends AbstractQuickStepTest {
32     private static final String CALCULATOR_PACKAGE =
33             resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR);
34 
35     @Test
testToast()36     public void testToast() throws Exception {
37         // b/150303529
38         if (Build.MODEL.contains("Cuttlefish")) return;
39 
40         startAppFast(CALCULATOR_PACKAGE);
41 
42         final UsageStatsManager usageStatsManager =
43                 mTargetContext.getSystemService(UsageStatsManager.class);
44         final int observerId = 0;
45 
46         try {
47             final String[] packages = new String[]{CALCULATOR_PACKAGE};
48 
49             // Set time limit for app.
50             runWithShellPermission(() ->
51                     usageStatsManager.registerAppUsageLimitObserver(observerId, packages,
52                             Duration.ofSeconds(600), Duration.ofSeconds(300),
53                             PendingIntent.getActivity(mTargetContext, -1, new Intent(), 0)));
54 
55             mLauncher.pressHome();
56             final DigitalWellBeingToast toast = getToast();
57 
58             waitForLauncherCondition("Toast is not visible", launcher -> toast.hasLimit());
59             assertEquals("Toast text: ", "5 minutes left today", toast.getText());
60 
61             // Unset time limit for app.
62             runWithShellPermission(
63                     () -> usageStatsManager.unregisterAppUsageLimitObserver(observerId));
64 
65             mLauncher.pressHome();
66             assertFalse("Toast is visible", getToast().hasLimit());
67         } finally {
68             runWithShellPermission(
69                     () -> usageStatsManager.unregisterAppUsageLimitObserver(observerId));
70         }
71     }
72 
getToast()73     private DigitalWellBeingToast getToast() {
74         executeOnLauncher(launcher -> launcher.getStateManager().goToState(OVERVIEW));
75         waitForState("Launcher internal state didn't switch to Overview", () -> OVERVIEW);
76         final TaskView task = getOnceNotNull("No latest task", launcher -> getLatestTask(launcher));
77 
78         return getFromLauncher(launcher -> {
79             assertTrue("Latest task is not Calculator",
80                     CALCULATOR_PACKAGE.equals(task.getTask().getTopComponent().getPackageName()));
81             return task.getDigitalWellBeingToast();
82         });
83     }
84 
getLatestTask(Launcher launcher)85     private TaskView getLatestTask(Launcher launcher) {
86         return launcher.<RecentsView>getOverviewPanel().getTaskViewAt(0);
87     }
88 
runWithShellPermission(Runnable action)89     private void runWithShellPermission(Runnable action) {
90         getInstrumentation().getUiAutomation().adoptShellPermissionIdentity();
91         try {
92             action.run();
93         } finally {
94             getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
95         }
96 
97     }
98 }
99