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

Commit 986f9cc1 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[DocsUI M3] Add UI test for navigation rail" into main

parents 3a35c553 cd6b5d50
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -17,11 +17,15 @@
package com.android.documentsui.bots;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.swipeLeft;
import static androidx.test.espresso.action.ViewActions.swipeRight;
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static org.hamcrest.Matchers.allOf;

import android.app.UiAutomation;
import android.content.Context;
import android.graphics.Rect;
@@ -83,6 +87,26 @@ public class SidebarBot extends Bots.BaseBot {
        closeDrawer();
    }

    /** Open navigation root item from the navigation rail layout. */
    public void openNavRailRoot(String label) throws UiObjectNotFoundException {
        // Use UiScrollable to scroll into the view.
        final UiSelector rootsList =
                new UiSelector()
                        .resourceId(mTargetPackage + ":id/nav_rail_container_roots")
                        .childSelector(new UiSelector().resourceId(mRootListId));
        new UiObject(rootsList.childSelector(new UiSelector())).waitForExists(mTimeout);
        new UiScrollable(rootsList).scrollIntoView(new UiSelector().text(label));

        // Use Espresso to click.
        onView(allOf(withText(label), isDescendantOfA(withId(R.id.nav_rail_container_roots))))
                .perform(click());
    }

    /** Open navigation drawer from the burger menu button within the navigation rail layout. */
    public void openDrawerFromNavRail() {
        onView(withId(R.id.nav_rail_burger_menu)).perform(click());
    }

    public void openDrawer() throws UiObjectNotFoundException {
        final UiSelector rootsList = new UiSelector().resourceId(
                mTargetPackage + ":id/container_roots").childSelector(
+1 −1
Original line number Diff line number Diff line
@@ -140,7 +140,7 @@ abstract class ActivityTestJunit4<T : Activity?> {
        mActivityScenario!!.close()
    }

    protected fun launchActivity() {
    protected open fun launchActivity() {
        val intent = Intent(context, FilesActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
        if (this.initialRoot != null) {
+101 −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

import android.app.ActivityOptions
import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM
import android.content.Intent
import android.content.pm.PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT
import android.graphics.Rect
import android.platform.test.annotations.RequiresFlagsEnabled
import android.platform.test.flag.junit.CheckFlagsRule
import android.platform.test.flag.junit.DeviceFlagsValueProvider
import android.provider.DocumentsContract
import android.view.Display
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.documentsui.files.FilesActivity
import com.android.documentsui.flags.Flags.FLAG_USE_MATERIAL3
import kotlin.math.roundToInt
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RequiresFlagsEnabled(FLAG_USE_MATERIAL3)
@RunWith(AndroidJUnit4::class)
class NavRailUiTest : ActivityTestJunit4<FilesActivity>() {
  @get:Rule val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()

  companion object {
    private const val MEDIUM_WINDOW_WIDTH = 700
    private const val MEDIUM_WINDOW_HEIGHT = 900
  }

  /** Override the base method to launch activity in a specified window size. */
  override fun launchActivity() {
    val intent = Intent(context, FilesActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
    if (this.initialRoot != null) {
      intent.setAction(Intent.ACTION_VIEW)
      intent.setDataAndType(this.initialRoot!!.uri, DocumentsContract.Root.MIME_TYPE_ITEM)
    }
    val density = context!!.resources.displayMetrics.density
    val options = ActivityOptions.makeBasic()
    options.launchWindowingMode = WINDOWING_MODE_FREEFORM
    options.setLaunchBounds(
      Rect(
        0,
        0,
        (MEDIUM_WINDOW_WIDTH * density).roundToInt(),
        (MEDIUM_WINDOW_HEIGHT * density).roundToInt(),
      )
    )
    options.setLaunchDisplayId(Display.DEFAULT_DISPLAY)
    mActivityScenario = ActivityScenario.launch(intent, options.toBundle())
  }

  /** Making sure the test device meets the minimum window size and have freeform window feature. */
  @Before
  fun assumeMinWindowSizeAndFreeFormWindowFeature() {
    assumeTrue(
      "Skipping test: test device doesn't support FreeForm window.",
      context!!.getPackageManager().hasSystemFeature(FEATURE_FREEFORM_WINDOW_MANAGEMENT),
    )
    val displayMetrics = context!!.resources.displayMetrics
    assumeTrue(
      "Skipping test: test device window size is too small to support medium layout.",
      displayMetrics.widthPixels / displayMetrics.density >= MEDIUM_WINDOW_WIDTH &&
        displayMetrics.heightPixels / displayMetrics.density >= MEDIUM_WINDOW_HEIGHT,
    )
  }

  @Test
  fun testNavRailRootsNavigation() {
    bots.main.assertWindowTitle(StubProvider.ROOT_0_ID)
    bots.roots.openNavRailRoot(StubProvider.ROOT_1_ID)
    bots.main.assertWindowTitle(StubProvider.ROOT_1_ID)
  }

  @Test
  fun testNavRailDrawerRootsNavigation() {
    bots.main.assertWindowTitle(StubProvider.ROOT_0_ID)
    bots.roots.openDrawerFromNavRail()
    bots.roots.openRoot(StubProvider.ROOT_1_ID)
    bots.main.assertWindowTitle(StubProvider.ROOT_1_ID)
  }
}