Skip to content

Instantly share code, notes, and snippets.

@DariusL

DariusL/A.java Secret

Created March 16, 2016 06:32
Show Gist options
  • Save DariusL/5c7488137703530fe488 to your computer and use it in GitHub Desktop.
Save DariusL/5c7488137703530fe488 to your computer and use it in GitHub Desktop.
package lt.segfoltas.breathcount;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
public class A implements Parcelable {
private int mInt;
@Nullable private A mA;
public A(int mInt, A mA) {
this.mInt = mInt;
this.mA = mA;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
A a = (A) o;
if (mInt != a.mInt) return false;
return mA != null ? mA.equals(a.mA) : a.mA == null;
}
@Override
public int hashCode() {
int result = mInt;
result = 31 * result + (mA != null ? mA.hashCode() : 0);
return result;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.mInt);
dest.writeParcelable(this.mA, flags);
}
public A() {
}
protected A(Parcel in) {
this.mInt = in.readInt();
this.mA = in.readParcelable(A.class.getClassLoader());
}
public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
@Override
public A createFromParcel(Parcel source) {
return new A(source);
}
@Override
public A[] newArray(int size) {
return new A[size];
}
};
}
package lt.segfoltas.breathcount;
import android.os.Parcel;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
@RunWith(RobolectricGradleTestRunner.class)
@Config(
constants = BuildConfig.class,
sdk = 21,
application = TestApplication.class)
public class ATest {
@Test
public void testParcel() throws Exception {
A empty = new A(5, null);
A full = new A(6, empty);
Parcel parcel = Parcel.obtain();
parcel.writeParcelable(empty, 0);
parcel.writeParcelable(full, 0);
parcel.setDataPosition(0);
A readEmpty = parcel.readParcelable(null);
A readFull = parcel.readParcelable(null);
assertThat(readEmpty, is(empty));
assertThat(readFull, is(full));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment