|
| 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.drawable |
| 9 | + |
| 10 | +import android.graphics.Canvas |
| 11 | +import android.graphics.Color |
| 12 | +import android.graphics.DashPathEffect |
| 13 | +import android.graphics.Paint |
| 14 | +import android.graphics.Path |
| 15 | +import android.util.DisplayMetrics |
| 16 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 17 | +import com.facebook.react.uimanager.Spacing |
| 18 | +import com.facebook.react.uimanager.style.BorderInsets |
| 19 | +import com.facebook.react.uimanager.style.BorderStyle |
| 20 | +import com.facebook.react.uimanager.style.LogicalEdge |
| 21 | +import org.assertj.core.api.Assertions.assertThat |
| 22 | +import org.junit.After |
| 23 | +import org.junit.Before |
| 24 | +import org.junit.Test |
| 25 | +import org.junit.runner.RunWith |
| 26 | +import org.mockito.kotlin.any |
| 27 | +import org.mockito.kotlin.argumentCaptor |
| 28 | +import org.mockito.kotlin.atLeastOnce |
| 29 | +import org.mockito.kotlin.mock |
| 30 | +import org.mockito.kotlin.verify |
| 31 | +import org.robolectric.RobolectricTestRunner |
| 32 | +import org.robolectric.RuntimeEnvironment |
| 33 | + |
| 34 | +@RunWith(RobolectricTestRunner::class) |
| 35 | +class BorderDrawableTest { |
| 36 | + |
| 37 | + @Before |
| 38 | + fun setUp() { |
| 39 | + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics().apply { density = 1f }) |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + fun tearDown() { |
| 44 | + DisplayMetricsHolder.setScreenDisplayMetrics(null) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun removingBorderStyleClearsDashedPathEffect() { |
| 49 | + val borderInsets = BorderInsets().apply { setBorderWidth(LogicalEdge.ALL, 8f) } |
| 50 | + val drawable = |
| 51 | + BorderDrawable( |
| 52 | + context = RuntimeEnvironment.getApplication(), |
| 53 | + borderWidth = Spacing(0f), |
| 54 | + borderRadius = null, |
| 55 | + borderInsets = borderInsets, |
| 56 | + borderStyle = BorderStyle.DASHED, |
| 57 | + ) |
| 58 | + drawable.setBounds(0, 0, 240, 120) |
| 59 | + drawable.setBorderWidth(Spacing.ALL, 8f) |
| 60 | + drawable.setBorderColor(LogicalEdge.ALL, Color.RED) |
| 61 | + |
| 62 | + assertThat(drawBorderPaint(drawable).pathEffect).isInstanceOf(DashPathEffect::class.java) |
| 63 | + |
| 64 | + drawable.borderStyle = null |
| 65 | + |
| 66 | + assertThat(drawBorderPaint(drawable).pathEffect).isNull() |
| 67 | + } |
| 68 | + |
| 69 | + private fun drawBorderPaint(drawable: BorderDrawable): Paint { |
| 70 | + val canvas = mock<Canvas>() |
| 71 | + drawable.draw(canvas) |
| 72 | + val paint = argumentCaptor<Paint>() |
| 73 | + verify(canvas, atLeastOnce()).drawPath(any<Path>(), paint.capture()) |
| 74 | + return paint.lastValue |
| 75 | + } |
| 76 | +} |
0 commit comments