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