Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 1885329 - Add logs to MockLocationUpdatesRule
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiAJ committed Mar 14, 2024
1 parent 8a49590 commit b4153b7
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.SystemClock
import android.util.Log
import androidx.test.core.app.ApplicationProvider
import org.junit.rules.ExternalResource
import org.mozilla.fenix.helpers.Constants.TAG
import org.mozilla.fenix.helpers.TestHelper.mDevice
import java.util.Date
import kotlin.random.Random
Expand All @@ -36,15 +37,17 @@ class MockLocationUpdatesRule : ExternalResource() {
}

override fun before() {
Log.i(TAG, "MockLocationUpdatesRule: Trying to enable the mock location setting on the device")
/* ADB command to enable the mock location setting on the device.
* Will not be turned back off due to limitations on knowing its initial state.
*/
Log.i(TAG, "MockLocationUpdatesRule: Trying to execute cmd \"appops set ${appContext.packageName} android:mock_location allow\"")
mDevice.executeShellCommand(
"appops set " +
appContext.packageName +
" android:mock_location allow",
)

Log.i(TAG, "MockLocationUpdatesRule: Executed cmd \"appops set ${appContext.packageName} android:mock_location allow\"")
// To mock locations we need a location provider, so we generate and set it here.
try {
locationManager.addTestProvider(
Expand All @@ -61,15 +64,18 @@ class MockLocationUpdatesRule : ExternalResource() {
)
} catch (ex: Exception) {
// unstable
Log.w("MockLocationUpdatesRule", "addTestProvider failed")
Log.i(TAG, "MockLocationUpdatesRule: Exception $ex caught, addTestProvider failed")
}
locationManager.setTestProviderEnabled(mockProviderName, true)
Log.i(TAG, "MockLocationUpdatesRule: Enabled the mock location setting on the device")
}

// Cleaning up the location provider after the test.
override fun after() {
Log.i(TAG, "MockLocationUpdatesRule: Trying to clean up the location provider")
locationManager.setTestProviderEnabled(mockProviderName, false)
locationManager.removeTestProvider(mockProviderName)
Log.i(TAG, "MockLocationUpdatesRule: Cleaned up the location provider")
}

/**
Expand All @@ -78,6 +84,7 @@ class MockLocationUpdatesRule : ExternalResource() {
* @param modifyLocation optional callback for modifying the constructed location before setting it.
*/
fun setMockLocation(modifyLocation: (Location.() -> Unit)? = null) {
Log.i(TAG, "setMockLocation: Trying to set the mock location")
check(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
"MockLocationUpdatesRule is supported only on Android devices " +
"running version >= Build.VERSION_CODES.M"
Expand Down Expand Up @@ -107,5 +114,6 @@ class MockLocationUpdatesRule : ExternalResource() {
}

locationManager.setTestProviderLocation(mockProviderName, location)
Log.i(TAG, "setMockLocation: The mock location was successfully set")
}
}

0 comments on commit b4153b7

Please sign in to comment.