1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.documentsui; 18 19 import static android.app.Activity.RESULT_OK; 20 import static android.content.Intent.ACTION_CREATE_DOCUMENT; 21 import static android.content.Intent.CATEGORY_DEFAULT; 22 import static android.content.Intent.CATEGORY_OPENABLE; 23 import static android.content.Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION; 24 import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION; 25 import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION; 26 27 import static com.android.documentsui.base.Providers.AUTHORITY_STORAGE; 28 29 import static com.google.common.truth.Truth.assertThat; 30 31 import android.app.Instrumentation; 32 import android.content.Intent; 33 import android.net.Uri; 34 import android.provider.DocumentsContract; 35 36 import androidx.test.filters.LargeTest; 37 import androidx.test.rule.ActivityTestRule; 38 import androidx.test.runner.AndroidJUnit4; 39 40 import com.android.documentsui.picker.PickActivity; 41 42 import org.junit.After; 43 import org.junit.Before; 44 import org.junit.Rule; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 48 import java.util.UUID; 49 50 51 @LargeTest 52 @RunWith(AndroidJUnit4.class) 53 public class ActionCreateDocumentUiTest extends DocumentsUiTestBase { 54 55 @Rule 56 public final ActivityTestRule<PickActivity> mRule = 57 new ActivityTestRule<>(PickActivity.class, false, false); 58 59 @Before setup()60 public void setup() throws Exception { 61 super.setUp(); 62 } 63 64 @After tearDown()65 public void tearDown() throws Exception { 66 super.tearDown(); 67 } 68 69 @Test testActionCreate_TextFile()70 public void testActionCreate_TextFile() throws Exception { 71 final Intent intent = new Intent(ACTION_CREATE_DOCUMENT); 72 intent.addCategory(CATEGORY_DEFAULT); 73 intent.addCategory(CATEGORY_OPENABLE); 74 intent.setType("*/*"); 75 intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, 76 DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary")); 77 78 mRule.launchActivity(intent); 79 80 final String fileName = UUID.randomUUID() + ".txt"; 81 82 bots.main.setDialogText(fileName); 83 device.waitForIdle(); 84 bots.main.clickSaveButton(); 85 86 final Instrumentation.ActivityResult activityResult = mRule.getActivityResult(); 87 assertThat(activityResult.getResultCode()).isEqualTo(RESULT_OK); 88 89 final Intent resultData = activityResult.getResultData(); 90 final Uri uri = resultData.getData(); 91 92 assertThat(uri.getAuthority()).isEqualTo(AUTHORITY_STORAGE); 93 assertThat(uri.getPath()).contains(fileName); 94 95 assertThat(resultData.getFlags()).isEqualTo(FLAG_GRANT_READ_URI_PERMISSION 96 | FLAG_GRANT_WRITE_URI_PERMISSION 97 | FLAG_GRANT_PERSISTABLE_URI_PERMISSION); 98 99 final boolean deletedSuccessfully = 100 DocumentsContract.deleteDocument(context.getContentResolver(), uri); 101 assertThat(deletedSuccessfully).isTrue(); 102 } 103 }