blob: 987882b621bcb43084acb8a0b5f9e6b5febdc83e [file] [log] [blame]
Chiao Chengedf7ab92012-09-14 12:00:21 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.database;
Chiao Chengedf7ab92012-09-14 12:00:21 -070018
19import android.database.Cursor;
20import android.net.Uri;
21import android.test.InstrumentationTestCase;
22import android.test.mock.MockContentProvider;
23import android.test.mock.MockContentResolver;
Walter Jang7e79b522016-08-23 09:33:41 -070024import android.test.suitebuilder.annotation.SmallTest;
Chiao Chengedf7ab92012-09-14 12:00:21 -070025
26import java.util.concurrent.CountDownLatch;
27import java.util.concurrent.TimeUnit;
28
29/**
30 * Unit test for {@link NoNullCursorAsyncQueryHandler}
31 */
Walter Jang7e79b522016-08-23 09:33:41 -070032@SmallTest
Chiao Chengedf7ab92012-09-14 12:00:21 -070033public class NoNullCursorAsyncQueryHandlerTest extends InstrumentationTestCase {
34
35 private MockContentResolver mMockContentResolver;
36
37 private static final String AUTHORITY = "com.android.contacts.common.unittest";
38 private static final Uri URI = Uri.parse("content://" + AUTHORITY);
39 private static final String[] PROJECTION = new String[]{"column1", "column2"};
40
41 @Override
42 public void setUp() throws Exception {
43 super.setUp();
44 mMockContentResolver = new MockContentResolver();
45 final MockContentProvider mMockContentProvider = new MockContentProvider() {
46 @Override
47 public Cursor query(Uri uri, String[] projection, String selection,
48 String[] selectionArgs,
49 String sortOrder) {
50 return null;
51 }
52 };
53
54 mMockContentResolver.addProvider(AUTHORITY, mMockContentProvider);
55 }
56
57 public void testCursorIsNotNull() throws Throwable {
58
59 final CountDownLatch latch = new CountDownLatch(1);
60 final ObjectHolder<Cursor> cursorHolder = ObjectHolder.newInstance();
61 final ObjectHolder<Boolean> ranHolder = ObjectHolder.newInstance(false);
62
63 runTestOnUiThread(new Runnable() {
64
65 @Override
66 public void run() {
67
68 NoNullCursorAsyncQueryHandler handler = new NoNullCursorAsyncQueryHandler(
69 mMockContentResolver) {
70 @Override
71 protected void onNotNullableQueryComplete(int token, Object cookie,
72 Cursor cursor) {
73 cursorHolder.obj = cursor;
74 ranHolder.obj = true;
75 latch.countDown();
76 }
77 };
78 handler.startQuery(1, null, URI, PROJECTION, null, null, null);
79 }
80 });
81
82 latch.await(5, TimeUnit.SECONDS);
83 assertFalse(cursorHolder.obj == null);
84 assertTrue(ranHolder.obj);
85 }
86
87 public void testCursorContainsCorrectCookies() throws Throwable {
88 final ObjectHolder<Boolean> ranHolder = ObjectHolder.newInstance(false);
89 final CountDownLatch latch = new CountDownLatch(1);
90 final ObjectHolder<Object> cookieHolder = ObjectHolder.newInstance();
91 final String cookie = "TEST COOKIE";
92 runTestOnUiThread(new Runnable() {
93 @Override
94 public void run() {
95 final NoNullCursorAsyncQueryHandler handler = new NoNullCursorAsyncQueryHandler(
96 mMockContentResolver) {
97 @Override
98 protected void onNotNullableQueryComplete(int token, Object cookie,
99 Cursor cursor) {
100 ranHolder.obj = true;
101 cookieHolder.obj = cookie;
102 latch.countDown();
103 }
104 };
105 handler.startQuery(1, cookie, URI, PROJECTION, null, null, null);
106 }
107 });
108
109 latch.await(5, TimeUnit.SECONDS);
110 assertSame(cookie, cookieHolder.obj);
111 assertTrue(ranHolder.obj);
112 }
113
114 public void testCursorContainsCorrectColumns() throws Throwable {
115 final ObjectHolder<Boolean> ranHolder = ObjectHolder.newInstance(false);
116 final CountDownLatch latch = new CountDownLatch(1);
117 final ObjectHolder<Cursor> cursorHolder = ObjectHolder.newInstance();
118 final String cookie = "TEST COOKIE";
119 runTestOnUiThread(new Runnable() {
120 @Override
121 public void run() {
122 final NoNullCursorAsyncQueryHandler handler = new NoNullCursorAsyncQueryHandler(
123 mMockContentResolver) {
124 @Override
125 protected void onNotNullableQueryComplete(int token, Object cookie,
126 Cursor cursor) {
127 ranHolder.obj = true;
128 cursorHolder.obj = cursor;
129 latch.countDown();
130 }
131 };
132 handler.startQuery(1, cookie, URI, PROJECTION, null, null, null);
133 }
134 });
135
136 latch.await(5, TimeUnit.SECONDS);
137 assertSame(PROJECTION, cursorHolder.obj.getColumnNames());
138 assertTrue(ranHolder.obj);
139 }
140
141 private static class ObjectHolder<T> {
142 public T obj;
143
144 public static <E> ObjectHolder<E> newInstance() {
145 return new ObjectHolder<E>();
146 }
147
148 public static <E> ObjectHolder<E> newInstance(E value) {
149 ObjectHolder<E> holder = new ObjectHolder<E>();
150 holder.obj = value;
151 return holder;
152 }
153 }
154}