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

Commit ea55aa08 authored by Ben Reich's avatar Ben Reich
Browse files

Introduce new TestFilesRule to extract file creation

This converts the BandSelectionUiTest to JUnit 4 and introduces a new
Rule that enables creation of test files to remove the odd nature of
file creation by overriding the method. It also migrates all the
default usages of initTestFiles to the Rule.

In future CLs it will introduce a Builder pattern to customise the base
files that will be created.

Bug: 407646510
Test: atest com.android.documentsui.BandSelectionUiTest
Flag: EXEMPT test change only
Change-Id: I81166f2112da2ab6e6f76be7143c239321bae2c1
parent f0239bfe
Loading
Loading
Loading
Loading
+79 −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.os.Bundle
import android.provider.DocumentsContract
import androidx.test.platform.app.InstrumentationRegistry
import com.android.documentsui.DocumentsProviderHelper
import com.android.documentsui.StubProvider
import com.android.documentsui.base.UserId
import org.junit.rules.ExternalResource

/** Rule that creates test files in a test. */
class TestFilesRule() : ExternalResource() {
    private val docsHelper = DocumentsProviderHelper(
        UserId.DEFAULT_USER,
        StubProvider.DEFAULT_AUTHORITY,
        InstrumentationRegistry.getInstrumentation().context,
        StubProvider.DEFAULT_AUTHORITY
    )

    override fun before() {
        docsHelper.clear(null, null)
        docsHelper.configure(null, Bundle.EMPTY)

        val root0 = docsHelper.getRoot(StubProvider.ROOT_0_ID)
        val root1 = docsHelper.getRoot(StubProvider.ROOT_1_ID)

        docsHelper.createFolder(root0, DIR_NAME_1)
        docsHelper.createDocument(root0, "text/plain", FILE_NAME_1)
        docsHelper.createDocument(root0, "image/png", FILE_NAME_2)
        docsHelper.createDocumentWithFlags(
            root0.documentId,
            "text/plain",
            FILE_NAME_NO_RENAME,
            DocumentsContract.Document.FLAG_SUPPORTS_WRITE
        )

        docsHelper.createDocument(root1, "text/plain", FILE_NAME_3)
        docsHelper.createDocument(root1, "text/plain", FILE_NAME_4)
    }

    override fun after() {
        docsHelper.cleanUp()
    }

    companion object {
        @JvmField
        val DIR_NAME_1: String = "Dir1"

        @JvmField
        val FILE_NAME_1: String = "file1.log"

        @JvmField
        val FILE_NAME_2: String = "file12.png"

        @JvmField
        val FILE_NAME_3: String = "anotherFile0.log"

        @JvmField
        val FILE_NAME_4: String = "poodles.text"

        @JvmField
        val FILE_NAME_NO_RENAME: String = "NO_RENAMEfile.txt"
    }
}
+0 −16
Original line number Diff line number Diff line
@@ -166,22 +166,6 @@ abstract class ActivityTestJunit4<T : Activity?> {
        device!!.waitForIdle()
    }

    @Throws(RemoteException::class)
    protected open fun initTestFiles() {
        mDocsHelper!!.createFolder(this.initialRoot, dirName1)
        mDocsHelper!!.createDocument(this.initialRoot, "text/plain", fileName1)
        mDocsHelper!!.createDocument(this.initialRoot, "image/png", fileName2)
        mDocsHelper!!.createDocumentWithFlags(
            initialRoot!!.documentId,
            "text/plain",
            fileNameNoRename,
            DocumentsContract.Document.FLAG_SUPPORTS_WRITE
        )

        mDocsHelper!!.createDocument(rootDir1, "text/plain", fileName3)
        mDocsHelper!!.createDocument(rootDir1, "text/plain", fileName4)
    }

    @Throws(IOException::class)
    private fun disableScreenOffAndSleepTimeouts() {
        initialScreenOffTimeoutValue = device!!.executeShellCommand(
+3 −6
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.documentsui;

import static com.android.documentsui.flags.Flags.FLAG_USE_SEARCH_V2_READ_ONLY;

import android.os.RemoteException;
import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
@@ -27,8 +26,8 @@ import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import androidx.test.filters.LargeTest;

import com.android.documentsui.files.FilesActivity;
import com.android.documentsui.rules.TestFilesRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

@@ -38,10 +37,8 @@ public class ArchiveUiTest extends ActivityTestJunit4<FilesActivity> {
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    @Before
    public void setUpTest() throws RemoteException {
        initTestFiles();
    }
    @Rule
    public final TestFilesRule mTestFilesRule = new TestFilesRule();

    private void archiveValid() throws Exception {
        bots.roots.openRoot("ResourcesProvider");
+15 −12
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui;

import android.graphics.Point;
@@ -22,25 +21,28 @@ import android.graphics.Rect;
import androidx.test.filters.LargeTest;

import com.android.documentsui.files.FilesActivity;
import com.android.documentsui.rules.TestFilesRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

@LargeTest
public class BandSelectionUiTest extends ActivityTest<FilesActivity> {
public class BandSelectionUiTest extends ActivityTestJunit4<FilesActivity> {

    public BandSelectionUiTest() {
        super(FilesActivity.class);
    }
    @Rule
    public TestFilesRule mTestFiles = new TestFilesRule();

    @Override
    public void setUp() throws Exception {
        super.setUp();
        initTestFiles();
    @Before
    public void setUpTest() {
        bots.roots.closeDrawer();
    }

    @Test
    public void testBandSelection_allFiles() throws Exception {
        bots.main.switchToGridMode();
        Rect dirListBounds = bots.directory.findDocumentsList().getBounds();
        Rect startDir = bots.directory.findDocument(dirName1).getBounds();
        Rect startDir = bots.directory.findDocument(TestFilesRule.DIR_NAME_1).getBounds();
        Point start = new Point(dirListBounds.right - 1, startDir.centerY());
        Point end = new Point(dirListBounds.left + 1, dirListBounds.bottom - 1);
        bots.gesture.bandSelection(start, end);
@@ -48,11 +50,12 @@ public class BandSelectionUiTest extends ActivityTest<FilesActivity> {
        bots.directory.assertSelection(4);
    }

    @Test
    public void testBandSelection_someFiles() throws Exception {
        bots.main.switchToGridMode();
        Rect dirListBounds = bots.directory.findDocumentsList().getBounds();
        Rect startDoc = bots.directory.findDocument(fileNameNoRename).getBounds();
        Rect endDoc = bots.directory.findDocument(fileName1).getBounds();
        Rect startDoc = bots.directory.findDocument(TestFilesRule.FILE_NAME_NO_RENAME).getBounds();
        Rect endDoc = bots.directory.findDocument(TestFilesRule.FILE_NAME_1).getBounds();
        // Start from right side of file NoRename.
        Point start = new Point(dirListBounds.right - 1, startDoc.bottom - 1);
        // End is center of file1
+0 −5
Original line number Diff line number Diff line
@@ -44,11 +44,6 @@ public class FilesActivityDefaultsUiTest extends ActivityTestJunit4<FilesActivit
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    @Override
    protected void initTestFiles() {
        // Overriding to init with no items in test roots
    }

    @Override
    protected RootInfo getInitialRoot() {
        return null;  // test the default, unaffected state of the app.
Loading