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.functional.assisted;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import dagger.Component;
22 import dagger.assisted.AssistedFactory;
23 import dagger.functional.assisted.subpackage.AccessibleFoo;
24 import dagger.functional.assisted.subpackage.AssistedDep;
25 import dagger.functional.assisted.subpackage.InaccessibleFooFactory;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 @RunWith(JUnit4.class)
31 public final class AssistedFactoryInaccessibleTest {
32   @Component
33   interface ParentComponent {
34     // Factory for an accessible type from another package
accessibleFooFactory()35     AccessibleFooFactory accessibleFooFactory();
36 
37     // Factory for an inaccessible type from another package
inaccessibleFooFactory()38     InaccessibleFooFactory inaccessibleFooFactory();
39   }
40 
41   @AssistedFactory
42   public interface AccessibleFooFactory {
43     // Use different parameter names than Foo to make sure we're not assuming they're the same.
create(AssistedDep factoryAssistedDep)44     AccessibleFoo create(AssistedDep factoryAssistedDep);
45   }
46 
47   @Test
testAccessibleFooFactory()48   public void testAccessibleFooFactory() {
49     AssistedDep assistedDep = new AssistedDep();
50     AccessibleFoo accessibleFoo =
51         DaggerAssistedFactoryInaccessibleTest_ParentComponent.create()
52             .accessibleFooFactory()
53             .create(assistedDep);
54     assertThat(accessibleFoo).isNotNull();
55     assertThat(accessibleFoo.dep).isNotNull();
56     assertThat(accessibleFoo.assistedDep).isEqualTo(assistedDep);
57   }
58 
59   @Test
testInaccessibleFooFactory()60   public void testInaccessibleFooFactory() {
61     AssistedDep assistedDep = new AssistedDep();
62     // We can't access InaccessibleFoo directly, so just use Object instead.
63     Object inaccessibleFoo =
64         DaggerAssistedFactoryInaccessibleTest_ParentComponent.create()
65             .inaccessibleFooFactory()
66             .create(assistedDep);
67     assertThat(inaccessibleFoo).isNotNull();
68   }
69 }
70