Chiao Cheng | edf7ab9 | 2012-09-14 12:00:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.database; |
Chiao Cheng | edf7ab9 | 2012-09-14 12:00:21 -0700 | [diff] [blame] | 18 | |
| 19 | import android.database.Cursor; |
| 20 | import android.net.Uri; |
| 21 | import android.test.InstrumentationTestCase; |
| 22 | import android.test.mock.MockContentProvider; |
| 23 | import android.test.mock.MockContentResolver; |
Walter Jang | 7e79b52 | 2016-08-23 09:33:41 -0700 | [diff] [blame] | 24 | import android.test.suitebuilder.annotation.SmallTest; |
Chiao Cheng | edf7ab9 | 2012-09-14 12:00:21 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.concurrent.CountDownLatch; |
| 27 | import java.util.concurrent.TimeUnit; |
| 28 | |
| 29 | /** |
| 30 | * Unit test for {@link NoNullCursorAsyncQueryHandler} |
| 31 | */ |
Walter Jang | 7e79b52 | 2016-08-23 09:33:41 -0700 | [diff] [blame] | 32 | @SmallTest |
Chiao Cheng | edf7ab9 | 2012-09-14 12:00:21 -0700 | [diff] [blame] | 33 | public 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 | } |