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

Commit 82e4f05e authored by Sergey Nikolaienkov's avatar Sergey Nikolaienkov
Browse files

Send KEYCODE_WAKEUP before ActionCreateDocumentUiTest

Make sure to properly set-up the device ahead of running
ActionCreateDocumentUiTest: "wake-up" the device, navigate to the home
screen, disable screen-offs etc.
For this introduce DocumentsUiTestBase "base" class, and make
ActionCreateDocumentUiTest extend it.

Bug: 259640894
Test: atest DocumentsUIGoogleTests
Change-Id: I0815e7f2780f437d7279789cfd79ede42be38a3b
parent 248eb0db
Loading
Loading
Loading
Loading
+34 −40
Original line number Diff line number Diff line
@@ -16,16 +16,19 @@

package com.android.documentsui;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static android.app.Activity.RESULT_OK;
import static android.content.Intent.ACTION_CREATE_DOCUMENT;
import static android.content.Intent.CATEGORY_DEFAULT;
import static android.content.Intent.CATEGORY_OPENABLE;
import static android.content.Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

import static com.android.documentsui.base.Providers.AUTHORITY_STORAGE;

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

import android.app.Activity;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.DocumentsContract;
@@ -33,11 +36,10 @@ import android.provider.DocumentsContract;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.UiDevice;

import com.android.documentsui.bots.Bots;
import com.android.documentsui.picker.PickActivity;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -48,62 +50,54 @@ import java.util.UUID;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class ActionCreateDocumentUiTest {
public class ActionCreateDocumentUiTest extends DocumentsUiTestBase {

    @Rule
    public final ActivityTestRule<PickActivity> mRule =
            new ActivityTestRule<>(PickActivity.class, false, false);

    private Context mTargetContext;
    private Context mContext;
    private Bots mBots;
    private UiDevice mDevice;

    @Before
    public void setup() {
        UiAutomation automation = getInstrumentation().getUiAutomation();
    public void setup() throws Exception {
        super.setUp();
    }

        mDevice = UiDevice.getInstance(getInstrumentation());
        mTargetContext = getInstrumentation().getTargetContext();
        mContext = getInstrumentation().getContext();
        mBots = new Bots(mDevice, automation, mTargetContext, 5000);
    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void testActionCreate_TextFile() throws Exception {
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        final Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
        intent.addCategory(CATEGORY_DEFAULT);
        intent.addCategory(CATEGORY_OPENABLE);
        intent.setType("*/*");

        Uri hintUri = DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary");
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, hintUri);
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
                DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary"));

        mRule.launchActivity(intent);

        String fileName = UUID.randomUUID().toString() + ".txt";
        final String fileName = UUID.randomUUID() + ".txt";

        mBots.main.setDialogText(fileName);
        mBots.main.clickSaveButton();
        mDevice.waitForIdle();
        bots.main.setDialogText(fileName);
        bots.main.clickSaveButton();
        device.waitForIdle();

        Instrumentation.ActivityResult activityResult = mRule.getActivityResult();
        final Instrumentation.ActivityResult activityResult = mRule.getActivityResult();
        assertThat(activityResult.getResultCode()).isEqualTo(RESULT_OK);

        Intent result = activityResult.getResultData();
        Uri uri = result.getData();
        int flags = result.getFlags();
        final Intent resultData = activityResult.getResultData();
        final Uri uri = resultData.getData();

        assertThat(activityResult.getResultCode()).isEqualTo(Activity.RESULT_OK);
        assertThat(uri.getAuthority()).isEqualTo(AUTHORITY_STORAGE);
        assertThat(uri.getPath()).contains(fileName);

        int expectedFlags =
                Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                        | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
        assertThat(resultData.getFlags()).isEqualTo(FLAG_GRANT_READ_URI_PERMISSION
                        | FLAG_GRANT_WRITE_URI_PERMISSION
                        | FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

        assertThat(flags).isEqualTo(expectedFlags);
        assertThat(DocumentsContract.deleteDocument(mContext.getContentResolver(), uri)).isTrue();
        final boolean deletedSuccessfully =
                DocumentsContract.deleteDocument(context.getContentResolver(), uri);
        assertThat(deletedSuccessfully).isTrue();
    }

}
 No newline at end of file
+89 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static java.util.Objects.requireNonNull;

import android.app.Instrumentation;
import android.content.Context;
import android.view.KeyEvent;

import androidx.test.uiautomator.UiDevice;

import com.android.documentsui.bots.Bots;

import java.io.IOException;


/** Base class for instrumentation tests for DocumentsUI Activities. */
class DocumentsUiTestBase {
    private static final int BOTS_TIMEOUT = 5000; // 5 seconds

    protected Context targetContext;
    protected Context context;
    protected UiDevice device;
    protected Bots bots;

    private String initialScreenOffTimeoutValue = null;
    private String initialSleepTimeoutValue = null;

    protected void setUp() throws Exception {
        final Instrumentation instrumentation = getInstrumentation();
        targetContext = instrumentation.getTargetContext();
        context = instrumentation.getContext();
        device = UiDevice.getInstance(instrumentation);

        disableScreenOffAndSleepTimeouts();

        device.setOrientationNatural();

        // "Wake-up" the device and navigate to the home page.
        device.pressKeyCode(KeyEvent.KEYCODE_WAKEUP);
        device.pressKeyCode(KeyEvent.KEYCODE_MENU);

        bots = new Bots(device, instrumentation.getUiAutomation(), targetContext, BOTS_TIMEOUT);
    }

    protected void tearDown() throws Exception {
        restoreScreenOffAndSleepTimeouts();
    }

    private void disableScreenOffAndSleepTimeouts() throws IOException {
        initialScreenOffTimeoutValue = device.executeShellCommand(
                "settings get system screen_off_timeout");
        initialSleepTimeoutValue = device.executeShellCommand(
                "settings get secure sleep_timeout");
        device.executeShellCommand("settings put system screen_off_timeout -1");
        device.executeShellCommand("settings put secure sleep_timeout -1");
    }

    private void restoreScreenOffAndSleepTimeouts() throws IOException {
        requireNonNull(initialScreenOffTimeoutValue);
        requireNonNull(initialSleepTimeoutValue);
        try {
            device.executeShellCommand(
                    "settings put system screen_off_timeout " + initialScreenOffTimeoutValue);
            device.executeShellCommand(
                    "settings put secure sleep_timeout " + initialSleepTimeoutValue);
        } finally {
            initialScreenOffTimeoutValue = null;
            initialSleepTimeoutValue = null;
        }
    }
}