-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Paywalls V2] Adds image background tests #1967
Merged
+151
−3
Merged
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
7f32542
Adds Result to revenuecatui.
JayShortway a584cf8
Makes a few things fake-public in purchases.
JayShortway 86813c9
Adds some extensions.
JayShortway 6e1badd
Modifies some things to use the new Result class.
JayShortway 04cd4d2
Fixes tests.
JayShortway 0167612
Adds PackageContext and VariableContextTests
JayShortway 1c4d4c4
Adds PresentedStackPartial.
JayShortway 8427cf7
Adds PaywallComponent.toComponentStyle() as well as ways to recursive…
JayShortway 72cdb32
Adds StyleFactory.
JayShortway 52419ea
Adds NonEmptyList.
JayShortway aa01767
Adds ResultTests.
JayShortway 6359f23
Adds StyleFactoryTests.
JayShortway 97198e2
Merge branch 'main' into pw2-stylefactory
JayShortway 40fe1de
Removes a FIXME.
JayShortway 52c152c
Fixes a typo.
JayShortway 83450c0
Fixes tests, locally at least.
JayShortway 4da75a1
Adds a failing test.
JayShortway 6a8424c
Fixes the failing test.
JayShortway 2fcff8d
Adds a missing @JvmSynthetic.
JayShortway ba1c466
Adds another failing test.
JayShortway 83561b7
Adds captureToImageCompat and assertPixelColorEquals.
JayShortway 0e5a50b
Adds assertTextColorEquals and assertTextLayoutResult.
JayShortway 884c163
Updates TextComponentViewTests.
JayShortway 5258dec
Minor adjustment to themeChangingTest.
JayShortway af5f7d5
Some more adjustment to themeChangingTest.
JayShortway 649cc5b
Separates themeChangingTest to a separate file.
JayShortway a32b3b4
ThemeController now has separate setLight and setDark methods.
JayShortway 7967af5
Updates TextComponentStyle and TextComponentView.
JayShortway 11d5abe
Removes some comments.
JayShortway 5c0423e
Updates assertPixelColorEquals.
JayShortway ab54f60
Adds BackgroundTests and fixes BackgroundStyle.
JayShortway 1370842
Adjusts a comment.
JayShortway ce73071
Adjusts another comment.
JayShortway 3b460fd
Adjusts another comment.
JayShortway 61cafa6
Merge branch 'pw2-text-theme-tests' into pw2-background-tests
JayShortway 69f5341
Minor update.
JayShortway 79107d1
Marks Shape.kt as JvmSynthetic at the file level.
JayShortway e633fa3
Adds `variable` and `locale` as `remember` keys in `rememberProcessed…
JayShortway 87e15f7
Fixes StyleFactoryTests.
JayShortway dd57c1f
Merge branch 'main' into pw2-stylefactory
JayShortway 842c556
Merge branch 'pw2-stylefactory' into pw2-text-theme-tests
JayShortway 722cac4
Merge branch 'pw2-text-theme-tests' into pw2-background-tests
JayShortway 729e23c
Merge branch 'main' into pw2-text-theme-tests
JayShortway 40e5f11
Merge branch 'pw2-text-theme-tests' into pw2-background-tests
JayShortway 29da06a
Merge branch 'main' into pw2-background-tests
JayShortway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
145 changes: 145 additions & 0 deletions
145
...st/kotlin/com/revenuecat/purchases/ui/revenuecatui/components/modifier/BackgroundTests.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,145 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.components.modifier | ||
|
||
import android.graphics.drawable.ColorDrawable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.requiredSize | ||
import androidx.compose.material3.Text | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.semantics.semantics | ||
import androidx.compose.ui.semantics.testTag | ||
import androidx.compose.ui.test.assertIsDisplayed | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import coil.Coil | ||
import coil.ImageLoader | ||
import coil.annotation.ExperimentalCoilApi | ||
import coil.test.FakeImageLoaderEngine | ||
import com.revenuecat.purchases.paywalls.components.common.Background | ||
import com.revenuecat.purchases.paywalls.components.properties.ImageUrls | ||
import com.revenuecat.purchases.paywalls.components.properties.ThemeImageUrls | ||
import com.revenuecat.purchases.ui.revenuecatui.assertions.assertPixelColorEquals | ||
import com.revenuecat.purchases.ui.revenuecatui.assertions.assertPixelColorPercentage | ||
import com.revenuecat.purchases.ui.revenuecatui.components.properties.toBackgroundStyle | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.annotation.GraphicsMode | ||
import org.robolectric.shadows.ShadowPixelCopy | ||
import java.net.URL | ||
|
||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
@Config(shadows = [ShadowPixelCopy::class], sdk = [26]) | ||
@OptIn(ExperimentalCoilApi::class) | ||
@RunWith(AndroidJUnit4::class) | ||
class BackgroundTests { | ||
|
||
private enum class TestUrl(val urlString: String, val color: Color) { | ||
Blue(urlString = "https://blue", color = Color.Blue), | ||
; | ||
} | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Before | ||
fun setup() { | ||
val engine = FakeImageLoaderEngine.Builder() | ||
.interceptAllTestUrls() | ||
.build() | ||
val imageLoader = ImageLoader.Builder(InstrumentationRegistry.getInstrumentation().targetContext) | ||
.components { add(engine) } | ||
.build() | ||
|
||
Coil.setImageLoader(imageLoader) | ||
} | ||
|
||
@After | ||
fun teardown() { | ||
Coil.reset() | ||
} | ||
|
||
@Test | ||
fun `Should properly set an image background`(): Unit = with(composeTestRule) { | ||
// Arrange | ||
val sizePx = 100 | ||
val testUrl = TestUrl.Blue | ||
val expectedColor = testUrl.color | ||
val background = Background.Image(ThemeImageUrls(light = testUrl.toImageUrls(size = sizePx))) | ||
setContent { | ||
val backgroundStyle = background.toBackgroundStyle() | ||
val sizeDp = with(LocalDensity.current) { sizePx.toDp() } | ||
|
||
// Act | ||
Box( | ||
modifier = Modifier | ||
.requiredSize(sizeDp) | ||
.background(backgroundStyle) | ||
.semantics { testTag = "box" } | ||
) | ||
} | ||
|
||
// Assert | ||
onNodeWithTag("box") | ||
.assertIsDisplayed() | ||
.assertPixelColorEquals(startX = 0, startY = 0, width = sizePx, height = sizePx, color = expectedColor) | ||
} | ||
|
||
@Test | ||
fun `Should draw image background behind content`(): Unit = with(composeTestRule) { | ||
// Arrange | ||
val sizePx = 100 | ||
val testUrl = TestUrl.Blue | ||
val expectedColor = testUrl.color | ||
val background = Background.Image(ThemeImageUrls(light = testUrl.toImageUrls(size = sizePx))) | ||
setContent { | ||
val backgroundStyle = background.toBackgroundStyle() | ||
val sizeDp = with(LocalDensity.current) { sizePx.toDp() } | ||
|
||
// Act | ||
Text( | ||
text = "Hello", | ||
modifier = Modifier | ||
.requiredSize(sizeDp) | ||
.background(backgroundStyle) | ||
.semantics { testTag = "box" } | ||
) | ||
} | ||
|
||
// Assert | ||
onNodeWithTag("box") | ||
.assertIsDisplayed() | ||
.assertPixelColorPercentage( | ||
startX = 0, | ||
startY = 0, | ||
width = sizePx, | ||
height = sizePx, | ||
color = expectedColor, | ||
// Text rendering might not be fully deterministic (e.g. due to anti aliasing, font settings, etc.) so | ||
// we're just verifying that the majority of the Composable shows the background, but not all of it. | ||
predicate = { percentage -> percentage in 0.6f..0.99f } | ||
) | ||
} | ||
|
||
private fun FakeImageLoaderEngine.Builder.interceptAllTestUrls(): FakeImageLoaderEngine.Builder = apply { | ||
TestUrl.values().forEach { testUrl -> | ||
intercept(testUrl.urlString, ColorDrawable(testUrl.color.toArgb())) | ||
} | ||
} | ||
|
||
private fun TestUrl.toImageUrls(size: Int): ImageUrls = | ||
ImageUrls( | ||
original = URL(urlString), | ||
webp = URL(urlString), | ||
webpLowRes = URL(urlString), | ||
width = size.toUInt(), | ||
height = size.toUInt(), | ||
) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bug was caught by the tests. It caused the image to not load at all, as we don't have a
Fetcher
that supports theURL
type.