1 package com.android.settings.notification;
2 
3 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
4 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
5 import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
6 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;
7 
8 import static com.google.common.truth.Truth.assertThat;
9 
10 import static org.robolectric.Robolectric.buildActivity;
11 
12 import android.content.ContentResolver;
13 import android.content.Intent;
14 import android.content.pm.UserInfo;
15 import android.os.UserHandle;
16 import android.os.UserManager;
17 import android.provider.Settings;
18 import android.view.View;
19 import android.widget.RadioButton;
20 
21 import com.android.settings.R;
22 import com.android.settings.RestrictedRadioButton;
23 import com.android.settings.notification.RedactionInterstitial.RedactionInterstitialFragment;
24 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
25 import com.android.settings.testutils.shadow.ShadowUtils;
26 
27 import org.junit.After;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 import org.robolectric.RuntimeEnvironment;
33 import org.robolectric.annotation.Config;
34 import org.robolectric.shadow.api.Shadow;
35 import org.robolectric.shadows.ShadowUserManager;
36 
37 @RunWith(RobolectricTestRunner.class)
38 @Config(shadows = {
39         ShadowUtils.class,
40         ShadowRestrictedLockUtilsInternal.class,
41 })
42 public class RedactionInterstitialTest {
43     private RedactionInterstitial mActivity;
44     private RedactionInterstitialFragment mFragment;
45 
46     @After
tearDown()47     public void tearDown() {
48         ShadowRestrictedLockUtilsInternal.reset();
49     }
50 
51     @Test
primaryUserDefaultStateTest()52     public void primaryUserDefaultStateTest() {
53         setupSettings(1 /* show */, 1 /* showUnredacted */);
54         setupActivity();
55 
56         assertHideAllVisible(true);
57         assertEnabledButtons(true /* all */, true /* redact */);
58         assertSelectedButton(R.id.show_all);
59     }
60 
61     @Test
primaryUserRedactSensitiveTest()62     public void primaryUserRedactSensitiveTest() {
63         setupSettings(1 /* show */, 0 /* showUnredacted */);
64         setupActivity();
65 
66         assertHideAllVisible(true);
67         assertEnabledButtons(true /* all */, true /* redact */);
68         assertSelectedButton(R.id.redact_sensitive);
69     }
70 
71     @Test
primaryUserHideAllTest()72     public void primaryUserHideAllTest() {
73         setupSettings(0 /* show */, 0 /* showUnredacted */);
74         setupActivity();
75 
76         assertHideAllVisible(true);
77         assertEnabledButtons(true /* all */, true /* redact */);
78         assertSelectedButton(R.id.hide_all);
79     }
80 
81     @Test
primaryUserUnredactedRestrictionTest()82     public void primaryUserUnredactedRestrictionTest() {
83         setupSettings(1 /* show */, 1 /* showUnredacted */);
84         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
85                 KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
86         setupActivity();
87 
88         assertHideAllVisible(true);
89         assertEnabledButtons(false /* all */, true /* redact */);
90         assertSelectedButton(R.id.redact_sensitive);
91     }
92 
93     @Test
primaryUserNotificationRestrictionTest()94     public void primaryUserNotificationRestrictionTest() {
95         setupSettings(1 /* show */, 1 /* showUnredacted */);
96         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
97                 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
98         setupActivity();
99 
100         assertHideAllVisible(true);
101         assertEnabledButtons(false /* all */, false /* redact */);
102         assertSelectedButton(R.id.hide_all);
103     }
104 
105     @Test
managedProfileNoRestrictionsTest()106     public void managedProfileNoRestrictionsTest() {
107         setupSettings(1 /* show */, 1 /* showUnredacted */);
108         final ShadowUserManager sum =
109                 Shadow.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
110         sum.addProfile(
111                 UserHandle.myUserId(), UserHandle.myUserId(),
112                 "work-profile"/* profileName */, UserInfo.FLAG_MANAGED_PROFILE);
113         setupActivity();
114 
115         assertHideAllVisible(false);
116         assertEnabledButtons(true /* all */, true /* redact */);
117         assertSelectedButton(R.id.show_all);
118     }
119 
120     @Test
managedProfileUnredactedRestrictionTest()121     public void managedProfileUnredactedRestrictionTest() {
122         setupSettings(1 /* show */, 1 /* showUnredacted */);
123         final ShadowUserManager sum =
124                 Shadow.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
125         sum.addProfile(
126                 UserHandle.myUserId(), UserHandle.myUserId(),
127                 "work-profile"/* profileName */, UserInfo.FLAG_MANAGED_PROFILE);
128         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
129                 KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
130         setupActivity();
131 
132         assertHideAllVisible(false);
133         assertEnabledButtons(false /* all */, true /* redact */);
134         assertSelectedButton(R.id.redact_sensitive);
135     }
136 
setupActivity()137     private void setupActivity() {
138         mActivity = buildActivity(RedactionInterstitial.class, new Intent()).setup().get();
139         mFragment = (RedactionInterstitialFragment)
140                 mActivity.getSupportFragmentManager().findFragmentById(R.id.main_content);
141         assertThat(mActivity).isNotNull();
142         assertThat(mFragment).isNotNull();
143     }
144 
setupSettings(int show, int showUnredacted)145     private void setupSettings(int show, int showUnredacted) {
146         final ContentResolver resolver = RuntimeEnvironment.application.getContentResolver();
147         Settings.Secure.putIntForUser(resolver,
148                 LOCK_SCREEN_SHOW_NOTIFICATIONS, show, UserHandle.myUserId());
149         Settings.Secure.putIntForUser(resolver,
150                 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, showUnredacted, UserHandle.myUserId());
151     }
152 
assertHideAllVisible(boolean visible)153     private void assertHideAllVisible(boolean visible) {
154         Assert.assertEquals(visible, getButton(R.id.hide_all).getVisibility() != View.GONE);
155     }
156 
assertEnabledButtons(boolean all, boolean redact)157     private void assertEnabledButtons(boolean all, boolean redact) {
158         Assert.assertEquals(all, buttonEnabled(R.id.show_all));
159         Assert.assertEquals(redact, buttonEnabled(R.id.redact_sensitive));
160     }
161 
assertSelectedButton(int resId)162     private void assertSelectedButton(int resId) {
163         Assert.assertEquals(resId == R.id.show_all, buttonChecked(R.id.show_all));
164         Assert.assertEquals(resId == R.id.redact_sensitive, buttonChecked(R.id.redact_sensitive));
165         Assert.assertEquals(resId == R.id.hide_all, buttonChecked(R.id.hide_all));
166     }
167 
buttonChecked(int resource)168     private boolean buttonChecked(int resource) {
169         return getButton(resource).isChecked();
170     }
171 
buttonEnabled(int resource)172     private boolean buttonEnabled(int resource) {
173         return !((RestrictedRadioButton) getButton(resource)).isDisabledByAdmin();
174     }
175 
getButton(int resource)176     private RadioButton getButton(int resource) {
177         return (RadioButton) mFragment.getView().findViewById(resource);
178     }
179 }
180