1 /*
2  * Copyright (C) 2023 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.ui.widget;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21 
22 import androidx.test.ext.junit.runners.AndroidJUnit4;
23 import androidx.test.filters.MediumTest;
24 
25 import com.android.launcher3.Launcher;
26 import com.android.launcher3.tapl.Widgets;
27 import com.android.launcher3.ui.AbstractLauncherUiTest;
28 import com.android.launcher3.ui.PortraitLandscapeRunner.PortraitLandscape;
29 import com.android.launcher3.util.rule.ScreenRecordRule.ScreenRecord;
30 import com.android.launcher3.widget.picker.WidgetsFullSheet;
31 import com.android.launcher3.widget.picker.WidgetsRecyclerView;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 /**
37  * This test run in both Out of process (Oop) and in-process (Ipc).
38  * Make sure the basic interactions with the WidgetPicker works.
39  */
40 @MediumTest
41 @RunWith(AndroidJUnit4.class)
42 public class TaplWidgetPickerTest extends AbstractLauncherUiTest<Launcher> {
43 
getWidgetsView(Launcher launcher)44     private WidgetsRecyclerView getWidgetsView(Launcher launcher) {
45         return WidgetsFullSheet.getWidgetsView(launcher);
46     }
47 
getWidgetsScroll(Launcher launcher)48     private int getWidgetsScroll(Launcher launcher) {
49         return getWidgetsView(launcher).computeVerticalScrollOffset();
50     }
51 
52     /**
53      * Open Widget picker, make sure the widget picker can scroll and then go to home screen.
54      */
55     @Test
56     @ScreenRecord
57     @PortraitLandscape
testWidgets()58     public void testWidgets() {
59         mLauncher.goHome();
60         // Test opening widgets.
61         executeOnLauncher(launcher ->
62                 assertTrue("Widgets is initially opened", getWidgetsView(launcher) == null));
63         Widgets widgets = mLauncher.getWorkspace().openAllWidgets();
64         assertNotNull("openAllWidgets() returned null", widgets);
65         widgets = mLauncher.getAllWidgets();
66         assertNotNull("getAllWidgets() returned null", widgets);
67         executeOnLauncher(launcher ->
68                 assertTrue("Widgets is not shown", getWidgetsView(launcher).isShown()));
69         executeOnLauncher(launcher -> assertEquals("Widgets is scrolled upon opening",
70                 0, getWidgetsScroll(launcher)));
71 
72         // Test flinging widgets.
73         widgets.flingForward();
74         Integer flingForwardY = getFromLauncher(launcher -> getWidgetsScroll(launcher));
75         executeOnLauncher(launcher -> assertTrue("Flinging forward didn't scroll widgets",
76                 flingForwardY > 0));
77 
78         widgets.flingBackward();
79         executeOnLauncher(launcher -> assertTrue("Flinging backward didn't scroll widgets",
80                 getWidgetsScroll(launcher) < flingForwardY));
81 
82         mLauncher.goHome();
83         waitForLauncherCondition("Widgets were not closed",
84                 launcher -> getWidgetsView(launcher) == null);
85     }
86 }
87