OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.superviseduser; | 5 package org.chromium.chrome.browser.superviseduser; |
6 | 6 |
7 import static org.hamcrest.CoreMatchers.is; | 7 import static org.hamcrest.CoreMatchers.is; |
8 import static org.junit.Assert.assertThat; | 8 import static org.junit.Assert.assertThat; |
9 import static org.mockito.ArgumentMatchers.any; | 9 import static org.mockito.ArgumentMatchers.any; |
10 import static org.mockito.ArgumentMatchers.anyLong; | 10 import static org.mockito.ArgumentMatchers.anyLong; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 * provider. The content provider aspects are tested with WebRestrictionsContent
ProviderTest. | 46 * provider. The content provider aspects are tested with WebRestrictionsContent
ProviderTest. |
47 */ | 47 */ |
48 @RunWith(LocalRobolectricTestRunner.class) | 48 @RunWith(LocalRobolectricTestRunner.class) |
49 @Config(manifest = Config.NONE) | 49 @Config(manifest = Config.NONE) |
50 public class SupervisedUserContentProviderUnitTest { | 50 public class SupervisedUserContentProviderUnitTest { |
51 @Rule | 51 @Rule |
52 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); | 52 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); |
53 | 53 |
54 private SupervisedUserContentProvider mSupervisedUserContentProvider; | 54 private SupervisedUserContentProvider mSupervisedUserContentProvider; |
55 | 55 |
| 56 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app"
; |
| 57 |
56 @Before | 58 @Before |
57 public void setUp() { | 59 public void setUp() { |
58 // Ensure clean state (in particular not signed in). | 60 // Ensure clean state (in particular not signed in). |
59 ContextUtils.getAppSharedPreferences().edit().clear().apply(); | 61 ContextUtils.getAppSharedPreferences().edit().clear().apply(); |
60 | 62 |
61 // Spy on the content provider so that we can watch its calls. Override
methods that wrap | 63 // Spy on the content provider so that we can watch its calls. Override
methods that wrap |
62 // things that can't be mocked (including native calls). | 64 // things that can't be mocked (including native calls). |
63 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr
ovider() { | 65 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr
ovider() { |
64 @Override | 66 @Override |
65 void startForcedSigninProcessor(Context context, Runnable onComplete
) { | 67 void startForcedSigninProcessor(Context context, Runnable onComplete
) { |
(...skipping 27 matching lines...) Expand all Loading... |
93 @After | 95 @After |
94 public void shutDown() { | 96 public void shutDown() { |
95 ContextUtils.getAppSharedPreferences().edit().clear().apply(); | 97 ContextUtils.getAppSharedPreferences().edit().clear().apply(); |
96 ChromeBrowserInitializer.setForTesting(null); | 98 ChromeBrowserInitializer.setForTesting(null); |
97 } | 99 } |
98 | 100 |
99 @Test | 101 @Test |
100 public void testShouldProceed_PermittedUrl() { | 102 public void testShouldProceed_PermittedUrl() { |
101 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); | 103 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); |
102 // Mock the native call for a permitted URL | 104 // Mock the native call for a permitted URL |
103 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc
eed("url"); | 105 WebRestrictionsResult result = |
| 106 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
104 assertThat(result.shouldProceed(), is(true)); | 107 assertThat(result.shouldProceed(), is(true)); |
105 verify(mSupervisedUserContentProvider) | 108 verify(mSupervisedUserContentProvider) |
106 .nativeShouldProceed(eq(1234L), | 109 .nativeShouldProceed(eq(1234L), |
107 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), | 110 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
108 eq("url")); | 111 eq("url")); |
109 } | 112 } |
110 | 113 |
111 @Test | 114 @Test |
112 public void testShouldProceed_ForbiddenUrl() { | 115 public void testShouldProceed_ForbiddenUrl() { |
113 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); | 116 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); |
114 // Modify the result of the native call to make this a forbidden URL | 117 // Modify the result of the native call to make this a forbidden URL |
115 doAnswer(new Answer<Void>() { | 118 doAnswer(new Answer<Void>() { |
116 | 119 |
117 @Override | 120 @Override |
118 public Void answer(InvocationOnMock invocation) throws Throwable { | 121 public Void answer(InvocationOnMock invocation) throws Throwable { |
119 invocation.<SupervisedUserQueryReply>getArgument(1).onQueryFaile
d(1, 2, 3, "url1", | 122 invocation.<SupervisedUserQueryReply>getArgument(1).onQueryFaile
d(1, 2, 3, "url1", |
120 "url2", "custodian", "custodianEmail", "secondCustodian"
, | 123 "url2", "custodian", "custodianEmail", "secondCustodian"
, |
121 "secondCustodianEmail"); | 124 "secondCustodianEmail"); |
122 return null; | 125 return null; |
123 } | 126 } |
124 | 127 |
125 }) | 128 }) |
126 .when(mSupervisedUserContentProvider) | 129 .when(mSupervisedUserContentProvider) |
127 .nativeShouldProceed(anyLong(), | 130 .nativeShouldProceed(anyLong(), |
128 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), | 131 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
129 anyString()); | 132 anyString()); |
130 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc
eed("url"); | 133 WebRestrictionsResult result = |
| 134 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
131 assertThat(result.shouldProceed(), is(false)); | 135 assertThat(result.shouldProceed(), is(false)); |
132 assertThat(result.errorIntCount(), is(3)); | 136 assertThat(result.errorIntCount(), is(3)); |
133 assertThat(result.getErrorInt(0), is(1)); | 137 assertThat(result.getErrorInt(0), is(1)); |
134 assertThat(result.getErrorInt(1), is(2)); | 138 assertThat(result.getErrorInt(1), is(2)); |
135 assertThat(result.getErrorInt(2), is(3)); | 139 assertThat(result.getErrorInt(2), is(3)); |
136 assertThat(result.errorStringCount(), is(6)); | 140 assertThat(result.errorStringCount(), is(6)); |
137 assertThat(result.getErrorString(0), is("url1")); | 141 assertThat(result.getErrorString(0), is("url1")); |
138 assertThat(result.getErrorString(1), is("url2")); | 142 assertThat(result.getErrorString(1), is("url2")); |
139 assertThat(result.getErrorString(2), is("custodian")); | 143 assertThat(result.getErrorString(2), is("custodian")); |
140 assertThat(result.getErrorString(3), is("custodianEmail")); | 144 assertThat(result.getErrorString(3), is("custodianEmail")); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 184 } |
181 | 185 |
182 @Test | 186 @Test |
183 public void testShouldProceed_withStartupSignedIn() throws ProcessInitExcept
ion { | 187 public void testShouldProceed_withStartupSignedIn() throws ProcessInitExcept
ion { |
184 // Set up a signed in user | 188 // Set up a signed in user |
185 ChromeSigninController.get(RuntimeEnvironment.application).setSignedInAc
countName("Dummy"); | 189 ChromeSigninController.get(RuntimeEnvironment.application).setSignedInAc
countName("Dummy"); |
186 // Mock things called during startup | 190 // Mock things called during startup |
187 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); | 191 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); |
188 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 192 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
189 | 193 |
190 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc
eed("url"); | 194 WebRestrictionsResult result = |
| 195 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
191 | 196 |
192 assertThat(result.shouldProceed(), is(true)); | 197 assertThat(result.shouldProceed(), is(true)); |
193 verify(mockBrowserInitializer).handleSynchronousStartup(); | 198 verify(mockBrowserInitializer).handleSynchronousStartup(); |
194 verify(mSupervisedUserContentProvider) | 199 verify(mSupervisedUserContentProvider) |
195 .nativeShouldProceed(eq(5678L), | 200 .nativeShouldProceed(eq(5678L), |
196 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), | 201 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
197 eq("url")); | 202 eq("url")); |
198 } | 203 } |
199 | 204 |
200 @SuppressWarnings("unchecked") | 205 @SuppressWarnings("unchecked") |
201 @Test | 206 @Test |
202 public void testShouldProceed_notSignedIn() throws ProcessInitException { | 207 public void testShouldProceed_notSignedIn() throws ProcessInitException { |
203 // Mock things called during startup | 208 // Mock things called during startup |
204 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); | 209 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); |
205 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 210 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
206 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class)
; | 211 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class)
; |
207 AccountManagerHelper.overrideAccountManagerHelperForTests( | 212 AccountManagerHelper.overrideAccountManagerHelperForTests( |
208 RuntimeEnvironment.application, mockDelegate); | 213 RuntimeEnvironment.application, mockDelegate); |
209 Account account = new Account("Google", "Dummy"); | 214 Account account = new Account("Google", "Dummy"); |
210 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); | 215 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); |
211 | 216 |
212 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc
eed("url"); | 217 WebRestrictionsResult result = |
| 218 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
213 | 219 |
214 assertThat(result.shouldProceed(), is(true)); | 220 assertThat(result.shouldProceed(), is(true)); |
215 verify(mockBrowserInitializer).handleSynchronousStartup(); | 221 verify(mockBrowserInitializer).handleSynchronousStartup(); |
216 verify(mSupervisedUserContentProvider) | 222 verify(mSupervisedUserContentProvider) |
217 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); | 223 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); |
218 verify(mSupervisedUserContentProvider) | 224 verify(mSupervisedUserContentProvider) |
219 .listenForChildAccountStatusChange(any(Callback.class)); | 225 .listenForChildAccountStatusChange(any(Callback.class)); |
220 verify(mSupervisedUserContentProvider) | 226 verify(mSupervisedUserContentProvider) |
221 .nativeShouldProceed(eq(5678L), | 227 .nativeShouldProceed(eq(5678L), |
222 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), | 228 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
223 eq("url")); | 229 eq("url")); |
224 } | 230 } |
225 | 231 |
226 @Test | 232 @Test |
227 public void testShouldProceed_cannotSignIn() throws ProcessInitException { | 233 public void testShouldProceed_cannotSignIn() { |
228 // Mock things called during startup | 234 // Mock things called during startup |
229 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); | 235 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); |
230 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 236 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
231 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class)
; | 237 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class)
; |
232 AccountManagerHelper.overrideAccountManagerHelperForTests( | 238 AccountManagerHelper.overrideAccountManagerHelperForTests( |
233 RuntimeEnvironment.application, mockDelegate); | 239 RuntimeEnvironment.application, mockDelegate); |
234 Account account = new Account("Google", "Dummy"); | 240 Account account = new Account("Google", "Dummy"); |
235 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); | 241 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); |
236 | 242 |
237 // Change the behavior of the forced sign-in processor to not sign in. | 243 // Change the behavior of the forced sign-in processor to not sign in. |
238 doAnswer(new Answer<Void>() { | 244 doAnswer(new Answer<Void>() { |
239 @Override | 245 @Override |
240 public Void answer(InvocationOnMock invocation) throws Throwable { | 246 public Void answer(InvocationOnMock invocation) throws Throwable { |
241 invocation.<Runnable>getArgument(1).run(); | 247 invocation.<Runnable>getArgument(1).run(); |
242 return null; | 248 return null; |
243 } | 249 } |
244 }) | 250 }) |
245 .when(mSupervisedUserContentProvider) | 251 .when(mSupervisedUserContentProvider) |
246 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); | 252 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); |
247 | 253 |
248 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc
eed("url"); | 254 WebRestrictionsResult result = |
| 255 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
249 | 256 |
250 assertThat(result.shouldProceed(), is(false)); | 257 assertThat(result.shouldProceed(), is(false)); |
251 assertThat(result.getErrorInt(0), is(5)); | 258 assertThat(result.getErrorInt(0), is(5)); |
252 } | 259 } |
| 260 |
| 261 @Test |
| 262 public void testShouldProceed_requestWhitelisted() { |
| 263 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); |
| 264 |
| 265 // Modify the result of the native call to block any URL. |
| 266 doAnswer(new Answer<Void>() { |
| 267 @Override |
| 268 public Void answer(InvocationOnMock invocation) throws Throwable { |
| 269 invocation.<SupervisedUserQueryReply>getArgument(1).onQueryFaile
d(1, 2, 3, "url1", |
| 270 "url2", "custodian", "custodianEmail", "secondCustodian"
, |
| 271 "secondCustodianEmail"); |
| 272 return null; |
| 273 } |
| 274 }) |
| 275 .when(mSupervisedUserContentProvider) |
| 276 .nativeShouldProceed(eq(1234L), |
| 277 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
| 278 anyString()); |
| 279 |
| 280 WebRestrictionsResult allowed = mSupervisedUserContentProvider.shouldPro
ceed( |
| 281 "com.google.android.gms.ui", "https://accounts.google.com/reauth
"); |
| 282 assertThat(allowed.shouldProceed(), is(true)); |
| 283 |
| 284 WebRestrictionsResult wrongUrl = mSupervisedUserContentProvider.shouldPr
oceed( |
| 285 "com.google.android.gms.ui", "http://www.example.com"); |
| 286 assertThat(wrongUrl.shouldProceed(), is(false)); |
| 287 |
| 288 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid
er.shouldProceed( |
| 289 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); |
| 290 assertThat(wrongCallingPackage.shouldProceed(), is(false)); |
| 291 |
| 292 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide
r.shouldProceed( |
| 293 null, "https://accounts.google.com/reauth"); |
| 294 assertThat(nullCallingPackage.shouldProceed(), is(false)); |
| 295 } |
253 } | 296 } |
OLD | NEW |