Skip to content

Commit

Permalink
Internal changes
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 681593506
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Oct 2, 2024
1 parent 826c2f1 commit 9091f76
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
@DefineComponent.Builder
public interface FragmentComponentBuilder {
FragmentComponentBuilder fragment(@BindsInstance Fragment fragment);

FragmentComponent build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RetainedLifecycle.OnClearedListener> listeners = new HashSet<>();
private boolean onClearedDispatched = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public <T extends ViewModel> 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) {
Expand Down
4 changes: 3 additions & 1 deletion java/dagger/hilt/android/internal/managers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
53 changes: 20 additions & 33 deletions javatests/dagger/hilt/android/EntryPointAccessorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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<HiltTestApplication>()
val entryPoint = EntryPointAccessors.fromApplication<ApplicationEntryPoint>(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<ApplicationEntryPoint>(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<ActivityEntryPoint>(activity)
Truth.assertThat(entryPoint.getString())
.isEqualTo(ACTIVITY_STRING)
Truth.assertThat(entryPoint.getString()).isEqualTo(ACTIVITY_STRING)
}

@Test
Expand All @@ -169,24 +157,23 @@ class EntryPointAccessorsTest {
val fragment = TestFragment()
activity.supportFragmentManager.beginTransaction().add(fragment, "").commitNow()
val entryPoint = EntryPointAccessors.fromFragment<FragmentEntryPoint>(fragment)
Truth.assertThat(entryPoint.getString())
.isEqualTo(FRAGMENT_STRING)
Truth.assertThat(entryPoint.getString()).isEqualTo(FRAGMENT_STRING)
}

@Test
fun testViewEntryPoint() {
val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get()
val view = TestView(activity)
val entryPoint = EntryPointAccessors.fromView<ViewEntryPoint>(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)
Expand Down

0 comments on commit 9091f76

Please sign in to comment.