Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 236507e8 authored by Luciano Pacheco's avatar Luciano Pacheco
Browse files

DocsUI: Fix TestRule for Material3 flag to reset the original value

The test InflateMessageDocumentHolderTest seems to run as M3 even for
device configurations that don't have the flag enabled yet.

This CL tries to fix this by changing CheckAndForceMaterial3Flag to
restore the original flag result after the test has run, to avoid
interference between tests.

Bug: 424456315
Test: atest com.android.documentsui.util.ThemeUtilsTest
Test: atest com.android.documentsui.dirlist.InflateMessageDocumentHolderTest
Flag: com.android.documentsui.flags.use_material3
Change-Id: I6902621a2e36561a1364a36cfaefd772b527ee06
parent f3da6608
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -223,7 +223,7 @@
-keep class com.android.documentsui.util.Material3Config {
-keep class com.android.documentsui.util.Material3Config {
  static int getRes(int);
  static int getRes(int);
  static void overrideMappingForTest(java.util.Map);
  static void overrideMappingForTest(java.util.Map);
  static void setEnabledForTest(boolean);
  static void setEnabledForTest(java.lang.Boolean);
}
}


# The SideSheetBehavior state is not directly accessed from DocumentsUI, but is used in tests
# The SideSheetBehavior state is not directly accessed from DocumentsUI, but is used in tests
+1 −1
Original line number Original line Diff line number Diff line
@@ -411,7 +411,7 @@ abstract class Material3Config private constructor() {
    }
    }


    @JvmStatic
    @JvmStatic
    fun setEnabledForTest(enabled: Boolean) {
    fun setEnabledForTest(enabled: Boolean?) {
      getInstance().forceMaterial3 = enabled
      getInstance().forceMaterial3 = enabled
      // Force the mapping to be re-initialized.
      // Force the mapping to be re-initialized.
      initialized = false
      initialized = false
+11 −1
Original line number Original line Diff line number Diff line
@@ -49,13 +49,23 @@ class CheckAndForceMaterial3Flag : TestRule {
                    flagsValueProvider.tearDownBeforeTest()
                    flagsValueProvider.tearDownBeforeTest()
                }
                }


                val originalFlagState = Material3Config.getInstance().forceMaterial3

                // The try/finally above takes care of checking the state of the DeviceFlag, so the
                // The try/finally above takes care of checking the state of the DeviceFlag, so the
                // code only reaches here if the flag is in the desired state.
                // code only reaches here if the flag is in the desired state.
                if (isMaterial3 != null) {
                if (isMaterial3 != null) {
                    // Only force if the use_material3 flag is in use (aka not-null).
                    // Only force if the use_material3 flag is in use (aka not-null).
                    Material3Config.setEnabledForTest(isMaterial3)
                    Material3Config.setEnabledForTest(isMaterial3)
                }
                }

                // Restore the flag value if it has changed.
                try {
                    base.evaluate()
                    base.evaluate()
                } finally {
                    if (originalFlagState != Material3Config.getInstance().forceMaterial3) {
                        Material3Config.setEnabledForTest(originalFlagState)
                    }
                }
            }
            }
        }
        }
    }
    }