1 /*
2  * Copyright (C) 2014 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 com.android.contacts.interactions;
18 
19 import android.app.LoaderManager;
20 
21 /**
22  * A {@link LoaderManager} that records which loaders have been completed.
23  * <p>
24  * You should wrap the existing LoaderManager with an instance of this class, which will then
25  * delegate to the original object.
26  * <p>
27  * Typically, one would override {@link android.app.Activity#getLoaderManager()} to return the
28  * TestLoaderManager and ensuring it wraps the {@link LoaderManager} for this object, e.g.:
29  * <pre>
30  *   private TestLoaderManager mTestLoaderManager;
31  *
32  *   public LoaderManager getLoaderManager() {
33  *     LoaderManager loaderManager = super.getLoaderManager();
34  *     if (mTestLoaderManager != null) {
35  *       mTestLoaderManager.setDelegate(loaderManager);
36  *       return mTestLoaderManager;
37  *     } else {
38  *       return loaderManager;
39  *     }
40  *   }
41  *
42  *   void setTestLoaderManager(TestLoaderManager testLoaderManager) {
43  *     mTestLoaderManager = testLoaderManager;
44  *   }
45  * </pre>
46  * In the tests, one would set the TestLoaderManager upon creating the activity, and then wait for
47  * the loader to complete.
48  * <pre>
49  *   public void testLoadedCorrect() {
50  *     TestLoaderManager testLoaderManager = new TestLoaderManager();
51  *     getActivity().setTestLoaderManager(testLoaderManager);
52  *     runOnUiThread(new Runnable() { public void run() { getActivity().startLoading(); } });
53  *     testLoaderManager.waitForLoader(R.id.test_loader_id);
54  *   }
55  * </pre>
56  * If the loader completes before the call to {@link #waitForLoaders(int...)}, the TestLoaderManager
57  * will have stored the fact that the loader has completed and correctly terminate immediately.
58  * <p>
59  * It one needs to wait for the same loader multiple times, call {@link #reset()} between the them
60  * as in:
61  * <pre>
62  *   public void testLoadedCorrect() {
63  *     TestLoaderManager testLoaderManager = new TestLoaderManager();
64  *     getActivity().setTestLoaderManager(testLoaderManager);
65  *     runOnUiThread(new Runnable() { public void run() { getActivity().startLoading(); } });
66  *     testLoaderManager.waitForLoader(R.id.test_loader_id);
67  *     testLoaderManager.reset();
68  *     // Load and wait again.
69  *     runOnUiThread(new Runnable() { public void run() { getActivity().startLoading(); } });
70  *     testLoaderManager.waitForLoader(R.id.test_loader_id);
71  *   }
72  * </pre>
73  */
74 abstract class TestLoaderManagerBase extends LoaderManager {
75 
76     /**
77      * Waits for the specified loaders to complete loading.
78      */
waitForLoaders(int... loaderIds)79     public abstract void waitForLoaders(int... loaderIds);
80 
81     /**
82      * Sets the object to which we delegate the actual work.
83      * <p>
84      * It can not be set to null. Once set, it cannot be changed (but it allows setting it to the
85      * same value again).
86      */
setDelegate(LoaderManager delegate)87     public abstract void setDelegate(LoaderManager delegate);
88 
89 }
90