aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/hilt/android/ViewModelSavedStateOwnerTest.java
blob: eab206efcdcbcd8e7b499e45c1f0972567bd8426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2022 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.os.Build;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.annotation.Nullable;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import dagger.hilt.android.lifecycle.HiltViewModel;
import dagger.hilt.android.testing.HiltAndroidRule;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.HiltTestApplication;
import javax.inject.Inject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

/** Test that you can use the Hilt ViewModel factory with other owners. */
@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 class ViewModelSavedStateOwnerTest {

  @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);

  @Test
  public void testViewModelSavedState() {
    try (ActivityScenario<TestActivity> scenario = ActivityScenario.launch(TestActivity.class)) {
      scenario.onActivity(
          activity -> {
            NavController navController =
                Navigation.findNavController(activity, R.id.nav_host_fragment);
            TestFragment startFragment = findTestFragment(activity);

            MyViewModel activityVm =
                getViewModel(activity, activity.getDefaultViewModelProviderFactory());
            MyViewModel fragmentVm =
                getViewModel(startFragment, startFragment.getDefaultViewModelProviderFactory());
            MyViewModel fragmentBackStackVm =
                getViewModel(
                    navController.getBackStackEntry(R.id.start_destination),
                    startFragment.getDefaultViewModelProviderFactory());
            MyViewModel navGraphVm =
                getViewModel(
                    navController.getBackStackEntry(R.id.nav_graph),
                    startFragment.getDefaultViewModelProviderFactory());

            // The activity shouldn't have any arguments since it was only set on the fragment.
            assertThat((String) activityVm.savedStateHandle.get("argument_key")).isNull();
            activityVm.savedStateHandle.set("other_key", "activity_other_key");

            // The fragment argument (set in the navgraph xml) should be set.
            assertThat((String) fragmentVm.savedStateHandle.get("argument_key"))
                .isEqualTo("fragment_argument");
            fragmentVm.savedStateHandle.set("other_key", "fragment_other_key");

            // The back stack entry also has the fragment arguments
            assertThat((String) fragmentBackStackVm.savedStateHandle.get("argument_key"))
                .isEqualTo("fragment_argument");
            fragmentBackStackVm.savedStateHandle.set("other_key", "fragment_backstack_other_key");

            // When the nav graph itself is the owner, then there should be no arguments.
            assertThat((String) navGraphVm.savedStateHandle.get("argument_key")).isNull();
            navGraphVm.savedStateHandle.set("other_key", "nav_graph_other_key");

            navController.navigate(R.id.next_destination);
          });

      // Now move to the next fragment to compare
      scenario.onActivity(
          activity -> {
            NavController navController =
                Navigation.findNavController(activity, R.id.nav_host_fragment);

            TestFragment nextFragment = findTestFragment(activity);

            MyViewModel activityVm =
                getViewModel(activity, activity.getDefaultViewModelProviderFactory());
            MyViewModel fragmentVm =
                getViewModel(nextFragment, nextFragment.getDefaultViewModelProviderFactory());
            MyViewModel navGraphVm =
                getViewModel(
                    navController.getBackStackEntry(R.id.nav_graph),
                    nextFragment.getDefaultViewModelProviderFactory());
            MyViewModel fragmentBackStackVm =
                getViewModel(
                    navController.getBackStackEntry(R.id.next_destination),
                    nextFragment.getDefaultViewModelProviderFactory());

            // The activity still shouldn't have any arguments, but since it is the same
            // owner (since the activity didn't change), the other key should still be set
            // from before.
            assertThat((String) activityVm.savedStateHandle.get("argument_key")).isNull();
            assertThat((String) activityVm.savedStateHandle.get("other_key"))
                .isEqualTo("activity_other_key");

            // The fragment argument should be set via the navgraph xml again. Also, since
            // this is a new fragment, the other key should not be set.
            assertThat((String) fragmentVm.savedStateHandle.get("argument_key"))
                .isEqualTo("next_fragment_argument");
            assertThat((String) fragmentVm.savedStateHandle.get("other_key")).isNull();

            // Same as using the fragment as the owner.
            assertThat((String) fragmentBackStackVm.savedStateHandle.get("argument_key"))
                .isEqualTo("next_fragment_argument");
            assertThat((String) fragmentBackStackVm.savedStateHandle.get("other_key")).isNull();

            // Similar to the activity case, the navgraph is the same so we expect the same
            // key to be set from before. Arguments should still be missing.
            assertThat((String) navGraphVm.savedStateHandle.get("argument_key")).isNull();
            assertThat((String) navGraphVm.savedStateHandle.get("other_key"))
                .isEqualTo("nav_graph_other_key");
          });
    }
  }

  private TestFragment findTestFragment(FragmentActivity activity) {
    return (TestFragment)
        activity
            .getSupportFragmentManager()
            .findFragmentById(R.id.nav_host_fragment)
            .getChildFragmentManager()
            .getPrimaryNavigationFragment();
  }

  private MyViewModel getViewModel(ViewModelStoreOwner owner, ViewModelProvider.Factory factory) {
    return new ViewModelProvider(owner, factory).get(MyViewModel.class);
  }

  @AndroidEntryPoint(FragmentActivity.class)
  public static class TestActivity extends Hilt_ViewModelSavedStateOwnerTest_TestActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.navigation_activity);
    }
  }

  @AndroidEntryPoint(Fragment.class)
  public static class TestFragment extends Hilt_ViewModelSavedStateOwnerTest_TestFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    }
  }

  @HiltViewModel
  static class MyViewModel extends ViewModel {
    final SavedStateHandle savedStateHandle;

    @Inject
    MyViewModel(SavedStateHandle savedStateHandle) {
      this.savedStateHandle = savedStateHandle;
    }
  }
}