xref: /aosp_15_r20/external/dagger2/javatests/dagger/android/support/AndroidSupportInjectionTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.os.Build;
24 import androidx.fragment.app.Fragment;
25 import androidx.fragment.app.FragmentActivity;
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 import dagger.android.AndroidInjector;
28 import dagger.android.HasAndroidInjector;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.Robolectric;
32 import org.robolectric.annotation.Config;
33 
34 @RunWith(AndroidJUnit4.class)
35 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead
36 @Config(sdk = Build.VERSION_CODES.P)
37 public final class AndroidSupportInjectionTest {
38   @Test
injectFragment_simpleApplication()39   public void injectFragment_simpleApplication() {
40     Fragment fragment = new Fragment();
41     startFragment(fragment);
42 
43     try {
44       AndroidSupportInjection.inject(fragment);
45       fail();
46     } catch (Exception e) {
47       assertThat(e).hasMessageThat().contains("No injector was found");
48     }
49   }
50 
51   private static class ApplicationReturnsNull extends Application
52       implements HasAndroidInjector {
53     @Override
androidInjector()54     public AndroidInjector<Object> androidInjector() {
55       return null;
56     }
57   }
58 
59   @Test
60   @Config(application = ApplicationReturnsNull.class)
fragmentInjector_returnsNull()61   public void fragmentInjector_returnsNull() {
62     Fragment fragment = new Fragment();
63     startFragment(fragment);
64 
65     try {
66       AndroidSupportInjection.inject(fragment);
67       fail();
68     } catch (Exception e) {
69       assertThat(e).hasMessageThat().contains("androidInjector() returned null");
70     }
71   }
72 
73   @Test
injectFragment_nullInput()74   public void injectFragment_nullInput() {
75     try {
76       AndroidSupportInjection.inject(null);
77       fail();
78     } catch (NullPointerException e) {
79       assertThat(e).hasMessageThat().contains("fragment");
80     }
81   }
82 
startFragment(Fragment fragment)83   void startFragment(Fragment fragment) {
84     Robolectric.setupActivity(FragmentActivity.class)
85         .getSupportFragmentManager()
86         .beginTransaction()
87         .add(fragment, "")
88         .commitNow();
89   }
90 }
91