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

Commit f7275578 authored by Luciano Pacheco's avatar Luciano Pacheco
Browse files

DocsUI M3: Add test rule to set force_material3

The test rule is to simplify the testing using the use_material3 flag,
forcing the config `force_material3` to be in sync with the desired flag
value.

Bug: 403375773
Test: atest DocumentsUIGoogleTests:com.android.documentsui.util.ThemeUtilsTest
Flag: com.android.documentsui.flags.use_material3
Change-Id: Id951da2f9b929ed6f1a94cf6fd886413b79b43de
parent d842dd59
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);
}
+8 −1
Original line number Diff line number Diff line
@@ -403,9 +403,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()
            }
        }
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -38,8 +38,6 @@ import android.content.ContentResolver;
import android.net.Uri;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
@@ -53,6 +51,7 @@ import com.android.documentsui.base.UserId;
import com.android.documentsui.files.FilesActivity;
import com.android.documentsui.filters.HugeLongTest;
import com.android.documentsui.inspector.InspectorActivity;
import com.android.documentsui.rules.CheckAndForceMaterial3Flag;
import com.android.documentsui.rules.TestFilesRule;

import org.junit.Rule;
@@ -64,7 +63,7 @@ import org.junit.runner.RunWith;
public class FilesActivityUiTest extends ActivityTestJunit4<FilesActivity> {

    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
    public final CheckAndForceMaterial3Flag mCheckFlagsRule = new CheckAndForceMaterial3Flag();

    @Rule
    public final TestFilesRule mTestFilesRule =
Loading