Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(560)

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProviderUnitTest.java

Issue 2695113002: [Webview, Child Accounts] Automatically sign in to Chrome if needed. (Closed)
Patch Set: Fix instrumentation tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.times; 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 17
18 import android.accounts.Account;
19 import android.content.Context;
20
21 import org.junit.After;
17 import org.junit.Before; 22 import org.junit.Before;
18 import org.junit.Rule; 23 import org.junit.Rule;
19 import org.junit.Test; 24 import org.junit.Test;
20 import org.junit.runner.RunWith; 25 import org.junit.runner.RunWith;
21 import org.mockito.Mockito; 26 import org.mockito.Mockito;
22 import org.mockito.invocation.InvocationOnMock; 27 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer; 28 import org.mockito.stubbing.Answer;
29 import org.robolectric.RuntimeEnvironment;
24 import org.robolectric.annotation.Config; 30 import org.robolectric.annotation.Config;
25 31
32 import org.chromium.base.Callback;
33 import org.chromium.base.ContextUtils;
34 import org.chromium.base.library_loader.ProcessInitException;
26 import org.chromium.chrome.browser.DisableHistogramsRule; 35 import org.chromium.chrome.browser.DisableHistogramsRule;
36 import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
37 import org.chromium.chrome.browser.superviseduser.SupervisedUserContentProvider. SupervisedUserQueryReply;
38 import org.chromium.components.signin.AccountManagerDelegate;
39 import org.chromium.components.signin.AccountManagerHelper;
40 import org.chromium.components.signin.ChromeSigninController;
27 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro vider.WebRestrictionsResult; 41 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro vider.WebRestrictionsResult;
28 import org.chromium.testing.local.LocalRobolectricTestRunner; 42 import org.chromium.testing.local.LocalRobolectricTestRunner;
29 43
30 /** 44 /**
31 * Tests of SupervisedUserContentProvider. This is tested as a simple class, not as a content 45 * Tests of SupervisedUserContentProvider. This is tested as a simple class, not as a content
32 * provider. The content provider aspects are tested with WebRestrictionsContent ProviderTest. 46 * provider. The content provider aspects are tested with WebRestrictionsContent ProviderTest.
33 */ 47 */
34 @RunWith(LocalRobolectricTestRunner.class) 48 @RunWith(LocalRobolectricTestRunner.class)
35 @Config(manifest = Config.NONE) 49 @Config(manifest = Config.NONE)
36 public class SupervisedUserContentProviderUnitTest { 50 public class SupervisedUserContentProviderUnitTest {
37 @Rule 51 @Rule
38 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR ule(); 52 public DisableHistogramsRule mDisableHistogramsRule = new DisableHistogramsR ule();
39 53
40 private SupervisedUserContentProvider mSupervisedUserContentProvider; 54 private SupervisedUserContentProvider mSupervisedUserContentProvider;
41 55
42 @Before 56 @Before
43 public void setUp() { 57 public void setUp() {
44 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr ovider()); 58 // Ensure clean state (in particular not signed in).
45 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor Testing(1234L); 59 ContextUtils.getAppSharedPreferences().edit().clear().apply();
60
61 // 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).
63 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr ovider() {
64 @Override
65 void startForcedSigninProcessor(Context context, Runnable onComplete ) {
66 ChromeSigninController.get(RuntimeEnvironment.application)
67 .setSignedInAccountName("Dummy");
68 onComplete.run();
69 }
70
71 @Override
72 void listenForChildAccountStatusChange(Callback<Boolean> callback) {
73 callback.onResult(true);
74 }
75
76 @Override
77 void nativeShouldProceed(long l, SupervisedUserQueryReply reply, Str ing url) {
78 reply.onQueryComplete();
79 }
80
81 @Override
82 void nativeRequestInsert(long l, SupervisedUserInsertReply reply, St ring url) {
83 reply.onInsertRequestSendComplete(true);
84 }
85
86 @Override
87 long nativeCreateSupervisedUserContentProvider() {
88 return 5678L;
89 }
90 });
91 }
92
93 @After
94 public void shutDown() {
95 ContextUtils.getAppSharedPreferences().edit().clear().apply();
96 ChromeBrowserInitializer.setForTesting(null);
46 } 97 }
47 98
48 @Test 99 @Test
49 public void testShouldProceed() throws InterruptedException { 100 public void testShouldProceed_PermittedUrl() {
101 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor Testing(1234L);
50 // Mock the native call for a permitted URL 102 // Mock the native call for a permitted URL
51 doAnswer(new Answer<Void>() {
52
53 @Override
54 public Void answer(InvocationOnMock invocation) throws Throwable {
55 Object args[] = invocation.getArguments();
56 ((SupervisedUserContentProvider.SupervisedUserQueryReply) args[1 ])
57 .onQueryComplete();
58 return null;
59 }
60
61 })
62 .when(mSupervisedUserContentProvider)
63 .nativeShouldProceed(anyLong(),
64 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class),
65 anyString());
66 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc eed("url"); 103 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc eed("url");
67 assertThat(result.shouldProceed(), is(true)); 104 assertThat(result.shouldProceed(), is(true));
68 verify(mSupervisedUserContentProvider) 105 verify(mSupervisedUserContentProvider)
69 .nativeShouldProceed(eq(1234L), 106 .nativeShouldProceed(eq(1234L),
70 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class), 107 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class),
71 eq("url")); 108 eq("url"));
72 // Mock the native call for a forbidden URL 109 }
110
111 @Test
112 public void testShouldProceed_ForbiddenUrl() {
113 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor Testing(1234L);
114 // Modify the result of the native call to make this a forbidden URL
73 doAnswer(new Answer<Void>() { 115 doAnswer(new Answer<Void>() {
74 116
75 @Override 117 @Override
76 public Void answer(InvocationOnMock invocation) throws Throwable { 118 public Void answer(InvocationOnMock invocation) throws Throwable {
77 Object args[] = invocation.getArguments(); 119 invocation.<SupervisedUserQueryReply>getArgument(1).onQueryFaile d(1, 2, 3, "url1",
78 ((SupervisedUserContentProvider.SupervisedUserQueryReply) args[1 ]) 120 "url2", "custodian", "custodianEmail", "secondCustodian" ,
79 .onQueryFailed(1, 2, 3, "url1", "url2", "custodian", "cu stodianEmail", 121 "secondCustodianEmail");
80 "secondCustodian", "secondCustodianEmail");
81 return null; 122 return null;
82 } 123 }
83 124
84 }) 125 })
85 .when(mSupervisedUserContentProvider) 126 .when(mSupervisedUserContentProvider)
86 .nativeShouldProceed(anyLong(), 127 .nativeShouldProceed(anyLong(),
87 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class), 128 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class),
88 anyString()); 129 anyString());
89 result = mSupervisedUserContentProvider.shouldProceed("url"); 130 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc eed("url");
90 assertThat(result.shouldProceed(), is(false)); 131 assertThat(result.shouldProceed(), is(false));
91 assertThat(result.errorIntCount(), is(3)); 132 assertThat(result.errorIntCount(), is(3));
92 assertThat(result.getErrorInt(0), is(1)); 133 assertThat(result.getErrorInt(0), is(1));
93 assertThat(result.getErrorInt(1), is(2)); 134 assertThat(result.getErrorInt(1), is(2));
94 assertThat(result.getErrorInt(2), is(3)); 135 assertThat(result.getErrorInt(2), is(3));
95 assertThat(result.errorStringCount(), is(6)); 136 assertThat(result.errorStringCount(), is(6));
96 assertThat(result.getErrorString(0), is("url1")); 137 assertThat(result.getErrorString(0), is("url1"));
97 assertThat(result.getErrorString(1), is("url2")); 138 assertThat(result.getErrorString(1), is("url2"));
98 assertThat(result.getErrorString(2), is("custodian")); 139 assertThat(result.getErrorString(2), is("custodian"));
99 assertThat(result.getErrorString(3), is("custodianEmail")); 140 assertThat(result.getErrorString(3), is("custodianEmail"));
100 assertThat(result.getErrorString(4), is("secondCustodian")); 141 assertThat(result.getErrorString(4), is("secondCustodian"));
101 assertThat(result.getErrorString(5), is("secondCustodianEmail")); 142 assertThat(result.getErrorString(5), is("secondCustodianEmail"));
102 } 143 }
103 144
104 @Test 145 @Test
105 public void testRequestInsert() throws InterruptedException { 146 public void testRequestInsert_ok() {
106 // Mock native call. 147 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor Testing(1234L);
107 doAnswer(new Answer<Void>() {
108 148
109 @Override 149 assertThat(mSupervisedUserContentProvider.requestInsert("url"), is(true) );
110 public Void answer(InvocationOnMock invocation) throws Throwable {
111 Object args[] = invocation.getArguments();
112 ((SupervisedUserContentProvider.SupervisedUserInsertReply) args[ 1])
113 .onInsertRequestSendComplete(true);
114 return null;
115 }
116 150
117 })
118 .when(mSupervisedUserContentProvider)
119 .nativeRequestInsert(anyLong(),
120 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class),
121 anyString());
122 assertThat(mSupervisedUserContentProvider.requestInsert("url"), is(true) );
123 verify(mSupervisedUserContentProvider) 151 verify(mSupervisedUserContentProvider)
124 .nativeRequestInsert(eq(1234L), 152 .nativeRequestInsert(eq(1234L),
125 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), 153 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class),
126 eq("url")); 154 eq("url"));
155 }
156
157 @Test
158 public void testRequestInsert_failed() {
159 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor Testing(1234L);
160 // Mock the native call to mock failure
127 doAnswer(new Answer<Void>() { 161 doAnswer(new Answer<Void>() {
128
129 @Override 162 @Override
130 public Void answer(InvocationOnMock invocation) throws Throwable { 163 public Void answer(InvocationOnMock invocation) throws Throwable {
131 Object args[] = invocation.getArguments(); 164 Object args[] = invocation.getArguments();
132 ((SupervisedUserContentProvider.SupervisedUserInsertReply) args[ 1]) 165 ((SupervisedUserContentProvider.SupervisedUserInsertReply) args[ 1])
133 .onInsertRequestSendComplete(false); 166 .onInsertRequestSendComplete(false);
134 return null; 167 return null;
135 } 168 }
136 169
137 }) 170 })
138 .when(mSupervisedUserContentProvider) 171 .when(mSupervisedUserContentProvider)
139 .nativeRequestInsert(anyLong(), 172 .nativeRequestInsert(anyLong(),
140 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), 173 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class),
141 anyString()); 174 anyString());
142 assertThat(mSupervisedUserContentProvider.requestInsert("url"), is(false )); 175 assertThat(mSupervisedUserContentProvider.requestInsert("url"), is(false ));
143 verify(mSupervisedUserContentProvider, times(2)) 176 verify(mSupervisedUserContentProvider)
144 .nativeRequestInsert(eq(1234L), 177 .nativeRequestInsert(eq(1234L),
145 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class), 178 any(SupervisedUserContentProvider.SupervisedUserInsertRe ply.class),
146 eq("url")); 179 eq("url"));
147 } 180 }
181
182 @Test
183 public void testShouldProceed_withStartupSignedIn() throws ProcessInitExcept ion {
184 // Set up a signed in user
185 ChromeSigninController.get(RuntimeEnvironment.application).setSignedInAc countName("Dummy");
186 // Mock things called during startup
187 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class);
188 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer);
189
190 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc eed("url");
191
192 assertThat(result.shouldProceed(), is(true));
193 verify(mockBrowserInitializer).handleSynchronousStartup();
194 verify(mSupervisedUserContentProvider)
195 .nativeShouldProceed(eq(5678L),
196 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class),
197 eq("url"));
198 }
199
200 @SuppressWarnings("unchecked")
201 @Test
202 public void testShouldProceed_notSignedIn() throws ProcessInitException {
203 // Mock things called during startup
204 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class);
205 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer);
206 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class) ;
207 AccountManagerHelper.overrideAccountManagerHelperForTests(
208 RuntimeEnvironment.application, mockDelegate);
209 Account account = new Account("Google", "Dummy");
210 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[] {account});
211
212 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc eed("url");
213
214 assertThat(result.shouldProceed(), is(true));
215 verify(mockBrowserInitializer).handleSynchronousStartup();
216 verify(mSupervisedUserContentProvider)
217 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla ss));
218 verify(mSupervisedUserContentProvider)
219 .listenForChildAccountStatusChange(any(Callback.class));
220 verify(mSupervisedUserContentProvider)
221 .nativeShouldProceed(eq(5678L),
222 any(SupervisedUserContentProvider.SupervisedUserQueryRep ly.class),
223 eq("url"));
224 }
225
226 @Test
227 public void testShouldProceed_cannotSignIn() throws ProcessInitException {
228 // Mock things called during startup
229 ChromeBrowserInitializer mockBrowserInitializer = mock(ChromeBrowserInit ializer.class);
230 ChromeBrowserInitializer.setForTesting(mockBrowserInitializer);
231 AccountManagerDelegate mockDelegate = mock(AccountManagerDelegate.class) ;
232 AccountManagerHelper.overrideAccountManagerHelperForTests(
233 RuntimeEnvironment.application, mockDelegate);
234 Account account = new Account("Google", "Dummy");
235 when(mockDelegate.getAccountsByType("Google")).thenReturn(new Account[] {account});
236
237 // Change the behavior of the forced sign-in processor to not sign in.
238 doAnswer(new Answer<Void>() {
239 @Override
240 public Void answer(InvocationOnMock invocation) throws Throwable {
241 invocation.<Runnable>getArgument(1).run();
242 return null;
243 }
244 })
245 .when(mSupervisedUserContentProvider)
246 .startForcedSigninProcessor(any(Context.class), any(Runnable.cla ss));
247
248 WebRestrictionsResult result = mSupervisedUserContentProvider.shouldProc eed("url");
249
250 assertThat(result.shouldProceed(), is(false));
251 assertThat(result.getErrorInt(0), is(5));
252 }
148 } 253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698