Index: chrome/android/junit/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProviderTest.java |
diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProviderTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProviderTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f14a90ab7d345b06eb3ce813000234490fc1807a |
--- /dev/null |
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/superviseduser/SupervisedUserContentProviderTest.java |
@@ -0,0 +1,142 @@ |
+// 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.chrome.browser.superviseduser; |
+ |
+import static org.hamcrest.CoreMatchers.is; |
+import static org.junit.Assert.assertThat; |
+import static org.mockito.Matchers.anyLong; |
+import static org.mockito.Matchers.anyString; |
+import static org.mockito.Mockito.doAnswer; |
+import static org.mockito.Mockito.verify; |
+ |
+import android.util.Pair; |
+ |
+import org.chromium.content.browser.BrowserStartupController; |
+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.mockito.invocation.InvocationOnMock; |
+import org.mockito.stubbing.Answer; |
+import org.robolectric.Robolectric; |
+import org.robolectric.annotation.Config; |
+ |
+/** |
+ * Tests of SupervisedUserContentProvider This is tested as a simple class, not as a content |
Bernhard Bauer
2015/12/14 17:58:37
This seems to be missing some punctuation.
aberent
2015/12/16 15:20:37
Done.
|
+ * provider. The content provider aspects are tested with WebRestrictionsContentProviderTest. |
+ */ |
+@RunWith(LocalRobolectricTestRunner.class) |
+@Config(manifest = Config.NONE) |
+public class SupervisedUserContentProviderTest { |
+ private SupervisedUserContentProvider mSupervisedUserContentProvider; |
+ private BrowserStartupController mBrowserStartupController; |
+ private Thread mNativeCallThread; |
+ |
+ @Before |
+ public void setUp() { |
+ mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentProvider()); |
+ mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderForTesting(1234); |
+ } |
+ |
+ @Test |
+ public void testShouldProceed() throws InterruptedException { |
+ class TestRunnable implements Runnable { |
+ private Pair<Boolean, String> mResult; |
+ |
+ @Override |
+ public void run() { |
+ this.mResult = mSupervisedUserContentProvider.shouldProceed("url"); |
+ } |
+ |
+ public Pair<Boolean, String> getResult() { |
+ return mResult; |
+ } |
+ } |
+ doAnswer(new Answer<Void>() { |
+ |
+ @Override |
+ public Void answer(InvocationOnMock invocation) throws Throwable { |
+ mNativeCallThread = Thread.currentThread(); |
+ mSupervisedUserContentProvider.onQueryComplete(true, null); |
+ return null; |
+ } |
+ |
+ }) |
+ .when(mSupervisedUserContentProvider) |
+ .nativeShouldProceed(anyLong(), anyString()); |
+ TestRunnable r1 = new TestRunnable(); |
+ Thread t1 = new Thread(r1); |
+ t1.start(); |
+ // Sleep is needed because of a bug in Robolectric, see |
+ // https://groups.google.com/forum/#!topic/robolectric/mlXNko2NFE0. |
Bernhard Bauer
2015/12/14 17:58:37
Urrr... Note https://groups.google.com/d/msg/robol
aberent
2015/12/16 15:20:37
Done. As discussed.
|
+ Thread.sleep(50); |
+ Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ t1.join(); |
+ verify(mSupervisedUserContentProvider).nativeShouldProceed(1234, "url"); |
+ // Assert has to be on main thread for failures to cause test failure. |
+ assertThat(r1.getResult(), is(new Pair<Boolean, String>(true, null))); |
+ // Check that the native code was called on the right thread. |
+ assertThat(mNativeCallThread, is(Thread.currentThread())); |
+ doAnswer(new Answer<Void>() { |
+ |
+ @Override |
+ public Void answer(InvocationOnMock invocation) throws Throwable { |
+ mNativeCallThread = Thread.currentThread(); |
+ mSupervisedUserContentProvider.onQueryComplete(false, "Hello"); |
+ return null; |
+ } |
+ |
+ }) |
+ .when(mSupervisedUserContentProvider) |
+ .nativeShouldProceed(anyLong(), anyString()); |
+ TestRunnable r2 = new TestRunnable(); |
+ Thread t2 = new Thread(r2); |
+ t2.start(); |
+ Thread.sleep(50); |
+ Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ t2.join(); |
+ assertThat(r2.getResult(), is(new Pair<Boolean, String>(false, "Hello"))); |
+ // Check that the native code was called on the UI thread. |
+ assertThat(mNativeCallThread, is(Thread.currentThread())); |
+ } |
+ |
+ @Test |
+ public void testRequestInsert() throws InterruptedException { |
+ class TestRunnable implements Runnable { |
+ private Pair<Boolean, String> mResult; |
+ |
+ @Override |
+ public void run() { |
+ mSupervisedUserContentProvider.requestInsert("url"); |
+ } |
+ |
+ public Pair<Boolean, String> getResult() { |
+ return mResult; |
+ } |
+ } |
+ doAnswer(new Answer<Void>() { |
+ |
+ @Override |
+ public Void answer(InvocationOnMock invocation) throws Throwable { |
+ mNativeCallThread = Thread.currentThread(); |
+ return null; |
+ } |
+ |
+ }) |
+ .when(mSupervisedUserContentProvider) |
+ .nativeRequestInsert(anyLong(), anyString()); |
+ TestRunnable r1 = new TestRunnable(); |
+ Thread t1 = new Thread(r1); |
+ t1.start(); |
+ // Sleep is needed because of a bug in Robolectric, see |
+ // https://groups.google.com/forum/#!topic/robolectric/mlXNko2NFE0. |
+ Thread.sleep(50); |
+ Robolectric.runUiThreadTasksIncludingDelayedTasks(); |
+ t1.join(); |
+ verify(mSupervisedUserContentProvider).nativeRequestInsert(1234, "url"); |
+ assertThat(mNativeCallThread, is(Thread.currentThread())); |
+ } |
+} |