Chromium Code Reviews| Index: components/web_restriction/junit/src/org/chromium/components/webrestriction/WebRestrictionsContentProviderTest.java |
| diff --git a/components/web_restriction/junit/src/org/chromium/components/webrestriction/WebRestrictionsContentProviderTest.java b/components/web_restriction/junit/src/org/chromium/components/webrestriction/WebRestrictionsContentProviderTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0b3788cc441e03c4556a127de5d2ccbd5992c741 |
| --- /dev/null |
| +++ b/components/web_restriction/junit/src/org/chromium/components/webrestriction/WebRestrictionsContentProviderTest.java |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.components.webrestriction; |
| + |
| +import static org.hamcrest.CoreMatchers.is; |
| +import static org.hamcrest.CoreMatchers.not; |
| +import static org.hamcrest.CoreMatchers.nullValue; |
| +import static org.junit.Assert.assertThat; |
| +import static org.mockito.Matchers.anyString; |
| +import static org.mockito.Mockito.verify; |
| +import static org.mockito.Mockito.when; |
| + |
| +import android.content.ContentResolver; |
| +import android.content.ContentValues; |
| +import android.database.Cursor; |
| +import android.net.Uri; |
| +import android.util.Pair; |
| + |
| +import org.chromium.testing.local.LocalRobolectricTestRunner; |
| +import org.junit.Before; |
| +import org.junit.Test; |
| +import org.junit.runner.RunWith; |
| +import org.mockito.Mockito; |
| +import org.robolectric.Robolectric; |
| +import org.robolectric.annotation.Config; |
| +import org.robolectric.shadows.ShadowContentResolver; |
| + |
| +/** |
| + * Tests of WebRestrictionsContentProvider. |
| + */ |
| +@RunWith(LocalRobolectricTestRunner.class) |
| +@Config(manifest = Config.NONE) |
| +public class WebRestrictionsContentProviderTest { |
| + private static final String AUTHORITY = "org.chromium.browser.DummyProvider"; |
| + |
| + private class DummyContentProvider extends WebRestrictionsContentProvider { |
| + @Override |
| + protected Pair<Boolean, String> shouldProceed(String url) { |
| + // TODO Auto-generated method stub |
|
Bernhard Bauer
2015/12/14 17:58:38
Pls clean up 😃
aberent
2015/12/16 15:20:37
Done.
|
| + return null; |
| + } |
| + |
| + @Override |
| + protected boolean canInsert() { |
| + // TODO Auto-generated method stub |
| + return false; |
| + } |
| + |
| + @Override |
| + protected void requestInsert(String url) { |
| + // TODO Auto-generated method stub |
| + } |
| + } |
| + |
| + private DummyContentProvider mContentProvider; |
| + private ContentResolver mContentResolver; |
| + private Uri mUri; |
| + |
| + @Before |
| + public void setUp() { |
| + mContentProvider = Mockito.spy(new DummyContentProvider()); |
| + mContentProvider.onCreate(); |
| + ShadowContentResolver.registerProvider(AUTHORITY, mContentProvider); |
| + mContentResolver = Robolectric.application.getContentResolver(); |
| + mUri = new Uri.Builder() |
| + .scheme(ContentResolver.SCHEME_CONTENT) |
| + .authority(AUTHORITY) |
| + .build(); |
| + } |
| + |
| + @Test |
| + public void testQuery() { |
| + when(mContentProvider.shouldProceed(anyString())) |
| + .thenReturn(new Pair<Boolean, String>(false, "Error Message")); |
| + Cursor cursor = mContentResolver.query(mUri, null, "url = 'dummy'", null, null); |
| + verify(mContentProvider).shouldProceed("dummy"); |
| + assertThat(cursor, is(not(nullValue()))); |
| + assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.BLOCKED)); |
| + assertThat(cursor.getString(1), is("Error Message")); |
| + when(mContentProvider.shouldProceed(anyString())) |
| + .thenReturn(new Pair<Boolean, String>(true, null)); |
| + cursor = mContentResolver.query(mUri, null, "url = 'dummy'", null, null); |
| + assertThat(cursor, is(not(nullValue()))); |
| + assertThat(cursor.getInt(0), is(WebRestrictionsContentProvider.PROCEED)); |
| + } |
| + |
| + @Test |
| + public void testInsert() { |
| + ContentValues values = new ContentValues(); |
| + values.put("url", "dummy2"); |
| + mContentResolver.insert(mUri, values); |
| + verify(mContentProvider).requestInsert("dummy2"); |
| + } |
| + |
| + @Test |
| + public void testGetType() { |
| + when(mContentProvider.canInsert()).thenReturn(false); |
| + assertThat(mContentResolver.getType(mUri), is(nullValue())); |
| + when(mContentProvider.canInsert()).thenReturn(true); |
| + assertThat(mContentResolver.getType(mUri), is(not(nullValue()))); |
| + } |
| +} |