• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.systemui.statusbar;
18 
19 import static com.android.systemui.Flags.FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE;
20 
21 import static org.mockito.Mockito.anyInt;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Intent;
29 
30 import androidx.test.ext.junit.runners.AndroidJUnit4;
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.dx.mockito.inline.extended.ExtendedMockito;
34 import com.android.dx.mockito.inline.extended.StaticMockitoSession;
35 import com.android.systemui.SysuiTestCase;
36 import com.android.systemui.flags.FakeFeatureFlags;
37 import com.android.systemui.flags.Flags;
38 import com.android.systemui.shared.recents.utilities.Utilities;
39 import com.android.systemui.utils.windowmanager.WindowManagerProvider;
40 
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.mockito.quality.Strictness;
48 
49 @SmallTest
50 @RunWith(AndroidJUnit4.class)
51 public class KeyboardShortcutsReceiverTest extends SysuiTestCase {
52 
53     private static final Intent SHOW_INTENT = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
54     private static final Intent DISMISS_INTENT =
55             new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
56 
57     private StaticMockitoSession mockitoSession;
58     private KeyboardShortcutsReceiver mKeyboardShortcutsReceiver;
59     private final FakeFeatureFlags mFeatureFlags = new FakeFeatureFlags();
60 
61     @Mock private KeyboardShortcuts mKeyboardShortcuts;
62     @Mock private KeyboardShortcutListSearch mKeyboardShortcutListSearch;
63     @Mock private WindowManagerProvider mWindowManagerProvider;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mSetFlagsRule.disableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
69         mKeyboardShortcuts.mContext = mContext;
70         mKeyboardShortcutListSearch.mContext = mContext;
71         KeyboardShortcuts.sInstance = mKeyboardShortcuts;
72         KeyboardShortcutListSearch.sInstance = mKeyboardShortcutListSearch;
73 
74         mKeyboardShortcutsReceiver = spy(new KeyboardShortcutsReceiver(mFeatureFlags,
75                 mWindowManagerProvider));
76     }
77 
78     @Before
startStaticMocking()79     public void startStaticMocking() {
80         mockitoSession =
81                 ExtendedMockito.mockitoSession()
82                         .spyStatic(Utilities.class)
83                         .strictness(Strictness.LENIENT)
84                         .startMocking();
85     }
86 
87     @After
endStaticMocking()88     public void endStaticMocking() {
89         mockitoSession.finishMocking();
90     }
91 
92     @Test
onReceive_whenFlagOffDeviceIsTablet_showKeyboardShortcuts()93     public void onReceive_whenFlagOffDeviceIsTablet_showKeyboardShortcuts() {
94         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, false);
95         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
96 
97         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
98 
99         verify(mKeyboardShortcuts).showKeyboardShortcuts(anyInt());
100         verify(mKeyboardShortcutListSearch, never()).showKeyboardShortcuts(anyInt());
101     }
102 
103     @Test
onReceive_whenFlagOffDeviceIsNotTablet_showKeyboardShortcuts()104     public void onReceive_whenFlagOffDeviceIsNotTablet_showKeyboardShortcuts() {
105         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, false);
106         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
107 
108         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
109 
110         verify(mKeyboardShortcuts).showKeyboardShortcuts(anyInt());
111         verify(mKeyboardShortcutListSearch, never()).showKeyboardShortcuts(anyInt());
112     }
113 
114     @Test
onReceive_whenFlagOnDeviceIsTablet_showKeyboardShortcutListSearch()115     public void onReceive_whenFlagOnDeviceIsTablet_showKeyboardShortcutListSearch() {
116         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
117         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
118 
119         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
120 
121         verify(mKeyboardShortcuts, never()).showKeyboardShortcuts(anyInt());
122         verify(mKeyboardShortcutListSearch).showKeyboardShortcuts(anyInt());
123     }
124 
125     @Test
onReceive_whenFlagOnDeviceIsNotTablet_showKeyboardShortcuts()126     public void onReceive_whenFlagOnDeviceIsNotTablet_showKeyboardShortcuts() {
127         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
128         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
129 
130         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
131 
132         verify(mKeyboardShortcuts).showKeyboardShortcuts(anyInt());
133         verify(mKeyboardShortcutListSearch, never()).showKeyboardShortcuts(anyInt());
134     }
135 
136     @Test
onShowIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotLaunchOldVersions()137     public void onShowIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotLaunchOldVersions() {
138         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
139         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
140         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
141 
142         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
143 
144         verifyNoMoreInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
145     }
146 
147     @Test
onShowIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotLaunchOldVersions()148     public void onShowIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotLaunchOldVersions() {
149         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
150         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
151         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
152 
153         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
154 
155         verifyNoMoreInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
156     }
157 
158     @Test
onDismissIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotDismissOldVersions()159     public void onDismissIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotDismissOldVersions() {
160         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
161         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
162         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
163 
164         mKeyboardShortcutsReceiver.onReceive(mContext, DISMISS_INTENT);
165 
166         verifyNoMoreInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
167     }
168 
169     @Test
onDismissIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotDismissOldVersions()170     public void onDismissIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotDismissOldVersions() {
171         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
172         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
173         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
174 
175         mKeyboardShortcutsReceiver.onReceive(mContext, DISMISS_INTENT);
176 
177         verifyNoMoreInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
178     }
179 }
180