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.logcat;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.view.View;
27 
28 import androidx.test.core.app.ActivityScenario;
29 import androidx.test.ext.junit.rules.ActivityScenarioRule;
30 import androidx.test.filters.LargeTest;
31 import androidx.test.platform.app.InstrumentationRegistry;
32 
33 import com.android.internal.app.ILogAccessDialogCallback;
34 import com.android.systemui.SysuiTestCase;
35 import com.android.systemui.res.R;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 
41 import java.util.concurrent.CountDownLatch;
42 import java.util.concurrent.TimeUnit;
43 
44 @LargeTest
45 public class LogAccessDialogActivityTest extends SysuiTestCase {
46 
47     public static final String EXTRA_CALLBACK = "EXTRA_CALLBACK";
48     private final DialogCallbackTestable mDialogCallback = new DialogCallbackTestable();
49     @Rule
50     public ActivityScenarioRule<DialogTestable> mActivityScenarioRule =
51             new ActivityScenarioRule<>(getIntent());
52 
53     static final class DialogCallbackTestable extends ILogAccessDialogCallback.Stub {
54 
55         int mNumOfApprove = 0;
56         int mNumOfDecline = 0;
57         CountDownLatch mCountDownLatch = new CountDownLatch(1);
58 
59         @Override
approveAccessForClient(int i, String s)60         public void approveAccessForClient(int i, String s) {
61             mNumOfApprove++;
62             mCountDownLatch.countDown();
63         }
64 
65         @Override
declineAccessForClient(int i, String s)66         public void declineAccessForClient(int i, String s) {
67             mNumOfDecline++;
68             mCountDownLatch.countDown();
69         }
70     }
71 
72     @Before
setUp()73     public void setUp() {
74         mDialogCallback.mNumOfDecline = 0;
75         mDialogCallback.mNumOfApprove = 0;
76         mDialogCallback.mCountDownLatch = new CountDownLatch(1);
77     }
78 
getIntent()79     private Intent getIntent() {
80         Context context = InstrumentationRegistry.getInstrumentation().getContext();
81         final Intent intent = new Intent(context, DialogTestable.class);
82 
83         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
84         intent.putExtra(Intent.EXTRA_PACKAGE_NAME, "packageName");
85         intent.putExtra(Intent.EXTRA_UID, 1);
86 
87         intent.putExtra(EXTRA_CALLBACK, mDialogCallback.asBinder());
88         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
89 
90         return intent;
91     }
92 
93     @Test
test_dialogDisappear_withoutClick_autoDeclined()94     public void test_dialogDisappear_withoutClick_autoDeclined() throws InterruptedException {
95         ActivityScenario<DialogTestable> activityScenario = mActivityScenarioRule.getScenario();
96         activityScenario.onActivity(Activity::finish);
97 
98         assertTrue(mDialogCallback.mCountDownLatch.await(2, TimeUnit.SECONDS));
99         assertEquals(mDialogCallback.mNumOfDecline, 1);
100         assertEquals(mDialogCallback.mNumOfApprove, 0);
101 
102     }
103 
104     @Test
test_clickAllow()105     public void test_clickAllow() throws InterruptedException {
106         ActivityScenario<DialogTestable> activityScenario = mActivityScenarioRule.getScenario();
107 
108         activityScenario.onActivity(activity -> {
109             View allowButton =
110                     activity.mAlertView.findViewById(R.id.log_access_dialog_allow_button);
111             assertNotNull(allowButton);
112             allowButton.performClick();
113         });
114 
115         assertTrue(mDialogCallback.mCountDownLatch.await(10, TimeUnit.SECONDS));
116         assertEquals(mDialogCallback.mNumOfDecline, 0);
117         assertEquals(mDialogCallback.mNumOfApprove, 1);
118     }
119 
120     @Test
test_clickDeny()121     public void test_clickDeny() throws InterruptedException {
122         ActivityScenario<DialogTestable> activityScenario = mActivityScenarioRule.getScenario();
123 
124         activityScenario.onActivity(activity -> {
125             View denyButton =
126                     activity.mAlertView.findViewById(R.id.log_access_dialog_deny_button);
127             assertNotNull(denyButton);
128             denyButton.performClick();
129         });
130 
131         assertTrue(mDialogCallback.mCountDownLatch.await(10, TimeUnit.SECONDS));
132         assertEquals(mDialogCallback.mNumOfDecline, 1);
133         assertEquals(mDialogCallback.mNumOfApprove, 0);
134     }
135 
136     public static class DialogTestable extends LogAccessDialogActivity {
137 
138         @Override
getTitleString(Context context, String callingPackage, int uid)139         protected String getTitleString(Context context, String callingPackage, int uid) {
140             return "DialogTitle";
141         }
142     }
143 }
144