[fenix] Bug 1813085 - If a message notification is being displayed, do not create another notification with the same message.
parent
d5b1251d82
commit
c275034e78
@ -0,0 +1,63 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.provider.Settings
|
||||
import androidx.annotation.RequiresApi
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Provides access to system properties.
|
||||
*/
|
||||
interface BootUtils {
|
||||
|
||||
/**
|
||||
* Gets the device boot count.
|
||||
*
|
||||
* **Only for Android versions N(24) and above.**
|
||||
*/
|
||||
@RequiresApi(Build.VERSION_CODES.N)
|
||||
fun getDeviceBootCount(context: Context): String
|
||||
|
||||
val deviceBootId: String?
|
||||
|
||||
val bootIdFileExists: Boolean
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* @return either the boot count or a boot id depending on the device Android version.
|
||||
*/
|
||||
fun getBootIdentifier(context: Context, bootUtils: BootUtils = BootUtilsImpl()): String {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
bootUtils.getDeviceBootCount(context)
|
||||
} else {
|
||||
return if (bootUtils.bootIdFileExists) {
|
||||
bootUtils.deviceBootId ?: NO_BOOT_IDENTIFIER
|
||||
} else {
|
||||
NO_BOOT_IDENTIFIER
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of [BootUtils].
|
||||
*/
|
||||
class BootUtilsImpl : BootUtils {
|
||||
private val bootIdFile by lazy { File("/proc/sys/kernel/random/boot_id") }
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N)
|
||||
override fun getDeviceBootCount(context: Context): String =
|
||||
Settings.Global.getString(context.contentResolver, Settings.Global.BOOT_COUNT)
|
||||
|
||||
override val deviceBootId: String? by lazy { bootIdFile.readLines().firstOrNull()?.trim() }
|
||||
|
||||
override val bootIdFileExists: Boolean by lazy { bootIdFile.exists() }
|
||||
}
|
||||
|
||||
private const val NO_BOOT_IDENTIFIER = "no boot identifier available"
|
@ -0,0 +1,79 @@
|
||||
package org.mozilla.fenix.utils
|
||||
|
||||
import android.os.Build
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.mozilla.fenix.utils.BootUtils.Companion.getBootIdentifier
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
private const val NO_BOOT_IDENTIFIER = "no boot identifier available"
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class BootUtilsTest {
|
||||
|
||||
private lateinit var bootUtils: BootUtils
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
bootUtils = mockk(relaxed = true)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [Build.VERSION_CODES.M])
|
||||
fun `WHEN no boot id file & Android version is less than N(24) THEN getBootIdentifier returns NO_BOOT_IDENTIFIER`() {
|
||||
every { bootUtils.bootIdFileExists }.returns(false)
|
||||
|
||||
assertEquals(NO_BOOT_IDENTIFIER, getBootIdentifier(testContext, bootUtils))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [Build.VERSION_CODES.M])
|
||||
fun `WHEN boot id file returns null & Android version is less than N(24) THEN getBootIdentifier returns NO_BOOT_IDENTIFIER`() {
|
||||
every { bootUtils.bootIdFileExists }.returns(true)
|
||||
every { bootUtils.deviceBootId }.returns(null)
|
||||
|
||||
assertEquals(NO_BOOT_IDENTIFIER, getBootIdentifier(testContext, bootUtils))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [Build.VERSION_CODES.M])
|
||||
fun `WHEN boot id file has text & Android version is less than N(24) THEN getBootIdentifier returns the boot id`() {
|
||||
every { bootUtils.bootIdFileExists }.returns(true)
|
||||
val bootId = "test"
|
||||
every { bootUtils.deviceBootId }.returns(bootId)
|
||||
|
||||
assertEquals(bootId, getBootIdentifier(testContext, bootUtils))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [Build.VERSION_CODES.M])
|
||||
fun `WHEN boot id file has text with whitespace & Android version is less than N(24) THEN getBootIdentifier returns the trimmed boot id`() {
|
||||
every { bootUtils.bootIdFileExists }.returns(true)
|
||||
val bootId = " test "
|
||||
every { bootUtils.deviceBootId }.returns(bootId)
|
||||
|
||||
assertEquals(bootId, getBootIdentifier(testContext, bootUtils))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [Build.VERSION_CODES.N])
|
||||
fun `WHEN Android version is N(24) THEN getBootIdentifier returns the boot count`() {
|
||||
val bootCount = "9"
|
||||
every { bootUtils.getDeviceBootCount(any()) }.returns(bootCount)
|
||||
assertEquals(bootCount, getBootIdentifier(testContext, bootUtils))
|
||||
}
|
||||
|
||||
@Test
|
||||
@Config(sdk = [Build.VERSION_CODES.O])
|
||||
fun `WHEN Android version is more than N(24) THEN getBootIdentifier returns the boot count`() {
|
||||
val bootCount = "9"
|
||||
every { bootUtils.getDeviceBootCount(any()) }.returns(bootCount)
|
||||
assertEquals(bootCount, getBootIdentifier(testContext, bootUtils))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue