Adam Powell | 211f4821 | 2019-11-04 13:33:35 -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 | |
| 17 | package androidx.ui.core |
| 18 | |
| 19 | import org.junit.Assert.assertEquals |
| 20 | import org.junit.Test |
| 21 | |
| 22 | class ModifierTest { |
| 23 | /** |
Adam Powell | dd72306 | 2019-12-04 13:52:59 -0800 | [diff] [blame] | 24 | * Verifies that the [Modifier.plus] operation results in [Modifier] instances that |
Adam Powell | 211f4821 | 2019-11-04 13:33:35 -0800 | [diff] [blame] | 25 | * [Modifier.foldIn] and [Modifier.foldOut] in the expected order. |
| 26 | */ |
| 27 | @Test |
| 28 | fun wrapElementChain() { |
Adam Powell | dd72306 | 2019-12-04 13:52:59 -0800 | [diff] [blame] | 29 | val chain = SampleModifier(1) + SampleModifier(2) + SampleModifier(3) |
Adam Powell | 211f4821 | 2019-11-04 13:33:35 -0800 | [diff] [blame] | 30 | val forwards = chain.foldIn(emptyList<Int>()) { acc, e -> |
| 31 | acc + (e as SampleModifier).value |
| 32 | } |
| 33 | val backwards = chain.foldOut(emptyList<Int>()) { e, acc -> |
| 34 | acc + (e as SampleModifier).value |
| 35 | } |
| 36 | assertEquals("1-3 folded in (forwards)", listOf(1, 2, 3), forwards) |
| 37 | assertEquals("1-3 folded out (backwards)", listOf(3, 2, 1), backwards) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private class SampleModifier(val value: Int) : Modifier.Element |