<commit_before>/*
 * Copyright 2022 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
 *
 *     https://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.compose.samples.crane.details

import androidx.compose.foundation.layout.Column
import androidx.compose.samples.crane.data.DestinationsRepository
import androidx.compose.samples.crane.data.ExploreModel
import androidx.compose.samples.crane.data.MADRID
import androidx.compose.samples.crane.home.MainActivity
import androidx.compose.samples.crane.ui.CraneTheme
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.compose.CameraPositionState
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.CountDownLatch
import javax.inject.Inject
import kotlin.math.pow

@HiltAndroidTest
class CityMapViewTests {
    @Inject
    lateinit var destinationsRepository: DestinationsRepository

    private lateinit var cityDetails: ExploreModel

    private val city = MADRID
    private val testExploreModel = ExploreModel(city, "description", "imageUrl")

    @get:Rule(order = 0)
    var hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    // Using MainActivity so we can initialise Hilt but not have to populate savedstate for
    // unused viewmodels.
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    private lateinit var mapState: CameraPositionState

    @Before
    fun setUp() {
        hiltRule.inject()

        cityDetails = destinationsRepository.getDestination(MADRID.name)!!
        mapState = CameraPositionState()

        val countDownLatch = CountDownLatch(1)

        composeTestRule.setContent {
            CraneTheme {
                Column {
                    CityMapView(
                        latitude = testExploreModel.city.latitude,
                        longitude = testExploreModel.city.longitude,
                        cameraPositionState = mapState,
                        onMapLoaded = {
                            countDownLatch.countDown()
                        }
                    )
                }
            }
        }

        // countDownLatch.await()
    }

    @Test
    fun mapView_cameraPositioned() {
        val expected = LatLng(
            testExploreModel.city.latitude.toDouble(),
            testExploreModel.city.longitude.toDouble()
        )

        assertEquals(expected.latitude, mapState.position.target.latitude.round(6))
        assertEquals(expected.longitude, mapState.position.target.longitude.round(6))
    }

    @Test
    fun mapView_zoomIn() {
        val zoomBefore = mapState.position.zoom
        composeTestRule.onNodeWithText("+")
            .assertIsDisplayed()
            .performClick()

        // Wait for the animation to happen
        composeTestRule.waitForIdle()

        assertTrue(zoomBefore < mapState.position.zoom)
    }

    @Test
    fun mapView_zoomOut() {
        val zoomBefore = mapState.position.zoom
        composeTestRule.onNodeWithText("-")
            .assertIsDisplayed()
            .performClick()

        // Wait for the animation to happen
        composeTestRule.waitForIdle()

        assertTrue(zoomBefore > mapState.position.zoom)
    }
}

private fun Double.round(decimals: Int = 2): Double =
    kotlin.math.round(this * 10f.pow(decimals)) / 10f.pow(decimals)
<commit_msg>Add a time limit to latch<commit_after>/*
 * Copyright 2022 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
 *
 *     https://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.compose.samples.crane.details

import androidx.compose.foundation.layout.Column
import androidx.compose.samples.crane.data.DestinationsRepository
import androidx.compose.samples.crane.data.ExploreModel
import androidx.compose.samples.crane.data.MADRID
import androidx.compose.samples.crane.home.MainActivity
import androidx.compose.samples.crane.ui.CraneTheme
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.compose.CameraPositionState
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.math.pow

@HiltAndroidTest
class CityMapViewTests {
    @Inject
    lateinit var destinationsRepository: DestinationsRepository

    private lateinit var cityDetails: ExploreModel

    private val city = MADRID
    private val testExploreModel = ExploreModel(city, "description", "imageUrl")

    @get:Rule(order = 0)
    var hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    // Using MainActivity so we can initialise Hilt but not have to populate savedstate for
    // unused viewmodels.
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    private lateinit var mapState: CameraPositionState

    @Before
    fun setUp() {
        hiltRule.inject()

        cityDetails = destinationsRepository.getDestination(MADRID.name)!!
        mapState = CameraPositionState()

        val countDownLatch = CountDownLatch(1)

        composeTestRule.setContent {
            CraneTheme {
                Column {
                    CityMapView(
                        latitude = testExploreModel.city.latitude,
                        longitude = testExploreModel.city.longitude,
                        cameraPositionState = mapState,
                        onMapLoaded = {
                            countDownLatch.countDown()
                        }
                    )
                }
            }
        }

        countDownLatch.await(1, TimeUnit.MINUTES)
    }

    @Test
    fun mapView_cameraPositioned() {
        val expected = LatLng(
            testExploreModel.city.latitude.toDouble(),
            testExploreModel.city.longitude.toDouble()
        )

        assertEquals(expected.latitude, mapState.position.target.latitude.round(6))
        assertEquals(expected.longitude, mapState.position.target.longitude.round(6))
    }

    @Test
    fun mapView_zoomIn() {
        val zoomBefore = mapState.position.zoom
        composeTestRule.onNodeWithText("+")
            .assertIsDisplayed()
            .performClick()

        // Wait for the animation to happen
        composeTestRule.waitForIdle()

        assertTrue(zoomBefore < mapState.position.zoom)
    }

    @Test
    fun mapView_zoomOut() {
        val zoomBefore = mapState.position.zoom
        composeTestRule.onNodeWithText("-")
            .assertIsDisplayed()
            .performClick()

        // Wait for the animation to happen
        composeTestRule.waitForIdle()

        assertTrue(zoomBefore > mapState.position.zoom)
    }
}

private fun Double.round(decimals: Int = 2): Double =
    kotlin.math.round(this * 10f.pow(decimals)) / 10f.pow(decimals)

{'fraction_non_alphanumeric': 0.08018260564204611, 'fraction_numerical': 0.004565141051152991, 'mean_word_length': 2.8176943699731902, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3961190', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2331, 'n_words': 542}