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 android.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.provider.DocumentsContract; 23 import android.test.mock.MockContentResolver; 24 import android.util.Log; 25 26 import java.util.concurrent.CountDownLatch; 27 import java.util.concurrent.TimeUnit; 28 import java.util.concurrent.TimeoutException; 29 30 /** 31 * A test resolver that enables this test suite to listen for notifications that mark when copy 32 * operations are done. 33 */ 34 class TestContentResolver extends MockContentResolver { 35 36 private static final String TAG = "TestContextResolver"; 37 38 private CountDownLatch mReadySignal; 39 private CountDownLatch mNotificationSignal; 40 private Context mContext; 41 TestContentResolver(Context context)42 public TestContentResolver(Context context) { 43 mContext = context; 44 mReadySignal = new CountDownLatch(1); 45 } 46 47 /** 48 * Wait for the given number of files to be copied to destination. Times out after 1 sec. 49 */ waitForChanges(int count)50 public void waitForChanges(int count) throws Exception { 51 // Wait for no more than 1 second by default. 52 waitForChanges(count, 1000); 53 } 54 55 /** 56 * Wait for files to be copied to destination. 57 * 58 * @param count Number of files to wait for. 59 * @param timeOut Timeout in ms. TimeoutException will be thrown if this function times out. 60 */ waitForChanges(int count, int timeOut)61 public void waitForChanges(int count, int timeOut) throws Exception { 62 mNotificationSignal = new CountDownLatch(count); 63 // Signal that the test is now waiting for files. 64 mReadySignal.countDown(); 65 if (!mNotificationSignal.await(timeOut, TimeUnit.MILLISECONDS)) { 66 throw new TimeoutException("Timed out waiting for file operations to complete."); 67 } 68 } 69 70 @Override notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork)71 public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) { 72 // Wait until the test is ready to receive file notifications. 73 try { 74 mReadySignal.await(); 75 } catch (InterruptedException e) { 76 Log.d(TAG, "Interrupted while waiting for file copy readiness"); 77 Thread.currentThread().interrupt(); 78 } 79 if (DocumentsContract.isDocumentUri(mContext, uri)) { 80 Log.d(TAG, "Notification: " + uri); 81 // Watch for document URI change notifications - this signifies the end of a copy. 82 mNotificationSignal.countDown(); 83 } 84 } 85 }