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; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.os.Build; 24 import androidx.fragment.app.Fragment; 25 import androidx.fragment.app.FragmentActivity; 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 import dagger.hilt.android.flags.FragmentGetContextFix; 28 import dagger.hilt.android.testing.BindValueIntoSet; 29 import dagger.hilt.android.testing.HiltAndroidRule; 30 import dagger.hilt.android.testing.HiltAndroidTest; 31 import dagger.hilt.android.testing.HiltTestApplication; 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.robolectric.Robolectric; 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 FragmentContextOnAttachTest { 43 44 @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); 45 46 @BindValueIntoSet 47 @FragmentGetContextFix.DisableFragmentGetContextFix 48 boolean disableGetContextFix = false; 49 50 /** Hilt Activity */ 51 @AndroidEntryPoint(FragmentActivity.class) 52 public static final class TestActivity extends Hilt_FragmentContextOnAttachTest_TestActivity {} 53 54 /** Hilt Fragment */ 55 @AndroidEntryPoint(Fragment.class) 56 public static final class TestFragment extends Hilt_FragmentContextOnAttachTest_TestFragment { 57 Context onAttachContextContext = null; 58 Context onAttachActivityContext = null; 59 60 @Override onAttach(Context context)61 public void onAttach(Context context) { 62 // Test that getContext() can be called at this point 63 onAttachContextContext = getContext(); 64 super.onAttach(context); 65 } 66 67 @Override onAttach(Activity activity)68 public void onAttach(Activity activity) { 69 // Test that getContext() can be called at this point 70 onAttachActivityContext = getContext(); 71 super.onAttach(activity); 72 } 73 } 74 75 @Test testGetContextAvailableBeforeSuperOnAttach()76 public void testGetContextAvailableBeforeSuperOnAttach() throws Exception { 77 FragmentActivity activity = Robolectric.setupActivity(TestActivity.class); 78 TestFragment fragment = new TestFragment(); 79 activity.getSupportFragmentManager().beginTransaction().add(fragment, "").commitNow(); 80 assertThat(fragment.onAttachContextContext).isNotNull(); 81 assertThat(fragment.onAttachActivityContext).isNotNull(); 82 } 83 84 // Tests the behavior when using the useFragmentGetContextFix flag. 85 @Test testGetContextReturnsNullAfterRemoval()86 public void testGetContextReturnsNullAfterRemoval() throws Exception { 87 FragmentActivity activity = Robolectric.setupActivity(TestActivity.class); 88 TestFragment fragment = new TestFragment(); 89 activity.getSupportFragmentManager().beginTransaction().add(fragment, "").commitNow(); 90 assertThat(fragment.getContext()).isNotNull(); 91 activity.getSupportFragmentManager().beginTransaction().remove(fragment).commitNow(); 92 // This should be null since the fix was enabled by the compiler flag and runtime flag 93 assertThat(fragment.getContext()).isNull(); 94 95 // Flip the flag so that we now disable the fix 96 disableGetContextFix = true; 97 TestFragment fragment2 = new TestFragment(); 98 activity.getSupportFragmentManager().beginTransaction().add(fragment2, "").commitNow(); 99 assertThat(fragment2.getContext()).isNotNull(); 100 activity.getSupportFragmentManager().beginTransaction().remove(fragment2).commitNow(); 101 // This should not be null since the fix was disabled by the runtime flag 102 assertThat(fragment2.getContext()).isNotNull(); 103 } 104 } 105