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

Commit a256c847 authored by Wenbo Jie (介文博)'s avatar Wenbo Jie (介文博) Committed by Android (Google) Code Review
Browse files

Merge "[DocsUI M3] Add unit test for empty state message" into main

parents 94130d12 383abcd1
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -29,11 +29,13 @@ public final class TestEnvironment implements DocumentsAdapter.Environment {
    private final Context testContext;
    private final TestEnv mEnv;
    private final ActionHandler mActionHandler;
    private boolean mInSearchMode;

    public TestEnvironment(Context testContext, TestEnv env, ActionHandler actionHandler) {
        this.testContext = testContext;
        mEnv = env;
        mActionHandler = actionHandler;
        mInSearchMode = false;
    }

    @Override
@@ -72,7 +74,7 @@ public final class TestEnvironment implements DocumentsAdapter.Environment {

    @Override
    public boolean isInSearchMode() {
        return false;
        return mInSearchMode;
    }

    @Override
@@ -88,4 +90,8 @@ public final class TestEnvironment implements DocumentsAdapter.Environment {
    @Override
    public void onBindDocumentHolder(DocumentHolder holder, Cursor cursor) {
    }

    public void setInSearchMode(boolean inSearchMode) {
        mInSearchMode = inSearchMode;
    }
}
+42 −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.testing

import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.core.graphics.createBitmap
import org.junit.Assert

/** Handy Junit assertions related to Drawable. */
class DrawableAsserts {
  companion object {
    @JvmStatic
    fun assertDrawablesEqual(actual: Drawable, expected: Drawable) {
      val bitmap1 = createBitmap(actual.intrinsicWidth, actual.intrinsicHeight)
      val canvas1 = Canvas(bitmap1)
      actual.setBounds(0, 0, actual.intrinsicWidth, actual.intrinsicHeight)
      actual.draw(canvas1)

      val bitmap2 = createBitmap(expected.intrinsicWidth, expected.intrinsicHeight)
      val canvas2 = Canvas(bitmap2)
      expected.setBounds(0, 0, expected.intrinsicWidth, expected.intrinsicHeight)
      expected.draw(canvas2)

      Assert.assertTrue("Drawables are not equal", bitmap1.sameAs(bitmap2))
    }
  }
}
+9 −0
Original line number Diff line number Diff line
@@ -59,10 +59,19 @@ public class TestModel extends Model {

    @Override
    public void reset() {
        // Intentionally not calling super.reset() because of AbstractActionHandlerTest relies
        // on the model to keep `mIds`.gs
        mLastId = 0;
        mCursor = new MatrixCursor(COLUMNS);
    }

    /**
     * Clear the model ids to make it become an empty model.
     */
    public void clearIds() {
        super.reset();
    }

    public void update() {
        DirectoryResult r = new DirectoryResult();
        r.setCursor(mCursor);
+44 −5
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@ import static com.android.documentsui.DevicePolicyResources.Strings.CANT_SELECT_
import static com.android.documentsui.DevicePolicyResources.Strings.CANT_SELECT_WORK_FILES_TITLE;
import static com.android.documentsui.DevicePolicyResources.Strings.WORK_PROFILE_OFF_ENABLE_BUTTON;
import static com.android.documentsui.DevicePolicyResources.Strings.WORK_PROFILE_OFF_ERROR_TITLE;
import static com.android.documentsui.testing.DrawableAsserts.assertDrawablesEqual;
import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;
import static com.android.documentsui.util.Material3Config.getRes;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
@@ -53,6 +56,7 @@ import com.android.documentsui.base.State;
import com.android.documentsui.base.UserId;
import com.android.documentsui.testing.TestActionHandler;
import com.android.documentsui.testing.TestEnv;
import com.android.documentsui.testing.TestModel;
import com.android.documentsui.testing.TestProvidersAccess;
import com.android.documentsui.testing.UserManagers;
import com.android.modules.utils.build.SdkLevel;
@@ -83,6 +87,7 @@ public final class MessageTest {
    private DevicePolicyManager mDevicePolicyManager;
    private TestActionHandler mTestActionHandler;
    private final TestConfigStore mTestConfigStore = new TestConfigStore();
    private DocumentsAdapter.Environment mEnv;

    @Parameter(0)
    public boolean isPrivateSpaceEnabled;
@@ -110,9 +115,8 @@ public final class MessageTest {
                .thenReturn(mDevicePolicyManager);
        when(mContext.getResources()).thenReturn(
                InstrumentationRegistry.getInstrumentation().getTargetContext().getResources());
        DocumentsAdapter.Environment env =
                new TestEnvironment(mContext, TestEnv.create(), mTestActionHandler);
        env.getDisplayState().action = State.ACTION_GET_CONTENT;
        mEnv = new TestEnvironment(mContext, TestEnv.create(), mTestActionHandler);
        mEnv.getDisplayState().action = State.ACTION_GET_CONTENT;

        isPrivateSpaceEnabled = SdkLevel.isAtLeastS() && isPrivateSpaceEnabled;
        if (SdkLevel.isAtLeastV()) {
@@ -129,11 +133,11 @@ public final class MessageTest {
            Map<UserId, String> userIdToLabelMap = new HashMap<>();
            userIdToLabelMap.put(TestProvidersAccess.USER_ID, personalLabel);
            userIdToLabelMap.put(mUserId, workLabel);
            mInflateMessage = new Message.InflateMessage(env, mDefaultCallback,
            mInflateMessage = new Message.InflateMessage(mEnv, mDefaultCallback,
                    TestProvidersAccess.USER_ID, mUserId, userIdToLabelMap, mUserManager,
                    mTestConfigStore);
        } else {
            mInflateMessage = new Message.InflateMessage(env, mDefaultCallback, mTestConfigStore);
            mInflateMessage = new Message.InflateMessage(mEnv, mDefaultCallback, mTestConfigStore);
        }
    }

@@ -258,4 +262,39 @@ public final class MessageTest {

        assertThat(mTestActionHandler.mRequestDisablingQuietModeHappened).isTrue();
    }

    @Test
    public void testInflateMessage_updateToEmptyMessage() {
        // Set model to empty.
        ((TestModel) mEnv.getModel()).clearIds();
        // Make sure we have a root doc for title access.
        mEnv.getDisplayState().stack.changeRoot(TestProvidersAccess.HOME);
        // Turn off search mode.
        ((TestEnvironment) mEnv).setInSearchMode(false);

        mInflateMessage.update(Model.Update.UPDATE);

        Drawable expectedDrawable = mContext.getDrawable(getRes(R.drawable.empty));
        assertDrawablesEqual(mInflateMessage.getIcon(), expectedDrawable);
    }

    @Test
    public void testInflateMessage_updateToEmptyMessage_InSearch() {
        // Set model to empty.
        ((TestModel) mEnv.getModel()).clearIds();
        // Make sure we have a root doc for title access.
        mEnv.getDisplayState().stack.changeRoot(TestProvidersAccess.HOME);
        // Turn on search mode.
        ((TestEnvironment) mEnv).setInSearchMode(true);

        mInflateMessage.update(Model.Update.UPDATE);

        final Drawable expectedDrawable;
        if (isUseMaterial3FlagEnabled()) {
            expectedDrawable = mContext.getDrawable(R.drawable.empty_search);
        } else {
            expectedDrawable = mContext.getDrawable(R.drawable.empty);
        }
        assertDrawablesEqual(mInflateMessage.getIcon(), expectedDrawable);
    }
}
+2 −25
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.documentsui.queries;

import static com.android.documentsui.testing.DrawableAsserts.assertDrawablesEqual;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
@@ -25,9 +27,6 @@ import static org.mockito.Mockito.spy;
import static java.util.Objects.requireNonNull;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
@@ -274,26 +273,4 @@ public final class SearchChipViewManagerTest {
        chipDataList.add(new SearchChipData(CHIP_TYPE, 0 /* titleRes */, TEST_MIME_TYPES));
        return chipDataList;
    }

    private void assertDrawablesEqual(Drawable actual, Drawable expected) {
        Bitmap bitmap1 =
                Bitmap.createBitmap(
                        actual.getIntrinsicWidth(),
                        actual.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap1);
        actual.setBounds(0, 0, actual.getIntrinsicWidth(), actual.getIntrinsicHeight());
        actual.draw(canvas1);

        Bitmap bitmap2 =
                Bitmap.createBitmap(
                        expected.getIntrinsicWidth(),
                        expected.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
        Canvas canvas2 = new Canvas(bitmap2);
        expected.setBounds(0, 0, expected.getIntrinsicWidth(), expected.getIntrinsicHeight());
        expected.draw(canvas2);

        assertTrue("Drawables are not equal", bitmap1.sameAs(bitmap2));
    }
}