/* * Copyright (C) 2017 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.internal.codegen; import androidx.room.compiler.processing.util.Source; import dagger.testing.compile.CompilerTests; import dagger.testing.golden.GoldenFileRule; import java.util.Collection; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class ElidedFactoriesTest { @Parameters(name = "{0}") public static Collection parameters() { return CompilerMode.TEST_PARAMETERS; } @Rule public GoldenFileRule goldenFileRule = new GoldenFileRule(); private final CompilerMode compilerMode; public ElidedFactoriesTest(CompilerMode compilerMode) { this.compilerMode = compilerMode; } @Test public void simpleComponent() throws Exception { Source injectedType = CompilerTests.javaSource( "test.InjectedType", "package test;", "", "import javax.inject.Inject;", "", "final class InjectedType {", " @Inject InjectedType() {}", "}"); Source dependsOnInjected = CompilerTests.javaSource( "test.DependsOnInjected", "package test;", "", "import javax.inject.Inject;", "", "final class DependsOnInjected {", " @Inject DependsOnInjected(InjectedType injected) {}", "}"); Source componentFile = CompilerTests.javaSource( "test.SimpleComponent", "package test;", "", "import dagger.Component;", "", "@Component", "interface SimpleComponent {", " DependsOnInjected dependsOnInjected();", "}"); CompilerTests.daggerCompiler(injectedType, dependsOnInjected, componentFile) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerSimpleComponent")); }); } @Test public void simpleComponent_injectsProviderOf_dependsOnScoped() throws Exception { Source scopedType = CompilerTests.javaSource( "test.ScopedType", "package test;", "", "import javax.inject.Inject;", "import javax.inject.Singleton;", "", "@Singleton", "final class ScopedType {", " @Inject ScopedType() {}", "}"); Source dependsOnScoped = CompilerTests.javaSource( "test.DependsOnScoped", "package test;", "", "import javax.inject.Inject;", "import javax.inject.Provider;", "", "final class DependsOnScoped {", " @Inject DependsOnScoped(ScopedType scoped) {}", "}"); Source needsProvider = CompilerTests.javaSource( "test.NeedsProvider", "package test;", "", "import javax.inject.Inject;", "import javax.inject.Provider;", "", "class NeedsProvider {", " @Inject NeedsProvider(Provider provider) {}", "}"); Source componentFile = CompilerTests.javaSource( "test.SimpleComponent", "package test;", "", "import dagger.Component;", "import javax.inject.Singleton;", "", "@Singleton", "@Component", "interface SimpleComponent {", " NeedsProvider needsProvider();", "}"); CompilerTests.daggerCompiler(scopedType, dependsOnScoped, componentFile, needsProvider) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerSimpleComponent")); }); } @Test public void scopedBinding_onlyUsedInSubcomponent() throws Exception { Source scopedType = CompilerTests.javaSource( "test.ScopedType", "package test;", "", "import javax.inject.Inject;", "import javax.inject.Singleton;", "", "@Singleton", "final class ScopedType {", " @Inject ScopedType() {}", "}"); Source dependsOnScoped = CompilerTests.javaSource( "test.DependsOnScoped", "package test;", "", "import javax.inject.Inject;", "import javax.inject.Provider;", "", "final class DependsOnScoped {", " @Inject DependsOnScoped(ScopedType scoped) {}", "}"); Source componentFile = CompilerTests.javaSource( "test.SimpleComponent", "package test;", "", "import dagger.Component;", "import javax.inject.Singleton;", "", "@Singleton", "@Component", "interface SimpleComponent {", " Sub sub();", "}"); Source subcomponentFile = CompilerTests.javaSource( "test.Sub", "package test;", "", "import dagger.Subcomponent;", "", "@Subcomponent", "interface Sub {", " DependsOnScoped dependsOnScoped();", "}"); CompilerTests.daggerCompiler(scopedType, dependsOnScoped, componentFile, subcomponentFile) .withProcessingOptions(compilerMode.processorOptions()) .compile( subject -> { subject.hasErrorCount(0); subject.generatedSource(goldenFileRule.goldenSource("test/DaggerSimpleComponent")); }); } }