• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Dagger Authors.
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 dagger.hilt.android;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.fail;
21 
22 import android.os.Build;
23 import androidx.fragment.app.FragmentActivity;
24 import androidx.lifecycle.Lifecycle.State;
25 import androidx.test.core.app.ActivityScenario;
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 import dagger.hilt.android.ActivityRetainedLifecycle.OnClearedListener;
28 import dagger.hilt.android.testing.HiltAndroidRule;
29 import dagger.hilt.android.testing.HiltAndroidTest;
30 import dagger.hilt.android.testing.HiltTestApplication;
31 import java.util.concurrent.atomic.AtomicReference;
32 import javax.inject.Inject;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.annotation.Config;
37 
38 @HiltAndroidTest
39 @RunWith(AndroidJUnit4.class)
40 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
41 @Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class)
42 public class ActivityRetainedClearedListenerTest {
43 
44   @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);
45 
46   @Test
onClearedInvoked()47   public void onClearedInvoked() {
48     final TestClearedListener callback = new TestClearedListener();
49     try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
50       scenario.onActivity(
51           activity -> activity.activityRetainedLifecycle.addOnClearedListener(callback));
52       assertThat(callback.onClearedInvoked).isEqualTo(0);
53 
54       // Recreate should not cause ViewModel to be cleared
55       scenario.recreate();
56       assertThat(callback.onClearedInvoked).isEqualTo(0);
57 
58       // Destroying activity (not due to recreate) should cause ViewModel to be cleared
59       scenario.moveToState(State.DESTROYED);
60       assertThat(callback.onClearedInvoked).isEqualTo(1);
61     }
62   }
63 
64   @Test
addOnClearedListener_tooLate()65   public void addOnClearedListener_tooLate() {
66     try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
67       // Grab the activity (leak it a bit) from the scenario and destroy it.
68       AtomicReference<TestActivity> testActivity = new AtomicReference<>();
69       scenario.onActivity(testActivity::set);
70       scenario.moveToState(State.DESTROYED);
71 
72       try {
73         TestClearedListener callback = new TestClearedListener();
74         testActivity.get().activityRetainedLifecycle.addOnClearedListener(callback);
75         fail("An exception should have been thrown.");
76       } catch (IllegalStateException e) {
77         assertThat(e)
78             .hasMessageThat()
79             .contains(
80                 "There was a race between the call to add/remove an OnClearedListener and "
81                     + "onCleared(). This can happen when posting to the Main thread from a "
82                     + "background thread, which is not supported.");
83       }
84     }
85   }
86 
87   @Test
removeOnClearedListener_tooLate()88   public void removeOnClearedListener_tooLate() {
89     try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
90       // Grab the activity (leak it a bit) from the scenario and destroy it.
91       AtomicReference<TestActivity> testActivity = new AtomicReference<>();
92       scenario.onActivity(testActivity::set);
93       scenario.moveToState(State.DESTROYED);
94 
95       try {
96         TestClearedListener callback = new TestClearedListener();
97         testActivity.get().activityRetainedLifecycle.removeOnClearedListener(callback);
98         fail("An exception should have been thrown.");
99       } catch (IllegalStateException e) {
100         assertThat(e)
101             .hasMessageThat()
102             .contains(
103                 "There was a race between the call to add/remove an OnClearedListener and "
104                     + "onCleared(). This can happen when posting to the Main thread from a "
105                     + "background thread, which is not supported.");
106       }
107     }
108   }
109 
110   static class TestClearedListener implements OnClearedListener {
111     int onClearedInvoked = 0;
112 
113     @Override
onCleared()114     public void onCleared() {
115       onClearedInvoked++;
116     }
117   }
118 
119   @AndroidEntryPoint(FragmentActivity.class)
120   public static final class TestActivity
121       extends Hilt_ActivityRetainedClearedListenerTest_TestActivity {
122     @Inject ActivityRetainedLifecycle activityRetainedLifecycle;
123   }
124 }
125