[fenix] 16373 Count the # of inflations done on startup (https://github.com/mozilla-mobile/fenix/pull/16778)
* For https://github.com/mozilla-mobile/fenix/issues/16373: Added performance Inflater to counter # of inflations This class is quite straight forward. The only thing that I have to point out is the onCreateView method. It usually calls its super if you don't override it. The problem with that is that the super.onCreateView actually uses android.view. as a prefix for the XML element it tries to inflate. So if we have an element that isn't part of that package, it'll crash. As I said in the code, a good example is ImageButton. Calling android.view.ImageButton will make the app crash. The method is implemented the same way that PhoneLayoutInflater does (Another example is the AsyncLayoutInflater) * For https://github.com/mozilla-mobile/fenix/issues/16373: Added test for PerformanceInflater This test got quite awkward / complicated fast. I wanted to test the to make sure we don't break *any* of our layouts and to do so, I decided to just retrieve all our XML in our /res/layout folder. However, this gets quite a bit outside of a unit test scope. The point was to get every layouts and get their LayoutID through the resources using the testContext we have. It gets even weirder, since some of the XML tags have special implementation in android. One of them is the <fragment> tag. That tag actually is inflated by the OS using the Factory2 that the Activity.java implements. In order to get around the fragment issue, we just return a basic FrameLayout since the system LayoutInflater doesn't deal won't ever get a <fragment> tag to inflate. Another issue was the <merge> tag. In order to inflate those, you need 1) a root view and 2) attach your view to it. In order to be able to test those layouts file, I had to create an empty FrameLayout and use it as the root view for testing. Again, I know this is beyond the spirit of a unit test but if we use this inflater, I think it should make sure that no layouts are broken by it. * For https://github.com/mozilla-mobile/fenix/issues/16373: Overrode getSystemService to return PerformanceInflater This allows PerformanceInflater to be called in every inflation to keep track of the number of inflations we do. * For https://github.com/mozilla-mobile/fenix/issues/16373: Added UI test for # of inflations * For https://github.com/mozilla-mobile/fenix/issues/16373: Lint fix * For #167373: Changed the LayoutInflater cloneInContext to take this instead of inflater The inflater parameter is set on the first call from the OS from the Window object. However, the activity itself sets multiple factories on the inflater during its creation (usually through AppCompatDelegateImpl.java). This means that, once we initially set the inflater with a null check, we pass an inflater that has no factory initially. However, since we keep a reference to it, when cloneInContext was called, it cloned the inflater with the original inflater which didn't have any factories set up. This meant that the app would crash on either browserFragment creation or any thing that required appCompat (such as ImageView and ImageButton). Now, passing itself with a cloneInContext means we keep all the factories initially set by the activity or the fragment. * For https://github.com/mozilla-mobile/fenix/issues/16373: Fixed code issues for PR. No behavior change * For https://github.com/mozilla-mobile/fenix/issues/16373: fixed some code nitspull/600/head
parent
a1bad0fdb9
commit
c441f2af27
@ -0,0 +1,76 @@
|
||||
/* 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.perf
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import org.mozilla.fenix.ext.getAndIncrementNoOverflow
|
||||
import java.lang.reflect.Modifier.PRIVATE
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
private val classPrefixList = arrayOf(
|
||||
"android.widget.",
|
||||
"android.webkit.",
|
||||
"android.app."
|
||||
)
|
||||
/**
|
||||
* Counts the number of inflations fenix does. This class behaves only as an inflation counter since
|
||||
* it takes the `inflater` that is given by the base system. This is done in order not to change
|
||||
* the behavior of the app since all we want to do is count the inflations done.
|
||||
*
|
||||
*/
|
||||
open class PerformanceInflater(
|
||||
inflater: LayoutInflater,
|
||||
context: Context
|
||||
) : LayoutInflater(
|
||||
inflater,
|
||||
context
|
||||
) {
|
||||
|
||||
override fun cloneInContext(newContext: Context?): LayoutInflater {
|
||||
return PerformanceInflater(this, newContext!!)
|
||||
}
|
||||
|
||||
override fun inflate(resource: Int, root: ViewGroup?, attachToRoot: Boolean): View {
|
||||
InflationCounter.inflationCount.getAndIncrementNoOverflow()
|
||||
return super.inflate(resource, root, attachToRoot)
|
||||
}
|
||||
|
||||
/**
|
||||
* This code was taken from the PhoneLayoutInflater.java located in the android source code
|
||||
* (Similarly, AsyncLayoutInflater implements it the exact same way too which can be found in the
|
||||
* Android Framework). This piece of code was taken from the other inflaters implemented by Android
|
||||
* since we do not want to change the inflater behavior except to count the number of inflations
|
||||
* that our app is doing for performance purposes. Looking at the `super.OnCreateView(name, attrs)`,
|
||||
* it hardcodes the prefix as "android.view." this means that a xml element such as
|
||||
* ImageButton will crash the app using android.view.ImageButton. This method only works with
|
||||
* XML tag that contains no prefix. This means that views such as androidx.recyclerview... will not
|
||||
* work with this method.
|
||||
*/
|
||||
@Suppress("EmptyCatchBlock")
|
||||
@Throws(ClassNotFoundException::class)
|
||||
override fun onCreateView(name: String?, attrs: AttributeSet?): View? {
|
||||
for (prefix in classPrefixList) {
|
||||
try {
|
||||
val view = createView(name, prefix, attrs)
|
||||
if (view != null) {
|
||||
return view
|
||||
}
|
||||
} catch (e: ClassNotFoundException) {
|
||||
// We want the super class to inflate if ever the view can't be inflated here
|
||||
}
|
||||
}
|
||||
return super.onCreateView(name, attrs)
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = PRIVATE)
|
||||
object InflationCounter {
|
||||
val inflationCount = AtomicInteger(0)
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/* 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.perf
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.FrameLayout
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import java.io.File
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class PerformanceInflaterTest {
|
||||
|
||||
private lateinit var perfInflater: MockInflater
|
||||
|
||||
private val layoutsNotToTest = setOf(
|
||||
"fragment_browser",
|
||||
"fragment_add_on_internal_settings"
|
||||
)
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
InflationCounter.inflationCount.set(0)
|
||||
|
||||
perfInflater = MockInflater(LayoutInflater.from(testContext), testContext)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN we inflate a view,THEN the inflation counter should increase`() {
|
||||
assertEquals(0, InflationCounter.inflationCount.get())
|
||||
perfInflater.inflate(R.layout.fragment_home, null, false)
|
||||
assertEquals(1, InflationCounter.inflationCount.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN inflating one of our resource file, the inflater should not crash`() {
|
||||
val fileList = File("./src/main/res/layout").listFiles()
|
||||
for (file in fileList!!) {
|
||||
val layoutName = file.name.split(".")[0]
|
||||
val layoutId = testContext.resources.getIdentifier(
|
||||
layoutName,
|
||||
"layout",
|
||||
testContext.packageName
|
||||
)
|
||||
|
||||
assertNotEquals(-1, layoutId)
|
||||
if (!layoutsNotToTest.contains(layoutName)) {
|
||||
perfInflater.inflate(layoutId, FrameLayout(testContext), true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class MockInflater(
|
||||
inflater: LayoutInflater,
|
||||
context: Context
|
||||
) : PerformanceInflater(
|
||||
inflater,
|
||||
context
|
||||
) {
|
||||
|
||||
override fun onCreateView(name: String?, attrs: AttributeSet?): View? {
|
||||
// We skip the fragment layout for the simple reason that it implements
|
||||
// a whole different inflate which is implemented in the activity.LayoutFactory
|
||||
// methods. To be able to properly test it here, we would have to copy the whole
|
||||
// inflater file (or create an activity) and pass our layout through the onCreateView
|
||||
// method of that activity.
|
||||
if (name!!.contains("fragment")) {
|
||||
return FrameLayout(testContext)
|
||||
}
|
||||
return super.onCreateView(name, attrs)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue