1 /* 2 * Copyright (C) 2016 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.android.support; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import android.app.Application; 23 import android.support.v4.app.Fragment; 24 import dagger.android.AndroidInjector; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.robolectric.RobolectricTestRunner; 28 import org.robolectric.annotation.Config; 29 import org.robolectric.shadows.support.v4.SupportFragmentTestUtil; 30 31 @Config(manifest = Config.NONE) 32 @RunWith(RobolectricTestRunner.class) 33 public final class AndroidSupportInjectionTest { 34 @Test injectFragment_simpleApplication()35 public void injectFragment_simpleApplication() { 36 Fragment fragment = new Fragment(); 37 SupportFragmentTestUtil.startFragment(fragment); 38 39 try { 40 AndroidSupportInjection.inject(fragment); 41 fail(); 42 } catch (Exception e) { 43 assertThat(e).hasMessageThat().contains("No injector was found"); 44 } 45 } 46 47 private static class ApplicationReturnsNull extends Application 48 implements HasSupportFragmentInjector { 49 @Override supportFragmentInjector()50 public AndroidInjector<Fragment> supportFragmentInjector() { 51 return null; 52 } 53 } 54 55 @Test 56 @Config(manifest = Config.NONE, application = ApplicationReturnsNull.class) fragmentInjector_returnsNull()57 public void fragmentInjector_returnsNull() { 58 Fragment fragment = new Fragment(); 59 SupportFragmentTestUtil.startFragment(fragment); 60 61 try { 62 AndroidSupportInjection.inject(fragment); 63 fail(); 64 } catch (Exception e) { 65 assertThat(e).hasMessageThat().contains("supportFragmentInjector() returned null"); 66 } 67 } 68 69 @Test injectFragment_nullInput()70 public void injectFragment_nullInput() { 71 try { 72 AndroidSupportInjection.inject(null); 73 fail(); 74 } catch (NullPointerException e) { 75 assertThat(e).hasMessageThat().contains("fragment"); 76 } 77 } 78 } 79