Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create RawEntityDereferencerTest, storage Reference-> CrdtEntity.Refe…
…rence (#4812) * Create RawEntityDereferencerTest, make storage Reference implement CrdtEntity.Reference. Also: Create ParcelableReference. * Add dep. Also, apparently read/writeBoolean is Q-only. * Just write null if there is no version map. * Just write null if there is no version map.
- Loading branch information
Jason Feinstein
committed
Mar 3, 2020
1 parent
ba7a107
commit a695797
Showing
12 changed files
with
459 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2020 Google LLC. | ||
* | ||
* This code may only be used under the BSD style license found at | ||
* http://polymer.github.io/LICENSE.txt | ||
* | ||
* Code distributed by Google as part of this project is also subject to an additional IP rights | ||
* grant found at | ||
* http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
package arcs.android.crdt | ||
|
||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import arcs.android.util.writeProto | ||
import arcs.core.storage.Reference | ||
import arcs.core.storage.StorageKeyParser | ||
|
||
/** Parcelable version of [Reference]. */ | ||
data class ParcelableReference(override val actual: Reference) : ParcelableReferencable { | ||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
super.writeToParcel(parcel, flags) | ||
parcel.writeString(actual.id) | ||
parcel.writeString(actual.storageKey.toString()) | ||
actual.version?.let { | ||
parcel.writeProto(it.toProto()) | ||
} ?: { | ||
parcel.writeTypedObject(null, flags) | ||
}() | ||
} | ||
|
||
override fun describeContents(): Int = 0 | ||
|
||
/* Don't use this directly, instead use ParcelableReferencable. */ | ||
internal companion object CREATOR : Parcelable.Creator<ParcelableReference> { | ||
override fun createFromParcel(parcel: Parcel): ParcelableReference { | ||
val id = requireNotNull(parcel.readString()) { | ||
"Required id not found in parcel for ParcelableReference" | ||
} | ||
val storageKeyString = requireNotNull(parcel.readString()) { | ||
"Required storageKey not found in parcel for ParcelableReference" | ||
} | ||
val versionMap = parcel.readVersionMap()?.takeIf { it.isNotEmpty() } | ||
|
||
return ParcelableReference( | ||
Reference(id, StorageKeyParser.parse(storageKeyString), versionMap) | ||
) | ||
} | ||
|
||
override fun newArray(size: Int): Array<ParcelableReference?> = arrayOfNulls(size) | ||
} | ||
} | ||
|
||
/** Writes the [Reference] to the receiving [Parcel]. */ | ||
fun Parcel.writeReference(reference: Reference, flags: Int) = | ||
writeTypedObject(ParcelableReference(reference), flags) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
javatests/arcs/android/storage/ParcelableReferenceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2020 Google LLC. | ||
* | ||
* This code may only be used under the BSD style license found at | ||
* http://polymer.github.io/LICENSE.txt | ||
* | ||
* Code distributed by Google as part of this project is also subject to an additional IP rights | ||
* grant found at | ||
* http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
package arcs.android.storage | ||
|
||
import android.os.Parcel | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import arcs.android.crdt.ParcelableRawEntity | ||
import arcs.android.crdt.readReferencable | ||
import arcs.android.crdt.writeReference | ||
import arcs.core.crdt.VersionMap | ||
import arcs.core.data.RawEntity | ||
import arcs.core.storage.Reference | ||
import arcs.core.storage.driver.RamDiskStorageKey | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ParcelableReferenceTest { | ||
@Before | ||
fun setUp() { | ||
RamDiskStorageKey.registerParser() | ||
} | ||
|
||
@Test | ||
fun parcelableRoundtrip_works_withNullVersionMap() { | ||
val expected = Reference("myId", RamDiskStorageKey("backingKey"), null) | ||
|
||
// Create a parcel and populate it with a ParcelableOperations object. | ||
val marshalled = with(Parcel.obtain()) { | ||
writeReference(expected, 0) | ||
marshall() | ||
} | ||
|
||
// Now unmarshall the parcel, so we can verify the contents. | ||
val unmarshalled = with(Parcel.obtain()) { | ||
unmarshall(marshalled, 0, marshalled.size) | ||
setDataPosition(0) | ||
readReferencable() | ||
} | ||
assertThat(unmarshalled).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun parcelableRoundtrip_works_withNonNullVersionMap() { | ||
val expected = Reference( | ||
"myId", | ||
RamDiskStorageKey("backingKey"), | ||
VersionMap("foo" to 1) | ||
) | ||
|
||
// Create a parcel and populate it with a ParcelableOperations object. | ||
val marshalled = with(Parcel.obtain()) { | ||
writeReference(expected, 0) | ||
marshall() | ||
} | ||
|
||
// Now unmarshall the parcel, so we can verify the contents. | ||
val unmarshalled = with(Parcel.obtain()) { | ||
unmarshall(marshalled, 0, marshalled.size) | ||
setDataPosition(0) | ||
readReferencable() | ||
} | ||
assertThat(unmarshalled).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun parcelableRoundtripWorks_whenReference_isPartOfRawEntity() { | ||
val expectedReference = Reference( | ||
"myId", | ||
RamDiskStorageKey("backingKey"), | ||
VersionMap("foo" to 1) | ||
) | ||
val expected = RawEntity( | ||
"myId", | ||
singletons = mapOf("foo" to expectedReference), | ||
collections = emptyMap() | ||
) | ||
|
||
// Create a parcel and populate it with a ParcelableOperations object. | ||
val marshalled = with(Parcel.obtain()) { | ||
writeTypedObject(ParcelableRawEntity(expected), 0) | ||
marshall() | ||
} | ||
|
||
// Now unmarshall the parcel, so we can verify the contents. | ||
val unmarshalled = with(Parcel.obtain()) { | ||
unmarshall(marshalled, 0, marshalled.size) | ||
setDataPosition(0) | ||
readReferencable() | ||
} | ||
assertThat(unmarshalled).isEqualTo(expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.