• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.documentsui.testing;
18 
19 import android.app.LoaderManager;
20 import android.content.AsyncTaskLoader;
21 import android.content.Loader;
22 import android.content.Loader.OnLoadCompleteListener;
23 import android.os.Bundle;
24 import android.util.SparseArray;
25 
26 import java.io.FileDescriptor;
27 import java.io.PrintWriter;
28 
29 /**
30  * A test double of {@link LoaderManager} that doesn't kick off loading when {@link Loader} is
31  * created. If caller needs to kick off loading caller can obtain the loader initialized and
32  * explicitly call {@link Loader#startLoading()}.
33  */
34 public class TestLoaderManager extends LoaderManager {
35 
36     private final SparseArray<Loader> mLoaders = new SparseArray<>();
37     private final SparseArray<OnLoadCompleteListener> mListeners = new SparseArray<>();
38 
39     @Override
initLoader(int id, Bundle args, LoaderCallbacks<D> callback)40     public <D> Loader<D> initLoader(int id, Bundle args,
41             LoaderCallbacks<D> callback) {
42         Loader<D> loader = mLoaders.get(id);
43         OnLoadCompleteListener<D> listener = callback::onLoadFinished;
44         if (loader == null) {
45             loader = callback.onCreateLoader(id, args);
46             mLoaders.put(id, loader);
47         } else {
48             loader.unregisterListener(mListeners.get(id));
49         }
50 
51         loader.registerListener(id, listener);
52         mListeners.put(id, listener);
53 
54         return loader;
55     }
56 
57     @Override
restartLoader(int id, Bundle args, LoaderCallbacks<D> callback)58     public <D> Loader<D> restartLoader(int id, Bundle args,
59             LoaderCallbacks<D> callback) {
60         if (mLoaders.get(id) != null) {
61             destroyLoader(id);
62         }
63 
64         return initLoader(id, args, callback);
65     }
66 
67     @Override
destroyLoader(int id)68     public void destroyLoader(int id) {
69         Loader loader = getLoader(id);
70         if (loader != null) {
71             loader.abandon();
72             mLoaders.remove(id);
73             mListeners.remove(id);
74         }
75     }
76 
77     @Override
getLoader(int id)78     public <D> Loader<D> getLoader(int id) {
79         return mLoaders.get(id);
80     }
81 
getListener(int id)82     public <D> OnLoadCompleteListener<D> getListener(int id) {
83         return mListeners.get(id);
84     }
85 
runAsyncTaskLoader(int id)86     public void runAsyncTaskLoader(int id) {
87         AsyncTaskLoader loader = (AsyncTaskLoader) getLoader(id);
88         loader.startLoading();
89     }
90 
91     @Override
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)92     public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
93 
94     }
95 }
96