Chromium Code Reviews| 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.ContentProvider; | |
| 19 import android.content.Context; | 20 import android.content.Context; |
| 20 | 21 |
| 21 import org.junit.After; | 22 import org.junit.After; |
| 22 import org.junit.Before; | 23 import org.junit.Before; |
| 23 import org.junit.Rule; | 24 import org.junit.Rule; |
| 24 import org.junit.Test; | 25 import org.junit.Test; |
| 25 import org.junit.runner.RunWith; | 26 import org.junit.runner.RunWith; |
| 26 import org.mockito.Mockito; | 27 import org.mockito.Mockito; |
| 27 import org.mockito.invocation.InvocationOnMock; | 28 import org.mockito.invocation.InvocationOnMock; |
| 28 import org.mockito.stubbing.Answer; | 29 import org.mockito.stubbing.Answer; |
| 29 import org.robolectric.RuntimeEnvironment; | 30 import org.robolectric.RuntimeEnvironment; |
| 30 import org.robolectric.annotation.Config; | 31 import org.robolectric.annotation.Config; |
| 32 import org.robolectric.annotation.Implementation; | |
| 33 import org.robolectric.annotation.Implements; | |
| 34 import org.robolectric.shadows.ShadowContentProvider; | |
| 31 | 35 |
| 32 import org.chromium.base.Callback; | 36 import org.chromium.base.Callback; |
| 33 import org.chromium.base.ContextUtils; | 37 import org.chromium.base.ContextUtils; |
| 34 import org.chromium.base.library_loader.ProcessInitException; | 38 import org.chromium.base.library_loader.ProcessInitException; |
| 35 import org.chromium.chrome.browser.DisableHistogramsRule; | 39 import org.chromium.chrome.browser.DisableHistogramsRule; |
| 36 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | 40 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
| 37 import org.chromium.chrome.browser.superviseduser.SupervisedUserContentProvider. SupervisedUserQueryReply; | 41 import org.chromium.chrome.browser.superviseduser.SupervisedUserContentProvider. SupervisedUserQueryReply; |
| 38 import org.chromium.components.signin.AccountManagerDelegate; | 42 import org.chromium.components.signin.AccountManagerDelegate; |
| 39 import org.chromium.components.signin.AccountManagerHelper; | 43 import org.chromium.components.signin.AccountManagerHelper; |
| 40 import org.chromium.components.signin.ChromeSigninController; | 44 import org.chromium.components.signin.ChromeSigninController; |
| 41 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro vider.WebRestrictionsResult; | 45 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro vider.WebRestrictionsResult; |
| 42 import org.chromium.testing.local.LocalRobolectricTestRunner; | 46 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 43 | 47 |
| 44 /** | 48 /** |
| 45 * Tests of SupervisedUserContentProvider. This is tested as a simple class, not as a content | 49 * Tests of SupervisedUserContentProvider. This is tested as a simple class, not as a content |
| 46 * provider. The content provider aspects are tested with WebRestrictionsContent ProviderTest. | 50 * provider. The content provider aspects are tested with WebRestrictionsContent ProviderTest. |
| 47 */ | 51 */ |
| 48 @RunWith(LocalRobolectricTestRunner.class) | 52 @RunWith(LocalRobolectricTestRunner.class) |
| 49 @Config(manifest = Config.NONE) | 53 @Config(manifest = Config.NONE) |
| 50 public class SupervisedUserContentProviderUnitTest { | 54 public class SupervisedUserContentProviderUnitTest { |
| 51 @Rule | 55 @Rule |
| 52 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR ule(); | 56 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR ule(); |
| 53 | 57 |
| 54 private SupervisedUserContentProvider mSupervisedUserContentProvider; | 58 private SupervisedUserContentProvider mSupervisedUserContentProvider; |
| 55 | 59 |
| 56 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app" ; | 60 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app" ; |
| 57 | 61 |
| 62 /** | |
| 63 * Robolectric ShadowContentProvider that implements getContext. | |
| 64 */ | |
| 65 @Implements(ContentProvider.class) | |
| 66 public static class MyShadowContentProvider extends ShadowContentProvider { | |
|
nyquist
2017/03/14 21:20:37
It seems like we are calling getContext() a few pl
mikecase (-- gone --)
2017/03/14 22:07:07
So for some info, in this commit they removed getC
nyquist
2017/03/14 22:17:30
Right; but it seems like they are attaching the co
| |
| 67 @Implementation | |
| 68 public final Context getContext() { | |
| 69 return RuntimeEnvironment.application; | |
| 70 } | |
| 71 } | |
| 72 | |
| 58 @Before | 73 @Before |
| 59 public void setUp() { | 74 public void setUp() { |
| 75 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati on); | |
| 76 | |
| 60 // Ensure clean state (in particular not signed in). | 77 // Ensure clean state (in particular not signed in). |
| 61 ContextUtils.getAppSharedPreferences().edit().clear().apply(); | 78 ContextUtils.getAppSharedPreferences().edit().clear().apply(); |
| 62 | 79 |
| 63 // Spy on the content provider so that we can watch its calls. Override methods that wrap | 80 // Spy on the content provider so that we can watch its calls. Override methods that wrap |
| 64 // things that can't be mocked (including native calls). | 81 // things that can't be mocked (including native calls). |
| 65 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr ovider() { | 82 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr ovider() { |
| 66 @Override | 83 @Override |
| 67 void startForcedSigninProcessor(Context context, Runnable onComplete ) { | 84 void startForcedSigninProcessor(Context context, Runnable onComplete ) { |
| 68 ChromeSigninController.get(RuntimeEnvironment.application) | 85 ChromeSigninController.get(RuntimeEnvironment.application) |
| 69 .setSignedInAccountName("Dummy"); | 86 .setSignedInAccountName("Dummy"); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 .nativeRequestInsert(anyLong(), | 193 .nativeRequestInsert(anyLong(), |
| 177 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), | 194 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), |
| 178 anyString()); | 195 anyString()); |
| 179 assertThat(mSupervisedUserContentProvider.requestInsert("url"), is(false )); | 196 assertThat(mSupervisedUserContentProvider.requestInsert("url"), is(false )); |
| 180 verify(mSupervisedUserContentProvider) | 197 verify(mSupervisedUserContentProvider) |
| 181 .nativeRequestInsert(eq(1234L), | 198 .nativeRequestInsert(eq(1234L), |
| 182 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), | 199 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), |
| 183 eq("url")); | 200 eq("url")); |
| 184 } | 201 } |
| 185 | 202 |
| 203 @Config(shadows = MyShadowContentProvider.class) | |
| 186 @Test | 204 @Test |
| 187 public void testShouldProceed_withStartupSignedIn() throws ProcessInitExcept ion { | 205 public void testShouldProceed_withStartupSignedIn() throws ProcessInitExcept ion { |
| 188 // Set up a signed in user | 206 // Set up a signed in user |
| 189 ChromeSigninController.get(RuntimeEnvironment.application).setSignedInAc countName("Dummy"); | 207 ChromeSigninController.get(RuntimeEnvironment.application).setSignedInAc countName("Dummy"); |
| 190 // Mock things called during startup | 208 // Mock things called during startup |
| 191 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class); | 209 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class); |
| 192 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 210 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
| 193 | 211 |
| 194 WebRestrictionsResult result = | 212 WebRestrictionsResult result = |
| 195 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC KAGE, "url"); | 213 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC KAGE, "url"); |
| 196 | 214 |
| 197 assertThat(result.shouldProceed(), is(true)); | 215 assertThat(result.shouldProceed(), is(true)); |
| 198 verify(mockBrowserInitializer).handleSynchronousStartup(); | 216 verify(mockBrowserInitializer).handleSynchronousStartup(); |
| 199 verify(mSupervisedUserContentProvider) | 217 verify(mSupervisedUserContentProvider) |
| 200 .nativeShouldProceed(eq(5678L), | 218 .nativeShouldProceed(eq(5678L), |
| 201 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class), | 219 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class), |
| 202 eq("url")); | 220 eq("url")); |
| 203 } | 221 } |
| 204 | 222 |
| 223 @Config(shadows = MyShadowContentProvider.class) | |
| 205 @SuppressWarnings("unchecked") | 224 @SuppressWarnings("unchecked") |
| 206 @Test | 225 @Test |
| 207 public void testShouldProceed_notSignedIn() throws ProcessInitException { | 226 public void testShouldProceed_notSignedIn() throws ProcessInitException { |
| 208 // Mock things called during startup | 227 // Mock things called during startup |
| 209 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class); | 228 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class); |
| 210 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 229 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
| 211 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class) ; | 230 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class) ; |
| 212 AccountManagerHelper.overrideAccountManagerHelperForTests( | 231 AccountManagerHelper.overrideAccountManagerHelperForTests( |
| 213 RuntimeEnvironment.application, mockDelegate); | 232 RuntimeEnvironment.application, mockDelegate); |
| 214 Account account = new Account("Google", "Dummy"); | 233 Account account = new Account("Google", "Dummy"); |
| 215 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[] {account}); | 234 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[] {account}); |
| 216 | 235 |
| 217 WebRestrictionsResult result = | 236 WebRestrictionsResult result = |
| 218 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC KAGE, "url"); | 237 mSupervisedUserContentProvider.shouldProceed(DEFAULT_CALLING_PAC KAGE, "url"); |
| 219 | 238 |
| 220 assertThat(result.shouldProceed(), is(true)); | 239 assertThat(result.shouldProceed(), is(true)); |
| 221 verify(mockBrowserInitializer).handleSynchronousStartup(); | 240 verify(mockBrowserInitializer).handleSynchronousStartup(); |
| 222 verify(mSupervisedUserContentProvider) | 241 verify(mSupervisedUserContentProvider) |
| 223 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla ss)); | 242 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla ss)); |
| 224 verify(mSupervisedUserContentProvider) | 243 verify(mSupervisedUserContentProvider) |
| 225 .listenForChildAccountStatusChange(any(Callback.class)); | 244 .listenForChildAccountStatusChange(any(Callback.class)); |
| 226 verify(mSupervisedUserContentProvider) | 245 verify(mSupervisedUserContentProvider) |
| 227 .nativeShouldProceed(eq(5678L), | 246 .nativeShouldProceed(eq(5678L), |
| 228 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class), | 247 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class), |
| 229 eq("url")); | 248 eq("url")); |
| 230 } | 249 } |
| 231 | 250 |
| 251 @Config(shadows = MyShadowContentProvider.class) | |
| 232 @Test | 252 @Test |
| 233 public void testShouldProceed_cannotSignIn() { | 253 public void testShouldProceed_cannotSignIn() { |
| 234 // Mock things called during startup | 254 // Mock things called during startup |
| 235 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class); | 255 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class); |
| 236 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); | 256 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer); |
| 237 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class) ; | 257 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class) ; |
| 238 AccountManagerHelper.overrideAccountManagerHelperForTests( | 258 AccountManagerHelper.overrideAccountManagerHelperForTests( |
| 239 RuntimeEnvironment.application, mockDelegate); | 259 RuntimeEnvironment.application, mockDelegate); |
| 240 Account account = new Account("Google", "Dummy"); | 260 Account account = new Account("Google", "Dummy"); |
| 241 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[] {account}); | 261 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[] {account}); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 | 307 |
| 288 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid er.shouldProceed( | 308 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid er.shouldProceed( |
| 289 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); | 309 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); |
| 290 assertThat(wrongCallingPackage.shouldProceed(), is(false)); | 310 assertThat(wrongCallingPackage.shouldProceed(), is(false)); |
| 291 | 311 |
| 292 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide r.shouldProceed( | 312 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide r.shouldProceed( |
| 293 null, "https://accounts.google.com/reauth"); | 313 null, "https://accounts.google.com/reauth"); |
| 294 assertThat(nullCallingPackage.shouldProceed(), is(false)); | 314 assertThat(nullCallingPackage.shouldProceed(), is(false)); |
| 295 } | 315 } |
| 296 } | 316 } |
| OLD | NEW |