Haoyu Zhang | c38bed1 | 2019-12-05 14:38:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Louis Pullen-Freilich | a2be36b | 2020-07-21 21:35:11 +0100 | [diff] [blame^] | 17 | package androidx.compose.ui.text.android.style |
Haoyu Zhang | c38bed1 | 2019-12-05 14:38:45 -0800 | [diff] [blame] | 18 | |
| 19 | import android.text.TextPaint |
| 20 | import androidx.test.filters.SmallTest |
Louis Pullen-Freilich | a2be36b | 2020-07-21 21:35:11 +0100 | [diff] [blame^] | 21 | import androidx.compose.ui.text.android.InternalPlatformTextApi |
Haoyu Zhang | c38bed1 | 2019-12-05 14:38:45 -0800 | [diff] [blame] | 22 | import com.nhaarman.mockitokotlin2.any |
| 23 | import com.nhaarman.mockitokotlin2.doReturn |
| 24 | import com.nhaarman.mockitokotlin2.mock |
| 25 | import com.nhaarman.mockitokotlin2.never |
| 26 | import com.nhaarman.mockitokotlin2.verify |
| 27 | import org.junit.Test |
| 28 | import org.junit.runner.RunWith |
| 29 | import org.junit.runners.JUnit4 |
| 30 | |
Siyamed Sinir | 21f7348 | 2020-06-17 11:36:22 -0700 | [diff] [blame] | 31 | @OptIn(InternalPlatformTextApi::class) |
Haoyu Zhang | c38bed1 | 2019-12-05 14:38:45 -0800 | [diff] [blame] | 32 | @SmallTest |
| 33 | @RunWith(JUnit4::class) |
| 34 | class LetterSpacingSpanPxTest { |
| 35 | @Test |
| 36 | fun updateDrawState() { |
| 37 | val letterSpacing = 10f |
| 38 | val textSize = 10f |
| 39 | val textScaleX = 2f |
| 40 | |
| 41 | val letterSpacingSpanPx = LetterSpacingSpanPx(letterSpacing) |
| 42 | |
| 43 | val textPaint = mock<TextPaint> { |
| 44 | on { this.textSize } doReturn textSize |
| 45 | on { this.textScaleX } doReturn textScaleX |
| 46 | } |
| 47 | |
| 48 | letterSpacingSpanPx.updateDrawState(textPaint) |
| 49 | |
| 50 | verify(textPaint).letterSpacing = 0.5f |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | fun updateDrawState_with_invalid_textSize() { |
| 55 | val letterSpacing = 10f |
| 56 | val textSize = 0f |
| 57 | val textScaleX = 2f |
| 58 | |
| 59 | val letterSpacingSpanPx = LetterSpacingSpanPx(letterSpacing) |
| 60 | |
| 61 | val textPaint = mock<TextPaint> { |
| 62 | on { this.textSize } doReturn textSize |
| 63 | on { this.textScaleX } doReturn textScaleX |
| 64 | } |
| 65 | |
| 66 | letterSpacingSpanPx.updateDrawState(textPaint) |
| 67 | |
| 68 | verify(textPaint, never()).letterSpacing = any() |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | fun updateMeasureState() { |
| 73 | val letterSpacing = 10f |
| 74 | val textSize = 10f |
| 75 | val textScaleX = 2f |
| 76 | |
| 77 | val letterSpacingSpanPx = LetterSpacingSpanPx(letterSpacing) |
| 78 | |
| 79 | val textPaint = mock<TextPaint> { |
| 80 | on { this.textSize } doReturn textSize |
| 81 | on { this.textScaleX } doReturn textScaleX |
| 82 | } |
| 83 | |
| 84 | letterSpacingSpanPx.updateMeasureState(textPaint) |
| 85 | |
| 86 | verify(textPaint).letterSpacing = 0.5f |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun updateMeasureState_with_invalid_textSize() { |
| 91 | val letterSpacing = 10f |
| 92 | val textSize = 0f |
| 93 | val textScaleX = 2f |
| 94 | |
| 95 | val letterSpacingSpanPx = LetterSpacingSpanPx(letterSpacing) |
| 96 | |
| 97 | val textPaint = mock<TextPaint> { |
| 98 | on { this.textSize } doReturn textSize |
| 99 | on { this.textScaleX } doReturn textScaleX |
| 100 | } |
| 101 | |
| 102 | letterSpacingSpanPx.updateMeasureState(textPaint) |
| 103 | |
| 104 | verify(textPaint, never()).letterSpacing = any() |
| 105 | } |
| 106 | } |