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 | 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; |
| 30 import org.robolectric.Robolectric; |
29 import org.robolectric.RuntimeEnvironment; | 31 import org.robolectric.RuntimeEnvironment; |
30 import org.robolectric.annotation.Config; | 32 import org.robolectric.annotation.Config; |
31 | 33 |
32 import org.chromium.base.Callback; | 34 import org.chromium.base.Callback; |
33 import org.chromium.base.ContextUtils; | 35 import org.chromium.base.ContextUtils; |
34 import org.chromium.base.library_loader.ProcessInitException; | 36 import org.chromium.base.library_loader.ProcessInitException; |
35 import org.chromium.chrome.browser.DisableHistogramsRule; | 37 import org.chromium.chrome.browser.DisableHistogramsRule; |
36 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; | 38 import org.chromium.chrome.browser.init.ChromeBrowserInitializer; |
37 import org.chromium.chrome.browser.superviseduser.SupervisedUserContentProvider.
SupervisedUserQueryReply; | 39 import org.chromium.chrome.browser.superviseduser.SupervisedUserContentProvider.
SupervisedUserQueryReply; |
38 import org.chromium.components.signin.AccountManagerDelegate; | 40 import org.chromium.components.signin.AccountManagerDelegate; |
39 import org.chromium.components.signin.AccountManagerHelper; | 41 import org.chromium.components.signin.AccountManagerHelper; |
40 import org.chromium.components.signin.ChromeSigninController; | 42 import org.chromium.components.signin.ChromeSigninController; |
41 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro
vider.WebRestrictionsResult; | 43 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro
vider.WebRestrictionsResult; |
42 import org.chromium.testing.local.LocalRobolectricTestRunner; | 44 import org.chromium.testing.local.LocalRobolectricTestRunner; |
43 | 45 |
44 /** | 46 /** |
45 * Tests of SupervisedUserContentProvider. This is tested as a simple class, not
as a content | 47 * 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. | 48 * provider. The content provider aspects are tested with WebRestrictionsContent
ProviderTest. |
47 */ | 49 */ |
48 @RunWith(LocalRobolectricTestRunner.class) | 50 @RunWith(LocalRobolectricTestRunner.class) |
49 @Config(manifest = Config.NONE) | 51 @Config(manifest = Config.NONE) |
50 public class SupervisedUserContentProviderUnitTest { | 52 public class SupervisedUserContentProviderUnitTest { |
51 @Rule | 53 @Rule |
52 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); | 54 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR
ule(); |
53 | 55 |
54 private SupervisedUserContentProvider mSupervisedUserContentProvider; | 56 private SupervisedUserContentProvider mSupervisedUserContentProvider; |
55 | 57 |
56 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app"
; | 58 private static final String DEFAULT_CALLING_PACKAGE = "com.example.some.app"
; |
57 | 59 |
| 60 // Override methods that wrap things that can't be mocked (including native
calls). |
| 61 private static class MySupervisedUserContentProvider extends SupervisedUserC
ontentProvider { |
| 62 @Override |
| 63 void startForcedSigninProcessor(Context context, Runnable onComplete) { |
| 64 ChromeSigninController.get(RuntimeEnvironment.application) |
| 65 .setSignedInAccountName("Dummy"); |
| 66 onComplete.run(); |
| 67 } |
| 68 |
| 69 @Override |
| 70 void listenForChildAccountStatusChange(Callback<Boolean> callback) { |
| 71 callback.onResult(true); |
| 72 } |
| 73 |
| 74 @Override |
| 75 void nativeShouldProceed(long l, SupervisedUserQueryReply reply, String
url) { |
| 76 reply.onQueryComplete(); |
| 77 } |
| 78 |
| 79 @Override |
| 80 void nativeRequestInsert(long l, SupervisedUserInsertReply reply, String
url) { |
| 81 reply.onInsertRequestSendComplete(true); |
| 82 } |
| 83 |
| 84 @Override |
| 85 long nativeCreateSupervisedUserContentProvider() { |
| 86 return 5678L; |
| 87 } |
| 88 } |
| 89 |
58 @Before | 90 @Before |
59 public void setUp() { | 91 public void setUp() { |
| 92 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
| 93 |
60 // Ensure clean state (in particular not signed in). | 94 // Ensure clean state (in particular not signed in). |
61 ContextUtils.getAppSharedPreferences().edit().clear().apply(); | 95 ContextUtils.getAppSharedPreferences().edit().clear().apply(); |
62 | 96 |
63 // Spy on the content provider so that we can watch its calls. Override
methods that wrap | 97 // Spy on the content provider so that we can watch its calls. |
64 // things that can't be mocked (including native calls). | 98 ProviderInfo info = new ProviderInfo(); |
65 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr
ovider() { | 99 info.authority = "foo.bar.baz"; |
66 @Override | 100 mSupervisedUserContentProvider = |
67 void startForcedSigninProcessor(Context context, Runnable onComplete
) { | 101 Mockito.spy(Robolectric.buildContentProvider(MySupervisedUserCon
tentProvider.class) |
68 ChromeSigninController.get(RuntimeEnvironment.application) | 102 .create(info) |
69 .setSignedInAccountName("Dummy"); | 103 .get()); |
70 onComplete.run(); | |
71 } | |
72 | |
73 @Override | |
74 void listenForChildAccountStatusChange(Callback<Boolean> callback) { | |
75 callback.onResult(true); | |
76 } | |
77 | |
78 @Override | |
79 void nativeShouldProceed(long l, SupervisedUserQueryReply reply, Str
ing url) { | |
80 reply.onQueryComplete(); | |
81 } | |
82 | |
83 @Override | |
84 void nativeRequestInsert(long l, SupervisedUserInsertReply reply, St
ring url) { | |
85 reply.onInsertRequestSendComplete(true); | |
86 } | |
87 | |
88 @Override | |
89 long nativeCreateSupervisedUserContentProvider() { | |
90 return 5678L; | |
91 } | |
92 }); | |
93 } | 104 } |
94 | 105 |
95 @After | 106 @After |
96 public void shutDown() { | 107 public void shutDown() { |
97 ContextUtils.getAppSharedPreferences().edit().clear().apply(); | 108 ContextUtils.getAppSharedPreferences().edit().clear().apply(); |
98 ChromeBrowserInitializer.setForTesting(null); | 109 ChromeBrowserInitializer.setForTesting(null); |
99 } | 110 } |
100 | 111 |
101 @Test | 112 @Test |
102 public void testShouldProceed_PermittedUrl() { | 113 public void testShouldProceed_PermittedUrl() { |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 298 |
288 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid
er.shouldProceed( | 299 WebRestrictionsResult wrongCallingPackage = mSupervisedUserContentProvid
er.shouldProceed( |
289 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); | 300 DEFAULT_CALLING_PACKAGE, "https://accounts.google.com/reauth"); |
290 assertThat(wrongCallingPackage.shouldProceed(), is(false)); | 301 assertThat(wrongCallingPackage.shouldProceed(), is(false)); |
291 | 302 |
292 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide
r.shouldProceed( | 303 WebRestrictionsResult nullCallingPackage = mSupervisedUserContentProvide
r.shouldProceed( |
293 null, "https://accounts.google.com/reauth"); | 304 null, "https://accounts.google.com/reauth"); |
294 assertThat(nullCallingPackage.shouldProceed(), is(false)); | 305 assertThat(nullCallingPackage.shouldProceed(), is(false)); |
295 } | 306 } |
296 } | 307 } |
OLD | NEW |