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