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.components.webrestrictions.browser; | 5 package org.chromium.components.webrestrictions.browser; |
6 | 6 |
7 import static org.hamcrest.CoreMatchers.is; | 7 import static org.hamcrest.CoreMatchers.is; |
8 import static org.hamcrest.CoreMatchers.not; | 8 import static org.hamcrest.CoreMatchers.not; |
9 import static org.hamcrest.CoreMatchers.nullValue; | 9 import static org.hamcrest.CoreMatchers.nullValue; |
10 import static org.junit.Assert.assertThat; | 10 import static org.junit.Assert.assertThat; |
11 import static org.mockito.ArgumentMatchers.anyString; | 11 import static org.mockito.ArgumentMatchers.anyString; |
12 import static org.mockito.Mockito.verify; | 12 import static org.mockito.Mockito.verify; |
13 import static org.mockito.Mockito.when; | 13 import static org.mockito.Mockito.when; |
14 | 14 |
15 import android.content.ContentResolver; | 15 import android.content.ContentResolver; |
16 import android.content.ContentValues; | 16 import android.content.ContentValues; |
17 import android.content.pm.ProviderInfo; | 17 import android.content.pm.ProviderInfo; |
18 import android.database.Cursor; | 18 import android.database.Cursor; |
19 import android.net.Uri; | 19 import android.net.Uri; |
20 | 20 |
21 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro
vider.WebRestrictionsResult; | |
22 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
23 import org.junit.Before; | 21 import org.junit.Before; |
24 import org.junit.Test; | 22 import org.junit.Test; |
25 import org.junit.runner.RunWith; | 23 import org.junit.runner.RunWith; |
| 24 import org.mockito.ArgumentMatchers; |
26 import org.mockito.Mockito; | 25 import org.mockito.Mockito; |
27 import org.robolectric.RuntimeEnvironment; | 26 import org.robolectric.RuntimeEnvironment; |
28 import org.robolectric.annotation.Config; | 27 import org.robolectric.annotation.Config; |
29 import org.robolectric.shadows.ShadowContentResolver; | 28 import org.robolectric.shadows.ShadowContentResolver; |
30 | 29 |
| 30 import org.chromium.components.webrestrictions.browser.WebRestrictionsContentPro
vider.WebRestrictionsResult; |
| 31 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 32 |
31 /** | 33 /** |
32 * Tests of WebRestrictionsContentProvider. | 34 * Tests of WebRestrictionsContentProvider. |
33 */ | 35 */ |
34 @RunWith(LocalRobolectricTestRunner.class) | 36 @RunWith(LocalRobolectricTestRunner.class) |
35 @Config(manifest = Config.NONE) | 37 @Config(manifest = Config.NONE) |
36 public class WebRestrictionsContentProviderTest { | 38 public class WebRestrictionsContentProviderTest { |
37 private static final String AUTHORITY = "org.chromium.browser.DummyProvider"
; | 39 private static final String AUTHORITY = "org.chromium.browser.DummyProvider"
; |
38 | 40 |
39 private WebRestrictionsContentProvider mContentProvider; | 41 private WebRestrictionsContentProvider mContentProvider; |
40 private ContentResolver mContentResolver; | 42 private ContentResolver mContentResolver; |
41 private Uri mUri; | 43 private Uri mUri; |
42 | 44 |
43 @Before | 45 @Before |
44 public void setUp() { | 46 public void setUp() { |
45 // The test needs a concrete version of the test class, but mocks the ad
ditional members as | 47 // The test needs a concrete version of the test class, but mocks the ad
ditional members as |
46 // necessary. | 48 // necessary. |
47 mContentProvider = Mockito.spy(new WebRestrictionsContentProvider() { | 49 mContentProvider = Mockito.spy(new WebRestrictionsContentProvider() { |
48 @Override | 50 @Override |
49 protected WebRestrictionsResult shouldProceed(String url) { | 51 protected WebRestrictionsResult shouldProceed(String callingPackage,
String url) { |
50 return null; | 52 return null; |
51 } | 53 } |
52 | 54 |
53 @Override | 55 @Override |
54 protected boolean canInsert() { | 56 protected boolean canInsert() { |
55 return false; | 57 return false; |
56 } | 58 } |
57 | 59 |
58 @Override | 60 @Override |
59 protected boolean requestInsert(String url) { | 61 protected boolean requestInsert(String url) { |
(...skipping 25 matching lines...) Expand all Loading... |
85 @Test | 87 @Test |
86 public void testQuery() { | 88 public void testQuery() { |
87 when(mContentProvider.contentProviderEnabled()).thenReturn(false); | 89 when(mContentProvider.contentProviderEnabled()).thenReturn(false); |
88 assertThat(mContentResolver.query(mUri.buildUpon().appendPath("authorize
d").build(), null, | 90 assertThat(mContentResolver.query(mUri.buildUpon().appendPath("authorize
d").build(), null, |
89 "url = 'dummy'", null, null), | 91 "url = 'dummy'", null, null), |
90 is(nullValue())); | 92 is(nullValue())); |
91 when(mContentProvider.contentProviderEnabled()).thenReturn(true); | 93 when(mContentProvider.contentProviderEnabled()).thenReturn(true); |
92 int errorInt[] = {42}; | 94 int errorInt[] = {42}; |
93 String errorString[] = {"Error Message"}; | 95 String errorString[] = {"Error Message"}; |
94 WebRestrictionsResult result = new WebRestrictionsResult(false, errorInt
, errorString); | 96 WebRestrictionsResult result = new WebRestrictionsResult(false, errorInt
, errorString); |
95 when(mContentProvider.shouldProceed(anyString())).thenReturn(result); | 97 when(mContentProvider.shouldProceed(ArgumentMatchers.<String>isNull(), a
nyString())) |
| 98 .thenReturn(result); |
96 Cursor cursor = mContentResolver.query(mUri.buildUpon().appendPath("auth
orized").build(), | 99 Cursor cursor = mContentResolver.query(mUri.buildUpon().appendPath("auth
orized").build(), |
97 null, "url = 'dummy'", null, null); | 100 null, "url = 'dummy'", null, null); |
98 verify(mContentProvider).shouldProceed("dummy"); | 101 verify(mContentProvider).shouldProceed(null, "dummy"); |
99 assertThat(cursor, is(not(nullValue()))); | 102 assertThat(cursor, is(not(nullValue()))); |
100 assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.BLOCKED))
; | 103 assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.BLOCKED))
; |
101 assertThat(cursor.getInt(1), is(42)); | 104 assertThat(cursor.getInt(1), is(42)); |
102 assertThat(cursor.getString(2), is("Error Message")); | 105 assertThat(cursor.getString(2), is("Error Message")); |
103 result = new WebRestrictionsResult(true, null, null); | 106 result = new WebRestrictionsResult(true, null, null); |
104 when(mContentProvider.shouldProceed(anyString())).thenReturn(result); | 107 when(mContentProvider.shouldProceed(ArgumentMatchers.<String>isNull(), a
nyString())) |
| 108 .thenReturn(result); |
105 cursor = mContentResolver.query(mUri.buildUpon().appendPath("authorized"
).build(), null, | 109 cursor = mContentResolver.query(mUri.buildUpon().appendPath("authorized"
).build(), null, |
106 "url = 'dummy'", null, null); | 110 "url = 'dummy'", null, null); |
107 assertThat(cursor, is(not(nullValue()))); | 111 assertThat(cursor, is(not(nullValue()))); |
108 assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.PROCEED))
; | 112 assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.PROCEED))
; |
109 } | 113 } |
110 | 114 |
111 @Test | 115 @Test |
112 public void testInsert() { | 116 public void testInsert() { |
113 ContentValues values = new ContentValues(); | 117 ContentValues values = new ContentValues(); |
114 values.put("url", "dummy2"); | 118 values.put("url", "dummy2"); |
(...skipping 24 matching lines...) Expand all Loading... |
139 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("request
ed").build()), | 143 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("request
ed").build()), |
140 is(nullValue())); | 144 is(nullValue())); |
141 when(mContentProvider.canInsert()).thenReturn(true); | 145 when(mContentProvider.canInsert()).thenReturn(true); |
142 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("request
ed").build()), | 146 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("request
ed").build()), |
143 is(not(nullValue()))); | 147 is(not(nullValue()))); |
144 when(mContentProvider.contentProviderEnabled()).thenReturn(false); | 148 when(mContentProvider.contentProviderEnabled()).thenReturn(false); |
145 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("junk").
build()), | 149 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("junk").
build()), |
146 is(nullValue())); | 150 is(nullValue())); |
147 } | 151 } |
148 } | 152 } |
OLD | NEW |