1 /* 2 * Copyright (C) 2020 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 com.android.documentsui.base.Providers.AUTHORITY_STORAGE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.app.Activity; 24 import android.app.Instrumentation; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.net.Uri; 28 import android.os.SystemClock; 29 import android.provider.DocumentsContract; 30 31 import androidx.test.filters.SmallTest; 32 import androidx.test.platform.app.InstrumentationRegistry; 33 import androidx.test.rule.ActivityTestRule; 34 35 import com.android.documentsui.base.DocumentInfo; 36 import com.android.documentsui.picker.PickActivity; 37 import com.android.documentsui.testing.TestProvidersAccess; 38 import com.android.documentsui.ui.TestDialogController; 39 import com.android.documentsui.util.VersionUtils; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 45 @SmallTest 46 public class PickActivityTest { 47 48 private static final String RESULT_EXTRA = "test_result_extra"; 49 private static final String RESULT_DATA = "123321"; 50 51 private Context mTargetContext; 52 private Intent mIntentGetContent; 53 private TestDialogController mTestDialogs; 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 mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 62 63 mIntentGetContent = new Intent(Intent.ACTION_GET_CONTENT); 64 mIntentGetContent.addCategory(Intent.CATEGORY_OPENABLE); 65 mIntentGetContent.setType("*/*"); 66 Uri hintUri = DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary"); 67 mIntentGetContent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, hintUri); 68 69 mTestDialogs = new TestDialogController(); 70 } 71 72 @Test testOnDocumentPicked()73 public void testOnDocumentPicked() { 74 DocumentInfo doc = new DocumentInfo(); 75 doc.userId = TestProvidersAccess.USER_ID; 76 doc.authority = "authority"; 77 doc.documentId = "documentId"; 78 79 PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); 80 pickActivity.mState.canShareAcrossProfile = true; 81 pickActivity.onDocumentPicked(doc); 82 SystemClock.sleep(3000); 83 84 Instrumentation.ActivityResult result = mRule.getActivityResult(); 85 assertThat(pickActivity.isFinishing()).isTrue(); 86 assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); 87 assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); 88 } 89 90 @Test testOnDocumentPicked_otherUser()91 public void testOnDocumentPicked_otherUser() { 92 if (VersionUtils.isAtLeastR()) { 93 DocumentInfo doc = new DocumentInfo(); 94 doc.userId = TestProvidersAccess.OtherUser.USER_ID; 95 doc.authority = "authority"; 96 doc.documentId = "documentId"; 97 98 PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); 99 pickActivity.mState.canShareAcrossProfile = true; 100 pickActivity.onDocumentPicked(doc); 101 SystemClock.sleep(3000); 102 103 Instrumentation.ActivityResult result = mRule.getActivityResult(); 104 assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); 105 assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); 106 } 107 } 108 109 @Test testOnDocumentPicked_otherUserDoesNotReturn()110 public void testOnDocumentPicked_otherUserDoesNotReturn() { 111 DocumentInfo doc = new DocumentInfo(); 112 doc.userId = TestProvidersAccess.OtherUser.USER_ID; 113 doc.authority = "authority"; 114 doc.documentId = "documentId"; 115 116 PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); 117 pickActivity.mState.canShareAcrossProfile = false; 118 pickActivity.getInjector().dialogs = mTestDialogs; 119 pickActivity.onDocumentPicked(doc); 120 SystemClock.sleep(3000); 121 122 assertThat(pickActivity.isFinishing()).isFalse(); 123 mTestDialogs.assertActionNotAllowedShown(); 124 } 125 } 126