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.settings.notification;
18 
19 import static com.android.internal.notification.NotificationAccessConfirmationActivityContract.EXTRA_COMPONENT_NAME;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.robolectric.Shadows.shadowOf;
24 
25 import android.app.Activity;
26 import android.content.ComponentName;
27 import android.content.Intent;
28 import android.content.pm.ApplicationInfo;
29 import android.content.pm.PackageInfo;
30 import android.widget.TextView;
31 
32 import androidx.annotation.Nullable;
33 
34 import com.android.settings.R;
35 
36 import com.google.common.base.Strings;
37 
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.robolectric.Robolectric;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class NotificationAccessConfirmationActivityTest {
46 
47     @Test
start_showsDialog()48     public void start_showsDialog() {
49         ComponentName cn = new ComponentName("com.example", "com.example.SomeService");
50         installPackage(cn.getPackageName(), "X");
51 
52         NotificationAccessConfirmationActivity activity = startActivityWithIntent(cn);
53 
54         assertThat(activity.isFinishing()).isFalse();
55         assertThat(getDialogText(activity)).isEqualTo(
56                 activity.getString(R.string.notification_listener_security_warning_summary, "X"));
57     }
58 
59     @Test
start_withMissingPackage_finishes()60     public void start_withMissingPackage_finishes() {
61         ComponentName cn = new ComponentName("com.example", "com.example.SomeService");
62 
63         NotificationAccessConfirmationActivity activity = startActivityWithIntent(cn);
64 
65         assertThat(getDialogText(activity)).isNull();
66         assertThat(activity.isFinishing()).isTrue();
67     }
68 
69     @Test
start_componentNameTooLong_finishes()70     public void start_componentNameTooLong_finishes() {
71         ComponentName longCn = new ComponentName("com.example", Strings.repeat("Blah", 150));
72         installPackage(longCn.getPackageName(), "<Unused>");
73 
74         NotificationAccessConfirmationActivity activity = startActivityWithIntent(longCn);
75 
76         assertThat(getDialogText(activity)).isNull();
77         assertThat(activity.isFinishing()).isTrue();
78     }
79 
startActivityWithIntent( ComponentName cn)80     private static NotificationAccessConfirmationActivity startActivityWithIntent(
81             ComponentName cn) {
82         return Robolectric.buildActivity(
83                         NotificationAccessConfirmationActivity.class,
84                         new Intent().putExtra(EXTRA_COMPONENT_NAME, cn))
85                 .setup()
86                 .get();
87     }
88 
installPackage(String packageName, String appName)89     private static void installPackage(String packageName, String appName) {
90         PackageInfo pi = new PackageInfo();
91         pi.packageName = packageName;
92         pi.applicationInfo = new ApplicationInfo();
93         pi.applicationInfo.packageName = packageName;
94         pi.applicationInfo.name = appName;
95         shadowOf(RuntimeEnvironment.application.getPackageManager()).installPackage(pi);
96     }
97 
98     @Nullable
getDialogText(Activity activity)99     private static String getDialogText(Activity activity) {
100         TextView tv = activity.getWindow().findViewById(android.R.id.message);
101         CharSequence text = (tv != null ? tv.getText() : null);
102         return text != null ? text.toString() : null;
103     }
104 
105 }
106