• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.testing;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import androidx.test.ext.junit.runners.AndroidJUnit4;
22 import dagger.MapKey;
23 import dagger.hilt.EntryPoint;
24 import dagger.hilt.InstallIn;
25 import dagger.hilt.components.SingletonComponent;
26 import java.util.Map;
27 import javax.inject.Inject;
28 import javax.inject.Provider;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.robolectric.annotation.Config;
33 
34 @HiltAndroidTest
35 @RunWith(AndroidJUnit4.class)
36 @Config(application = HiltTestApplication.class)
37 public final class BindValueIntoMapTest {
38   private static final String KEY1 = "SOME_KEY";
39   private static final String KEY2 = "SOME_OTHER_KEY";
40   private static final String VALUE1 = "SOME_VALUE";
41   private static final String VALUE2 = "SOME_OTHER_VALUE";
42   private static final String VALUE3 = "A_THIRD_VALUE";
43 
44   @BindValueIntoMap
45   @MyMapKey(KEY1)
46   String boundValue1 = VALUE1;
47 
48   @BindValueIntoMap
49   @MyMapKey(KEY2)
50   String boundValue2 = VALUE2;
51 
52   @EntryPoint
53   @InstallIn(SingletonComponent.class)
54   public interface BindValuesIntoMapEntryPoint {
getStringStringMap()55     Map<String, String> getStringStringMap();
56   }
57 
58   @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this);
59 
60   @Inject Provider<Map<String, String>> mapProvider;
61 
62   @Test
testInjectedAndModified()63   public void testInjectedAndModified() throws Exception {
64     rule.inject();
65     Map<String, String> oldMap = mapProvider.get();
66     assertThat(oldMap).containsExactly(KEY1, VALUE1, KEY2, VALUE2);
67     boundValue1 = VALUE3;
68     Map<String, String> newMap = mapProvider.get();
69     assertThat(oldMap).containsExactly(KEY1, VALUE1, KEY2, VALUE2);
70     assertThat(newMap).containsExactly(KEY1, VALUE3, KEY2, VALUE2);
71   }
72 
73   @MapKey
74   @interface MyMapKey {
value()75     String value();
76   }
77 }
78