1/*
2 * Copyright 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
17package androidx.loader.app;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertNull;
21import static org.junit.Assert.assertTrue;
22import static org.mockito.Mockito.mock;
23
24import android.content.Context;
25import android.support.test.filters.SmallTest;
26
27import androidx.loader.app.test.DummyLoader;
28import androidx.loader.content.Loader;
29
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.junit.runners.JUnit4;
33
34@RunWith(JUnit4.class)
35@SmallTest
36public class LoaderViewModelTest {
37
38    @Test
39    public void testHasRunningLoaders() {
40        LoaderManagerImpl.LoaderViewModel loaderViewModel = new LoaderManagerImpl.LoaderViewModel();
41        assertFalse("LoaderViewModel should not be running with before putLoader",
42                loaderViewModel.hasRunningLoaders());
43
44        AlwaysRunningLoaderInfo info = new AlwaysRunningLoaderInfo(mock(Context.class));
45        loaderViewModel.putLoader(0, info);
46        assertTrue("LoaderViewModel should be running after a running LoaderInfo is added",
47                loaderViewModel.hasRunningLoaders());
48
49        loaderViewModel.removeLoader(0);
50        assertFalse("LoaderViewModel should not be running after all LoaderInfos are removed",
51                loaderViewModel.hasRunningLoaders());
52    }
53
54    @Test
55    public void testOnCleared() {
56        LoaderManagerImpl.LoaderViewModel loaderViewModel = new LoaderManagerImpl.LoaderViewModel();
57        AlwaysRunningLoaderInfo info = new AlwaysRunningLoaderInfo(mock(Context.class));
58        loaderViewModel.putLoader(0, info);
59
60        assertFalse("LoaderInfo shouldn't be destroyed before onCleared", info.mDestroyed);
61        loaderViewModel.onCleared();
62        assertTrue("LoaderInfo should be destroyed after onCleared", info.mDestroyed);
63        assertNull("LoaderInfo should be removed from LoaderViewModel after onCleared",
64                loaderViewModel.getLoader(0));
65    }
66
67    private class AlwaysRunningLoaderInfo extends LoaderManagerImpl.LoaderInfo<Boolean> {
68        boolean mDestroyed = false;
69
70        AlwaysRunningLoaderInfo(Context context) {
71            super(0, null, new DummyLoader(context), null);
72        }
73
74        @Override
75        boolean isCallbackWaitingForData() {
76            return true;
77        }
78
79        @Override
80        Loader<Boolean> destroy(boolean reset) {
81            mDestroyed = true;
82            return super.destroy(reset);
83        }
84    }
85}
86