Commit e9e06761 authored by Michael Comella's avatar Michael Comella Committed by Michael Comella
Browse files

For #17920: add ManufacturerCodes.

parent 2e45483e
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -19,12 +19,10 @@ import androidx.fragment.app.FragmentManager
import mozilla.components.support.ktx.android.os.resetAfter
import org.mozilla.fenix.Config
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.utils.ManufacturerCodes
import org.mozilla.fenix.utils.Mockable
import java.util.concurrent.atomic.AtomicLong

private const val MANUFACTURE_HUAWEI: String = "HUAWEI"
private const val MANUFACTURE_ONE_PLUS: String = "OnePlus"

private val logger = Performance.logger
private val mainLooper = Looper.getMainLooper()

@@ -145,5 +143,5 @@ class StrictModeManager(
     * To add a new manufacturer to the list, log "Build.MANUFACTURER" from the device to get the
     * exact name of the manufacturer.
     */
    private val strictModeExceptionList = setOf(MANUFACTURE_HUAWEI, MANUFACTURE_ONE_PLUS)
    private val strictModeExceptionList = setOf(ManufacturerCodes.HUAWEI, ManufacturerCodes.ONE_PLUS)
}
+22 −0
Original line number Diff line number Diff line
/* 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.os.Build

/**
 * A listing of codes returned by [android.os.Build.MANUFACTURER] for different manufacturers.
 * While we try to get the casing accurate, it may be good to use .equals(str, ignoreCase = true)
 * to do the comparison.
 */
object ManufacturerCodes {
    const val HUAWEI: String = "HUAWEI"
    const val LG = "LGE"
    const val ONE_PLUS: String = "OnePlus"
    const val SAMSUNG = "samsung"

    val isLG get() = Build.MANUFACTURER.equals(LG, ignoreCase = true)
    val isSamsung get() = Build.MANUFACTURER.equals(SAMSUNG, ignoreCase = true)
}
+69 −0
Original line number Diff line number Diff line
/* 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.os.Build
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.lang.reflect.Modifier

class ManufacturerCodesTest {

    private val manufacturerField = Build::class.java.getDeclaredField("MANUFACTURER")
    private var manufacturer: String?
        get() = Build.MANUFACTURER
        set(value) { manufacturerField.set(null, value) }

    @Before
    fun setUp() {
        enableManufacturerModifications()
        manufacturer = null // reset to default state before test.
        assertEquals(null, Build.MANUFACTURER) // sanity check.
    }

    private fun enableManufacturerModifications() {
        // Mocking, which might be simpler, doesn't seem to work so we use reflection.
        // Methodology via https://stackoverflow.com/a/3301720/2219998
        val modifiers = manufacturerField.javaClass.getDeclaredField("modifiers")
        modifiers.isAccessible = true
        modifiers.setInt(manufacturerField, manufacturerField.modifiers and Modifier.FINAL.inv())
    }

    @After
    fun tearDown() {
        // After this method, Build.MANUFACTURER appears to return to
        // static final so we don't need to undo that.
        manufacturer = null
        assertEquals(null, Build.MANUFACTURER) // sanity check.
    }

    @Test // To reduce boilerplate, we avoid best practice and put several tests in one.
    fun testIsLG() {
        manufacturer = "LGE" // expected value for lg devices
        assertTrue(ManufacturerCodes.isLG)

        manufacturer = "lge" // unexpected value but is still an lg device
        assertTrue(ManufacturerCodes.isLG)

        manufacturer = "samsung"
        assertFalse(ManufacturerCodes.isLG)
    }

    @Test // To reduce boilerplate, we avoid best practice and put several tests in one.
    fun testIsSamsung() {
        manufacturer = "samsung" // expected value for samsung devices
        assertTrue(ManufacturerCodes.isSamsung)

        manufacturer = "SaMsUnG" // unexpected value but is still a samsung device
        assertTrue(ManufacturerCodes.isSamsung)

        manufacturer = "LGE"
        assertFalse(ManufacturerCodes.isSamsung)
    }
}