forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize the fragment component context in getContext() as well so …
…getContext() can be called before super.onAttach. RELNOTES=Initialize the fragment component context in getContext() as well so getContext() can be called before super.onAttach. This also makes getContext() correctly return null after being removed. PiperOrigin-RevId: 373867639
- Loading branch information
1 parent
8235703
commit 8acc433
Showing
3 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
javatests/dagger/hilt/android/FragmentContextOnAttachTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2021 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.fragment.app.FragmentActivity; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import dagger.hilt.android.testing.HiltAndroidRule; | ||
import dagger.hilt.android.testing.HiltAndroidTest; | ||
import dagger.hilt.android.testing.HiltTestApplication; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.annotation.Config; | ||
|
||
@HiltAndroidTest | ||
@RunWith(AndroidJUnit4.class) | ||
// Robolectric requires Java9 to run API 29 and above, so use API 28 instead | ||
@Config(sdk = Build.VERSION_CODES.P, application = HiltTestApplication.class) | ||
public final class FragmentContextOnAttachTest { | ||
|
||
@Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); | ||
|
||
/** Hilt Activity */ | ||
@AndroidEntryPoint(FragmentActivity.class) | ||
public static final class TestActivity extends Hilt_FragmentContextOnAttachTest_TestActivity {} | ||
|
||
/** Hilt Fragment */ | ||
@AndroidEntryPoint(Fragment.class) | ||
public static final class TestFragment extends Hilt_FragmentContextOnAttachTest_TestFragment { | ||
Context onAttachContextContext = null; | ||
Context onAttachActivityContext = null; | ||
|
||
@Override | ||
public void onAttach(Context context) { | ||
// Test that getContext() can be called at this point | ||
onAttachContextContext = getContext(); | ||
super.onAttach(context); | ||
} | ||
|
||
@Override | ||
public void onAttach(Activity activity) { | ||
// Test that getContext() can be called at this point | ||
onAttachActivityContext = getContext(); | ||
super.onAttach(activity); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetContextAvailableBeforeSuperOnAttach() throws Exception { | ||
FragmentActivity activity = Robolectric.setupActivity(TestActivity.class); | ||
TestFragment fragment = new TestFragment(); | ||
activity.getSupportFragmentManager().beginTransaction().add(fragment, "").commitNow(); | ||
assertThat(fragment.onAttachContextContext).isNotNull(); | ||
assertThat(fragment.onAttachActivityContext).isNotNull(); | ||
} | ||
} |