|
| 1 | +/* |
| 2 | + * Copyright 2020 Square Inc. |
| 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 | +package com.squareup.workflow.ui.compose.internal |
| 17 | + |
| 18 | +import androidx.compose.FrameManager |
| 19 | +import androidx.compose.mutableStateOf |
| 20 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 21 | +import androidx.ui.foundation.Text |
| 22 | +import androidx.ui.test.assertIsDisplayed |
| 23 | +import androidx.ui.test.createComposeRule |
| 24 | +import androidx.ui.test.findByText |
| 25 | +import com.squareup.workflow.ui.ViewEnvironment |
| 26 | +import com.squareup.workflow.ui.ViewRegistry |
| 27 | +import com.squareup.workflow.ui.compose.bindCompose |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | + |
| 32 | +@RunWith(AndroidJUnit4::class) |
| 33 | +class ViewRegistriesTest { |
| 34 | + |
| 35 | + @Rule @JvmField val composeRule = createComposeRule() |
| 36 | + |
| 37 | + @Test fun showRendering_recomposes_whenFactoryChanged() { |
| 38 | + val registry1 = ViewRegistry(bindCompose<String> { rendering, _ -> |
| 39 | + Text(rendering) |
| 40 | + }) |
| 41 | + val registry2 = ViewRegistry(bindCompose<String> { rendering, _ -> |
| 42 | + Text(rendering.reversed()) |
| 43 | + }) |
| 44 | + val registry = mutableStateOf(registry1) |
| 45 | + |
| 46 | + composeRule.setContent { |
| 47 | + registry.value.showRendering("hello", ViewEnvironment(registry.value)) |
| 48 | + } |
| 49 | + |
| 50 | + findByText("hello").assertIsDisplayed() |
| 51 | + FrameManager.framed { |
| 52 | + registry.value = registry2 |
| 53 | + } |
| 54 | + findByText("olleh").assertIsDisplayed() |
| 55 | + } |
| 56 | +} |
0 commit comments