|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.android.sessionreplay.material |
| 8 | + |
| 9 | +import android.graphics.Rect |
| 10 | +import android.graphics.Typeface |
| 11 | +import android.graphics.drawable.Drawable |
| 12 | +import android.text.Layout |
| 13 | +import com.datadog.android.api.InternalLogger |
| 14 | +import com.datadog.android.sessionreplay.ImagePrivacy |
| 15 | +import com.datadog.android.sessionreplay.material.forge.ForgeConfigurator |
| 16 | +import com.datadog.android.sessionreplay.material.internal.ChipWireframeMapper |
| 17 | +import com.datadog.android.sessionreplay.material.internal.densityNormalized |
| 18 | +import com.datadog.android.sessionreplay.recorder.MappingContext |
| 19 | +import com.datadog.android.sessionreplay.utils.AsyncJobStatusCallback |
| 20 | +import com.datadog.android.sessionreplay.utils.ColorStringFormatter |
| 21 | +import com.datadog.android.sessionreplay.utils.DrawableToColorMapper |
| 22 | +import com.datadog.android.sessionreplay.utils.GlobalBounds |
| 23 | +import com.datadog.android.sessionreplay.utils.OPAQUE_ALPHA_VALUE |
| 24 | +import com.datadog.android.sessionreplay.utils.ViewBoundsResolver |
| 25 | +import com.datadog.android.sessionreplay.utils.ViewIdentifierResolver |
| 26 | +import com.google.android.material.chip.Chip |
| 27 | +import fr.xgouchet.elmyr.Forge |
| 28 | +import fr.xgouchet.elmyr.annotation.FloatForgery |
| 29 | +import fr.xgouchet.elmyr.annotation.Forgery |
| 30 | +import fr.xgouchet.elmyr.annotation.IntForgery |
| 31 | +import fr.xgouchet.elmyr.annotation.LongForgery |
| 32 | +import fr.xgouchet.elmyr.annotation.StringForgery |
| 33 | +import fr.xgouchet.elmyr.junit5.ForgeConfiguration |
| 34 | +import fr.xgouchet.elmyr.junit5.ForgeExtension |
| 35 | +import org.junit.jupiter.api.BeforeEach |
| 36 | +import org.junit.jupiter.api.Test |
| 37 | +import org.junit.jupiter.api.extension.ExtendWith |
| 38 | +import org.junit.jupiter.api.extension.Extensions |
| 39 | +import org.mockito.ArgumentMatchers.anyInt |
| 40 | +import org.mockito.Mock |
| 41 | +import org.mockito.junit.jupiter.MockitoExtension |
| 42 | +import org.mockito.junit.jupiter.MockitoSettings |
| 43 | +import org.mockito.kotlin.any |
| 44 | +import org.mockito.kotlin.doReturn |
| 45 | +import org.mockito.kotlin.eq |
| 46 | +import org.mockito.kotlin.isNull |
| 47 | +import org.mockito.kotlin.mock |
| 48 | +import org.mockito.kotlin.times |
| 49 | +import org.mockito.kotlin.verify |
| 50 | +import org.mockito.kotlin.whenever |
| 51 | +import org.mockito.quality.Strictness |
| 52 | + |
| 53 | +@Extensions( |
| 54 | + ExtendWith(MockitoExtension::class), |
| 55 | + ExtendWith(ForgeExtension::class) |
| 56 | +) |
| 57 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 58 | +@ForgeConfiguration(value = ForgeConfigurator::class) |
| 59 | +class ChipWireframeMapperTest { |
| 60 | + |
| 61 | + private lateinit var testedChipWireframeMapper: ChipWireframeMapper |
| 62 | + |
| 63 | + @Mock |
| 64 | + lateinit var mockChipView: Chip |
| 65 | + |
| 66 | + @Mock |
| 67 | + lateinit var mockViewBoundsResolver: ViewBoundsResolver |
| 68 | + |
| 69 | + @Mock |
| 70 | + lateinit var mockAsyncJobStatusCallback: AsyncJobStatusCallback |
| 71 | + |
| 72 | + @Mock |
| 73 | + lateinit var mockInternalLogger: InternalLogger |
| 74 | + |
| 75 | + @Mock |
| 76 | + lateinit var mockViewIdentifierResolver: ViewIdentifierResolver |
| 77 | + |
| 78 | + @Mock |
| 79 | + lateinit var mockColorStringFormatter: ColorStringFormatter |
| 80 | + |
| 81 | + @Mock |
| 82 | + lateinit var mockDrawableToColorMapper: DrawableToColorMapper |
| 83 | + |
| 84 | + @Mock |
| 85 | + lateinit var mockChipDrawable: Drawable |
| 86 | + |
| 87 | + @LongForgery |
| 88 | + var fakeViewId: Long = 0L |
| 89 | + |
| 90 | + @StringForgery |
| 91 | + var fakeText: String = "" |
| 92 | + |
| 93 | + @Forgery |
| 94 | + lateinit var fakeMappingContext: MappingContext |
| 95 | + |
| 96 | + @Forgery |
| 97 | + lateinit var fakeGlobalBounds: GlobalBounds |
| 98 | + |
| 99 | + lateinit var fakeDrawableBounds: Rect |
| 100 | + |
| 101 | + @IntForgery |
| 102 | + private var fakeDrawableHeight: Int = 0 |
| 103 | + |
| 104 | + @IntForgery |
| 105 | + private var fakeDrawableWidth: Int = 0 |
| 106 | + |
| 107 | + @Mock |
| 108 | + lateinit var mockLayout: Layout |
| 109 | + |
| 110 | + @FloatForgery(0f, 255f) |
| 111 | + var fakeFontSize: Float = 0f |
| 112 | + |
| 113 | + @IntForgery(min = 0, max = 0xffffff) |
| 114 | + var fakeTextColor: Int = 0 |
| 115 | + |
| 116 | + @BeforeEach |
| 117 | + fun `set up`(forge: Forge) { |
| 118 | + fakeDrawableBounds = Rect( |
| 119 | + forge.aSmallInt(), |
| 120 | + forge.aSmallInt(), |
| 121 | + forge.aSmallInt(), |
| 122 | + forge.aSmallInt() |
| 123 | + ) |
| 124 | + mockChipView = mockChip() |
| 125 | + whenever( |
| 126 | + mockViewBoundsResolver.resolveViewGlobalBounds( |
| 127 | + mockChipView, |
| 128 | + fakeMappingContext.systemInformation.screenDensity |
| 129 | + ) |
| 130 | + ).thenReturn(fakeGlobalBounds) |
| 131 | + whenever( |
| 132 | + mockViewIdentifierResolver.resolveViewId( |
| 133 | + mockChipView |
| 134 | + ) |
| 135 | + ).thenReturn(fakeViewId) |
| 136 | + testedChipWireframeMapper = ChipWireframeMapper( |
| 137 | + viewIdentifierResolver = mockViewIdentifierResolver, |
| 138 | + colorStringFormatter = mockColorStringFormatter, |
| 139 | + viewBoundsResolver = mockViewBoundsResolver, |
| 140 | + drawableToColorMapper = mockDrawableToColorMapper |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun `M resolves card view wireframe W map`() { |
| 146 | + // When |
| 147 | + testedChipWireframeMapper.map( |
| 148 | + mockChipView, |
| 149 | + fakeMappingContext, |
| 150 | + mockAsyncJobStatusCallback, |
| 151 | + mockInternalLogger |
| 152 | + ) |
| 153 | + |
| 154 | + // Then |
| 155 | + val density = fakeMappingContext.systemInformation.screenDensity |
| 156 | + |
| 157 | + verify(fakeMappingContext.imageWireframeHelper).createImageWireframe( |
| 158 | + view = eq(mockChipView), |
| 159 | + // Background drawable doesn't need to be masked. |
| 160 | + imagePrivacy = eq(ImagePrivacy.MASK_NONE), |
| 161 | + currentWireframeIndex = anyInt(), |
| 162 | + x = eq( |
| 163 | + fakeGlobalBounds.x + fakeDrawableBounds.left.toLong() |
| 164 | + .densityNormalized(density) |
| 165 | + ), |
| 166 | + y = eq( |
| 167 | + fakeGlobalBounds.y + fakeDrawableBounds.top.toLong() |
| 168 | + .densityNormalized(density) |
| 169 | + ), |
| 170 | + width = eq(fakeDrawableWidth), |
| 171 | + height = eq(fakeDrawableHeight), |
| 172 | + usePIIPlaceholder = eq(false), |
| 173 | + drawable = eq(mockChipDrawable), |
| 174 | + drawableCopier = any(), |
| 175 | + asyncJobStatusCallback = eq(mockAsyncJobStatusCallback), |
| 176 | + clipping = isNull(), |
| 177 | + shapeStyle = isNull(), |
| 178 | + border = isNull(), |
| 179 | + prefix = any() |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + private fun mockChip(): Chip { |
| 184 | + return mock { |
| 185 | + whenever(it.text).thenReturn(fakeText) |
| 186 | + whenever(it.chipDrawable).thenReturn(mockChipDrawable) |
| 187 | + whenever(mockChipDrawable.bounds).thenReturn(fakeDrawableBounds) |
| 188 | + whenever(mockChipDrawable.intrinsicWidth).thenReturn(fakeDrawableWidth) |
| 189 | + whenever(mockChipDrawable.intrinsicHeight).thenReturn(fakeDrawableHeight) |
| 190 | + |
| 191 | + whenever(it.layout) doReturn mockLayout |
| 192 | + whenever(it.typeface) doReturn Typeface.SERIF |
| 193 | + whenever(it.textSize) doReturn fakeFontSize |
| 194 | + whenever(it.currentTextColor) doReturn fakeTextColor |
| 195 | + whenever(it.textAlignment) doReturn 0 |
| 196 | + whenever(it.gravity) doReturn 0 |
| 197 | + whenever( |
| 198 | + mockColorStringFormatter.formatColorAndAlphaAsHexString( |
| 199 | + fakeTextColor, |
| 200 | + OPAQUE_ALPHA_VALUE |
| 201 | + ) |
| 202 | + ) doReturn "" |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments