aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/hilt/android/AndroidEntryPointBaseClassTest.java
blob: 1078e7dbfa318db149949fd5205a50015dc9afdf (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
/*
 * Copyright (C) 2020 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 androidx.activity.ComponentActivity;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import dagger.hilt.android.other.pkg.AndroidEntryPointBaseClassOtherPkg;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

/**
 * Regression test for https://github.com/google/dagger/issues/1910
 *
 * <p>There are 8 different tests to cover 3 levels of inheritance where each level uses either the
 * long-form (L) or short-form (S) of @AndroidEntryPoint:
 *
 * <ol>
 *   <li> L -> L -> L
 *   <li> L -> L -> S
 *   <li> L -> S -> L
 *   <li> L -> S -> S
 *   <li> S -> L -> L
 *   <li> S -> L -> S
 *   <li> S -> S -> L
 *   <li> S -> S -> S
 * </ol>
 *
 * Note: We don't actually test injection in this class because Bazel doesn't do bytecode injection.
 * We're only testing that the classes build, and verifying their inheritance matches what we
 * expect.
 */
@RunWith(AndroidJUnit4.class)
// Robolectric requires Java9 to run API 29 and above, so use API 28 instead
@Config(sdk = Build.VERSION_CODES.P)
public final class AndroidEntryPointBaseClassTest {

  @AndroidEntryPoint
  public static final class SSActivity extends AndroidEntryPointBaseClassOtherPkg.SBaseActivity {}

  @AndroidEntryPoint
  public static final class SLActivity extends AndroidEntryPointBaseClassOtherPkg.LBaseActivity {}

  @AndroidEntryPoint(AndroidEntryPointBaseClassOtherPkg.SBaseActivity.class)
  public static final class LSActivity extends Hilt_AndroidEntryPointBaseClassTest_LSActivity {}

  @AndroidEntryPoint(AndroidEntryPointBaseClassOtherPkg.LBaseActivity.class)
  public static final class LLActivity extends Hilt_AndroidEntryPointBaseClassTest_LLActivity {}

  @AndroidEntryPoint(LL.class)
  public static final class LLL extends Hilt_AndroidEntryPointBaseClassTest_LLL {}

  @AndroidEntryPoint(LS.class)
  public static final class LLS extends Hilt_AndroidEntryPointBaseClassTest_LLS {}

  @AndroidEntryPoint(SL.class)
  public static final class LSL extends Hilt_AndroidEntryPointBaseClassTest_LSL {}

  @AndroidEntryPoint(SS.class)
  public static final class LSS extends Hilt_AndroidEntryPointBaseClassTest_LSS {}

  @AndroidEntryPoint
  public static final class SLL extends LL {}

  @AndroidEntryPoint
  public static final class SLS extends LS {}

  @AndroidEntryPoint
  public static final class SSL extends SL {}

  @AndroidEntryPoint
  public static final class SSS extends SS {}

  @AndroidEntryPoint(L.class)
  public static class LL extends Hilt_AndroidEntryPointBaseClassTest_LL {}

  @AndroidEntryPoint(S.class)
  public static class LS extends Hilt_AndroidEntryPointBaseClassTest_LS {}

  @AndroidEntryPoint
  public static class SL extends L {}

  @AndroidEntryPoint
  public static class SS extends S {}

  @AndroidEntryPoint(ComponentActivity.class)
  public static class L extends Hilt_AndroidEntryPointBaseClassTest_L {}

  @AndroidEntryPoint
  public static class S extends ComponentActivity {}

  @Test
  public void checkGeneratedClassHierarchy_shortForm() throws Exception {
    // When using the short form notation, the generated top level class is not actually assignable
    // to the generated base classes at compile time
    assertIsNotAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_SSS.class,
        Hilt_AndroidEntryPointBaseClassTest_S.class);
    assertIsNotAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_SS.class,
        Hilt_AndroidEntryPointBaseClassTest_S.class);
  }

  @Test
  public void checkGeneratedClassHierarchy_longForm() throws Exception {
    // When using the long form notation, they are assignable at compile time
    assertIsAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_LLL.class,
        Hilt_AndroidEntryPointBaseClassTest_LL.class);
    assertIsAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_LL.class,
        Hilt_AndroidEntryPointBaseClassTest_L.class);
  }

  @Test
  public void checkGeneratedClassHierarchy_shortFormRoot() throws Exception {
    // If the root is short-form, then the child class cannot be assigned to it.
    assertIsNotAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_LLS.class,
        Hilt_AndroidEntryPointBaseClassTest_S.class);
    assertIsNotAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_LS.class,
        Hilt_AndroidEntryPointBaseClassTest_S.class);
  }

  @Test
  public void checkGeneratedClassHierarchy_longFormRoot() throws Exception {
    // If the root is long-form, then the child class can be assigned to it.
    assertIsAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_SSL.class,
        Hilt_AndroidEntryPointBaseClassTest_L.class);
    assertIsAssignableTo(
        Hilt_AndroidEntryPointBaseClassTest_SL.class,
        Hilt_AndroidEntryPointBaseClassTest_L.class);
  }

  /** Asserts that the {@code class1} is not assignable to the {@code class2}. */
  private static void assertIsNotAssignableTo(Class<?> class1, Class<?> class2) {
    assertThat(class2.isAssignableFrom(class1)).isFalse();
  }

  /** Asserts that the {@code class1} is assignable to the {@code class2}. */
  private static void assertIsAssignableTo(Class<?> class1, Class<?> class2) {
    assertThat(class1).isAssignableTo(class2);
  }
}