From 25a190535f35c4ccbd897879b142c5508c5bcc49 Mon Sep 17 00:00:00 2001 From: Bastien Wermeille <5320541+Ph0tonic@users.noreply.github.com> Date: Fri, 5 Jun 2020 20:16:08 +0200 Subject: [PATCH 1/5] Fix #136 --- .../data_manager/HeatmapDataManagerTest.kt | 18 +++++++++---- .../ch/epfl/sdp/utils/IntersectionUtils.kt | 0 .../data_manager/HeatmapDataManager.kt | 3 ++- .../java/ch/epfl/sdp/utils/IdentifierUtils.kt | 27 +++++++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) rename app/src/{main => androidTest}/java/ch/epfl/sdp/utils/IntersectionUtils.kt (100%) create mode 100644 app/src/main/java/ch/epfl/sdp/utils/IdentifierUtils.kt diff --git a/app/src/androidTest/java/ch/epfl/sdp/database/data_manager/HeatmapDataManagerTest.kt b/app/src/androidTest/java/ch/epfl/sdp/database/data_manager/HeatmapDataManagerTest.kt index 5de1e6088..eb335bbad 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/database/data_manager/HeatmapDataManagerTest.kt +++ b/app/src/androidTest/java/ch/epfl/sdp/database/data_manager/HeatmapDataManagerTest.kt @@ -8,6 +8,8 @@ import ch.epfl.sdp.database.data.HeatmapPointData import ch.epfl.sdp.database.providers.HeatmapRepositoryProvider import ch.epfl.sdp.database.repository.IHeatmapRepository import ch.epfl.sdp.utils.Auth +import ch.epfl.sdp.utils.IdentifierUtils +import com.google.firebase.iid.FirebaseInstanceId import com.mapbox.mapboxsdk.geometry.LatLng import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat @@ -29,12 +31,14 @@ class HeatmapDataManagerTest { @Test fun addMeasureToHeatmapForNewHeatmapCallsUpdateHeatmap() { + val deviceId = IdentifierUtils.id() + Auth.accountId.value = DUMMY_HEATMAP_ID + val expectedHeatmapId = Auth.accountId.value + "__" + deviceId val expectedHeatMapData = HeatmapData(mutableListOf( HeatmapPointData(DUMMY_LOCATION, DUMMY_INTENSITY) - ), DUMMY_HEATMAP_ID) + ), expectedHeatmapId) val expectedGroupId = DUMMY_GROUP_ID - Auth.accountId.value = DUMMY_HEATMAP_ID val repo = Mockito.mock(IHeatmapRepository::class.java) Mockito.`when`(repo.getGroupHeatmaps(expectedGroupId)).thenReturn(MutableLiveData(mutableMapOf())) @@ -48,11 +52,15 @@ class HeatmapDataManagerTest { @Test fun addMeasureToHeatmapForExistingHeatmapCallsUpdateHeatmap() { + val deviceId = IdentifierUtils.id() + Auth.accountId.value = DUMMY_HEATMAP_ID + val expectedHeatmapId = Auth.accountId.value + "__" + deviceId + val expectedGroupId = DUMMY_GROUP_ID + val expectedHeatMapData = HeatmapData(mutableListOf( HeatmapPointData(DUMMY_LOCATION, DUMMY_INTENSITY) - ), DUMMY_HEATMAP_ID) - val expectedGroupId = DUMMY_GROUP_ID - val previousHeatMapData = HeatmapData(mutableListOf(), DUMMY_HEATMAP_ID) + ), expectedHeatmapId) + val previousHeatMapData = HeatmapData(mutableListOf(), expectedHeatmapId) Auth.accountId.value = DUMMY_HEATMAP_ID diff --git a/app/src/main/java/ch/epfl/sdp/utils/IntersectionUtils.kt b/app/src/androidTest/java/ch/epfl/sdp/utils/IntersectionUtils.kt similarity index 100% rename from app/src/main/java/ch/epfl/sdp/utils/IntersectionUtils.kt rename to app/src/androidTest/java/ch/epfl/sdp/utils/IntersectionUtils.kt diff --git a/app/src/main/java/ch/epfl/sdp/database/data_manager/HeatmapDataManager.kt b/app/src/main/java/ch/epfl/sdp/database/data_manager/HeatmapDataManager.kt index 64f558d75..9718926d9 100644 --- a/app/src/main/java/ch/epfl/sdp/database/data_manager/HeatmapDataManager.kt +++ b/app/src/main/java/ch/epfl/sdp/database/data_manager/HeatmapDataManager.kt @@ -6,6 +6,7 @@ import ch.epfl.sdp.database.data.HeatmapData import ch.epfl.sdp.database.data.HeatmapPointData import ch.epfl.sdp.database.providers.HeatmapRepositoryProvider import ch.epfl.sdp.utils.Auth +import ch.epfl.sdp.utils.IdentifierUtils import com.mapbox.mapboxsdk.geometry.LatLng class HeatmapDataManager { @@ -13,7 +14,7 @@ class HeatmapDataManager { private val heatmapRepository = HeatmapRepositoryProvider.provide() fun addMeasureToHeatmap(groupId: String, location: LatLng, intensity: Double) { - val heatmapId = Auth.accountId.value + val heatmapId = Auth.accountId.value + "__" + IdentifierUtils.id() val heatmaps = heatmapRepository.getGroupHeatmaps(groupId).value val heatmapData = if (heatmaps != null && heatmaps.containsKey(heatmapId)) { diff --git a/app/src/main/java/ch/epfl/sdp/utils/IdentifierUtils.kt b/app/src/main/java/ch/epfl/sdp/utils/IdentifierUtils.kt new file mode 100644 index 000000000..fce6b7bb7 --- /dev/null +++ b/app/src/main/java/ch/epfl/sdp/utils/IdentifierUtils.kt @@ -0,0 +1,27 @@ +package ch.epfl.sdp.utils + +import android.content.Context.MODE_PRIVATE +import android.content.SharedPreferences +import ch.epfl.sdp.MainApplication +import java.util.* + +object IdentifierUtils { + private var uniqueID: String? = null + private const val PREF_UNIQUE_ID = "PREF_UNIQUE_ID" + + @Synchronized + fun id(): String { + if (uniqueID == null) { + val sharedPrefs: SharedPreferences = MainApplication.applicationContext().getSharedPreferences( + PREF_UNIQUE_ID, MODE_PRIVATE) + uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null) + if (uniqueID == null) { + uniqueID = UUID.randomUUID().toString() + val editor: SharedPreferences.Editor = sharedPrefs.edit() + editor.putString(PREF_UNIQUE_ID, uniqueID) + editor.apply() + } + } + return uniqueID!! + } +} \ No newline at end of file From c23d7267547e3a79133a3eb02e9f8607091842d5 Mon Sep 17 00:00:00 2001 From: Bastien Wermeille <5320541+Ph0tonic@users.noreply.github.com> Date: Fri, 5 Jun 2020 20:41:28 +0200 Subject: [PATCH 2/5] Fix tests --- .../java/ch/epfl/sdp/ui/maps/MapActivityTest.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt b/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt index 8fae50c54..046387df0 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt +++ b/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt @@ -45,6 +45,7 @@ import ch.epfl.sdp.drone.DroneInstanceMock import ch.epfl.sdp.ui.maps.offline.OfflineManagerActivity import ch.epfl.sdp.utils.Auth import ch.epfl.sdp.utils.CentralLocationManager +import ch.epfl.sdp.utils.IdentifierUtils import com.mapbox.mapboxsdk.geometry.LatLng import org.hamcrest.CoreMatchers import org.hamcrest.Matchers.* @@ -183,6 +184,7 @@ class MapActivityTest { MainDataManager.groupId.value = DUMMY_GROUP_ID MainDataManager.role.value = Role.OPERATOR } + val expectedHeatmapId = DUMMY_GROUP_ID + "__" + IdentifierUtils.id() mActivityRule.launchActivity(Intent()) mUiDevice.wait(Until.hasObject(By.desc(applicationContext().getString(R.string.map_ready))), MAP_LOADING_TIMEOUT) @@ -196,10 +198,10 @@ class MapActivityTest { val heatmaps = mActivityRule.activity.heatmapManager.getGroupHeatmaps(DUMMY_GROUP_ID) assertThat(heatmaps.value, `is`(notNullValue())) - assertThat(heatmaps.value!![FAKE_ACCOUNT_ID], `is`(notNullValue())) - assertThat(heatmaps.value!![FAKE_ACCOUNT_ID]!!.value, `is`(notNullValue())) - assertThat(heatmaps.value!![FAKE_ACCOUNT_ID]!!.value!!.dataPoints, `is`(notNullValue())) - assertThat(heatmaps.value!![FAKE_ACCOUNT_ID]!!.value!!.dataPoints.size, equalTo(1)) + assertThat(heatmaps.value!![expectedHeatmapId], `is`(notNullValue())) + assertThat(heatmaps.value!![expectedHeatmapId]!!.value, `is`(notNullValue())) + assertThat(heatmaps.value!![expectedHeatmapId]!!.value!!.dataPoints, `is`(notNullValue())) + assertThat(heatmaps.value!![expectedHeatmapId]!!.value!!.dataPoints.size, equalTo(1)) } @Test From 09367c7db63c99d6cbf66e7826222ec934872a9f Mon Sep 17 00:00:00 2001 From: Bastien Wermeille <5320541+Ph0tonic@users.noreply.github.com> Date: Fri, 5 Jun 2020 20:47:05 +0200 Subject: [PATCH 3/5] Fix issues with heatmapTests --- app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt b/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt index 046387df0..7a87144c9 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt +++ b/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt @@ -184,7 +184,7 @@ class MapActivityTest { MainDataManager.groupId.value = DUMMY_GROUP_ID MainDataManager.role.value = Role.OPERATOR } - val expectedHeatmapId = DUMMY_GROUP_ID + "__" + IdentifierUtils.id() + val expectedHeatmapId = Auth.accountId.value + "__" + IdentifierUtils.id() mActivityRule.launchActivity(Intent()) mUiDevice.wait(Until.hasObject(By.desc(applicationContext().getString(R.string.map_ready))), MAP_LOADING_TIMEOUT) From 5fe0022c66b5893dd4e7f0c096a7d1e5cef89805 Mon Sep 17 00:00:00 2001 From: Bastien Wermeille <5320541+Ph0tonic@users.noreply.github.com> Date: Fri, 5 Jun 2020 20:49:03 +0200 Subject: [PATCH 4/5] Clean imports --- app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt b/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt index 7a87144c9..f3bae2955 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt +++ b/app/src/androidTest/java/ch/epfl/sdp/ui/maps/MapActivityTest.kt @@ -38,10 +38,10 @@ import ch.epfl.sdp.database.repository.MarkerRepository import ch.epfl.sdp.database.repository.SearchGroupRepository import ch.epfl.sdp.database.repository.UserRepository import ch.epfl.sdp.drone.Drone +import ch.epfl.sdp.drone.DroneInstanceMock import ch.epfl.sdp.mission.SimpleQuadStrategy import ch.epfl.sdp.mission.SpiralStrategy import ch.epfl.sdp.searcharea.QuadrilateralArea -import ch.epfl.sdp.drone.DroneInstanceMock import ch.epfl.sdp.ui.maps.offline.OfflineManagerActivity import ch.epfl.sdp.utils.Auth import ch.epfl.sdp.utils.CentralLocationManager From abca7526eeac1b067be68eb3a6eea8dc9bba583a Mon Sep 17 00:00:00 2001 From: Bastien Wermeille <5320541+Ph0tonic@users.noreply.github.com> Date: Fri, 5 Jun 2020 21:06:59 +0200 Subject: [PATCH 5/5] Move intersectionUtils tests --- .../java/ch/epfl/sdp/searcharea/QuadrilateralBuilderTest.kt | 2 -- .../java/ch/epfl/sdp/utils}/IntersectionUtilsTest.kt | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) rename app/src/{test/java/ch/epfl/sdp => androidTest/java/ch/epfl/sdp/utils}/IntersectionUtilsTest.kt (98%) diff --git a/app/src/androidTest/java/ch/epfl/sdp/searcharea/QuadrilateralBuilderTest.kt b/app/src/androidTest/java/ch/epfl/sdp/searcharea/QuadrilateralBuilderTest.kt index c17853bc1..4dbfc7016 100644 --- a/app/src/androidTest/java/ch/epfl/sdp/searcharea/QuadrilateralBuilderTest.kt +++ b/app/src/androidTest/java/ch/epfl/sdp/searcharea/QuadrilateralBuilderTest.kt @@ -1,7 +1,6 @@ package ch.epfl.sdp.searcharea import androidx.arch.core.executor.testing.InstantTaskExecutorRule -import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread import ch.epfl.sdp.utils.IntersectionUtils import com.mapbox.mapboxsdk.geometry.LatLng import org.hamcrest.CoreMatchers @@ -9,7 +8,6 @@ import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Rule import org.junit.Test -import timber.log.Timber import kotlin.random.Random class QuadrilateralBuilderTest { diff --git a/app/src/test/java/ch/epfl/sdp/IntersectionUtilsTest.kt b/app/src/androidTest/java/ch/epfl/sdp/utils/IntersectionUtilsTest.kt similarity index 98% rename from app/src/test/java/ch/epfl/sdp/IntersectionUtilsTest.kt rename to app/src/androidTest/java/ch/epfl/sdp/utils/IntersectionUtilsTest.kt index 53af420e6..feb6ad604 100644 --- a/app/src/test/java/ch/epfl/sdp/IntersectionUtilsTest.kt +++ b/app/src/androidTest/java/ch/epfl/sdp/utils/IntersectionUtilsTest.kt @@ -1,4 +1,4 @@ -package ch.epfl.sdp +package ch.epfl.sdp.utils import ch.epfl.sdp.utils.IntersectionUtils import com.mapbox.mapboxsdk.geometry.LatLng