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 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20 
21 import static com.android.documentsui.base.Providers.AUTHORITY_STORAGE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import android.app.Activity;
26 import android.app.Instrumentation;
27 import android.app.UiAutomation;
28 import android.content.Context;
29 import android.content.Intent;
30 import android.net.Uri;
31 import android.provider.DocumentsContract;
32 import android.support.test.uiautomator.UiDevice;
33 
34 import androidx.test.filters.LargeTest;
35 import androidx.test.rule.ActivityTestRule;
36 import androidx.test.runner.AndroidJUnit4;
37 
38 import com.android.documentsui.bots.Bots;
39 import com.android.documentsui.picker.PickActivity;
40 
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 
46 import java.util.UUID;
47 
48 
49 @LargeTest
50 @RunWith(AndroidJUnit4.class)
51 public class ActionCreateDocumentUiTest {
52 
53     @Rule
54     public final ActivityTestRule<PickActivity> mRule =
55             new ActivityTestRule<>(PickActivity.class, false, false);
56 
57     private Context mTargetContext;
58     private Context mContext;
59     private Bots mBots;
60     private UiDevice mDevice;
61 
62     @Before
setup()63     public void setup() {
64         UiAutomation automation = getInstrumentation().getUiAutomation();
65 
66         mDevice = UiDevice.getInstance(getInstrumentation());
67         mTargetContext = getInstrumentation().getTargetContext();
68         mContext = getInstrumentation().getContext();
69         mBots = new Bots(mDevice, automation, mTargetContext, 5000);
70     }
71 
72     @Test
testActionCreate_TextFile()73     public void testActionCreate_TextFile() throws Exception {
74         Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
75         intent.addCategory(Intent.CATEGORY_DEFAULT);
76         intent.addCategory(Intent.CATEGORY_OPENABLE);
77         intent.setType("*/*");
78 
79         Uri hintUri = DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary");
80         intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, hintUri);
81 
82         mRule.launchActivity(intent);
83 
84         String fileName = UUID.randomUUID().toString() + ".txt";
85 
86         mBots.main.setDialogText(fileName);
87         mBots.main.clickSaveButton();
88         mDevice.waitForIdle();
89 
90         Instrumentation.ActivityResult activityResult = mRule.getActivityResult();
91 
92         Intent result = activityResult.getResultData();
93         Uri uri = result.getData();
94         int flags = result.getFlags();
95 
96         assertThat(activityResult.getResultCode()).isEqualTo(Activity.RESULT_OK);
97         assertThat(uri.getAuthority()).isEqualTo(AUTHORITY_STORAGE);
98         assertThat(uri.getPath()).contains(fileName);
99 
100         int expectedFlags =
101                 Intent.FLAG_GRANT_READ_URI_PERMISSION
102                         | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
103                         | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
104 
105         assertThat(flags).isEqualTo(expectedFlags);
106         assertThat(DocumentsContract.deleteDocument(mContext.getContentResolver(), uri)).isTrue();
107     }
108 
109 }