Skip to content

Commit

Permalink
Polymorphic support
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 committed May 24, 2024
1 parent f8022bb commit f10c5e8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
package com.emergetools.snapshots

import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator

enum class SnapshotType {
COMPOSABLE,
VIEW,
ACTIVITY,
}

@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonClassDiscriminator("metadataType")
sealed class SnapshotMetadata {
abstract val name: String
abstract val displayName: String?
abstract val fqn: String
abstract val type: SnapshotType
abstract val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig?
}

@Serializable
internal data class SnapshotImageMetadata(
// Used as the primary key
override val name: String,
// User defined name, or set to defaults by our backend
override val displayName: String?,
// Filename of the outputted image
val filename: String,
// FQN of the test class
override val fqn: String,
override val type: SnapshotType,
// Compose-specific metadata, only set if type == COMPOSABLE
override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) : SnapshotMetadata()
@Serializable
internal class SuccessMetadata(
// Used as the primary key
override val name: String,
// User defined name, or set to defaults by our backend
override val displayName: String?,
// Filename of the outputted image
val filename: String,
// FQN of the test class
override val fqn: String,
override val type: SnapshotType,
// Compose-specific metadata, only set if type == COMPOSABLE
override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) : SnapshotMetadata()

@Serializable
internal data class SnapshotErrorMetadata(
// Used as the primary key
override val name: String,
// User defined name, or set to defaults by our backend
override val displayName: String?,
// FQN of the test class
override val fqn: String,
override val type: SnapshotType,
// Compose-specific metadata, only set if type == COMPOSABLE
override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) : SnapshotMetadata()
@Serializable
internal class ErrorMetadata(
// Used as the primary key
override val name: String,
// User defined name, or set to defaults by our backend
override val displayName: String?,
// FQN of the test class
override val fqn: String,
override val type: SnapshotType,
// Compose-specific metadata, only set if type == COMPOSABLE
override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) : SnapshotMetadata()
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ internal object SnapshotSaver {
private val args: Bundle
get() = InstrumentationRegistry.getArguments()

private val saveMetadata: Boolean
get() = args.getBoolean(ARG_KEY_SAVE_METADATA, false) ||
args.getString(ARG_KEY_SAVE_METADATA, "false").toBoolean()

fun save(
displayName: String?,
bitmap: Bitmap,
Expand All @@ -56,10 +60,7 @@ internal object SnapshotSaver {
keyName = keyName,
bitmap = bitmap
)
if (
args.getBoolean(ARG_KEY_SAVE_METADATA, false) ||
args.getString(ARG_KEY_SAVE_METADATA, "false").toBoolean()
) {
if (saveMetadata) {
saveMetadata(
snapshotsDir = snapshotsDir,
displayName = displayName,
Expand Down Expand Up @@ -90,14 +91,16 @@ internal object SnapshotSaver {
displayName = displayName,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)
saveErrorMetadata(
snapshotsDir = snapshotsDir,
displayName = displayName,
keyName = keyName,
type = type,
fqn = fqn,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)
if (saveMetadata) {
saveErrorMetadata(
snapshotsDir = snapshotsDir,
displayName = displayName,
keyName = keyName,
type = type,
fqn = fqn,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)
}
}

private fun saveImage(
Expand All @@ -118,17 +121,16 @@ internal object SnapshotSaver {
type: SnapshotType,
composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) {
val metadata = SnapshotImageMetadata(
val metadata: SnapshotMetadata = SnapshotMetadata.SuccessMetadata(
name = keyName,
// TODO: Ryan remove in future
keyName = keyName,
displayName = displayName,
filename = "$keyName$PNG_EXTENSION",
fqn = fqn,
type = type,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)

Log.d(TAG, "Saving error metadata for $keyName")
val jsonString = Json.encodeToString(metadata)

saveFile(snapshotsDir, "$keyName$JSON_EXTENSION") {
Expand All @@ -144,14 +146,15 @@ internal object SnapshotSaver {
type: SnapshotType,
composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null,
) {
val metadata = SnapshotErrorMetadata(
val metadata: SnapshotMetadata = SnapshotMetadata.ErrorMetadata(
name = keyName,
displayName = displayName,
fqn = fqn,
type = type,
composePreviewSnapshotConfig = composePreviewSnapshotConfig,
)

Log.d(TAG, "Saving error metadata for $keyName")
val jsonString = Json.encodeToString(metadata)

saveFile(snapshotsDir, "$keyName$JSON_EXTENSION") {
Expand Down

0 comments on commit f10c5e8

Please sign in to comment.