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.chrome.browser.superviseduser; | |
6 | |
7 import static org.hamcrest.CoreMatchers.is; | |
8 import static org.junit.Assert.assertThat; | |
9 import static org.mockito.Matchers.anyLong; | |
10 import static org.mockito.Matchers.anyString; | |
11 import static org.mockito.Mockito.doAnswer; | |
12 import static org.mockito.Mockito.verify; | |
13 | |
14 import android.util.Pair; | |
15 | |
16 import org.chromium.content.browser.BrowserStartupController; | |
17 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
18 import org.junit.Before; | |
19 import org.junit.Test; | |
20 import org.junit.runner.RunWith; | |
21 import org.mockito.Mockito; | |
22 import org.mockito.invocation.InvocationOnMock; | |
23 import org.mockito.stubbing.Answer; | |
24 import org.robolectric.Robolectric; | |
25 import org.robolectric.annotation.Config; | |
26 | |
27 /** | |
28 * 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.
| |
29 * provider. The content provider aspects are tested with WebRestrictionsContent ProviderTest. | |
30 */ | |
31 @RunWith(LocalRobolectricTestRunner.class) | |
32 @Config(manifest = Config.NONE) | |
33 public class SupervisedUserContentProviderTest { | |
34 private SupervisedUserContentProvider mSupervisedUserContentProvider; | |
35 private BrowserStartupController mBrowserStartupController; | |
36 private Thread mNativeCallThread; | |
37 | |
38 @Before | |
39 public void setUp() { | |
40 mSupervisedUserContentProvider = Mockito.spy(new SupervisedUserContentPr ovider()); | |
41 mSupervisedUserContentProvider.setNativeSupervisedUserContentProviderFor Testing(1234); | |
42 } | |
43 | |
44 @Test | |
45 public void testShouldProceed() throws InterruptedException { | |
46 class TestRunnable implements Runnable { | |
47 private Pair<Boolean, String> mResult; | |
48 | |
49 @Override | |
50 public void run() { | |
51 this.mResult = mSupervisedUserContentProvider.shouldProceed("url "); | |
52 } | |
53 | |
54 public Pair<Boolean, String> getResult() { | |
55 return mResult; | |
56 } | |
57 } | |
58 doAnswer(new Answer<Void>() { | |
59 | |
60 @Override | |
61 public Void answer(InvocationOnMock invocation) throws Throwable { | |
62 mNativeCallThread = Thread.currentThread(); | |
63 mSupervisedUserContentProvider.onQueryComplete(true, null); | |
64 return null; | |
65 } | |
66 | |
67 }) | |
68 .when(mSupervisedUserContentProvider) | |
69 .nativeShouldProceed(anyLong(), anyString()); | |
70 TestRunnable r1 = new TestRunnable(); | |
71 Thread t1 = new Thread(r1); | |
72 t1.start(); | |
73 // Sleep is needed because of a bug in Robolectric, see | |
74 // 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.
| |
75 Thread.sleep(50); | |
76 Robolectric.runUiThreadTasksIncludingDelayedTasks(); | |
77 t1.join(); | |
78 verify(mSupervisedUserContentProvider).nativeShouldProceed(1234, "url"); | |
79 // Assert has to be on main thread for failures to cause test failure. | |
80 assertThat(r1.getResult(), is(new Pair<Boolean, String>(true, null))); | |
81 // Check that the native code was called on the right thread. | |
82 assertThat(mNativeCallThread, is(Thread.currentThread())); | |
83 doAnswer(new Answer<Void>() { | |
84 | |
85 @Override | |
86 public Void answer(InvocationOnMock invocation) throws Throwable { | |
87 mNativeCallThread = Thread.currentThread(); | |
88 mSupervisedUserContentProvider.onQueryComplete(false, "Hello"); | |
89 return null; | |
90 } | |
91 | |
92 }) | |
93 .when(mSupervisedUserContentProvider) | |
94 .nativeShouldProceed(anyLong(), anyString()); | |
95 TestRunnable r2 = new TestRunnable(); | |
96 Thread t2 = new Thread(r2); | |
97 t2.start(); | |
98 Thread.sleep(50); | |
99 Robolectric.runUiThreadTasksIncludingDelayedTasks(); | |
100 t2.join(); | |
101 assertThat(r2.getResult(), is(new Pair<Boolean, String>(false, "Hello")) ); | |
102 // Check that the native code was called on the UI thread. | |
103 assertThat(mNativeCallThread, is(Thread.currentThread())); | |
104 } | |
105 | |
106 @Test | |
107 public void testRequestInsert() throws InterruptedException { | |
108 class TestRunnable implements Runnable { | |
109 private Pair<Boolean, String> mResult; | |
110 | |
111 @Override | |
112 public void run() { | |
113 mSupervisedUserContentProvider.requestInsert("url"); | |
114 } | |
115 | |
116 public Pair<Boolean, String> getResult() { | |
117 return mResult; | |
118 } | |
119 } | |
120 doAnswer(new Answer<Void>() { | |
121 | |
122 @Override | |
123 public Void answer(InvocationOnMock invocation) throws Throwable { | |
124 mNativeCallThread = Thread.currentThread(); | |
125 return null; | |
126 } | |
127 | |
128 }) | |
129 .when(mSupervisedUserContentProvider) | |
130 .nativeRequestInsert(anyLong(), anyString()); | |
131 TestRunnable r1 = new TestRunnable(); | |
132 Thread t1 = new Thread(r1); | |
133 t1.start(); | |
134 // Sleep is needed because of a bug in Robolectric, see | |
135 // https://groups.google.com/forum/#!topic/robolectric/mlXNko2NFE0. | |
136 Thread.sleep(50); | |
137 Robolectric.runUiThreadTasksIncludingDelayedTasks(); | |
138 t1.join(); | |
139 verify(mSupervisedUserContentProvider).nativeRequestInsert(1234, "url"); | |
140 assertThat(mNativeCallThread, is(Thread.currentThread())); | |
141 } | |
142 } | |
OLD | NEW |