1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dialer.database;
import android.content.ContentValues;
import android.net.Uri;
import android.provider.BlockedNumberContract;
import android.provider.BlockedNumberContract.BlockedNumbers;
import android.test.InstrumentationTestCase;
import android.test.mock.MockContentResolver;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.contacts.common.compat.CompatUtils;
import com.android.contacts.common.test.mocks.MockContentProvider;
import com.android.contacts.common.test.mocks.MockContentProvider.Query;
import com.android.dialer.compat.FilteredNumberCompat;
import com.android.dialer.database.FilteredNumberAsyncQueryHandler.OnBlockNumberListener;
import com.android.dialer.database.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
import com.android.dialer.database.FilteredNumberAsyncQueryHandler.OnHasBlockedNumbersListener;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberSources;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberTypes;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@SmallTest
public class FilteredNumberAsyncQueryHandlerTest extends InstrumentationTestCase {
private static final String E164_NUMBER = "+16502530000";
private static final String NUMBER = "6502530000";
private static final String COUNTRY_ISO = "US";
private static final Integer ID = 1;
private static final Uri BLOCKED_NUMBER_URI_N = CompatUtils.isNCompatible() ?
Uri.withAppendedPath(BlockedNumberContract.AUTHORITY_URI, "blocked") : null;
private static final Uri BLOCKED_NUMBER_URI_M =
Uri.withAppendedPath(FilteredNumberContract.AUTHORITY_URI, "filtered_numbers_table");
private static final Uri BLOCKED_NUMBER_URI = CompatUtils.isNCompatible() ? BLOCKED_NUMBER_URI_N
: BLOCKED_NUMBER_URI_M;
private static final Uri EXPECTED_URI = Uri.fromParts("android", "google", "dialer");
private final MockContentResolver mContentResolver = new MockContentResolver();
private final MockContentProvider mContentProvider = new MockContentProvider();
@Override
public void setUp() throws Exception {
super.setUp();
FilteredNumberCompat.setIsEnabledForTest(true);
if (CompatUtils.isNCompatible()) {
mContentResolver.addProvider(BlockedNumberContract.AUTHORITY, mContentProvider);
} else {
mContentResolver.addProvider(FilteredNumberContract.AUTHORITY, mContentProvider);
}
}
public void testHasBlockedNumbers_Disabled() throws Throwable {
if (!CompatUtils.isNCompatible()) {
return;
}
FilteredNumberCompat.setIsEnabledForTest(false);
final MockContentResolver resolver = new MockContentResolver();
MockContentProvider disabledProvider = new MockContentProvider();
resolver.addProvider(FilteredNumberContract.AUTHORITY, disabledProvider);
disabledProvider.expectQuery(BLOCKED_NUMBER_URI_M).withProjection(FilteredNumberColumns._ID)
.withSelection(FilteredNumberColumns.TYPE + "="
+ FilteredNumberTypes.BLOCKED_NUMBER, null).returnRow(ID);
final HasBlockedNumbersListener listener = new HasBlockedNumbersListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(resolver).hasBlockedNumbers(listener);
}
});
assertTrue(listener.waitForCallback());
disabledProvider.verify();
}
public void testHasBlockedNumbers_NoResults() throws Throwable {
newHasBlockedNumbersExpectedQuery().returnEmptyCursor();
final HasBlockedNumbersListener listener = new HasBlockedNumbersListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(mContentResolver).hasBlockedNumbers(listener);
}
});
assertFalse(listener.waitForCallback());
mContentProvider.verify();
}
public void testHasBlockedNumbers() throws Throwable {
newHasBlockedNumbersExpectedQuery().returnRow(ID);
final HasBlockedNumbersListener listener = new HasBlockedNumbersListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(mContentResolver).hasBlockedNumbers(listener);
}
});
assertTrue(listener.waitForCallback());
mContentProvider.verify();
}
public void testIsBlockedNumber_Disabled() throws Throwable {
if (!CompatUtils.isNCompatible()) {
return;
}
FilteredNumberCompat.setIsEnabledForTest(false);
final MockContentResolver resolver = new MockContentResolver();
MockContentProvider disabledProvider = new MockContentProvider();
resolver.addProvider(FilteredNumberContract.AUTHORITY, disabledProvider);
disabledProvider.expectQuery(BLOCKED_NUMBER_URI_M)
.withProjection(FilteredNumberColumns._ID, FilteredNumberColumns.TYPE)
.withSelection(FilteredNumberColumns.NORMALIZED_NUMBER + " = ?", E164_NUMBER)
.returnRow(ID, FilteredNumberTypes.BLOCKED_NUMBER);
final CheckBlockedListener listener = new CheckBlockedListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(resolver)
.isBlockedNumber(listener, NUMBER, COUNTRY_ISO);
}
});
assertEquals(ID, listener.waitForCallback());
mContentProvider.verify();
}
public void testIsBlockedNumber_NoResults() throws Throwable {
newIsBlockedNumberExpectedQuery().returnEmptyCursor();
final CheckBlockedListener listener = new CheckBlockedListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(mContentResolver)
.isBlockedNumber(listener, NUMBER, COUNTRY_ISO);
}
});
assertNull(listener.waitForCallback());
mContentProvider.verify();
}
public void testIsBlockedNumber() throws Throwable {
if (CompatUtils.isNCompatible()) {
newIsBlockedNumberExpectedQuery().returnRow(ID);
} else {
newIsBlockedNumberExpectedQuery().returnRow(ID, FilteredNumberTypes.BLOCKED_NUMBER);
}
final CheckBlockedListener listener = new CheckBlockedListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(mContentResolver)
.isBlockedNumber(listener, NUMBER, COUNTRY_ISO);
}
});
assertEquals(ID, listener.waitForCallback());
mContentProvider.verify();
}
public void testBlockNumber_Disabled() throws Throwable {
if (!CompatUtils.isNCompatible()) {
return;
}
FilteredNumberCompat.setIsEnabledForTest(false);
final MockContentResolver resolver = new MockContentResolver();
MockContentProvider disabledProvider = new MockContentProvider();
resolver.addProvider(FilteredNumberContract.AUTHORITY, disabledProvider);
disabledProvider.expectInsert(BLOCKED_NUMBER_URI_M, newBlockNumberContentValuesM(),
EXPECTED_URI);
final BlockNumberListener listener = new BlockNumberListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(resolver).blockNumber(listener, E164_NUMBER,
NUMBER, COUNTRY_ISO);
}
});
assertSame(EXPECTED_URI, listener.waitForCallback());
disabledProvider.verify();
}
public void testBlockNumber() throws Throwable {
mContentProvider.expectInsert(BLOCKED_NUMBER_URI, newBlockNumberContentValues(),
EXPECTED_URI);
final BlockNumberListener listener = new BlockNumberListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(mContentResolver).blockNumber(listener,
E164_NUMBER, NUMBER, COUNTRY_ISO);
}
});
assertSame(EXPECTED_URI, listener.waitForCallback());
mContentProvider.verify();
}
public void testBlockNumber_NullNormalizedNumber() throws Throwable {
mContentProvider.expectInsert(BLOCKED_NUMBER_URI, newBlockNumberContentValues(),
EXPECTED_URI);
final BlockNumberListener listener = new BlockNumberListener();
runTestOnUiThread(new Runnable() {
@Override
public void run() {
new FilteredNumberAsyncQueryHandler(mContentResolver).blockNumber(listener,
NUMBER, COUNTRY_ISO);
}
});
assertSame(EXPECTED_URI, listener.waitForCallback());
mContentProvider.verify();
}
private Query newIsBlockedNumberExpectedQuery() {
if (CompatUtils.isNCompatible()) {
return newIsBlockedNumberExpectedQueryN();
}
return newIsBlockedNumberExpectedQueryM();
}
private Query newIsBlockedNumberExpectedQueryN() {
return mContentProvider.expectQuery(BLOCKED_NUMBER_URI)
.withProjection(BlockedNumbers.COLUMN_ID)
.withSelection(BlockedNumbers.COLUMN_E164_NUMBER + " = ?", E164_NUMBER);
}
private Query newIsBlockedNumberExpectedQueryM() {
return mContentProvider.expectQuery(BLOCKED_NUMBER_URI)
.withProjection(FilteredNumberColumns._ID, FilteredNumberColumns.TYPE)
.withSelection(FilteredNumberColumns.NORMALIZED_NUMBER + " = ?", E164_NUMBER);
}
private Query newHasBlockedNumbersExpectedQuery() {
if (CompatUtils.isNCompatible()) {
return newHasBlockedNumbersExpectedQueryN();
}
return newHasBlockedNumbersExpectedQueryM();
}
private Query newHasBlockedNumbersExpectedQueryN() {
return mContentProvider.expectQuery(BLOCKED_NUMBER_URI)
.withProjection(BlockedNumbers.COLUMN_ID)
.withSelection(null, null);
}
private Query newHasBlockedNumbersExpectedQueryM() {
return mContentProvider.expectQuery(BLOCKED_NUMBER_URI).withProjection(
FilteredNumberColumns._ID)
.withSelection(FilteredNumberColumns.TYPE + "="
+ FilteredNumberTypes.BLOCKED_NUMBER, null);
}
private ContentValues newBlockNumberContentValues() {
if (CompatUtils.isNCompatible()) {
return newBlockNumberContentValuesN();
}
return newBlockNumberContentValuesM();
}
private ContentValues newBlockNumberContentValuesN() {
ContentValues contentValues = new ContentValues();
contentValues.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, NUMBER);
return contentValues;
}
private ContentValues newBlockNumberContentValuesM() {
ContentValues contentValues = new ContentValues();
contentValues.put(FilteredNumberColumns.NORMALIZED_NUMBER, E164_NUMBER);
contentValues.put(FilteredNumberColumns.NUMBER, NUMBER);
contentValues.put(FilteredNumberColumns.COUNTRY_ISO, COUNTRY_ISO);
contentValues.put(FilteredNumberColumns.TYPE, FilteredNumberTypes.BLOCKED_NUMBER);
contentValues.put(FilteredNumberColumns.SOURCE, FilteredNumberSources.USER);
return contentValues;
}
private class CheckBlockedListener implements OnCheckBlockedListener {
public final CountDownLatch onCheckCompleteCalled;
public Integer id;
public CheckBlockedListener() {
onCheckCompleteCalled = new CountDownLatch(1);
}
@Override
public void onCheckComplete(Integer id) {
this.id = id;
onCheckCompleteCalled.countDown();
}
public Integer waitForCallback() throws InterruptedException {
if (!onCheckCompleteCalled.await(5000, TimeUnit.MILLISECONDS)) {
throw new IllegalStateException("Waiting on callback timed out.");
}
return id;
}
}
private class HasBlockedNumbersListener implements OnHasBlockedNumbersListener {
public final CountDownLatch onHasBlockedNumbersCalled;
public boolean hasBlockedNumbers;
public HasBlockedNumbersListener() {
onHasBlockedNumbersCalled = new CountDownLatch(1);
}
@Override
public void onHasBlockedNumbers(boolean hasBlockedNumbers) {
this.hasBlockedNumbers = hasBlockedNumbers;
onHasBlockedNumbersCalled.countDown();
}
public boolean waitForCallback() throws InterruptedException {
if (!onHasBlockedNumbersCalled.await(5000, TimeUnit.MILLISECONDS)) {
throw new IllegalStateException("Waiting on callback timed out.");
}
return hasBlockedNumbers;
}
}
private class BlockNumberListener implements OnBlockNumberListener {
public final CountDownLatch onBlockCompleteCalled;
public Uri uri;
public BlockNumberListener() {
onBlockCompleteCalled = new CountDownLatch(1);
}
@Override
public void onBlockComplete(Uri uri) {
this.uri = uri;
onBlockCompleteCalled.countDown();
}
public Uri waitForCallback() throws InterruptedException {
if (!onBlockCompleteCalled.await(5000, TimeUnit.MILLISECONDS)) {
throw new IllegalStateException("Waiting on callback timed out.");
}
return uri;
}
}
}
|