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