1 /*
2  * Copyright (C) 2024 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 
17 package com.android.launcher3;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.content.Intent;
23 import android.platform.test.annotations.LargeTest;
24 import android.view.KeyEvent;
25 
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import com.android.launcher3.allapps.ActivityAllAppsContainerView;
29 import com.android.launcher3.allapps.SearchRecyclerView;
30 import com.android.launcher3.ui.AbstractLauncherUiTest;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 @LargeTest
36 @RunWith(AndroidJUnit4.class)
37 public class LauncherIntentTest extends AbstractLauncherUiTest<Launcher> {
38 
39     public final Intent allAppsIntent = new Intent(Intent.ACTION_ALL_APPS);
40 
41     @Test
testAllAppsIntent()42     public void testAllAppsIntent() {
43         // Try executing ALL_APPS intent
44         executeOnLauncher(launcher -> launcher.onNewIntent(allAppsIntent));
45         // A-Z view with Main adapter should be loaded
46         assertOnMainAdapterAToZView();
47 
48 
49         // Try Moving to search view now
50         moveToSearchView();
51         // Try executing ALL_APPS intent
52         executeOnLauncher(launcher -> launcher.onNewIntent(allAppsIntent));
53         // A-Z view with Main adapter should be loaded
54         assertOnMainAdapterAToZView();
55     }
56 
57     // Highlights the search bar, then fills text to display the SearchView.
moveToSearchView()58     private void moveToSearchView() {
59         // All Apps view should be loaded
60         assertTrue("Launcher internal state is not All Apps",
61                 isInState(() -> LauncherState.ALL_APPS));
62         executeOnLauncher(launcher -> launcher.getAppsView().getSearchView().requestFocus());
63         // Search view should be in focus
64         waitForLauncherCondition("Search view is not in focus.",
65                 launcher -> launcher.getAppsView().getSearchView().hasFocus());
66         mLauncher.pressAndHoldKeyCode(KeyEvent.KEYCODE_C, 0);
67         // Upon key press, search recycler view should be loaded
68         waitForLauncherCondition("Search view not active.",
69                 launcher -> launcher.getAppsView().getActiveRecyclerView()
70                         instanceof SearchRecyclerView);
71         mLauncher.unpressKeyCode(KeyEvent.KEYCODE_C, 0);
72     }
73 
74     // Checks if main adapter view is selected, search bar is out of focus and scroller is at start.
assertOnMainAdapterAToZView()75     private void assertOnMainAdapterAToZView() {
76         // All Apps State should be loaded
77         assertTrue("Launcher internal state is not All Apps",
78                 isInState(() -> LauncherState.ALL_APPS));
79 
80         // A-Z recycler view should be active.
81         waitForLauncherCondition("A-Z view not active.",
82                 launcher -> !(launcher.getAppsView().getActiveRecyclerView()
83                         instanceof SearchRecyclerView));
84         // Personal Adapter should be selected.
85         waitForLauncherCondition("Not on Main Adapter View",
86                 launcher -> launcher.getAppsView().getCurrentPage()
87                         == ActivityAllAppsContainerView.AdapterHolder.MAIN);
88         // Search view should not be in focus
89         waitForLauncherCondition("Search view has focus.",
90                 launcher -> !launcher.getAppsView().getSearchView().hasFocus());
91         // Scroller should be at top
92         executeOnLauncher(launcher -> assertEquals(
93                 "All Apps started in already scrolled state", 0,
94                 getAllAppsScroll(launcher)));
95     }
96 }
97