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.verifyZeroInteractions;
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 
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.mockito.quality.Strictness;
47 
48 @SmallTest
49 @RunWith(AndroidJUnit4.class)
50 public class KeyboardShortcutsReceiverTest extends SysuiTestCase {
51 
52     private static final Intent SHOW_INTENT = new Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS);
53     private static final Intent DISMISS_INTENT =
54             new Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS);
55 
56     private StaticMockitoSession mockitoSession;
57     private KeyboardShortcutsReceiver mKeyboardShortcutsReceiver;
58     private final FakeFeatureFlags mFeatureFlags = new FakeFeatureFlags();
59 
60     @Mock private KeyboardShortcuts mKeyboardShortcuts;
61     @Mock private KeyboardShortcutListSearch mKeyboardShortcutListSearch;
62 
63     @Before
setUp()64     public void setUp() {
65         MockitoAnnotations.initMocks(this);
66         mSetFlagsRule.disableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
67         mKeyboardShortcuts.mContext = mContext;
68         mKeyboardShortcutListSearch.mContext = mContext;
69         KeyboardShortcuts.sInstance = mKeyboardShortcuts;
70         KeyboardShortcutListSearch.sInstance = mKeyboardShortcutListSearch;
71 
72         mKeyboardShortcutsReceiver = spy(new KeyboardShortcutsReceiver(mFeatureFlags));
73     }
74 
75     @Before
startStaticMocking()76     public void startStaticMocking() {
77         mockitoSession =
78                 ExtendedMockito.mockitoSession()
79                         .spyStatic(Utilities.class)
80                         .strictness(Strictness.LENIENT)
81                         .startMocking();
82     }
83 
84     @After
endStaticMocking()85     public void endStaticMocking() {
86         mockitoSession.finishMocking();
87     }
88 
89     @Test
onReceive_whenFlagOffDeviceIsTablet_showKeyboardShortcuts()90     public void onReceive_whenFlagOffDeviceIsTablet_showKeyboardShortcuts() {
91         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, false);
92         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
93 
94         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
95 
96         verify(mKeyboardShortcuts).showKeyboardShortcuts(anyInt());
97         verify(mKeyboardShortcutListSearch, never()).showKeyboardShortcuts(anyInt());
98     }
99 
100     @Test
onReceive_whenFlagOffDeviceIsNotTablet_showKeyboardShortcuts()101     public void onReceive_whenFlagOffDeviceIsNotTablet_showKeyboardShortcuts() {
102         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, false);
103         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
104 
105         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
106 
107         verify(mKeyboardShortcuts).showKeyboardShortcuts(anyInt());
108         verify(mKeyboardShortcutListSearch, never()).showKeyboardShortcuts(anyInt());
109     }
110 
111     @Test
onReceive_whenFlagOnDeviceIsTablet_showKeyboardShortcutListSearch()112     public void onReceive_whenFlagOnDeviceIsTablet_showKeyboardShortcutListSearch() {
113         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
114         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
115 
116         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
117 
118         verify(mKeyboardShortcuts, never()).showKeyboardShortcuts(anyInt());
119         verify(mKeyboardShortcutListSearch).showKeyboardShortcuts(anyInt());
120     }
121 
122     @Test
onReceive_whenFlagOnDeviceIsNotTablet_showKeyboardShortcuts()123     public void onReceive_whenFlagOnDeviceIsNotTablet_showKeyboardShortcuts() {
124         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
125         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
126 
127         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
128 
129         verify(mKeyboardShortcuts).showKeyboardShortcuts(anyInt());
130         verify(mKeyboardShortcutListSearch, never()).showKeyboardShortcuts(anyInt());
131     }
132 
133     @Test
onShowIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotLaunchOldVersions()134     public void onShowIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotLaunchOldVersions() {
135         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
136         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
137         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
138 
139         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
140 
141         verifyZeroInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
142     }
143 
144     @Test
onShowIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotLaunchOldVersions()145     public void onShowIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotLaunchOldVersions() {
146         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
147         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
148         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
149 
150         mKeyboardShortcutsReceiver.onReceive(mContext, SHOW_INTENT);
151 
152         verifyZeroInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
153     }
154 
155     @Test
onDismissIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotDismissOldVersions()156     public void onDismissIntent_rewriteFlagOn_oldFlagOn_isLargeScreen_doesNotDismissOldVersions() {
157         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
158         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
159         when(Utilities.isLargeScreen(mContext)).thenReturn(true);
160 
161         mKeyboardShortcutsReceiver.onReceive(mContext, DISMISS_INTENT);
162 
163         verifyZeroInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
164     }
165 
166     @Test
onDismissIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotDismissOldVersions()167     public void onDismissIntent_rewriteFlagOn_oldFlagOn_isSmallScreen_doesNotDismissOldVersions() {
168         mFeatureFlags.set(Flags.SHORTCUT_LIST_SEARCH_LAYOUT, true);
169         mSetFlagsRule.enableFlags(FLAG_KEYBOARD_SHORTCUT_HELPER_REWRITE);
170         when(Utilities.isLargeScreen(mContext)).thenReturn(false);
171 
172         mKeyboardShortcutsReceiver.onReceive(mContext, DISMISS_INTENT);
173 
174         verifyZeroInteractions(mKeyboardShortcuts, mKeyboardShortcutListSearch);
175     }
176 }
177