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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProviderTest.java

Issue 2831823003: Convert ChromeActivityTestCaseBase direct children to JUnit4 (Closed)
Patch Set: rebase and convert newly added test Created 3 years, 7 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 android.accounts.Account; 7 import android.accounts.Account;
8 import android.content.ContentProviderClient; 8 import android.content.ContentProviderClient;
9 import android.content.ContentResolver; 9 import android.content.ContentResolver;
10 import android.database.Cursor; 10 import android.database.Cursor;
11 import android.net.Uri; 11 import android.net.Uri;
12 import android.os.RemoteException; 12 import android.os.RemoteException;
13 import android.support.test.InstrumentationRegistry;
13 import android.support.test.filters.SmallTest; 14 import android.support.test.filters.SmallTest;
14 15
16 import org.junit.After;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
15 import org.chromium.base.ThreadUtils; 23 import org.chromium.base.ThreadUtils;
24 import org.chromium.base.test.util.CommandLineFlags;
16 import org.chromium.base.test.util.RetryOnFailure; 25 import org.chromium.base.test.util.RetryOnFailure;
17 import org.chromium.chrome.browser.ChromeActivity; 26 import org.chromium.chrome.browser.ChromeActivity;
27 import org.chromium.chrome.browser.ChromeSwitches;
18 import org.chromium.chrome.browser.childaccounts.ChildAccountService; 28 import org.chromium.chrome.browser.childaccounts.ChildAccountService;
19 import org.chromium.chrome.browser.preferences.PrefServiceBridge; 29 import org.chromium.chrome.browser.preferences.PrefServiceBridge;
20 import org.chromium.chrome.test.ChromeActivityTestCaseBase; 30 import org.chromium.chrome.test.ChromeActivityTestRule;
31 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
21 import org.chromium.chrome.test.util.browser.signin.SigninTestUtil; 32 import org.chromium.chrome.test.util.browser.signin.SigninTestUtil;
22 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro vider; 33 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro vider;
23 34
24 import java.util.concurrent.Callable; 35 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutionException; 36 import java.util.concurrent.ExecutionException;
26 37
27 /** 38 /**
28 * Instrumentation test for SupervisedUserContentProvider. 39 * Instrumentation test for SupervisedUserContentProvider.
29 */ 40 */
41 @RunWith(ChromeJUnit4ClassRunner.class)
30 @RetryOnFailure 42 @RetryOnFailure
31 public class SupervisedUserContentProviderTest extends ChromeActivityTestCaseBas e<ChromeActivity> { 43 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
44 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG})
45 public class SupervisedUserContentProviderTest {
46 @Rule
47 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
48 new ChromeActivityTestRule<>(ChromeActivity.class);
49
32 private static final String DEFAULT_ACCOUNT = "test@gmail.com"; 50 private static final String DEFAULT_ACCOUNT = "test@gmail.com";
33 private static final String AUTHORITY_SUFFIX = ".SupervisedUserProvider"; 51 private static final String AUTHORITY_SUFFIX = ".SupervisedUserProvider";
34 private ContentResolver mResolver; 52 private ContentResolver mResolver;
35 private String mAuthority; 53 private String mAuthority;
36 private Uri mUri; 54 private Uri mUri;
37 55
38 public SupervisedUserContentProviderTest() { 56 @Before
39 super(ChromeActivity.class); 57 public void setUp() throws Exception {
40 } 58 SigninTestUtil.setUpAuthForTest(InstrumentationRegistry.getInstrumentati on());
41 59
42 @Override 60 // In principle the SupervisedUserContentProvider should work whenever C hrome is installed
43 public void setUp() throws Exception { 61 // (even if it isn't running), but to test it we need to set up a dummy child, and to do
44 super.setUp(); 62 // this within a test we need to start Chrome.
45 mResolver = getInstrumentation().getTargetContext().getContentResolver() ; 63 mActivityTestRule.startMainActivityOnBlankPage();
46 assertNotNull(mResolver); 64 mResolver = InstrumentationRegistry.getInstrumentation()
47 mAuthority = getInstrumentation().getTargetContext().getPackageName() + AUTHORITY_SUFFIX; 65 .getTargetContext()
66 .getContentResolver();
67 Assert.assertNotNull(mResolver);
68 mAuthority =
69 InstrumentationRegistry.getInstrumentation().getTargetContext(). getPackageName()
70 + AUTHORITY_SUFFIX;
48 mUri = new Uri.Builder() 71 mUri = new Uri.Builder()
49 .scheme(ContentResolver.SCHEME_CONTENT) 72 .scheme(ContentResolver.SCHEME_CONTENT)
50 .authority(mAuthority) 73 .authority(mAuthority)
51 .path("authorized") 74 .path("authorized")
52 .build(); 75 .build();
53 SigninTestUtil.resetSigninState(); 76 SigninTestUtil.resetSigninState();
54 } 77 }
55 78
56 @Override 79 @After
57 public void tearDown() throws Exception { 80 public void tearDown() throws Exception {
58 SigninTestUtil.resetSigninState(); 81 SigninTestUtil.resetSigninState();
59 SigninTestUtil.tearDownAuthForTest(); 82 SigninTestUtil.tearDownAuthForTest();
60 super.tearDown();
61 } 83 }
62 84
63 @Override 85 @Test
64 public void startMainActivity() throws InterruptedException {
65 SigninTestUtil.setUpAuthForTest(getInstrumentation());
66
67 // In principle the SupervisedUserContentProvider should work whenever C hrome is installed
68 // (even if it isn't running), but to test it we need to set up a dummy child, and to do
69 // this within a test we need to start Chrome.
70 startMainActivityOnBlankPage();
71 }
72
73 @SmallTest 86 @SmallTest
74 public void testSupervisedUserDisabled() throws RemoteException, ExecutionEx ception { 87 public void testSupervisedUserDisabled() throws RemoteException, ExecutionEx ception {
75 ContentProviderClient client = mResolver.acquireContentProviderClient(mA uthority); 88 ContentProviderClient client = mResolver.acquireContentProviderClient(mA uthority);
76 assertNotNull(client); 89 Assert.assertNotNull(client);
77 Cursor cursor = client.query(mUri, null, "url = 'http://google.com'", nu ll, null); 90 Cursor cursor = client.query(mUri, null, "url = 'http://google.com'", nu ll, null);
78 assertNull(cursor); 91 Assert.assertNull(cursor);
79 } 92 }
80 93
94 @Test
81 @SmallTest 95 @SmallTest
82 public void testNoSupervisedUser() throws RemoteException, ExecutionExceptio n { 96 public void testNoSupervisedUser() throws RemoteException, ExecutionExceptio n {
83 assertFalse(ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() { 97 Assert.assertFalse(ThreadUtils.runOnUiThreadBlocking(new Callable<Boolea n>() {
84 98
85 @Override 99 @Override
86 public Boolean call() throws Exception { 100 public Boolean call() throws Exception {
87 PrefServiceBridge.getInstance().setSupervisedUserId(""); 101 PrefServiceBridge.getInstance().setSupervisedUserId("");
88 return ChildAccountService.isChildAccount(); 102 return ChildAccountService.isChildAccount();
89 } 103 }
90 104
91 })); 105 }));
92 ContentProviderClient client = mResolver.acquireContentProviderClient(mA uthority); 106 ContentProviderClient client = mResolver.acquireContentProviderClient(mA uthority);
93 assertNotNull(client); 107 Assert.assertNotNull(client);
94 SupervisedUserContentProvider.enableContentProviderForTesting(); 108 SupervisedUserContentProvider.enableContentProviderForTesting();
95 Cursor cursor = client.query(mUri, null, "url = 'http://google.com'", nu ll, null); 109 Cursor cursor = client.query(mUri, null, "url = 'http://google.com'", nu ll, null);
96 assertNotNull(cursor); 110 Assert.assertNotNull(cursor);
97 assertEquals(WebRestrictionsContentProvider.BLOCKED, cursor.getInt(0)); 111 Assert.assertEquals(WebRestrictionsContentProvider.BLOCKED, cursor.getIn t(0));
98 cursor = client.query(mUri, null, "url = 'http://www.notgoogle.com'", nu ll, null); 112 cursor = client.query(mUri, null, "url = 'http://www.notgoogle.com'", nu ll, null);
99 assertNotNull(cursor); 113 Assert.assertNotNull(cursor);
100 assertEquals(WebRestrictionsContentProvider.BLOCKED, cursor.getInt(0)); 114 Assert.assertEquals(WebRestrictionsContentProvider.BLOCKED, cursor.getIn t(0));
101 } 115 }
102 116
117 @Test
103 @SmallTest 118 @SmallTest
104 public void testWithSupervisedUser() throws RemoteException, ExecutionExcept ion { 119 public void testWithSupervisedUser() throws RemoteException, ExecutionExcept ion {
105 final Account account = SigninTestUtil.addAndSignInTestAccount(); 120 final Account account = SigninTestUtil.addAndSignInTestAccount();
106 assertNotNull(account); 121 Assert.assertNotNull(account);
107 assertTrue(ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean>() { 122 Assert.assertTrue(ThreadUtils.runOnUiThreadBlocking(new Callable<Boolean >() {
108 123
109 @Override 124 @Override
110 public Boolean call() throws Exception { 125 public Boolean call() throws Exception {
111 PrefServiceBridge.getInstance().setSupervisedUserId("ChildAccoun tSUID"); 126 PrefServiceBridge.getInstance().setSupervisedUserId("ChildAccoun tSUID");
112 return ChildAccountService.isChildAccount(); 127 return ChildAccountService.isChildAccount();
113 } 128 }
114 129
115 })); 130 }));
116 ContentProviderClient client = mResolver.acquireContentProviderClient(mA uthority); 131 ContentProviderClient client = mResolver.acquireContentProviderClient(mA uthority);
117 assertNotNull(client); 132 Assert.assertNotNull(client);
118 SupervisedUserContentProvider.enableContentProviderForTesting(); 133 SupervisedUserContentProvider.enableContentProviderForTesting();
119 // setFilter for testing sets a default filter that blocks by default. 134 // setFilter for testing sets a default filter that blocks by default.
120 mResolver.call(mUri, "setFilterForTesting", null, null); 135 mResolver.call(mUri, "setFilterForTesting", null, null);
121 Cursor cursor = client.query(mUri, null, "url = 'http://www.google.com'" , null, null); 136 Cursor cursor = client.query(mUri, null, "url = 'http://www.google.com'" , null, null);
122 assertNotNull(cursor); 137 Assert.assertNotNull(cursor);
123 assertEquals(WebRestrictionsContentProvider.BLOCKED, cursor.getInt(0)); 138 Assert.assertEquals(WebRestrictionsContentProvider.BLOCKED, cursor.getIn t(0));
124 } 139 }
125 } 140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698