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.allapps;
17 
18 import static com.android.launcher3.util.rule.TestStabilityRule.LOCAL;
19 import static com.android.launcher3.util.rule.TestStabilityRule.PLATFORM_POSTSUBMIT;
20 
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.view.KeyEvent;
25 
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.launcher3.Launcher;
30 import com.android.launcher3.LauncherState;
31 import com.android.launcher3.tapl.HomeAllApps;
32 import com.android.launcher3.ui.AbstractLauncherUiTest;
33 import com.android.launcher3.util.rule.TestStabilityRule;
34 import com.android.launcher3.views.ActivityContext;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4.class)
41 public class TaplKeyboardFocusTest extends AbstractLauncherUiTest<Launcher> {
42 
43     @Test
testAllAppsFocusApp()44     public void testAllAppsFocusApp() {
45         final HomeAllApps allApps = mLauncher.goHome().switchToAllApps();
46         assertTrue("Launcher internal state is not All Apps",
47                 isInState(() -> LauncherState.ALL_APPS));
48         allApps.freeze();
49         try {
50             mLauncher.pressAndHoldKeyCode(KeyEvent.KEYCODE_DPAD_DOWN, 0);
51             executeOnLauncher(launcher -> assertNotNull("No focused child.",
52                     launcher.getAppsView().getActiveRecyclerView().getApps().getFocusedChild()));
53         } finally {
54             allApps.unfreeze();
55         }
56     }
57 
58     @Test
testAllAppsExitSearchAndFocusApp()59     public void testAllAppsExitSearchAndFocusApp() {
60         final HomeAllApps allApps = mLauncher.goHome().switchToAllApps();
61         assertTrue("Launcher internal state is not All Apps",
62                 isInState(() -> LauncherState.ALL_APPS));
63         allApps.freeze();
64         try {
65             executeOnLauncher(launcher -> launcher.getAppsView().getSearchView().requestFocus());
66             waitForLauncherCondition("Search view does not have focus.",
67                     launcher -> launcher.getAppsView().getSearchView().hasFocus());
68 
69             mLauncher.pressAndHoldKeyCode(KeyEvent.KEYCODE_DPAD_DOWN, 0);
70             executeOnLauncher(launcher -> assertNotNull("No focused child.",
71                     launcher.getAppsView().getActiveRecyclerView().getApps().getFocusedChild()));
72         } finally {
73             allApps.unfreeze();
74         }
75     }
76 
77     @Test
testAllAppsExitSearchAndFocusSearchResults()78     public void testAllAppsExitSearchAndFocusSearchResults() {
79         final HomeAllApps allApps = mLauncher.goHome().switchToAllApps();
80         assertTrue("Launcher internal state is not All Apps",
81                 isInState(() -> LauncherState.ALL_APPS));
82         allApps.freeze();
83         try {
84             executeOnLauncher(launcher -> launcher.getAppsView().getSearchView().requestFocus());
85             waitForLauncherCondition("Search view does not have focus.",
86                     launcher -> launcher.getAppsView().getSearchView().hasFocus());
87 
88             mLauncher.pressAndHoldKeyCode(KeyEvent.KEYCODE_C, 0);
89             waitForLauncherCondition("Search view not active.",
90                     launcher -> launcher.getAppsView().getActiveRecyclerView()
91                             instanceof SearchRecyclerView);
92             mLauncher.unpressKeyCode(KeyEvent.KEYCODE_C, 0);
93 
94             executeOnLauncher(launcher -> launcher.getAppsView().getSearchUiManager().getEditText()
95                     .hideKeyboard(/* clearFocus= */ false));
96             waitForLauncherCondition("Keyboard still visible.",
97                     ActivityContext::isSoftwareKeyboardHidden);
98 
99             mLauncher.pressAndHoldKeyCode(KeyEvent.KEYCODE_DPAD_DOWN, 0);
100             mLauncher.unpressKeyCode(KeyEvent.KEYCODE_DPAD_DOWN, 0);
101             waitForLauncherCondition("No focused child", launcher ->
102                     launcher.getAppsView().getActiveRecyclerView().getApps().getFocusedChild()
103                             != null);
104         } finally {
105             allApps.unfreeze();
106         }
107     }
108 }
109