Skip to content

Commit 96e66e7

Browse files
jorge-cabfacebook-github-bot
authored andcommitted
Add unit tests for CompositeBackgroundDrawable layer ordering optimization (#47913)
Summary: Pull Request resolved: #47913 CompositeBackgroundDrawable has some logic for inserting the layers that are not set through the constructor. This unit test makes sure they are being properly ordered. Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D66381515 fbshipit-source-id: 514979a4a97fa159c1d1c3c923afe969eafc21c3
1 parent cb7cd89 commit 96e66e7

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager
9+
10+
import android.annotation.TargetApi
11+
import android.content.Context
12+
import android.graphics.Color
13+
import android.graphics.drawable.LayerDrawable
14+
import com.facebook.react.common.annotations.UnstableReactNativeAPI
15+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
16+
import com.facebook.react.uimanager.drawable.BackgroundDrawable
17+
import com.facebook.react.uimanager.drawable.BorderDrawable
18+
import com.facebook.react.uimanager.drawable.CompositeBackgroundDrawable
19+
import com.facebook.react.uimanager.drawable.InsetBoxShadowDrawable
20+
import com.facebook.react.uimanager.drawable.OutlineDrawable
21+
import com.facebook.react.uimanager.drawable.OutsetBoxShadowDrawable
22+
import com.facebook.react.uimanager.style.OutlineStyle
23+
import org.assertj.core.api.Assertions.assertThat
24+
import org.junit.Before
25+
import org.junit.Test
26+
import org.junit.runner.RunWith
27+
import org.mockito.MockedStatic
28+
import org.mockito.Mockito.mockStatic
29+
import org.robolectric.RobolectricTestRunner
30+
import org.robolectric.RuntimeEnvironment
31+
32+
@TargetApi(29)
33+
@OptIn(UnstableReactNativeAPI::class)
34+
@RunWith(RobolectricTestRunner::class)
35+
class CompositeBackgroundDrawableTest {
36+
37+
private val ctx: Context = RuntimeEnvironment.getApplication()
38+
private lateinit var rnFeatureFlags: MockedStatic<ReactNativeFeatureFlags>
39+
40+
@Before
41+
fun setup() {
42+
rnFeatureFlags = mockStatic(ReactNativeFeatureFlags::class.java)
43+
rnFeatureFlags
44+
.`when`<Boolean> { ReactNativeFeatureFlags.enableNewBackgroundAndBorderDrawables() }
45+
.thenReturn(true)
46+
}
47+
48+
@Test
49+
fun testCompositeBackgroundDrawableLayerOrdering() {
50+
51+
val background = BackgroundDrawable(ctx)
52+
val outerShadows =
53+
LayerDrawable(arrayOf(OutsetBoxShadowDrawable(ctx, Color.BLACK, 1f, 1f, 0f, 1f)))
54+
val innerShadows =
55+
LayerDrawable(arrayOf(InsetBoxShadowDrawable(ctx, Color.BLACK, 1f, 1f, 0f, 1f)))
56+
val border = BorderDrawable(ctx, null, null, null, null)
57+
val outline =
58+
OutlineDrawable(
59+
ctx,
60+
outlineColor = Color.BLACK,
61+
outlineOffset = 0f,
62+
outlineStyle = OutlineStyle.SOLID,
63+
outlineWidth = 1f)
64+
65+
/** Create CompositeBackgroundDrawable with constructor */
66+
val control =
67+
CompositeBackgroundDrawable(
68+
context = ctx,
69+
outerShadows = outerShadows,
70+
background = background,
71+
border = border,
72+
innerShadows = innerShadows,
73+
outline = outline)
74+
75+
/**
76+
* Create CompositeBackgroundDrawable with shuffled method functions (triggers layer ordering
77+
* logic)
78+
*/
79+
val test = CompositeBackgroundDrawable(ctx)
80+
test.withNewInnerShadow(innerShadows)
81+
test.withNewOuterShadow(outerShadows)
82+
test.withNewBorder(border)
83+
test.withNewOutline(outline)
84+
test.withNewBackground(background)
85+
86+
/** Verify that the two CompositeBackgroundDrawables are equivalent */
87+
assertThat(test.getDrawable(0)).isEqualTo(control.getDrawable(0))
88+
assertThat(test.getDrawable(1)).isEqualTo(control.getDrawable(1))
89+
assertThat(test.getDrawable(2)).isEqualTo(control.getDrawable(2))
90+
assertThat(test.getDrawable(3)).isEqualTo(control.getDrawable(3))
91+
assertThat(test.getDrawable(4)).isEqualTo(control.getDrawable(4))
92+
}
93+
}

0 commit comments

Comments
 (0)