From 9091f76d42c00d7550bf39cb8cb749adc63904f4 Mon Sep 17 00:00:00 2001 From: Wanying Ding Date: Wed, 2 Oct 2024 14:30:13 -0700 Subject: [PATCH] Internal changes RELNOTES=n/a PiperOrigin-RevId: 681593506 --- .../builders/FragmentComponentBuilder.java | 1 + .../lifecycle/RetainedLifecycleImpl.java | 4 +- .../ActivityRetainedComponentManager.java | 2 + .../hilt/android/internal/managers/BUILD | 4 +- .../hilt/android/EntryPointAccessorsTest.kt | 53 +++++++------------ 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/java/dagger/hilt/android/internal/builders/FragmentComponentBuilder.java b/java/dagger/hilt/android/internal/builders/FragmentComponentBuilder.java index 11cc89ff752..4f95c07bf0e 100644 --- a/java/dagger/hilt/android/internal/builders/FragmentComponentBuilder.java +++ b/java/dagger/hilt/android/internal/builders/FragmentComponentBuilder.java @@ -25,5 +25,6 @@ @DefineComponent.Builder public interface FragmentComponentBuilder { FragmentComponentBuilder fragment(@BindsInstance Fragment fragment); + FragmentComponent build(); } diff --git a/java/dagger/hilt/android/internal/lifecycle/RetainedLifecycleImpl.java b/java/dagger/hilt/android/internal/lifecycle/RetainedLifecycleImpl.java index 5be3144a01f..806377b9be7 100644 --- a/java/dagger/hilt/android/internal/lifecycle/RetainedLifecycleImpl.java +++ b/java/dagger/hilt/android/internal/lifecycle/RetainedLifecycleImpl.java @@ -25,7 +25,9 @@ import java.util.Set; /** Internal implementation. Do not use. */ -public final class RetainedLifecycleImpl implements ActivityRetainedLifecycle, ViewModelLifecycle { +public final class RetainedLifecycleImpl + implements ActivityRetainedLifecycle, + ViewModelLifecycle { private final Set listeners = new HashSet<>(); private boolean onClearedDispatched = false; diff --git a/java/dagger/hilt/android/internal/managers/ActivityRetainedComponentManager.java b/java/dagger/hilt/android/internal/managers/ActivityRetainedComponentManager.java index dc3539c34d6..5c3817c6fd2 100644 --- a/java/dagger/hilt/android/internal/managers/ActivityRetainedComponentManager.java +++ b/java/dagger/hilt/android/internal/managers/ActivityRetainedComponentManager.java @@ -120,6 +120,8 @@ public T create( @Override public ActivityRetainedComponent generatedComponent() { + // TODO(wanyingd): don't need the component lock since the component is stored in ViewModel + // which does its own locking if (component == null) { synchronized (componentLock) { if (component == null) { diff --git a/java/dagger/hilt/android/internal/managers/BUILD b/java/dagger/hilt/android/internal/managers/BUILD index 950b51129ee..ab1321f462b 100644 --- a/java/dagger/hilt/android/internal/managers/BUILD +++ b/java/dagger/hilt/android/internal/managers/BUILD @@ -34,7 +34,9 @@ android_library( "ServiceComponentManager.java", "ViewComponentManager.java", ], - exports = [":saved_state_handle_holder"], + exports = [ + ":saved_state_handle_holder", + ], deps = [ ":component_supplier", ":saved_state_handle_holder", diff --git a/javatests/dagger/hilt/android/EntryPointAccessorsTest.kt b/javatests/dagger/hilt/android/EntryPointAccessorsTest.kt index fb4afc07764..c741a35e6a7 100644 --- a/javatests/dagger/hilt/android/EntryPointAccessorsTest.kt +++ b/javatests/dagger/hilt/android/EntryPointAccessorsTest.kt @@ -27,14 +27,18 @@ import com.google.common.truth.Truth import dagger.Module import dagger.Provides import dagger.hilt.EntryPoint +import dagger.hilt.EntryPoints import dagger.hilt.InstallIn import dagger.hilt.android.components.ActivityComponent import dagger.hilt.android.components.FragmentComponent +import dagger.hilt.android.components.FragmentRetainedComponent import dagger.hilt.android.components.ViewComponent +import dagger.hilt.android.internal.managers.InternalFragmentRetainedComponent import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import dagger.hilt.android.testing.HiltTestApplication import dagger.hilt.components.SingletonComponent +import javax.inject.Inject import javax.inject.Qualifier import org.junit.Rule import org.junit.Test @@ -55,24 +59,15 @@ class EntryPointAccessorsTest { const val VIEW_STRING = "VIEW_STRING" } - @get:Rule - var rule = HiltAndroidRule(this) + @get:Rule var rule = HiltAndroidRule(this) - @Qualifier - @Retention(AnnotationRetention.BINARY) - annotation class ApplicationLevel + @Qualifier @Retention(AnnotationRetention.BINARY) annotation class ApplicationLevel - @Qualifier - @Retention(AnnotationRetention.BINARY) - annotation class ActivityLevel + @Qualifier @Retention(AnnotationRetention.BINARY) annotation class ActivityLevel - @Qualifier - @Retention(AnnotationRetention.BINARY) - annotation class FragmentLevel + @Qualifier @Retention(AnnotationRetention.BINARY) annotation class FragmentLevel - @Qualifier - @Retention(AnnotationRetention.BINARY) - annotation class ViewLevel + @Qualifier @Retention(AnnotationRetention.BINARY) annotation class ViewLevel @Module @InstallIn(SingletonComponent::class) @@ -117,50 +112,43 @@ class EntryPointAccessorsTest { @EntryPoint @InstallIn(SingletonComponent::class) internal interface ApplicationEntryPoint { - @ApplicationLevel - fun getString(): String + @ApplicationLevel fun getString(): String } @EntryPoint @InstallIn(ActivityComponent::class) internal interface ActivityEntryPoint { - @ActivityLevel - fun getString(): String + @ActivityLevel fun getString(): String } @EntryPoint @InstallIn(FragmentComponent::class) internal interface FragmentEntryPoint { - @FragmentLevel - fun getString(): String + @FragmentLevel fun getString(): String } @EntryPoint @InstallIn(ViewComponent::class) internal interface ViewEntryPoint { - @ViewLevel - fun getString(): String + @ViewLevel fun getString(): String } @Test fun testApplicationEntryPoint() { val app = getApplicationContext() val entryPoint = EntryPointAccessors.fromApplication(app) - Truth.assertThat(entryPoint.getString()) - .isEqualTo(APPLICATION_STRING) + Truth.assertThat(entryPoint.getString()).isEqualTo(APPLICATION_STRING) val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get() val applicationEntryPoint = EntryPointAccessors.fromApplication(activity) - Truth.assertThat(applicationEntryPoint.getString()) - .isEqualTo(APPLICATION_STRING) + Truth.assertThat(applicationEntryPoint.getString()).isEqualTo(APPLICATION_STRING) } @Test fun testActivityEntryPoint() { val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get() val entryPoint = EntryPointAccessors.fromActivity(activity) - Truth.assertThat(entryPoint.getString()) - .isEqualTo(ACTIVITY_STRING) + Truth.assertThat(entryPoint.getString()).isEqualTo(ACTIVITY_STRING) } @Test @@ -169,8 +157,7 @@ class EntryPointAccessorsTest { val fragment = TestFragment() activity.supportFragmentManager.beginTransaction().add(fragment, "").commitNow() val entryPoint = EntryPointAccessors.fromFragment(fragment) - Truth.assertThat(entryPoint.getString()) - .isEqualTo(FRAGMENT_STRING) + Truth.assertThat(entryPoint.getString()).isEqualTo(FRAGMENT_STRING) } @Test @@ -178,15 +165,15 @@ class EntryPointAccessorsTest { val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get() val view = TestView(activity) val entryPoint = EntryPointAccessors.fromView(view) - Truth.assertThat(entryPoint.getString()) - .isEqualTo(VIEW_STRING) + Truth.assertThat(entryPoint.getString()).isEqualTo(VIEW_STRING) } @AndroidEntryPoint(FragmentActivity::class) class TestActivity : Hilt_EntryPointAccessorsTest_TestActivity() @AndroidEntryPoint(Fragment::class) - class TestFragment : Hilt_EntryPointAccessorsTest_TestFragment() + class TestFragment : Hilt_EntryPointAccessorsTest_TestFragment() { + } @AndroidEntryPoint(View::class) class TestView(context: Context) : Hilt_EntryPointAccessorsTest_TestView(context)