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.SettingsShadowResources;
25 import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
26 import com.android.settings.testutils.shadow.ShadowUtils;
27 
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.robolectric.RobolectricTestRunner;
33 import org.robolectric.RuntimeEnvironment;
34 import org.robolectric.annotation.Config;
35 import org.robolectric.shadow.api.Shadow;
36 import org.robolectric.shadows.ShadowUserManager;
37 
38 @RunWith(RobolectricTestRunner.class)
39 @Config(shadows = {
40         ShadowUtils.class,
41         ShadowRestrictedLockUtilsInternal.class,
42         SettingsShadowResources.class,
43 })
44 public class RedactionInterstitialTest {
45     private RedactionInterstitial mActivity;
46     private RedactionInterstitialFragment mFragment;
47 
48     @After
tearDown()49     public void tearDown() {
50         ShadowRestrictedLockUtilsInternal.reset();
51     }
52 
53     @Test
primaryUserDefaultStateTest()54     public void primaryUserDefaultStateTest() {
55         setupSettings(1 /* show */, 1 /* showUnredacted */);
56         setupActivity();
57 
58         assertHideAllVisible(true);
59         assertEnabledButtons(true /* all */, true /* redact */);
60         assertSelectedButton(R.id.show_all);
61     }
62 
63     @Test
primaryUserRedactSensitiveTest()64     public void primaryUserRedactSensitiveTest() {
65         setupSettings(1 /* show */, 0 /* showUnredacted */);
66         setupActivity();
67 
68         assertHideAllVisible(true);
69         assertEnabledButtons(true /* all */, true /* redact */);
70         assertSelectedButton(R.id.redact_sensitive);
71     }
72 
73     @Test
primaryUserHideAllTest()74     public void primaryUserHideAllTest() {
75         setupSettings(0 /* show */, 0 /* showUnredacted */);
76         setupActivity();
77 
78         assertHideAllVisible(true);
79         assertEnabledButtons(true /* all */, true /* redact */);
80         assertSelectedButton(R.id.hide_all);
81     }
82 
83     @Test
primaryUserUnredactedRestrictionTest()84     public void primaryUserUnredactedRestrictionTest() {
85         setupSettings(1 /* show */, 1 /* showUnredacted */);
86         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
87                 KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
88         setupActivity();
89 
90         assertHideAllVisible(true);
91         assertEnabledButtons(false /* all */, true /* redact */);
92         assertSelectedButton(R.id.redact_sensitive);
93     }
94 
95     @Test
primaryUserNotificationRestrictionTest()96     public void primaryUserNotificationRestrictionTest() {
97         setupSettings(1 /* show */, 1 /* showUnredacted */);
98         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
99                 KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
100         setupActivity();
101 
102         assertHideAllVisible(true);
103         assertEnabledButtons(false /* all */, false /* redact */);
104         assertSelectedButton(R.id.hide_all);
105     }
106 
107     @Test
managedProfileNoRestrictionsTest()108     public void managedProfileNoRestrictionsTest() {
109         setupSettings(1 /* show */, 1 /* showUnredacted */);
110         final ShadowUserManager sum =
111                 Shadow.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
112         sum.addProfile(
113                 UserHandle.myUserId(), UserHandle.myUserId(),
114                 "work-profile"/* profileName */, UserInfo.FLAG_MANAGED_PROFILE);
115         setupActivity();
116 
117         assertHideAllVisible(false);
118         assertEnabledButtons(true /* all */, true /* redact */);
119         assertSelectedButton(R.id.show_all);
120     }
121 
122     @Test
managedProfileUnredactedRestrictionTest()123     public void managedProfileUnredactedRestrictionTest() {
124         setupSettings(1 /* show */, 1 /* showUnredacted */);
125         final ShadowUserManager sum =
126                 Shadow.extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
127         sum.addProfile(
128                 UserHandle.myUserId(), UserHandle.myUserId(),
129                 "work-profile"/* profileName */, UserInfo.FLAG_MANAGED_PROFILE);
130         ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
131                 KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
132         setupActivity();
133 
134         assertHideAllVisible(false);
135         assertEnabledButtons(false /* all */, true /* redact */);
136         assertSelectedButton(R.id.redact_sensitive);
137     }
138 
139     @Test
defaultShowSensitiveContent_configDeny()140     public void defaultShowSensitiveContent_configDeny() {
141         final ContentResolver resolver = RuntimeEnvironment.application.getContentResolver();
142         Settings.Secure.putIntForUser(resolver,
143                 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1, UserHandle.myUserId());
144         setupConfig(false);
145         setupActivity();
146 
147         assertSelectedButton(R.id.redact_sensitive);
148     }
149 
150     @Test
defaultShowSensitiveContent_configAllow()151     public void defaultShowSensitiveContent_configAllow() {
152         final ContentResolver resolver = RuntimeEnvironment.application.getContentResolver();
153         Settings.Secure.putIntForUser(resolver,
154                 LOCK_SCREEN_SHOW_NOTIFICATIONS, 1, UserHandle.myUserId());
155         setupConfig(true);
156         setupActivity();
157 
158         assertSelectedButton(R.id.show_all);
159     }
160 
setupActivity()161     private void setupActivity() {
162         mActivity = buildActivity(RedactionInterstitial.class, new Intent()).setup().get();
163         mFragment = (RedactionInterstitialFragment)
164                 mActivity.getSupportFragmentManager().findFragmentById(R.id.main_content);
165         assertThat(mActivity).isNotNull();
166         assertThat(mFragment).isNotNull();
167     }
168 
setupConfig(boolean allowSensitiveContent)169     private void setupConfig(boolean allowSensitiveContent) {
170         SettingsShadowResources.overrideResource(
171                 R.bool.default_allow_sensitive_lockscreen_content, allowSensitiveContent);
172     }
173 
setupSettings(int show, int showUnredacted)174     private void setupSettings(int show, int showUnredacted) {
175         final ContentResolver resolver = RuntimeEnvironment.application.getContentResolver();
176         Settings.Secure.putIntForUser(resolver,
177                 LOCK_SCREEN_SHOW_NOTIFICATIONS, show, UserHandle.myUserId());
178         Settings.Secure.putIntForUser(resolver,
179                 LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, showUnredacted, UserHandle.myUserId());
180     }
181 
assertHideAllVisible(boolean visible)182     private void assertHideAllVisible(boolean visible) {
183         Assert.assertEquals(visible, getButton(R.id.hide_all).getVisibility() != View.GONE);
184     }
185 
assertEnabledButtons(boolean all, boolean redact)186     private void assertEnabledButtons(boolean all, boolean redact) {
187         Assert.assertEquals(all, buttonEnabled(R.id.show_all));
188         Assert.assertEquals(redact, buttonEnabled(R.id.redact_sensitive));
189     }
190 
assertSelectedButton(int resId)191     private void assertSelectedButton(int resId) {
192         Assert.assertEquals(resId == R.id.show_all, buttonChecked(R.id.show_all));
193         Assert.assertEquals(resId == R.id.redact_sensitive, buttonChecked(R.id.redact_sensitive));
194         Assert.assertEquals(resId == R.id.hide_all, buttonChecked(R.id.hide_all));
195     }
196 
buttonChecked(int resource)197     private boolean buttonChecked(int resource) {
198         return getButton(resource).isChecked();
199     }
200 
buttonEnabled(int resource)201     private boolean buttonEnabled(int resource) {
202         return !((RestrictedRadioButton) getButton(resource)).isDisabledByAdmin();
203     }
204 
getButton(int resource)205     private RadioButton getButton(int resource) {
206         return (RadioButton) mFragment.getView().findViewById(resource);
207     }
208 }
209