1/* 2 * Copyright (C) 2008 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 17package android.content; 18 19import android.content.ContentQueryMap; 20import android.content.ContentResolver; 21import android.content.ContentValues; 22import android.database.Cursor; 23import android.os.Handler; 24import android.os.Looper; 25import android.provider.Settings; 26import android.test.AndroidTestCase; 27import android.test.suitebuilder.annotation.MediumTest; 28 29import java.util.Observable; 30import java.util.Observer; 31 32/** Test of {@link ContentQueryMap} */ 33public class ContentQueryMapTest extends AndroidTestCase { 34 /** Helper class to run test code in a new thread with a Looper. */ 35 private abstract class LooperThread extends Thread { 36 public Throwable mError = null; 37 public boolean mSuccess = false; 38 39 abstract void go(); 40 41 public void run() { 42 try { 43 Looper.prepare(); 44 go(); 45 Looper.loop(); 46 } catch (Throwable e) { 47 mError = e; 48 } 49 } 50 } 51 52 @MediumTest 53 public void testContentQueryMap() throws Throwable { 54 LooperThread thread = new LooperThread() { 55 void go() { 56 ContentResolver r = getContext().getContentResolver(); 57 Settings.System.putString(r, "test", "Value"); 58 Cursor cursor = r.query( 59 Settings.System.CONTENT_URI, 60 new String[] { 61 Settings.System.NAME, 62 Settings.System.VALUE, 63 }, null, null, null); 64 65 final ContentQueryMap cqm = new ContentQueryMap( 66 cursor, Settings.System.NAME, true, null); 67 // Get the current state of the CQM. This forces a requery and means that the 68 // call to getValues() below won't do a requery(). 69 cqm.getRows(); 70 71 // The cache won't notice changes until the loop runs. 72 Settings.System.putString(r, "test", "New Value"); 73 ContentValues v = cqm.getValues("test"); 74 String value = v.getAsString(Settings.System.VALUE); 75 assertEquals("Value", value); 76 77 // Use an Observer to find out when the cache does update. 78 cqm.addObserver(new Observer() { 79 public void update(Observable o, Object arg) { 80 // Should have the new values by now. 81 ContentValues v = cqm.getValues("test"); 82 String value = v.getAsString(Settings.System.VALUE); 83 assertEquals("New Value", value); 84 Looper.myLooper().quit(); 85 cqm.close(); 86 mSuccess = true; 87 } 88 }); 89 90 // Give up after a few seconds, if it doesn't. 91 new Handler().postDelayed(new Runnable() { 92 public void run() { 93 fail("Timed out"); 94 } 95 }, 5000); 96 } 97 }; 98 99 thread.start(); 100 thread.join(); 101 if (thread.mError != null) throw thread.mError; 102 assertTrue(thread.mSuccess); 103 } 104} 105