1 /*
2  * Copyright (C) 2020 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.screenshot;
18 
19 import static com.android.systemui.screenshot.ScreenshotController.ACTION_TYPE_SHARE;
20 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ID;
21 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED;
22 import static com.android.systemui.statusbar.phone.CentralSurfaces.SYSTEM_DIALOG_REASON_SCREENSHOT;
23 
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.ArgumentMatchers.isNull;
30 import static org.mockito.Mockito.doAnswer;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.verify;
33 
34 import android.app.PendingIntent;
35 import android.content.Intent;
36 import android.os.Bundle;
37 import android.testing.AndroidTestingRunner;
38 
39 import androidx.test.filters.SmallTest;
40 
41 import com.android.systemui.SysuiTestCase;
42 import com.android.systemui.plugins.ActivityStarter;
43 import com.android.systemui.settings.FakeDisplayTracker;
44 import com.android.systemui.shared.system.ActivityManagerWrapper;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.mockito.stubbing.Answer;
52 
53 import java.util.concurrent.ExecutionException;
54 import java.util.concurrent.TimeoutException;
55 
56 @RunWith(AndroidTestingRunner.class)
57 @SmallTest
58 public class ActionProxyReceiverTest extends SysuiTestCase {
59     @Mock
60     private ActivityManagerWrapper mMockActivityManagerWrapper;
61     @Mock
62     private ScreenshotSmartActions mMockScreenshotSmartActions;
63     @Mock
64     private PendingIntent mMockPendingIntent;
65     @Mock
66     private ActivityStarter mActivityStarter;
67 
68     private Intent mIntent;
69     private FakeDisplayTracker mDisplayTracker = new FakeDisplayTracker(mContext);
70 
71     @Before
setup()72     public void setup() throws InterruptedException, ExecutionException, TimeoutException {
73         MockitoAnnotations.initMocks(this);
74         mIntent = new Intent(mContext, ActionProxyReceiver.class)
75                 .putExtra(ScreenshotController.EXTRA_ACTION_INTENT, mMockPendingIntent);
76     }
77 
78     @Test
testPendingIntentSentWithStatusBar()79     public void testPendingIntentSentWithStatusBar() throws PendingIntent.CanceledException {
80         ActionProxyReceiver actionProxyReceiver = constructActionProxyReceiver();
81         // ensure that the pending intent call is passed through
82         doAnswer((Answer<Object>) invocation -> {
83             ((Runnable) invocation.getArgument(0)).run();
84             return null;
85         }).when(mActivityStarter).executeRunnableDismissingKeyguard(
86                 any(Runnable.class), isNull(), anyBoolean(), anyBoolean(), anyBoolean());
87 
88         actionProxyReceiver.onReceive(mContext, mIntent);
89 
90         verify(mMockActivityManagerWrapper).closeSystemWindows(SYSTEM_DIALOG_REASON_SCREENSHOT);
91         verify(mActivityStarter).executeRunnableDismissingKeyguard(
92                 any(Runnable.class), isNull(), eq(true), eq(true), eq(true));
93         verify(mMockPendingIntent).send(
94                 eq(mContext), anyInt(), isNull(), isNull(), isNull(), isNull(), any(Bundle.class));
95     }
96 
97     @Test
testSmartActionsNotNotifiedByDefault()98     public void testSmartActionsNotNotifiedByDefault() {
99         ActionProxyReceiver actionProxyReceiver = constructActionProxyReceiver();
100 
101         actionProxyReceiver.onReceive(mContext, mIntent);
102 
103         verify(mMockScreenshotSmartActions, never())
104                 .notifyScreenshotAction(anyString(), anyString(), anyBoolean(),
105                         any(Intent.class));
106     }
107 
108     @Test
testSmartActionsNotifiedIfEnabled()109     public void testSmartActionsNotifiedIfEnabled() {
110         ActionProxyReceiver actionProxyReceiver = constructActionProxyReceiver();
111         mIntent.putExtra(EXTRA_SMART_ACTIONS_ENABLED, true);
112         String testId = "testID";
113         mIntent.putExtra(EXTRA_ID, testId);
114 
115         actionProxyReceiver.onReceive(mContext, mIntent);
116 
117         verify(mMockScreenshotSmartActions).notifyScreenshotAction(
118                 testId, ACTION_TYPE_SHARE, false, null);
119     }
120 
constructActionProxyReceiver()121     private ActionProxyReceiver constructActionProxyReceiver() {
122         return new ActionProxyReceiver(
123                 mMockActivityManagerWrapper,
124                 mMockScreenshotSmartActions,
125                 mDisplayTracker,
126                 mActivityStarter
127         );
128     }
129 }
130