OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.components.webrestriction; |
| 6 |
| 7 import static org.hamcrest.CoreMatchers.is; |
| 8 import static org.hamcrest.CoreMatchers.not; |
| 9 import static org.hamcrest.CoreMatchers.nullValue; |
| 10 import static org.junit.Assert.assertThat; |
| 11 import static org.mockito.Matchers.anyString; |
| 12 import static org.mockito.Mockito.verify; |
| 13 import static org.mockito.Mockito.when; |
| 14 |
| 15 import android.content.ContentResolver; |
| 16 import android.content.ContentValues; |
| 17 import android.content.pm.ProviderInfo; |
| 18 import android.database.Cursor; |
| 19 import android.net.Uri; |
| 20 import android.util.Pair; |
| 21 |
| 22 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 23 import org.junit.Before; |
| 24 import org.junit.Test; |
| 25 import org.junit.runner.RunWith; |
| 26 import org.mockito.Mockito; |
| 27 import org.robolectric.Robolectric; |
| 28 import org.robolectric.annotation.Config; |
| 29 import org.robolectric.shadows.ShadowContentResolver; |
| 30 |
| 31 /** |
| 32 * Tests of WebRestrictionsContentProvider. |
| 33 */ |
| 34 @RunWith(LocalRobolectricTestRunner.class) |
| 35 @Config(manifest = Config.NONE) |
| 36 public class WebRestrictionsContentProviderTest { |
| 37 private static final String AUTHORITY = "org.chromium.browser.DummyProvider"
; |
| 38 |
| 39 private WebRestrictionsContentProvider mContentProvider; |
| 40 private ContentResolver mContentResolver; |
| 41 private Uri mUri; |
| 42 |
| 43 @Before |
| 44 public void setUp() { |
| 45 // The test needs a concrete version of the test class, but mocks the ad
ditional members as |
| 46 // necessary. |
| 47 mContentProvider = Mockito.spy(new WebRestrictionsContentProvider() { |
| 48 @Override |
| 49 protected Pair<Boolean, String> shouldProceed(String url) { |
| 50 return null; |
| 51 } |
| 52 |
| 53 @Override |
| 54 protected boolean canInsert() { |
| 55 return false; |
| 56 } |
| 57 |
| 58 @Override |
| 59 protected boolean requestInsert(String url) { |
| 60 return false; |
| 61 } |
| 62 }); |
| 63 mContentProvider.onCreate(); |
| 64 ShadowContentResolver.registerProvider(AUTHORITY, mContentProvider); |
| 65 ProviderInfo info = new ProviderInfo(); |
| 66 info.authority = AUTHORITY; |
| 67 mContentProvider.attachInfo(null, info); |
| 68 mContentResolver = Robolectric.application.getContentResolver(); |
| 69 mUri = new Uri.Builder() |
| 70 .scheme(ContentResolver.SCHEME_CONTENT) |
| 71 .authority(AUTHORITY) |
| 72 .build(); |
| 73 } |
| 74 |
| 75 @Test |
| 76 public void testQuery() { |
| 77 when(mContentProvider.shouldProceed(anyString())) |
| 78 .thenReturn(new Pair<Boolean, String>(false, "Error Message")); |
| 79 Cursor cursor = mContentResolver.query(mUri.buildUpon().appendPath("auth
orized").build(), |
| 80 null, "url = 'dummy'", null, null); |
| 81 verify(mContentProvider).shouldProceed("dummy"); |
| 82 assertThat(cursor, is(not(nullValue()))); |
| 83 assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.BLOCKED))
; |
| 84 assertThat(cursor.getString(1), is("Error Message")); |
| 85 when(mContentProvider.shouldProceed(anyString())) |
| 86 .thenReturn(new Pair<Boolean, String>(true, null)); |
| 87 cursor = mContentResolver.query(mUri.buildUpon().appendPath("authorized"
).build(), null, |
| 88 "url = 'dummy'", null, null); |
| 89 assertThat(cursor, is(not(nullValue()))); |
| 90 assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.PROCEED))
; |
| 91 } |
| 92 |
| 93 @Test |
| 94 public void testInsert() { |
| 95 ContentValues values = new ContentValues(); |
| 96 values.put("url", "dummy2"); |
| 97 when(mContentProvider.requestInsert(anyString())).thenReturn(false); |
| 98 assertThat( |
| 99 mContentResolver.insert(mUri.buildUpon().appendPath("requested")
.build(), values), |
| 100 is(nullValue())); |
| 101 verify(mContentProvider).requestInsert("dummy2"); |
| 102 values.put("url", "dummy3"); |
| 103 when(mContentProvider.requestInsert(anyString())).thenReturn(true); |
| 104 assertThat( |
| 105 mContentResolver.insert(mUri.buildUpon().appendPath("requested")
.build(), values), |
| 106 is(not(nullValue()))); |
| 107 verify(mContentProvider).requestInsert("dummy3"); |
| 108 } |
| 109 |
| 110 @Test |
| 111 public void testGetType() { |
| 112 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("junk").
build()), |
| 113 is(nullValue())); |
| 114 when(mContentProvider.canInsert()).thenReturn(false); |
| 115 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("request
ed").build()), |
| 116 is(nullValue())); |
| 117 when(mContentProvider.canInsert()).thenReturn(true); |
| 118 assertThat(mContentResolver.getType(mUri.buildUpon().appendPath("request
ed").build()), |
| 119 is(not(nullValue()))); |
| 120 } |
| 121 } |
OLD | NEW |