For #26723 - Fix WallpapersObserver leaking a view from HomeFragment

The observer initialized with a fragment View would outlive the fragment and in
that allow for leaking the entire fragment layout.
This commit is contained in:
Mugurell 2022-08-31 17:46:22 +03:00 committed by mergify[bot]
parent b7f9457cc4
commit 93859d8560
2 changed files with 16 additions and 3 deletions

View File

@ -121,7 +121,8 @@ import kotlin.math.min
@Suppress("TooManyFunctions", "LargeClass")
class HomeFragment : Fragment() {
private val args by navArgs<HomeFragmentArgs>()
private lateinit var bundleArgs: Bundle
@VisibleForTesting
internal lateinit var bundleArgs: Bundle
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
@ -163,7 +164,7 @@ class HomeFragment : Fragment() {
private var appBarLayout: AppBarLayout? = null
private lateinit var currentMode: CurrentMode
@VisibleForTesting
internal lateinit var wallpapersObserver: WallpapersObserver
internal var wallpapersObserver: WallpapersObserver? = null
private val topSitesFeature = ViewBoundFeatureWrapper<TopSitesFeature>()
private val messagingFeature = ViewBoundFeatureWrapper<MessagingFeature>()
@ -418,7 +419,7 @@ class HomeFragment : Fragment() {
// Otherwise the portrait wallpaper may remain shown on landscape,
// see https://github.com/mozilla-mobile/fenix/issues/26638
runBlockingIncrement {
wallpapersObserver.applyCurrentWallpaper()
wallpapersObserver?.applyCurrentWallpaper()
}
}
}
@ -700,6 +701,7 @@ class HomeFragment : Fragment() {
_sessionControlInteractor = null
sessionControlView = null
appBarLayout = null
wallpapersObserver = null
_binding = null
bundleArgs.clear()
}

View File

@ -19,6 +19,7 @@ import mozilla.components.feature.top.sites.TopSite
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@ -156,4 +157,14 @@ class HomeFragmentTest {
assertFalse(homeFragment.shouldEnableWallpaper())
}
@Test
fun `GIVEN the wallpaper feature is active WHEN the fragment view is destroyed THEN cleanup the wallpaper observer`() {
homeFragment.bundleArgs = mockk(relaxed = true)
homeFragment.wallpapersObserver = mockk()
homeFragment.onDestroyView()
assertNull(homeFragment.wallpapersObserver)
}
}