Skip to content

Commit 92a9c6a

Browse files
committed
Implementing Parcelable in ParseFile
1 parent aaf740a commit 92a9c6a

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

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

Lines changed: 51 additions & 1 deletion
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

Lines changed: 34 additions & 0 deletions
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
@@ -492,6 +499,33 @@ public void testCancel() {
492499
}
493500
}
494501

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

0 commit comments

Comments
 (0)