<commit_before>/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.ui.material

import androidx.test.filters.MediumTest
import androidx.ui.core.FirstBaseline
import androidx.ui.core.IntPx
import androidx.ui.core.LastBaseline
import androidx.ui.core.LayoutCoordinates
import androidx.ui.core.OnChildPositioned
import androidx.ui.core.PxPosition
import androidx.ui.core.Text
import androidx.ui.core.dp
import androidx.ui.core.globalPosition
import androidx.ui.core.round
import androidx.ui.core.withDensity
import androidx.ui.layout.DpConstraints
import androidx.ui.layout.Wrap
import androidx.ui.test.assertIsVisible
import androidx.ui.test.createComposeRule
import androidx.ui.test.doClick
import androidx.ui.test.findByText
import com.google.common.truth.Truth
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@MediumTest
@RunWith(JUnit4::class)
class SnackbarTest {

    @get:Rule
    val composeTestRule = createComposeRule(disableTransitions = true)

    val longText = "Message Is very long and long and long and long and long " +
            "and long and long and long and long and long and long"

    @Test
    fun defaultSnackbar_semantics() {
        var clicked = false
        composeTestRule.setMaterialContent {
            Wrap {
                Snackbar(text = "Message", actionText = "UNDO", onActionClick = { clicked = true })
            }
        }

        findByText("Message")
            .assertIsVisible()

        Truth.assertThat(clicked).isFalse()

        findByText("UNDO")
            .assertIsVisible()
            .doClick()

        Truth.assertThat(clicked).isTrue()
    }

    @Test
    fun snackbar_shortTextOnly_sizes() {
        var textCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            Snackbar(
                text = {
                    OnChildPositioned(onPositioned = { textCoords = it }) {
                        Text("Message")
                    }
                }
            )
        }
        sizes
            .assertHeightEqualsTo(48.dp)
            .assertWidthEqualsTo(300.dp)
        Truth.assertThat(textCoords).isNotNull()
        textCoords?.let {
            withDensity(composeTestRule.density) {
                val alignmentLines = it.providedAlignmentLines
                Truth.assertThat(alignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(alignmentLines.get(FirstBaseline))
                    .isEqualTo(alignmentLines.get(LastBaseline))
                Truth.assertThat(it.position.y.round() + alignmentLines.getValue(FirstBaseline))
                    .isEqualTo(30.dp.toIntPx())
            }
        }
    }

    @Test
    fun snackbar_shortTextAndButton_alignment() {
        var snackCoords: LayoutCoordinates? = null
        var textCoords: LayoutCoordinates? = null
        var buttonCoords: LayoutCoordinates? = null
        var buttonTextCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            OnChildPositioned(onPositioned = { snackCoords = it }) {
                Snackbar(
                    text = {
                        OnChildPositioned(onPositioned = { textCoords = it }) {
                            Text("Message")
                        }
                    },
                    action = {
                        OnChildPositioned(onPositioned = { buttonCoords = it }) {
                            Button(style = TextButtonStyle(), onClick = {}) {
                                OnChildPositioned(onPositioned = { buttonTextCoords = it }) {
                                    Text("Undo")
                                }
                            }
                        }
                    }
                )
            }
        }
        sizes
            .assertHeightEqualsTo(48.dp)
            .assertWidthEqualsTo(300.dp)
        Truth.assertThat(textCoords).isNotNull()
        Truth.assertThat(buttonCoords).isNotNull()
        Truth.assertThat(buttonTextCoords).isNotNull()
        Truth.assertThat(snackCoords).isNotNull()
        val localTextCoords = textCoords
        val localButtonCoords = buttonCoords
        val localButtonTextCoords = buttonTextCoords
        val localSnackCoords = snackCoords

        if (localTextCoords != null &&
            localButtonCoords != null &&
            localButtonTextCoords != null &&
            localSnackCoords != null
        ) {
            withDensity(composeTestRule.density) {
                val textAlignmentLines = localTextCoords.providedAlignmentLines
                val buttonAlignmentLines = localButtonTextCoords.providedAlignmentLines
                val buttonTextPos =
                    localSnackCoords.childToLocal(localButtonTextCoords, PxPosition.Origin)
                Truth.assertThat(textAlignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(buttonAlignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(
                    localTextCoords.globalPosition.y.round() +
                            textAlignmentLines.getValue(FirstBaseline)
                ).isEqualTo(30.dp.toIntPx())
                Truth.assertThat(
                    buttonTextPos.y.round() + buttonAlignmentLines.getValue(FirstBaseline)
                ).isEqualTo(30.dp.toIntPx())
            }
        }
    }

    @Test
    fun snackbar_longText_sizes() {
        var textCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            Snackbar(
                text = {
                    OnChildPositioned(onPositioned = { textCoords = it }) {
                        Text(longText, maxLines = 2)
                    }
                }
            )
        }
        sizes
            .assertHeightEqualsTo(68.dp)
            .assertWidthEqualsTo(300.dp)
        Truth.assertThat(textCoords).isNotNull()
        textCoords?.let {
            withDensity(composeTestRule.density) {
                val alignmentLines = it.providedAlignmentLines

                Truth.assertThat(alignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(alignmentLines.get(LastBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(alignmentLines.get(FirstBaseline))
                    .isNotEqualTo(alignmentLines.get(LastBaseline))
                Truth.assertThat(it.position.y.round() + alignmentLines.getValue(FirstBaseline))
                    .isEqualTo(30.dp.toIntPx())
            }
        }
    }

    @Test
    fun snackbar_longTextAndButton_alignment() {
        var snackCoords: LayoutCoordinates? = null
        var textCoords: LayoutCoordinates? = null
        var buttonCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            OnChildPositioned(onPositioned = { snackCoords = it }) {
                Snackbar(
                    text = {
                        OnChildPositioned(onPositioned = { textCoords = it }) {
                            Text(longText, maxLines = 2)
                        }
                    },
                    action = {
                        OnChildPositioned(onPositioned = { buttonCoords = it }) {
                            Button(text = "Undo", style = TextButtonStyle(), onClick = {})
                        }
                    }<commit_msg>Mark failing snackbar tests as Ignore.<commit_after>/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.ui.material

import androidx.test.filters.MediumTest
import androidx.ui.core.FirstBaseline
import androidx.ui.core.IntPx
import androidx.ui.core.LastBaseline
import androidx.ui.core.LayoutCoordinates
import androidx.ui.core.OnChildPositioned
import androidx.ui.core.PxPosition
import androidx.ui.core.Text
import androidx.ui.core.dp
import androidx.ui.core.globalPosition
import androidx.ui.core.round
import androidx.ui.core.withDensity
import androidx.ui.layout.DpConstraints
import androidx.ui.layout.Wrap
import androidx.ui.test.assertIsVisible
import androidx.ui.test.createComposeRule
import androidx.ui.test.doClick
import androidx.ui.test.findByText
import com.google.common.truth.Truth
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@MediumTest
@RunWith(JUnit4::class)
class SnackbarTest {

    @get:Rule
    val composeTestRule = createComposeRule(disableTransitions = true)

    val longText = "Message Is very long and long and long and long and long " +
            "and long and long and long and long and long and long"

    @Test
    fun defaultSnackbar_semantics() {
        var clicked = false
        composeTestRule.setMaterialContent {
            Wrap {
                Snackbar(text = "Message", actionText = "UNDO", onActionClick = { clicked = true })
            }
        }

        findByText("Message")
            .assertIsVisible()

        Truth.assertThat(clicked).isFalse()

        findByText("UNDO")
            .assertIsVisible()
            .doClick()

        Truth.assertThat(clicked).isTrue()
    }

    @Test
    fun snackbar_shortTextOnly_sizes() {
        var textCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            Snackbar(
                text = {
                    OnChildPositioned(onPositioned = { textCoords = it }) {
                        Text("Message")
                    }
                }
            )
        }
        sizes
            .assertHeightEqualsTo(48.dp)
            .assertWidthEqualsTo(300.dp)
        Truth.assertThat(textCoords).isNotNull()
        textCoords?.let {
            withDensity(composeTestRule.density) {
                val alignmentLines = it.providedAlignmentLines
                Truth.assertThat(alignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(alignmentLines.get(FirstBaseline))
                    .isEqualTo(alignmentLines.get(LastBaseline))
                Truth.assertThat(it.position.y.round() + alignmentLines.getValue(FirstBaseline))
                    .isEqualTo(30.dp.toIntPx())
            }
        }
    }

    @Test
    @Ignore // b/146432509
    fun snackbar_shortTextAndButton_alignment() {
        var snackCoords: LayoutCoordinates? = null
        var textCoords: LayoutCoordinates? = null
        var buttonCoords: LayoutCoordinates? = null
        var buttonTextCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            OnChildPositioned(onPositioned = { snackCoords = it }) {
                Snackbar(
                    text = {
                        OnChildPositioned(onPositioned = { textCoords = it }) {
                            Text("Message")
                        }
                    },
                    action = {
                        OnChildPositioned(onPositioned = { buttonCoords = it }) {
                            Button(style = TextButtonStyle(), onClick = {}) {
                                OnChildPositioned(onPositioned = { buttonTextCoords = it }) {
                                    Text("Undo")
                                }
                            }
                        }
                    }
                )
            }
        }
        sizes
            .assertHeightEqualsTo(48.dp)
            .assertWidthEqualsTo(300.dp)
        Truth.assertThat(textCoords).isNotNull()
        Truth.assertThat(buttonCoords).isNotNull()
        Truth.assertThat(buttonTextCoords).isNotNull()
        Truth.assertThat(snackCoords).isNotNull()
        val localTextCoords = textCoords
        val localButtonCoords = buttonCoords
        val localButtonTextCoords = buttonTextCoords
        val localSnackCoords = snackCoords

        if (localTextCoords != null &&
            localButtonCoords != null &&
            localButtonTextCoords != null &&
            localSnackCoords != null
        ) {
            withDensity(composeTestRule.density) {
                val textAlignmentLines = localTextCoords.providedAlignmentLines
                val buttonAlignmentLines = localButtonTextCoords.providedAlignmentLines
                val buttonTextPos =
                    localSnackCoords.childToLocal(localButtonTextCoords, PxPosition.Origin)
                Truth.assertThat(textAlignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(buttonAlignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(
                    localTextCoords.globalPosition.y.round() +
                            textAlignmentLines.getValue(FirstBaseline)
                ).isEqualTo(30.dp.toIntPx())
                Truth.assertThat(
                    buttonTextPos.y.round() + buttonAlignmentLines.getValue(FirstBaseline)
                ).isEqualTo(30.dp.toIntPx())
            }
        }
    }

    @Test
    fun snackbar_longText_sizes() {
        var textCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            Snackbar(
                text = {
                    OnChildPositioned(onPositioned = { textCoords = it }) {
                        Text(longText, maxLines = 2)
                    }
                }
            )
        }
        sizes
            .assertHeightEqualsTo(68.dp)
            .assertWidthEqualsTo(300.dp)
        Truth.assertThat(textCoords).isNotNull()
        textCoords?.let {
            withDensity(composeTestRule.density) {
                val alignmentLines = it.providedAlignmentLines

                Truth.assertThat(alignmentLines.get(FirstBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(alignmentLines.get(LastBaseline)).isNotEqualTo(IntPx.Zero)
                Truth.assertThat(alignmentLines.get(FirstBaseline))
                    .isNotEqualTo(alignmentLines.get(LastBaseline))
                Truth.assertThat(it.position.y.round() + alignmentLines.getValue(FirstBaseline))
                    .isEqualTo(30.dp.toIntPx())
            }
        }
    }

    @Test
    @Ignore // b/146432509
    fun snackbar_longTextAndButton_alignment() {
        var snackCoords: LayoutCoordinates? = null
        var textCoords: LayoutCoordinates? = null
        var buttonCoords: LayoutCoordinates? = null
        val sizes = composeTestRule.setMaterialContentAndCollectSizes(
            parentConstraints = DpConstraints(maxWidth = 300.dp)
        ) {
            OnChildPositioned(onPositioned = { snackCoords = it }) {
                Snackbar(
                    text = {
                        OnChildPositioned(onPositioned = { textCoords = it }) {
                            Text(longText, maxLines = 2)
                        }
                    },
                    action = {
                        OnChildPositioned(onPositioned = { buttonCoords = it }) {
                            Button(text = "Undo", style = TextButtonStyle(), onClick = {})
                        }
                    }
{'fraction_non_alphanumeric': 0.08152077807250221, 'fraction_numerical': 0.006601827291482464, 'mean_word_length': 1.8634599156118143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7322792', 'n_tokens_mistral': 4590, 'n_tokens_neox': 4355, 'n_words': 805}