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