Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/BlockingFileProviderTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/BlockingFileProviderTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/BlockingFileProviderTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c66a577662134f4e52cec180cef29c4d7647abf8 |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/BlockingFileProviderTest.java |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser; |
| + |
| +import android.content.Context; |
| +import android.net.Uri; |
| +import android.os.AsyncTask; |
| +import android.os.ParcelFileDescriptor; |
| +import android.test.suitebuilder.annotation.LargeTest; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.content.browser.test.NativeLibraryTestBase; |
| + |
| +import java.io.FileNotFoundException; |
| + |
| +/** |
| + * Tests working of BlockingFileProvider. |
| + * |
| + * The openFile should be blocked till notify is called. These tests can timeout if the notify does |
| + * not work correctly. |
| + */ |
| +public class BlockingFileProviderTest extends NativeLibraryTestBase { |
| + private ParcelFileDescriptor openFileFromProvider(Uri uri) { |
| + BlockingFileProvider provider = new BlockingFileProvider(); |
| + ParcelFileDescriptor file = null; |
| + try { |
| + provider.openFile(uri, "r"); |
| + } catch (FileNotFoundException e) { |
| + assert false : "Failed to open file."; |
| + } |
| + return file; |
| + } |
| + |
| + @SmallTest |
| + public void testOpenFileWhenReady() { |
| + Uri uri = BlockingFileProvider.generateUriAndBlockAccess(getInstrumentation().getContext()); |
| + BlockingFileProvider.notifyFileReady(uri, null); |
| + ParcelFileDescriptor file = openFileFromProvider(uri); |
| + // File should be null because the notify passes a null file uri. |
| + assertNull(file); |
| + } |
| + |
| + @LargeTest |
| + public void testBlockedOnAsyncNotify() { |
| + final Context context = getInstrumentation().getContext(); |
| + final Uri uri = BlockingFileProvider.generateUriAndBlockAccess(context); |
| + new AsyncTask<Void, Void, Void>() { |
| + @Override |
| + protected Void doInBackground(Void... params) { |
| + try { |
| + Thread.sleep(200); |
|
Ted C
2016/08/27 00:22:12
i think you can make this much shorter...we just w
ssid
2016/09/06 22:25:36
Done.
|
| + } catch (InterruptedException e) { |
| + // Ignore exception. |
| + } |
| + BlockingFileProvider.notifyFileReady(uri, null); |
| + return null; |
| + } |
| + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| + ParcelFileDescriptor file = openFileFromProvider(uri); |
| + // File should be null because the notify passes a null file uri. |
| + assertNull(file); |
| + } |
| + |
| + @LargeTest |
| + public void testFileChanged() { |
| + final Context context = getInstrumentation().getContext(); |
| + Uri uri1 = BlockingFileProvider.generateUriAndBlockAccess(context); |
| + final Uri uri2 = BlockingFileProvider.generateUriAndBlockAccess(context); |
| + new AsyncTask<Void, Void, Void>() { |
| + @Override |
| + protected Void doInBackground(Void... params) { |
| + try { |
| + Thread.sleep(200); |
|
Ted C
2016/08/27 00:22:12
same timing comment
ssid
2016/09/06 22:25:36
Done.
|
| + } catch (InterruptedException e) { |
| + // Ignore exception. |
| + } |
| + BlockingFileProvider.notifyFileReady(uri2, null); |
| + return null; |
| + } |
| + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| + |
| + // This should be unblocked irrespective of when the second notify is called. |
| + ParcelFileDescriptor file1 = openFileFromProvider(uri2); |
|
Ted C
2016/08/27 00:22:12
i find it odd that file1 == uri2, I'd recommend m
ssid
2016/09/06 22:25:36
Done.
|
| + assertNull(file1); |
| + |
| + // This should not be blocked even without a notify since file was changed. |
| + ParcelFileDescriptor file2 = openFileFromProvider(uri1); |
| + // File should be null because the notify passes a null file uri. |
| + assertNull(file2); |
| + } |
| +} |