Skip to content

Commit

Permalink
Merge pull request #1105 from JakeWharton/jw/more-functional/2017-10-13
Browse files Browse the repository at this point in the history
Port more resource binding to functional tests.
  • Loading branch information
JakeWharton authored Oct 13, 2017
2 parents a9ddc0d + acf89ad commit a639ef3
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package butterknife.functional;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.test.InstrumentationRegistry;
import butterknife.BindBitmap;
import butterknife.BindBool;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;

public final class BindBitmapTest {
private final Context context = InstrumentationRegistry.getContext();

static class Target {
@BindBitmap(R.drawable.pixel) Bitmap actual;
}

@Test public void asBitmap() {
Target target = new Target();
Bitmap expected = BitmapFactory.decodeResource(context.getResources(), R.drawable.pixel);

Unbinder unbinder = new BindBitmapTest$Target_ViewBinding(target, context);
assertTrue(target.actual.sameAs(expected));

unbinder.unbind();
assertTrue(target.actual.sameAs(expected));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package butterknife.functional;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.test.InstrumentationRegistry;
import butterknife.BindColor;
import butterknife.Unbinder;
Expand All @@ -12,18 +13,33 @@
public final class BindColorTest {
private final Context context = InstrumentationRegistry.getContext();

static class Target {
static class IntTarget {
@BindColor(R.color.red) int actual;
}

@Test public void asInt() {
Target target = new Target();
IntTarget target = new IntTarget();
int expected = context.getResources().getColor(R.color.red);

Unbinder unbinder = new BindColorTest$Target_ViewBinding(target, context);
Unbinder unbinder = new BindColorTest$IntTarget_ViewBinding(target, context);
assertThat(target.actual).isEqualTo(expected);

unbinder.unbind();
assertThat(target.actual).isEqualTo(expected);
}

static class ColorStateListTarget {
@BindColor(R.color.colors) ColorStateList actual;
}

@Test public void asColorStateList() {
ColorStateListTarget target = new ColorStateListTarget();
ColorStateList expected = context.getResources().getColorStateList(R.color.colors);

Unbinder unbinder = new BindColorTest$ColorStateListTarget_ViewBinding(target, context);
assertThat(target.actual.toString()).isEqualTo(expected.toString());

unbinder.unbind();
assertThat(target.actual.toString()).isEqualTo(expected.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package butterknife.functional;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.support.test.InstrumentationRegistry;
import butterknife.BindBitmap;
import butterknife.BindDrawable;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue;

public final class BindDrawableTest {
private final Context context = InstrumentationRegistry.getContext();

static class Target {
@BindDrawable(R.drawable.circle) Drawable actual;
}

@Test public void asDrawable() {
Target target = new Target();
Drawable expected = context.getResources().getDrawable(R.drawable.circle);

Unbinder unbinder = new BindDrawableTest$Target_ViewBinding(target, context);
assertThat(target.actual.getConstantState()).isEqualTo(expected.getConstantState());

unbinder.unbind();
assertThat(target.actual.getConstantState()).isEqualTo(expected.getConstantState());
}
}
11 changes: 11 additions & 0 deletions butterknife/src/androidTest/res/color/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="#ffff0000"
android:state_pressed="true"
/>
<item
android:color="#ff0000ff"
android:state_focused="true"
/>
<item android:color="#ff000000"/>
</selector>
13 changes: 13 additions & 0 deletions butterknife/src/androidTest/res/drawable/circle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<size
android:height="10px"
android:width="10px"
/>
<solid
android:color="#fff"
/>
</shape>
Binary file added butterknife/src/androidTest/res/drawable/pixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 1 addition & 55 deletions butterknife/src/test/java/butterknife/BindBitmapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,7 @@
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;

public class BindBitmapTest {
@Test public void simple() {
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
+ "package test;\n"
+ "import android.graphics.Bitmap;\n"
+ "import butterknife.BindBitmap;\n"
+ "public class Test {\n"
+ " @BindBitmap(1) Bitmap one;\n"
+ "}"
);

JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", ""
+ "// Generated code from Butter Knife. Do not modify!\n"
+ "package test;\n"
+ "import android.content.Context;\n"
+ "import android.content.res.Resources;\n"
+ "import android.graphics.BitmapFactory;\n"
+ "import android.support.annotation.CallSuper;\n"
+ "import android.support.annotation.UiThread;\n"
+ "import android.view.View;\n"
+ "import butterknife.Unbinder;\n"
+ "import java.lang.Deprecated;\n"
+ "import java.lang.Override;\n"
+ "import java.lang.SuppressWarnings;\n"
+ "public class Test_ViewBinding implements Unbinder {\n"
+ " /**\n"
+ " * @deprecated Use {@link #Test_ViewBinding(Test, Context)} for direct creation.\n"
+ " * Only present for runtime invocation through {@code ButterKnife.bind()}.\n"
+ " */\n"
+ " @Deprecated\n"
+ " @UiThread\n"
+ " public Test_ViewBinding(Test target, View source) {\n"
+ " this(target, source.getContext());\n"
+ " }\n"
+ " @UiThread\n"
+ " @SuppressWarnings(\"ResourceType\")\n"
+ " public Test_ViewBinding(Test target, Context context) {\n"
+ " Resources res = context.getResources();\n"
+ " target.one = BitmapFactory.decodeResource(res, 1);\n"
+ " }\n"
+ " @Override\n"
+ " @CallSuper\n"
+ " public void unbind() {\n"
+ " }\n"
+ "}"
);

assertAbout(javaSource()).that(source)
.withCompilerOptions("-Xlint:-processing")
.processedWith(new ButterKnifeProcessor())
.compilesWithoutWarnings()
.and()
.generatesSources(bindingSource);
}

public final class BindBitmapTest {
@Test public void typeMustBeBitmap() {
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
+ "package test;\n"
Expand Down
52 changes: 0 additions & 52 deletions butterknife/src/test/java/butterknife/BindColorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,58 +59,6 @@ public final class BindColorTest {
.generatesSources(bindingSource);
}

@Test public void simpleColorStateList() {
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
+ "package test;\n"
+ "import android.content.res.ColorStateList;\n"
+ "import butterknife.BindColor;\n"
+ "public class Test {\n"
+ " @BindColor(1) ColorStateList one;\n"
+"}"
);

JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", ""
+ "// Generated code from Butter Knife. Do not modify!\n"
+ "package test;\n"
+ "import android.content.Context;\n"
+ "import android.support.annotation.CallSuper;\n"
+ "import android.support.annotation.UiThread;\n"
+ "import android.support.v4.content.ContextCompat;\n"
+ "import android.view.View;\n"
+ "import butterknife.Unbinder;\n"
+ "import java.lang.Deprecated;\n"
+ "import java.lang.Override;\n"
+ "import java.lang.SuppressWarnings;\n"
+ "public class Test_ViewBinding implements Unbinder {\n"
+ " /**\n"
+ " * @deprecated Use {@link #Test_ViewBinding(Test, Context)} for direct creation.\n"
+ " * Only present for runtime invocation through {@code ButterKnife.bind()}.\n"
+ " */\n"
+ " @Deprecated\n"
+ " @UiThread\n"
+ " public Test_ViewBinding(Test target, View source) {\n"
+ " this(target, source.getContext());\n"
+ " }\n"
+ " @UiThread\n"
+ " @SuppressWarnings(\"ResourceType\")\n"
+ " public Test_ViewBinding(Test target, Context context) {\n"
+ " target.one = ContextCompat.getColorStateList(context, 1);\n"
+ " }\n"
+ " @Override\n"
+ " @CallSuper\n"
+ " public void unbind() {\n"
+ " }\n"
+ "}"
);

assertAbout(javaSource()).that(source)
.withCompilerOptions("-Xlint:-processing")
.processedWith(new ButterKnifeProcessor())
.compilesWithoutWarnings()
.and()
.generatesSources(bindingSource);
}

@Test public void simpleColorStateListSdk23() {
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
+ "package test;\n"
Expand Down
54 changes: 1 addition & 53 deletions butterknife/src/test/java/butterknife/BindDrawableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,7 @@
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;

public class BindDrawableTest {
@Test public void simple() {
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
+ "package test;\n"
+ "import android.graphics.drawable.Drawable;\n"
+ "import butterknife.BindDrawable;\n"
+ "public class Test {\n"
+ " @BindDrawable(1) Drawable one;\n"
+ "}"
);

JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", ""
+ "// Generated code from Butter Knife. Do not modify!\n"
+ "package test;\n"
+ "import android.content.Context;\n"
+ "import android.support.annotation.CallSuper;\n"
+ "import android.support.annotation.UiThread;\n"
+ "import android.support.v4.content.ContextCompat;\n"
+ "import android.view.View;\n"
+ "import butterknife.Unbinder;\n"
+ "import java.lang.Deprecated;\n"
+ "import java.lang.Override;\n"
+ "import java.lang.SuppressWarnings;\n"
+ "public class Test_ViewBinding implements Unbinder {\n"
+ " /**\n"
+ " * @deprecated Use {@link #Test_ViewBinding(Test, Context)} for direct creation.\n"
+ " * Only present for runtime invocation through {@code ButterKnife.bind()}.\n"
+ " */\n"
+ " @Deprecated\n"
+ " @UiThread\n"
+ " public Test_ViewBinding(Test target, View source) {\n"
+ " this(target, source.getContext());\n"
+ " }\n"
+ " @UiThread\n"
+ " @SuppressWarnings(\"ResourceType\")\n"
+ " public Test_ViewBinding(Test target, Context context) {\n"
+ " target.one = ContextCompat.getDrawable(context, 1);\n"
+ " }\n"
+ " @Override\n"
+ " @CallSuper\n"
+ " public void unbind() {\n"
+ " }\n"
+ "}"
);

assertAbout(javaSource()).that(source)
.withCompilerOptions("-Xlint:-processing")
.processedWith(new ButterKnifeProcessor())
.compilesWithoutWarnings()
.and()
.generatesSources(bindingSource);
}

public final class BindDrawableTest {
@Test public void simpleSdk21() {
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
+ "package test;\n"
Expand Down

0 comments on commit a639ef3

Please sign in to comment.