• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.internal.managers;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import androidx.lifecycle.Lifecycle;
23 import android.os.Build;
24 import androidx.fragment.app.Fragment;
25 import androidx.fragment.app.FragmentActivity;
26 import androidx.test.core.app.ActivityScenario;
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 import dagger.hilt.android.AndroidEntryPoint;
29 import dagger.hilt.android.internal.managers.ViewComponentManager.FragmentContextWrapper;
30 import dagger.hilt.android.testing.HiltAndroidRule;
31 import dagger.hilt.android.testing.HiltAndroidTest;
32 import dagger.hilt.android.testing.HiltTestApplication;
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 final class FragmentContextWrapperLeakTest {
43   /** An activity to test injection. */
44   @AndroidEntryPoint(FragmentActivity.class)
45   public static final class TestActivity extends Hilt_FragmentContextWrapperLeakTest_TestActivity {}
46 
47   /** A fragment to test injection. */
48   @AndroidEntryPoint(Fragment.class)
49   public static final class TestFragment extends Hilt_FragmentContextWrapperLeakTest_TestFragment {}
50 
51   @Rule public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
52 
53   @Test
testFragmentContextWrapperDoesNotLeakFragment()54   public void testFragmentContextWrapperDoesNotLeakFragment() {
55     try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
56       TestFragment fragment = new TestFragment();
57       scenario.onActivity(
58           activity ->
59               activity
60                   .getSupportFragmentManager()
61                   .beginTransaction()
62                   .add(fragment, "TestFragment")
63                   .commitNow());
64 
65       FragmentContextWrapper fragmentContextWrapper =
66           (FragmentContextWrapper) fragment.getContext();
67       assertThat(fragmentContextWrapper.getFragment()).isEqualTo(fragment);
68       scenario.moveToState(Lifecycle.State.DESTROYED);
69       NullPointerException exception =
70           assertThrows(NullPointerException.class, fragmentContextWrapper::getFragment);
71       assertThat(exception).hasMessageThat().contains("The fragment has already been destroyed");
72     }
73   }
74 }
75