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

Commit db742eef authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Android (Google) Code Review
Browse files

Merge changes from topic "docsui_force_material3_config" into main

* changes:
  DocsUI M3: Use the config force_material3
  DocsUI M3: Update all tests to use the new test rule
  DocsUI M3: Add test rule to set force_material3
parents b97d85e8 9bc58ae3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -218,5 +218,6 @@

-keep class com.android.documentsui.util.Material3Config {
  static int getRes(int);
  static void overrideForTest(java.util.Map);
  static void overrideMappingForTest(java.util.Map);
  static void setEnabledForTest(boolean);
}
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ class FlagUtils {
    companion object {
        @JvmStatic
        fun isUseMaterial3FlagEnabled(): Boolean {
            return Flags.useMaterial3()
            return Flags.useMaterial3() && Material3Config.getInstance().forceMaterial3 == true
        }

        @JvmStatic
+10 −5
Original line number Diff line number Diff line
@@ -373,13 +373,11 @@ abstract class Material3Config private constructor() {
    @JvmStatic
    @AnyRes
    fun getRes(@AnyRes originalResourceId: Int): Int {
      // NOTE: isUseMaterial3FlagEnabled() already checks for the config forceMaterial3.
      if (!isUseMaterial3FlagEnabled()) {
        return originalResourceId
      }
      // TODO(lucmult): Enable this condition when all the resources are merged in one APK.
      // if (!(Material3Config.getInstance().forceMaterial3 ?: false)) {
      //   return originalResourceId
      // }

      if (!initialized) {
        initializeIdMapping()
      }
@@ -402,9 +400,16 @@ abstract class Material3Config private constructor() {
    }

    @JvmStatic
    fun overrideForTest(overrides: Map<Int, Int>) {
    fun overrideMappingForTest(overrides: Map<Int, Int>) {
      initialized = true
      idMapping = overrides
    }

    @JvmStatic
    fun setEnabledForTest(enabled: Boolean) {
      getInstance().forceMaterial3 = enabled
      // Force the mapping to be re-initialized.
      initialized = false
    }
  }
}
+1 −0
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ android_library {
        "androidx.test.espresso.core",
        "androidx.test.rules",
        "androidx.test.uiautomator_uiautomator",
        "flag-junit",
        "mockito-target",
        "ub-janktesthelper",
    ],
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.rules

import android.platform.test.flag.junit.AnnotationsRetriever
import android.platform.test.flag.junit.DeviceFlagsValueProvider
import android.platform.test.flag.junit.IFlagsValueProvider
import com.android.documentsui.flags.Flags
import com.android.documentsui.util.Material3Config
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
 * A TestRule that checks for the flag like the `CheckFlagsRule`, but this also overrides the config
 * `force_material3` to sync with the desired state of the flag `use_material3`.
 *
 * @RequiresFlagsEnabled(FLAG_USE_MATERIAL3) => forces the config `force_material3` to true.
 * @RequiresFlagsDisabled(FLAG_USE_MATERIAL3) => forces the config `force_material3` to false.
 */
class CheckAndForceMaterial3Flag : TestRule {
    private val flagsValueProvider: IFlagsValueProvider = DeviceFlagsValueProvider()

    override fun apply(base: Statement, description: Description?): Statement? {
        return object : Statement() {
            @Throws(Throwable::class)
            override fun evaluate() {
                val flagAnnotations = AnnotationsRetriever.getFlagAnnotations(description)
                val isMaterial3 = flagAnnotations.mRequiredFlagValues[Flags.FLAG_USE_MATERIAL3]

                flagsValueProvider.setUp()
                try {
                    flagAnnotations.assumeAllRequiredFlagsMatchProvider(flagsValueProvider)
                } finally {
                    flagsValueProvider.tearDownBeforeTest()
                }

                // 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.
                if (isMaterial3 != null) {
                    // Only force if the use_material3 flag is in use (aka not-null).
                    Material3Config.setEnabledForTest(isMaterial3)
                }
                base.evaluate()
            }
        }
    }
}
Loading