Search in sources :

Example 46 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class BindViewTest method failsOptionalRootViewBinding.

@Test
public void failsOptionalRootViewBinding() throws Exception {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.content.Context;\n" + "import android.view.View;\n" + "import butterknife.OnClick;\n" + "import butterknife.Optional;\n" + "public class Test extends View {\n" + "  @Optional @OnClick void doStuff() {}\n" + "  public Test(Context context) {\n" + "    super(context);\n" + "  }\n" + "}");
    assertAbout(javaSource()).that(source).processedWith(new ButterKnifeProcessor()).failsToCompile().withErrorContaining("ID-free binding must not be annotated with @Optional. (test.Test.doStuff)").in(source).onLine(7);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 47 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class BindViewTest method bindingGeneratedView.

@Test
public void bindingGeneratedView() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindView;\n" + "@PerformGeneration\n" + "public class Test {\n" + "    @BindView(1) GeneratedView thing;\n" + "}");
    // w/o the GeneratingProcessor it can't find `class GeneratedView`
    assertAbout(javaSources()).that(ImmutableList.of(source, TestGeneratingProcessor.ANNOTATION)).processedWith(new ButterKnifeProcessor()).failsToCompile().withErrorContaining("cannot find symbol");
    // now the GeneratingProcessor should let it compile
    assertAbout(javaSources()).that(ImmutableList.of(source, TestGeneratingProcessor.ANNOTATION)).processedWith(new ButterKnifeProcessor(), new TestGeneratingProcessor("GeneratedView", "package test;", "import android.content.Context;", "import android.view.View;", "public class GeneratedView extends View {", "  public GeneratedView(Context context) {", "    super(context);", "  }", "}")).compilesWithoutError().withNoteContaining("@BindView field with unresolved type (GeneratedView)").and().withNoteContaining("must elsewhere be generated as a View or interface").and().and().generatesFileNamed(StandardLocation.CLASS_OUTPUT, "test", "Test_ViewBinding.class");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 48 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class BindViewTest method oneFindPerId.

@Test
public void oneFindPerId() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import butterknife.BindView;\n" + "import butterknife.OnClick;\n" + "public class Test {\n" + "  @BindView(1) View thing1;\n" + "  @OnClick(1) void doStuff() {}\n" + "}");
    JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "package test;\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.DebouncingOnClickListener;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  private Test target;\n" + "  private View view1;\n" + "  @UiThread\n" + "  public Test_ViewBinding(final Test target, View source) {\n" + "    this.target = target;\n" + "    View view;\n" + "    view = Utils.findRequiredView(source, 1, \"field 'thing1' and method 'doStuff'\");\n" + "    target.thing1 = view;\n" + "    view1 = view;\n" + "    view.setOnClickListener(new DebouncingOnClickListener() {\n" + "      @Override\n" + "      public void doClick(View p0) {\n" + "        target.doStuff();\n" + "      }\n" + "    });\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    Test target = this.target;\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" + "    this.target = null;\n" + "    target.thing1 = null;\n" + "    view1.setOnClickListener(null);\n" + "    view1 = null;\n" + "  }\n" + "}");
    assertAbout(javaSource()).that(source).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutWarnings().and().generatesSources(bindingSource);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 49 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class BindViewsTest method bindingArrayWithCast.

@Test
public void bindingArrayWithCast() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.widget.TextView;\n" + "import butterknife.BindViews;\n" + "public class Test {\n" + "    @BindViews({1, 2, 3}) TextView[] thing;\n" + "}");
    JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "package test;\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import android.widget.TextView;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  private Test target;\n" + "  @UiThread\n" + "  public Test_ViewBinding(Test target, View source) {\n" + "    this.target = target;\n" + "    target.thing = Utils.arrayOf(\n" + "        Utils.findRequiredViewAsType(source, 1, \"field 'thing'\", TextView.class), \n" + "        Utils.findRequiredViewAsType(source, 2, \"field 'thing'\", TextView.class), \n" + "        Utils.findRequiredViewAsType(source, 3, \"field 'thing'\", TextView.class));\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    Test target = this.target;\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" + "    this.target = null;\n" + "    target.thing = null;\n" + "  }\n" + "}");
    assertAbout(javaSource()).that(source).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutWarnings().and().generatesSources(bindingSource);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 50 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class BindViewsTest method bindingArrayWithGenerics.

@Test
public void bindingArrayWithGenerics() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import butterknife.BindViews;\n" + "public class Test<T extends View> {\n" + "    @BindViews({1, 2, 3}) T[] thing;\n" + "}");
    JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "package test;\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  private Test target;\n" + "  @UiThread\n" + "  public Test_ViewBinding(Test target, View source) {\n" + "    this.target = target;\n" + "    target.thing = Utils.arrayOf(\n" + "        Utils.findRequiredView(source, 1, \"field 'thing'\"), \n" + "        Utils.findRequiredView(source, 2, \"field 'thing'\"), \n" + "        Utils.findRequiredView(source, 3, \"field 'thing'\"));\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    Test target = this.target;\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" + "    this.target = null;\n" + "    target.thing = null;\n" + "  }\n" + "}");
    assertAbout(javaSource()).that(source).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutError().and().generatesSources(bindingSource);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Aggregations

ButterKnifeProcessor (butterknife.compiler.ButterKnifeProcessor)121 JavaFileObject (javax.tools.JavaFileObject)121 Test (org.junit.Test)121