Skip to content

Commit 09b4a3d

Browse files
committed
Implementing Parcelable in ParseFile
1 parent a8c380d commit 09b4a3d

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

Parse/src/main/java/com/parse/ParseFile.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
package com.parse;
1010

11+
import android.os.Parcel;
12+
import android.os.Parcelable;
13+
1114
import org.json.JSONException;
1215
import org.json.JSONObject;
1316

@@ -40,7 +43,7 @@
4043
* object.save();
4144
* </pre>
4245
*/
43-
public class ParseFile {
46+
public class ParseFile implements Parcelable {
4447

4548
/* package for tests */ static ParseFileController getFileController() {
4649
return ParseCorePlugins.getInstance().getFileController();
@@ -221,6 +224,22 @@ public ParseFile(byte[] data, String contentType) {
221224
this(null, data, contentType);
222225
}
223226

227+
/**
228+
* Creates a new file instance from a Parcel {@code source}. This is used when unparceling
229+
* a non-dirty ParseFile. Subclasses that need Parcelable behavior should provide their own
230+
* {@link android.os.Parcelable.Creator} and override this constructor.
231+
*
232+
* @param source
233+
* the source Parcel
234+
*/
235+
protected ParseFile(Parcel source) {
236+
this(new State.Builder()
237+
.url(source.readString())
238+
.name(source.readString())
239+
.mimeType(source.readByte() == 1 ? source.readString() : null)
240+
.build());
241+
}
242+
224243
/* package for tests */ ParseFile(State state) {
225244
this.state = state;
226245
}
@@ -705,4 +724,35 @@ public void cancel() {
705724

706725
return json;
707726
}
727+
728+
@Override
729+
public int describeContents() {
730+
return 0;
731+
}
732+
733+
@Override
734+
public void writeToParcel(Parcel dest, int flags) {
735+
if (isDirty()) {
736+
throw new RuntimeException("Unable to parcel an unsaved ParseFile.");
737+
}
738+
dest.writeString(getUrl()); // Not null
739+
dest.writeString(getName()); // Not null
740+
String type = state.mimeType(); // Nullable
741+
dest.writeByte(type != null ? (byte) 1 : 0);
742+
if (type != null) {
743+
dest.writeString(type);
744+
}
745+
}
746+
747+
public final static Creator<ParseFile> CREATOR = new Creator<ParseFile>() {
748+
@Override
749+
public ParseFile createFromParcel(Parcel source) {
750+
return new ParseFile(source);
751+
}
752+
753+
@Override
754+
public ParseFile[] newArray(int size) {
755+
return new ParseFile[size];
756+
}
757+
};
708758
}

Parse/src/test/java/com/parse/ParseFileTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
*/
99
package com.parse;
1010

11+
import android.os.Parcel;
12+
1113
import org.junit.After;
1214
import org.junit.Before;
1315
import org.junit.Rule;
1416
import org.junit.Test;
1517
import org.junit.rules.TemporaryFolder;
18+
import org.junit.runner.RunWith;
1619
import org.mockito.ArgumentCaptor;
1720
import org.mockito.Matchers;
21+
import org.robolectric.RobolectricTestRunner;
22+
import org.robolectric.annotation.Config;
1823

1924
import java.io.File;
2025
import java.io.InputStream;
@@ -36,6 +41,8 @@
3641
import static org.mockito.Mockito.verify;
3742
import static org.mockito.Mockito.when;
3843

44+
@RunWith(RobolectricTestRunner.class)
45+
@Config(constants = BuildConfig.class, sdk = 23)
3946
public class ParseFileTest {
4047

4148
@Rule
@@ -491,6 +498,33 @@ public void testCancel() {
491498
}
492499
}
493500

501+
@Test
502+
public void testParcelable() {
503+
String mime = "mime";
504+
String name = "name";
505+
String url = "url";
506+
ParseFile file = new ParseFile(new ParseFile.State.Builder()
507+
.name(name)
508+
.mimeType(mime)
509+
.url(url)
510+
.build());
511+
Parcel parcel = Parcel.obtain();
512+
file.writeToParcel(parcel, 0);
513+
parcel.setDataPosition(0);
514+
file = ParseFile.CREATOR.createFromParcel(parcel);
515+
assertEquals(file.getName(), name);
516+
assertEquals(file.getUrl(), url);
517+
assertEquals(file.getState().mimeType(), mime);
518+
assertFalse(file.isDirty());
519+
}
520+
521+
@Test( expected = RuntimeException.class )
522+
public void testDontParcelIfDirty() {
523+
ParseFile file = new ParseFile(new ParseFile.State.Builder().build());
524+
Parcel parcel = Parcel.obtain();
525+
file.writeToParcel(parcel, 0);
526+
}
527+
494528
// TODO(grantland): testEncode
495529
// TODO(grantland): testDecode
496530
}

0 commit comments

Comments
 (0)