1 /* 2 * Copyright (C) 2016 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.services; 18 19 import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE; 20 21 import static com.google.common.collect.Lists.newArrayList; 22 23 import android.net.Uri; 24 import android.provider.DocumentsContract; 25 26 import androidx.test.filters.MediumTest; 27 28 import java.util.List; 29 30 /** 31 * With this test we're interested in testing a common ancestor class shared by DeleteJob, CopyJob, 32 * and MoveJob. Since DeleteJob is simplest, we use that. 33 */ 34 @MediumTest 35 public class JobErrorHandlingTest extends AbstractJobTest<DeleteJob> { 36 testRecoversFromInvalidUri()37 public void testRecoversFromInvalidUri() throws Exception { 38 Uri invalidUri1 = Uri.parse("content://poodles/chuckleberry/ham"); 39 Uri validUri = mDocs.createDocument(mSrcRoot, "text/plain", "test2.txt"); 40 Uri invalidUri2 = Uri.parse("content://poodles/chuckleberry/ham"); 41 mDocs.writeDocument(validUri, FRUITY_BYTES); 42 43 createJob(newArrayList(invalidUri1, validUri, invalidUri2), 44 DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId)).run(); 45 mJobListener.waitForFinished(); 46 mJobListener.assertFinished(); 47 48 // verify that the document associated with the one valid uri was deleted. 49 mDocs.assertChildCount(mSrcRoot, 0); 50 } 51 testRecordsInvalidUris()52 public void testRecordsInvalidUris() throws Exception { 53 Uri invalidUri1 = Uri.parse("content://poodles/chuckleberry/ham"); 54 Uri validUri = mDocs.createDocument(mSrcRoot, "text/plain", "test2.txt"); 55 Uri invalidUri2 = Uri.parse("content://poodles/chuckleberry/ham"); 56 mDocs.writeDocument(validUri, FRUITY_BYTES); 57 58 createJob(newArrayList(invalidUri1, validUri, invalidUri2), 59 DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId)).run(); 60 mJobListener.waitForFinished(); 61 62 mJobListener.assertUriFailed(invalidUri1); 63 mJobListener.assertUriFailed(invalidUri2); 64 } 65 testReportsCorrectFailureCount()66 public void testReportsCorrectFailureCount() throws Exception { 67 Uri invalidUri1 = Uri.parse("content://poodles/chuckleberry/ham"); 68 Uri validUri = mDocs.createDocument(mSrcRoot, "text/plain", "test2.txt"); 69 Uri invalidUri2 = Uri.parse("content://poodles/chuckleberry/ham"); 70 mDocs.writeDocument(validUri, FRUITY_BYTES); 71 72 createJob(newArrayList(invalidUri1, validUri, invalidUri2), 73 DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId)).run(); 74 mJobListener.waitForFinished(); 75 76 mJobListener.assertFailureCount(2); 77 } 78 79 /** 80 * Creates a job with a stack consisting to the default src directory. 81 */ createJob(List<Uri> srcs, Uri srcParent)82 private DeleteJob createJob(List<Uri> srcs, Uri srcParent) throws Exception { 83 Uri stack = DocumentsContract.buildDocumentUri(AUTHORITY, mSrcRoot.documentId); 84 return createJob(OPERATION_DELETE, srcs, srcParent, stack); 85 } 86 } 87