Jeff Hamilton | ebd19e0 | 2010-07-21 16:33:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | package android.test; |
| 18 | |
| 19 | import android.content.Loader; |
| 20 | import android.content.Loader.OnLoadCompleteListener; |
| 21 | import android.os.AsyncTask; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
| 25 | |
| 26 | import java.util.concurrent.ArrayBlockingQueue; |
| 27 | |
| 28 | /** |
| 29 | * A convenience class for testing {@link Loader}s. This test case |
| 30 | * provides a simple way to synchronously get the result from a Loader making |
| 31 | * it easy to assert that the Loader returns the expected result. |
| 32 | */ |
| 33 | public class LoaderTestCase extends AndroidTestCase { |
| 34 | static { |
| 35 | // Force class loading of AsyncTask on the main thread so that it's handlers are tied to |
| 36 | // the main thread and responses from the worker thread get delivered on the main thread. |
| 37 | // The tests are run on another thread, allowing them to block waiting on a response from |
| 38 | // the code running on the main thread. The main thread can't block since the AysncTask |
| 39 | // results come in via the event loop. |
| 40 | new AsyncTask<Void, Void, Void>() { |
| 41 | @Override |
| 42 | protected Void doInBackground(Void... args) {return null;} |
| 43 | @Override |
| 44 | protected void onPostExecute(Void result) {} |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Runs a Loader synchronously and returns the result of the load. The loader will |
| 50 | * be started, stopped, and destroyed by this method so it cannot be reused. |
| 51 | * |
| 52 | * @param loader The loader to run synchronously |
| 53 | * @return The result from the loader |
| 54 | */ |
| 55 | public <T> T getLoaderResultSynchronously(final Loader<T> loader) { |
| 56 | // The test thread blocks on this queue until the loader puts it's result in |
| 57 | final ArrayBlockingQueue<T> queue = new ArrayBlockingQueue<T>(1); |
| 58 | |
| 59 | // This callback runs on the "main" thread and unblocks the test thread |
| 60 | // when it puts the result into the blocking queue |
| 61 | final OnLoadCompleteListener<T> listener = new OnLoadCompleteListener<T>() { |
| 62 | @Override |
| 63 | public void onLoadComplete(Loader<T> completedLoader, T data) { |
| 64 | // Shut the loader down |
| 65 | completedLoader.unregisterListener(this); |
| 66 | completedLoader.stopLoading(); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 67 | completedLoader.reset(); |
Jeff Hamilton | ebd19e0 | 2010-07-21 16:33:10 -0500 | [diff] [blame] | 68 | |
| 69 | // Store the result, unblocking the test thread |
| 70 | queue.add(data); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | // This handler runs on the "main" thread of the process since AsyncTask |
| 75 | // is documented as needing to run on the main thread and many Loaders use |
| 76 | // AsyncTask |
| 77 | final Handler mainThreadHandler = new Handler(Looper.getMainLooper()) { |
| 78 | @Override |
| 79 | public void handleMessage(Message msg) { |
| 80 | loader.registerListener(0, listener); |
| 81 | loader.startLoading(); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | // Ask the main thread to start the loading process |
| 86 | mainThreadHandler.sendEmptyMessage(0); |
| 87 | |
| 88 | // Block on the queue waiting for the result of the load to be inserted |
| 89 | T result; |
| 90 | while (true) { |
| 91 | try { |
| 92 | result = queue.take(); |
| 93 | break; |
| 94 | } catch (InterruptedException e) { |
| 95 | throw new RuntimeException("waiting thread interrupted", e); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | } |