Skip to content

Commit

Permalink
Port some resource binding tests to be functional.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Oct 13, 2017
1 parent dc88730 commit cda0bad
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 526 deletions.
1 change: 1 addition & 0 deletions butterknife/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
androidTestImplementation deps.junit
androidTestImplementation deps.truth
androidTestImplementation deps.support.test.runner
androidTestAnnotationProcessor project(':butterknife-compiler')

testImplementation deps.junit
testImplementation deps.truth
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package butterknife.functional;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import butterknife.BindArray;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

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

static class StringArrayTarget {
@BindArray(R.array.string_one_two_three) String[] actual;
}

@Test public void asStringArray() {
StringArrayTarget target = new StringArrayTarget();
String[] expected = context.getResources().getStringArray(R.array.string_one_two_three);

Unbinder unbinder = new BindArrayTest$StringArrayTarget_ViewBinding(target, context);
assertThat(target.actual).isEqualTo(expected);

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

static class IntArrayTarget {
@BindArray(R.array.int_one_two_three) int[] actual;
}

@Test public void asIntArray() {
IntArrayTarget target = new IntArrayTarget();
int[] expected = context.getResources().getIntArray(R.array.int_one_two_three);

Unbinder unbinder = new BindArrayTest$IntArrayTarget_ViewBinding(target, context);
assertThat(target.actual).isEqualTo(expected);

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

static class CharSequenceArrayTarget {
@BindArray(R.array.int_one_two_three) CharSequence[] actual;
}

@Test public void asCharSequenceArray() {
CharSequenceArrayTarget target = new CharSequenceArrayTarget();
CharSequence[] expected = context.getResources().getTextArray(R.array.int_one_two_three);

Unbinder unbinder = new BindArrayTest$CharSequenceArrayTarget_ViewBinding(target, context);
assertThat(target.actual).isEqualTo(expected);

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

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

import static com.google.common.truth.Truth.assertThat;

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

static class Target {
@BindBool(R.bool.just_true) boolean actual;
}

@Test public void asBoolean() {
Target target = new Target();
boolean expected = context.getResources().getBoolean(R.bool.just_true);

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

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

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import butterknife.BindColor;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

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

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

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

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

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

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import butterknife.BindDimen;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

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

static class IntTarget {
@BindDimen(R.dimen.twelve_point_two_dp) int actual;
}

@Test public void asInt() {
IntTarget target = new IntTarget();
int expected = context.getResources().getDimensionPixelSize(R.dimen.twelve_point_two_dp);

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

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

static class FloatTarget {
@BindDimen(R.dimen.twelve_point_two_dp) float actual;
}

@Test public void asFloat() {
FloatTarget target = new FloatTarget();
float expected = context.getResources().getDimension(R.dimen.twelve_point_two_dp);

Unbinder unbinder = new BindDimenTest$FloatTarget_ViewBinding(target, context);
assertThat(target.actual).isEqualTo(expected);

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

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.util.TypedValue;
import butterknife.BindFloat;
import butterknife.BindInt;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

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

static class Target {
@BindFloat(R.dimen.twelve_point_two) float actual;
}

@Test public void asFloat() {
Target target = new Target();
TypedValue value = new TypedValue();
context.getResources().getValue(R.dimen.twelve_point_two, value, true);
float expected = value.getFloat();

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

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

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import butterknife.BindInt;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

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

static class Target {
@BindInt(R.integer.twelve) int actual;
}

@Test public void asInt() {
Target target = new Target();
int expected = context.getResources().getInteger(R.integer.twelve);

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

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

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import butterknife.BindString;
import butterknife.Unbinder;
import butterknife.test.R;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

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

static class Target {
@BindString(R.string.hey) String actual;
}

@Test public void simpleInt() {
Target target = new Target();
String expected = context.getString(R.string.hey);

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

unbinder.unbind();
assertThat(target.actual).isEqualTo(expected);
}
}
18 changes: 18 additions & 0 deletions butterknife/src/androidTest/res/values/values.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<resources>
<bool name="just_true">true</bool>
<color name="red">#ffff0000</color>
<integer name="twelve">12</integer>
<dimen name="twelve_point_two_dp">12.2dp</dimen>
<item name="twelve_point_two" format="float" type="dimen">12.2</item>
<string name="hey">Hey</string>
<string-array name="string_one_two_three">
<item>One</item>
<item>Two</item>
<item>Three</item>
</string-array>
<integer-array name="int_one_two_three">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
</resources>
Loading

0 comments on commit cda0bad

Please sign in to comment.