dmytroKarataiev / EarthquakeSurvival

Earthquake tracking application (RxJava, SQLite, Content Provider, Sync Adapter).
MIT License
13 stars 1 forks source link

Sweep: Write comprehensive unit tests for app/src/main/java/com/adkdevelopment/earthquakesurvival/utils/LocationUtils.java #66

Open wwzeng1 opened 2 hours ago

sweep-fundrise[bot] commented 2 hours ago

🚀 Here's the PR! #67

Sweeping

Search Results

Response

Let's create a new file named app/src/test/java/com/adkdevelopment/earthquakesurvival/utils/LocationUtilsTest.java to implement comprehensive unit tests for LocationUtils.java.

app/src/test/java/com/adkdevelopment/earthquakesurvival/utils/LocationUtilsTest.java

--- 
+++ 
@@ -1 +1,123 @@
+package com.adkdevelopment.earthquakesurvival.utils;

+import android.content.Context;
+import android.content.SharedPreferences;
+import android.location.Location;
+
+import com.google.android.gms.location.Geofence;
+import com.google.android.gms.location.GeofenceStatusCodes;
+import com.google.android.gms.maps.model.LatLng;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+@RunWith(MockitoJUnitRunner.class)
+public class LocationUtilsTest {
+
+    @Mock
+    private Context mockContext;
+
+    @Mock
+    private SharedPreferences mockSharedPreferences;
+
+    @Mock
+    private SharedPreferences.Editor mockEditor;
+
+    @Before
+    public void setUp() {
+        when(mockContext.getString(anyInt())).thenReturn("mock_string");
+        when(mockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mockSharedPreferences);
+        when(mockSharedPreferences.edit()).thenReturn(mockEditor);
+    }
+
+    @Test
+    public void testGetErrorString() {
+        assertEquals("mock_string", LocationUtils.getErrorString(mockContext, GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE));
+        assertEquals("mock_string", LocationUtils.getErrorString(mockContext, GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES));
+        assertEquals("mock_string", LocationUtils.getErrorString(mockContext, GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS));
+        assertEquals("mock_string", LocationUtils.getErrorString(mockContext, 9999));
+    }
+
+    @Test
+    public void testGetTransitionString() {
+        assertEquals("mock_string", LocationUtils.getTransitionString(mockContext, Geofence.GEOFENCE_TRANSITION_ENTER));
+        assertEquals("mock_string", LocationUtils.getTransitionString(mockContext, Geofence.GEOFENCE_TRANSITION_EXIT));
+        assertEquals("mock_string", LocationUtils.getTransitionString(mockContext, 9999));
+    }
+
+    @Test
+    public void testGetTransitionDetails() {
+        List<Geofence> triggeringGeofences = new ArrayList<>();
+        Geofence mockGeofence1 = mock(Geofence.class);
+        Geofence mockGeofence2 = mock(Geofence.class);
+        when(mockGeofence1.getRequestId()).thenReturn("geofence1");
+        when(mockGeofence2.getRequestId()).thenReturn("geofence2");
+        triggeringGeofences.add(mockGeofence1);
+        triggeringGeofences.add(mockGeofence2);
+
+        List<String> result = LocationUtils.getTransitionDetails(mockContext, Geofence.GEOFENCE_TRANSITION_ENTER, triggeringGeofences);
+
+        assertEquals(3, result.size());
+        assertEquals("mock_string", result.get(0));
+        assertEquals("geofence1", result.get(1));
+        assertEquals("geofence2", result.get(2));
+    }
+
+    @Test
+    public void testGetLocation() {
+        when(mockSharedPreferences.getLong(eq("mock_string"), anyLong())).thenReturn(Double.doubleToRawLongBits(1.0));
+        LatLng result = LocationUtils.getLocation(mockContext);
+        assertEquals(1.0, result.latitude, 0.0001);
+        assertEquals(1.0, result.longitude, 0.0001);
+    }
+
+    @Test
+    public void testSetLocation() {
+        Location mockLocation = mock(Location.class);
+        when(mockLocation.getLatitude()).thenReturn(1.0);
+        when(mockLocation.getLongitude()).thenReturn(2.0);
+
+        LocationUtils.setLocation(mockContext, mockLocation);
+
+        verify(mockEditor).putLong(eq("mock_string"), eq(Double.doubleToRawLongBits(1.0)));
+        verify(mockEditor).putLong(eq("mock_string"), eq(Double.doubleToRawLongBits(2.0)));
+        verify(mockEditor).apply();
+    }
+
+    @Test
+    public void testSetLocationWithNullLocation() {
+        LocationUtils.setLocation(mockContext, null);
+        verifyNoInteractions(mockEditor);
+    }
+
+    @Test
+    public void testGetDistance() {
+        LatLng point1 = new LatLng(0, 0);
+        LatLng point2 = new LatLng(1, 1);
+        double distance = LocationUtils.getDistance(point1, point2);
+        assertTrue(distance > 0);
+        assertEquals(97.69, distance, 0.01);
+
+        LatLng point3 = new LatLng(0, 0);
+        LatLng point4 = new LatLng(0, 0);
+        distance = LocationUtils.getDistance(point3, point4);
+        assertEquals(0.0, distance, 0.0001);
+    }
+
+    @Test
+    public void testGetDistanceWithZeroLatitude() {
+        LatLng point1 = new LatLng(0, 0);
+        LatLng point2 = new LatLng(0, 1);
+        double distance = LocationUtils.getDistance(point1, point2);
+        assertEquals(0.0, distance, 0.0001);
+    }
+}

This comprehensive test file covers all the public methods in LocationUtils.java:

  1. testGetErrorString: Tests the getErrorString method with different error codes, including all specific GeofenceStatusCodes and an unknown code.

  2. testGetTransitionString: Tests the getTransitionString method with different transition types, including both valid Geofence transitions and an unknown transition type.

  3. testGetTransitionDetails: Tests the getTransitionDetails method with mock geofences, verifying that it correctly processes the transition type and geofence IDs.

  4. testGetLocation: Tests the getLocation method with mocked SharedPreferences, ensuring it correctly retrieves and converts the stored latitude and longitude.

  5. testSetLocation: Tests the setLocation method with a mock Location object, verifying that it correctly stores the latitude and longitude in SharedPreferences.

  6. testSetLocationWithNullLocation: Tests the setLocation method with a null Location object, ensuring it doesn't interact with SharedPreferences in this case.

  7. testGetDistance: Tests the getDistance method with different LatLng pairs, including a known distance calculation and a zero distance case.

  8. testGetDistanceWithZeroLatitude: Tests the getDistance method with a zero latitude in the second point, which is a special case in the method's implementation.

These tests cover various scenarios and edge cases, ensuring the correct behavior of the LocationUtils methods. The use of Mockito for mocking Android-specific classes allows for isolated testing of the utility methods without depending on the actual Android framework.

Continue the conversation with Sweep here: http://localhost:3000/c/5a7192a6-a7a1-4502-b55e-d8c4ae602ed3.

:book: For more information on how to use Sweep, please read our documentation.

Tracking ID: 0fdc4bf397