| 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; |
| 11 import static org.mockito.ArgumentMatchers.anyString; | 11 import static org.mockito.ArgumentMatchers.anyString; |
| 12 import static org.mockito.ArgumentMatchers.eq; | 12 import static org.mockito.ArgumentMatchers.eq; |
| 13 import static org.mockito.Mockito.doAnswer; | 13 import static org.mockito.Mockito.doAnswer; |
| 14 import static org.mockito.Mockito.mock; | 14 import static org.mockito.Mockito.mock; |
| 15 import static org.mockito.Mockito.verify; | 15 import static org.mockito.Mockito.verify; |
| 16 import static org.mockito.Mockito.when; | 16 import static org.mockito.Mockito.when; |
| 17 | 17 |
| 18 import android.accounts.Account; | 18 import android.accounts.Account; |
| 19 import android.content.Context; | 19 import android.content.Context; |
| 20 import android.content.pm.ProviderInfo; | 20 import android.content.pm.ProviderInfo; |
| 21 | 21 |
| 22 import org.junit.After; | 22 import org.junit.After; |
| 23 import org.junit.Before; | 23 import org.junit.Before; |
| 24 import org.junit.BeforeClass; |
| 24 import org.junit.Rule; | 25 import org.junit.Rule; |
| 25 import org.junit.Test; | 26 import org.junit.Test; |
| 26 import org.junit.runner.RunWith; | 27 import org.junit.runner.RunWith; |
| 27 import org.mockito.Mockito; | 28 import org.mockito.Mockito; |
| 28 import org.mockito.invocation.InvocationOnMock; | 29 import org.mockito.invocation.InvocationOnMock; |
| 29 import org.mockito.stubbing.Answer; | 30 import org.mockito.stubbing.Answer; |
| 30 import org.robolectric.Robolectric; | 31 import org.robolectric.Robolectric; |
| 31 import org.robolectric.RuntimeEnvironment; | 32 import org.robolectric.RuntimeEnvironment; |
| 32 import org.robolectric.annotation.Config; | 33 import org.robolectric.annotation.Config; |
| 33 | 34 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 50 @RunWith(LocalRobolectricTestRunner.class) | 51 @RunWith(LocalRobolectricTestRunner.class) |
| 51 @Config(manifest = Config.NONE) | 52 @Config(manifest = Config.NONE) |
| 52 public class SupervisedUserContentProviderUnitTest { | 53 public class SupervisedUserContentProviderUnitTest { |
| 53 @Rule | 54 @Rule |
| 54 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); | 55 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); |
| 55 | 56 |
| 56 private SupervisedUserContentProvider mSupervisedUserContentProvider; | 57 private SupervisedUserContentProvider mSupervisedUserContentProvider; |
| 57 | 58 |
| 58 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app"
; | 59 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app"
; |
| 59 | 60 |
| 61 private static AccountManagerDelegate sMockDelegate; |
| 62 |
| 63 @BeforeClass |
| 64 public static void overrideAccountManagerHelper() { |
| 65 sMockDelegate = mock(AccountManagerDelegate.class); |
| 66 AccountManagerHelper.overrideAccountManagerHelperForTests(sMockDelegate)
; |
| 67 } |
| 68 |
| 60 // Override methods that wrap things that can't be mocked (including native
calls). | 69 // Override methods that wrap things that can't be mocked (including native
calls). |
| 61 private static class MySupervisedUserContentProvider extends SupervisedUserC
ontentProvider { | 70 private static class MySupervisedUserContentProvider extends SupervisedUserC
ontentProvider { |
| 62 @Override | 71 @Override |
| 63 void startForcedSigninProcessor(Context context, Runnable onComplete) { | 72 void startForcedSigninProcessor(Context context, Runnable onComplete) { |
| 64 ChromeSigninController.get().setSignedInAccountName("Dummy"); | 73 ChromeSigninController.get().setSignedInAccountName("Dummy"); |
| 65 onComplete.run(); | 74 onComplete.run(); |
| 66 } | 75 } |
| 67 | 76 |
| 68 @Override | 77 @Override |
| 69 void listenForChildAccountStatusChange(Callback<Boolean> callback) { | 78 void listenForChildAccountStatusChange(Callback<Boolean> callback) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), | 220 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
| 212 eq("url")); | 221 eq("url")); |
| 213 } | 222 } |
| 214 | 223 |
| 215 @SuppressWarnings("unchecked") | 224 @SuppressWarnings("unchecked") |
| 216 @Test | 225 @Test |
| 217 public void testShouldProceed_notSignedIn() throws ProcessInitException { | 226 public void testShouldProceed_notSignedIn() throws ProcessInitException { |
| 218 // Mock things called during startup | 227 // Mock things called during startup |
| 219 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); | 228 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); |
| 220 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 229 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
| 221 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class)
; | |
| 222 AccountManagerHelper.overrideAccountManagerHelperForTests( | |
| 223 RuntimeEnvironment.application, mockDelegate); | |
| 224 Account account = new Account("Google", "Dummy"); | 230 Account account = new Account("Google", "Dummy"); |
| 225 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); | 231 when(sMockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); |
| 226 | 232 |
| 227 WebRestrictionsResult result = | 233 WebRestrictionsResult result = |
| 228 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); | 234 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
| 229 | 235 |
| 230 assertThat(result.shouldProceed(), is(true)); | 236 assertThat(result.shouldProceed(), is(true)); |
| 231 verify(mockBrowserInitializer).handleSynchronousStartup(); | 237 verify(mockBrowserInitializer).handleSynchronousStartup(); |
| 232 verify(mSupervisedUserContentProvider) | 238 verify(mSupervisedUserContentProvider) |
| 233 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); | 239 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); |
| 234 verify(mSupervisedUserContentProvider) | 240 verify(mSupervisedUserContentProvider) |
| 235 .listenForChildAccountStatusChange(any(Callback.class)); | 241 .listenForChildAccountStatusChange(any(Callback.class)); |
| 236 verify(mSupervisedUserContentProvider) | 242 verify(mSupervisedUserContentProvider) |
| 237 .nativeShouldProceed(eq(5678L), | 243 .nativeShouldProceed(eq(5678L), |
| 238 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), | 244 any(SupervisedUserContentProvider.SupervisedUserQueryRep
ly.class), |
| 239 eq("url")); | 245 eq("url")); |
| 240 | |
| 241 AccountManagerHelper.resetAccountManagerHelperForTests(); | |
| 242 } | 246 } |
| 243 | 247 |
| 244 @Test | 248 @Test |
| 245 public void testShouldProceed_cannotSignIn() { | 249 public void testShouldProceed_cannotSignIn() { |
| 246 // Mock things called during startup | 250 // Mock things called during startup |
| 247 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); | 251 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit
ializer.class); |
| 248 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 252 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
| 249 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class)
; | |
| 250 AccountManagerHelper.overrideAccountManagerHelperForTests( | |
| 251 RuntimeEnvironment.application, mockDelegate); | |
| 252 Account account = new Account("Google", "Dummy"); | 253 Account account = new Account("Google", "Dummy"); |
| 253 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); | 254 when(sMockDelegate.getAccountsByType("Google")).thenReturn(new Account[]
{account}); |
| 254 | 255 |
| 255 // Change the behavior of the forced sign-in processor to not sign in. | 256 // Change the behavior of the forced sign-in processor to not sign in. |
| 256 doAnswer(new Answer<Void>() { | 257 doAnswer(new Answer<Void>() { |
| 257 @Override | 258 @Override |
| 258 public Void answer(InvocationOnMock invocation) throws Throwable { | 259 public Void answer(InvocationOnMock invocation) throws Throwable { |
| 259 invocation.<Runnable>getArgument(1).run(); | 260 invocation.<Runnable>getArgument(1).run(); |
| 260 return null; | 261 return null; |
| 261 } | 262 } |
| 262 }) | 263 }) |
| 263 .when(mSupervisedUserContentProvider) | 264 .when(mSupervisedUserContentProvider) |
| 264 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); | 265 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla
ss)); |
| 265 | 266 |
| 266 WebRestrictionsResult result = | 267 WebRestrictionsResult result = |
| 267 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); | 268 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC
KAGE, "url"); |
| 268 | 269 |
| 269 assertThat(result.shouldProceed(), is(false)); | 270 assertThat(result.shouldProceed(), is(false)); |
| 270 assertThat(result.getErrorInt(0), is(5)); | 271 assertThat(result.getErrorInt(0), is(5)); |
| 271 | |
| 272 AccountManagerHelper.resetAccountManagerHelperForTests(); | |
| 273 } | 272 } |
| 274 | 273 |
| 275 @Test | 274 @Test |
| 276 public void testShouldProceed_requestWhitelisted() { | 275 public void testShouldProceed_requestWhitelisted() { |
| 277 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); | 276 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor
Testing(1234L); |
| 278 | 277 |
| 279 // Modify the result of the native call to block any URL. | 278 // Modify the result of the native call to block any URL. |
| 280 doAnswer(new Answer<Void>() { | 279 doAnswer(new Answer<Void>() { |
| 281 @Override | 280 @Override |
| 282 public Void answer(InvocationOnMock invocation) throws Throwable { | 281 public Void answer(InvocationOnMock invocation) throws Throwable { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 301 | 300 |
| 302 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid
er.shouldProceed( | 301 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid
er.shouldProceed( |
| 303 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); | 302 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); |
| 304 assertThat(wrongCallingPackage.shouldProceed(), is(false)); | 303 assertThat(wrongCallingPackage.shouldProceed(), is(false)); |
| 305 | 304 |
| 306 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide
r.shouldProceed( | 305 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide
r.shouldProceed( |
| 307 null, "https://accounts.google.com/reauth"); | 306 null, "https://accounts.google.com/reauth"); |
| 308 assertThat(nullCallingPackage.shouldProceed(), is(false)); | 307 assertThat(nullCallingPackage.shouldProceed(), is(false)); |
| 309 } | 308 } |
| 310 } | 309 } |
| OLD | NEW |