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

Commit 4622cee7 authored by Sergey Nikolaienkov's avatar Sergey Nikolaienkov Committed by Automerger Merge Worker
Browse files

Send KEYCODE_WAKEUP before ActionCreateDocumentUiTest am: 82e4f05e

parents a8c413cc 82e4f05e
Loading
Loading
Loading
Loading
+34 −40
Original line number Original line Diff line number Diff line
@@ -16,16 +16,19 @@


package com.android.documentsui;
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.android.documentsui.base.Providers.AUTHORITY_STORAGE;


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


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


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


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


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


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


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

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


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


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

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


        mRule.launchActivity(intent);
        mRule.launchActivity(intent);


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


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


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


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


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


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


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

}
}
 No newline at end of file
+89 −0
Original line number Original line 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;
        }
    }
}