From 9c55c5d5bd8f7e9c1fca55684b052ee5caa52cb7 Mon Sep 17 00:00:00 2001 From: Mugurell Date: Wed, 11 Jan 2023 09:28:21 +0200 Subject: [PATCH] Bug 1809592 - Update display orientation listener In testing it was seen that the display orientation change could get reported even without a screen rotation and because of the previous UNSPECIFIED default this would trigger the "onDisplayRotationChanged" callback. --- .../components/compose/cfr/helper/DisplayOrientationListener.kt | 3 +-- .../compose/cfr/helper/DisplayOrientationListenerTest.kt | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mobile/android/android-components/components/compose/cfr/src/main/java/mozilla/components/compose/cfr/helper/DisplayOrientationListener.kt b/mobile/android/android-components/components/compose/cfr/src/main/java/mozilla/components/compose/cfr/helper/DisplayOrientationListener.kt index 0c781fa61957..52690f0e8042 100644 --- a/mobile/android/android-components/components/compose/cfr/src/main/java/mozilla/components/compose/cfr/helper/DisplayOrientationListener.kt +++ b/mobile/android/android-components/components/compose/cfr/src/main/java/mozilla/components/compose/cfr/helper/DisplayOrientationListener.kt @@ -5,7 +5,6 @@ package mozilla.components.compose.cfr.helper import android.content.Context -import android.content.pm.ActivityInfo import android.hardware.display.DisplayManager import android.hardware.display.DisplayManager.DisplayListener import androidx.annotation.VisibleForTesting @@ -28,7 +27,7 @@ internal class DisplayOrientationListener( private val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager @VisibleForTesting - internal var currentOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + internal var currentOrientation = displayManager.displays[0].rotation /** * Start listening for display orientation changes. diff --git a/mobile/android/android-components/components/compose/cfr/src/test/java/mozilla/components/compose/cfr/helper/DisplayOrientationListenerTest.kt b/mobile/android/android-components/components/compose/cfr/src/test/java/mozilla/components/compose/cfr/helper/DisplayOrientationListenerTest.kt index 47da45ea4165..caea5b3368bf 100644 --- a/mobile/android/android-components/components/compose/cfr/src/test/java/mozilla/components/compose/cfr/helper/DisplayOrientationListenerTest.kt +++ b/mobile/android/android-components/components/compose/cfr/src/test/java/mozilla/components/compose/cfr/helper/DisplayOrientationListenerTest.kt @@ -24,6 +24,10 @@ class DisplayOrientationListenerTest { @Before fun setup() { doReturn(displayManager).`when`(context).getSystemService(Context.DISPLAY_SERVICE) + + val display: Display = mock() + doReturn(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT).`when`(display).rotation + doReturn(arrayOf(display)).`when`(displayManager).displays } @Test @@ -68,9 +72,8 @@ class DisplayOrientationListenerTest { fun `WHEN a display is changed but doesn't have a new rotation THEN don't inform the client`() { var hasRotationChanged = false val listener = DisplayOrientationListener(context) { hasRotationChanged = true } - listener.currentOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE val display: Display = mock() - doReturn(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE).`when`(display).rotation + doReturn(listener.currentOrientation).`when`(display).rotation doReturn(display).`when`(displayManager).getDisplay(1) listener.onDisplayChanged(1) @@ -82,7 +85,6 @@ class DisplayOrientationListenerTest { fun `WHEN a display is changed and has a new rotation THEN inform the client and remember the new rotation`() { var hasRotationChanged = false val listener = DisplayOrientationListener(context) { hasRotationChanged = true } - listener.currentOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT val display: Display = mock() doReturn(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE).`when`(display).rotation doReturn(display).`when`(displayManager).getDisplay(1) -- 2.11.4.GIT