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 com.google.common.collect.ImmutableSet; 23 import dagger.hilt.EntryPoint; 24 import dagger.hilt.InstallIn; 25 import dagger.hilt.components.SingletonComponent; 26 import java.util.Set; 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 BindElementsIntoSetTest { 38 private static final String SET_STRING_1 = "SetString1"; 39 private static final String SET_STRING_2 = "SetString2"; 40 private static final String SET_STRING_3 = "SetString3"; 41 42 @BindElementsIntoSet Set<String> bindElementsSet1 = ImmutableSet.of(SET_STRING_1); 43 44 @BindElementsIntoSet Set<String> bindElementsSet2 = ImmutableSet.of(SET_STRING_2); 45 46 @EntryPoint 47 @InstallIn(SingletonComponent.class) 48 public interface BindElementsIntoSetEntryPoint { getStringSet()49 Set<String> getStringSet(); 50 } 51 52 @Rule public final HiltAndroidRule rule = new HiltAndroidRule(this); 53 54 @Inject Set<String> stringSet; 55 56 @Inject Provider<Set<String>> providedStringSet; 57 58 @Test testMutated()59 public void testMutated() throws Exception { 60 rule.inject(); 61 // basic check that initial/default values are properly injected 62 assertThat(providedStringSet.get()).containsExactly(SET_STRING_1, SET_STRING_2); 63 // Test empty sets (something that cannot be done with @BindValueIntoSet) 64 bindElementsSet1 = ImmutableSet.of(); 65 bindElementsSet2 = ImmutableSet.of(); 66 assertThat(providedStringSet.get()).isEmpty(); 67 // Test multiple elements in set. 68 bindElementsSet1 = ImmutableSet.of(SET_STRING_1, SET_STRING_2, SET_STRING_3); 69 assertThat(providedStringSet.get()).containsExactly(SET_STRING_1, SET_STRING_2, SET_STRING_3); 70 } 71 72 } 73