From ef5e4d7630b5d80beb41c3191535d2d9421a153b Mon Sep 17 00:00:00 2001 From: Diksha Gohlyan Date: Fri, 8 May 2020 09:33:27 -0700 Subject: Add manifest file for only DocumentsUI res for tests 1. Move some functional tests from unit tests. 2. The manifest will be used by unit tests, manifest does not include the providers, because they conflict with the providers from real documentsui Test: atest DocumentsUIGoogleTests Fixes: 155692975 Change-Id: I97f2e0a7f601e7c01c7d36689540658c1930913a --- Android.bp | 23 ++ AndroidManifestForUnitTests.xml | 131 +++++++++ tests/Android.bp | 8 + .../android/documentsui/ArchivesProviderTest.java | 326 +++++++++++++++++++++ .../com/android/documentsui/PickActivityTest.java | 125 ++++++++ .../documentsui/ThemeOverlayManagerTest.java | 227 ++++++++++++++ .../com/android/documentsui/PickActivityTest.java | 125 -------- .../documentsui/archives/ArchivesProviderTest.java | 325 -------------------- .../documentsui/theme/ThemeOverlayManagerTest.java | 225 -------------- 9 files changed, 840 insertions(+), 675 deletions(-) create mode 100644 AndroidManifestForUnitTests.xml create mode 100644 tests/functional/com/android/documentsui/ArchivesProviderTest.java create mode 100644 tests/functional/com/android/documentsui/PickActivityTest.java create mode 100644 tests/functional/com/android/documentsui/ThemeOverlayManagerTest.java delete mode 100644 tests/unit/com/android/documentsui/PickActivityTest.java delete mode 100644 tests/unit/com/android/documentsui/archives/ArchivesProviderTest.java delete mode 100644 tests/unit/com/android/documentsui/theme/ThemeOverlayManagerTest.java diff --git a/Android.bp b/Android.bp index 04605702d..3494449cb 100644 --- a/Android.bp +++ b/Android.bp @@ -99,6 +99,29 @@ android_library { min_sdk_version: "29", } +android_library { + name: "DocumentsUI-res-lib-tests", + + manifest: "AndroidManifestForUnitTests.xml", + + static_libs: [ + "androidx.appcompat_appcompat", + "com.google.android.material_material", + ], + + resource_dirs: [ + "res", + ], + + aaptflags: [ + "--auto-add-overlay", + ], + + sdk_version: "system_current", + target_sdk_version: "29", + min_sdk_version: "29", +} + android_app { name: "DocumentsUI", diff --git a/AndroidManifestForUnitTests.xml b/AndroidManifestForUnitTests.xml new file mode 100644 index 000000000..717cddff1 --- /dev/null +++ b/AndroidManifestForUnitTests.xml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/Android.bp b/tests/Android.bp index 10393421c..e0b427de7 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -29,6 +29,14 @@ filegroup { ], } +filegroup { + name: "DocumentsUIUnitTests-srcs", + srcs: [ + "common/**/*.java", + "unit/**/*.java", + ], +} + android_library { name: "DocumentsUITests-res-lib", diff --git a/tests/functional/com/android/documentsui/ArchivesProviderTest.java b/tests/functional/com/android/documentsui/ArchivesProviderTest.java new file mode 100644 index 000000000..1a2333168 --- /dev/null +++ b/tests/functional/com/android/documentsui/ArchivesProviderTest.java @@ -0,0 +1,326 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.documentsui; + +import static android.content.ContentResolver.wrap; + +import static com.google.common.truth.Truth.assertThat; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; + +import android.content.ContentProviderClient; +import android.content.ContentResolver; +import android.content.Context; +import android.database.ContentObserver; +import android.database.Cursor; +import android.media.ExifInterface; +import android.net.Uri; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.provider.DocumentsContract; +import android.text.TextUtils; + +import androidx.test.InstrumentationRegistry; +import androidx.test.filters.MediumTest; +import androidx.test.runner.AndroidJUnit4; + +import com.android.documentsui.archives.ArchivesProvider; +import com.android.documentsui.archives.ResourcesProvider; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +@RunWith(AndroidJUnit4.class) +@MediumTest +public class ArchivesProviderTest { + + private Context mContext; + private ExecutorService mExecutor = null; + + @Before + public void setUp() throws Exception { + mContext = InstrumentationRegistry.getContext(); + mExecutor = Executors.newSingleThreadExecutor(); + } + + @After + public void tearDown() throws Exception { + mExecutor.shutdown(); + assertTrue(mExecutor.awaitTermination(3 /* timeout */, TimeUnit.SECONDS)); + } + + @Test + public void testQueryRoots() throws InterruptedException, RemoteException { + final ContentResolver resolver = mContext.getContentResolver(); + final Uri rootsUri = DocumentsContract.buildRootsUri(ArchivesProvider.AUTHORITY); + try (ContentProviderClient client = resolver.acquireUnstableContentProviderClient( + rootsUri)) { + Cursor cursor = client.query(rootsUri, null, null, null, null, null); + assertNotNull("Cursor must not be null.", cursor); + assertEquals(0, cursor.getCount()); + } + } + + @Test + public void testOpen_Success() throws InterruptedException { + final Uri sourceUri = DocumentsContract.buildDocumentUri( + ResourcesProvider.AUTHORITY, "archive.zip"); + final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, + ParcelFileDescriptor.MODE_READ_ONLY); + + final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( + ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); + + final ContentResolver resolver = mContext.getContentResolver(); + final CountDownLatch latch = new CountDownLatch(1); + + final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( + archiveUri); + ArchivesProvider.acquireArchive(client, archiveUri); + + { + final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); + assertNotNull("Cursor must not be null. File not found?", cursor); + + assertEquals(0, cursor.getCount()); + final Bundle extras = cursor.getExtras(); + assertEquals(true, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); + assertNull(extras.getString(DocumentsContract.EXTRA_ERROR)); + + final Uri notificationUri = cursor.getNotificationUri(); + assertNotNull(notificationUri); + + resolver.registerContentObserver(notificationUri, false, new ContentObserver(null) { + @Override + public void onChange(boolean selfChange, Uri uri) { + latch.countDown(); + } + }); + } + + latch.await(3, TimeUnit.SECONDS); + { + final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); + assertNotNull("Cursor must not be null. File not found?", cursor); + + assertEquals(3, cursor.getCount()); + final Bundle extras = cursor.getExtras(); + assertEquals(false, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); + assertNull(extras.getString(DocumentsContract.EXTRA_ERROR)); + } + + ArchivesProvider.releaseArchive(client, archiveUri); + client.release(); + } + + @Test + public void testOpen_Failure() throws InterruptedException { + final Uri sourceUri = DocumentsContract.buildDocumentUri( + ResourcesProvider.AUTHORITY, "broken.zip"); + final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, + ParcelFileDescriptor.MODE_READ_ONLY); + + final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( + ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); + + final ContentResolver resolver = mContext.getContentResolver(); + final CountDownLatch latch = new CountDownLatch(1); + + final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( + archiveUri); + ArchivesProvider.acquireArchive(client, archiveUri); + + { + // TODO: Close this and any other cursor in this file. + final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); + assertNotNull("Cursor must not be null. File not found?", cursor); + + assertEquals(0, cursor.getCount()); + final Bundle extras = cursor.getExtras(); + assertEquals(true, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); + assertNull(extras.getString(DocumentsContract.EXTRA_ERROR)); + + final Uri notificationUri = cursor.getNotificationUri(); + assertNotNull(notificationUri); + + resolver.registerContentObserver(notificationUri, false, new ContentObserver(null) { + @Override + public void onChange(boolean selfChange, Uri uri) { + latch.countDown(); + } + }); + } + + latch.await(3, TimeUnit.SECONDS); + { + final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); + assertNotNull("Cursor must not be null. File not found?", cursor); + + assertEquals(0, cursor.getCount()); + final Bundle extras = cursor.getExtras(); + assertEquals(false, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); + assertFalse(TextUtils.isEmpty(extras.getString(DocumentsContract.EXTRA_ERROR))); + } + + ArchivesProvider.releaseArchive(client, archiveUri); + client.release(); + } + + @Test + public void testOpen_ClosesOnRelease() throws InterruptedException { + final Uri sourceUri = DocumentsContract.buildDocumentUri( + ResourcesProvider.AUTHORITY, "archive.zip"); + final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, + ParcelFileDescriptor.MODE_READ_ONLY); + + final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( + ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); + + final ContentResolver resolver = mContext.getContentResolver(); + + final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( + archiveUri); + + // Acquire twice to ensure that the refcount works correctly. + ArchivesProvider.acquireArchive(client, archiveUri); + ArchivesProvider.acquireArchive(client, archiveUri); + + { + final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); + assertNotNull("Cursor must not be null. File not found?", cursor); + } + + ArchivesProvider.releaseArchive(client, archiveUri); + + { + final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); + assertNotNull("Cursor must not be null. File not found?", cursor); + } + + ArchivesProvider.releaseArchive(client, archiveUri); + + try { + resolver.query(childrenUri, null, null, null, null, null); + fail("The archive was expected to be invalid on the last release call."); + } catch (IllegalStateException e) { + // Expected. + } + + client.release(); + } + + @Test + public void testNoNotificationAfterAllReleased() throws InterruptedException, RemoteException { + final Uri sourceUri = DocumentsContract.buildDocumentUri( + ResourcesProvider.AUTHORITY, "archive.zip"); + final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, + ParcelFileDescriptor.MODE_READ_ONLY); + + final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( + ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); + + final ContentResolver resolver = mContext.getContentResolver(); + + final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( + archiveUri); + + ArchivesProvider.acquireArchive(client, archiveUri); + final Cursor cursor = client.query(childrenUri, null, null, null, null, null); + final Bundle extra = cursor.getExtras(); + assertTrue(extra.getBoolean(DocumentsContract.EXTRA_LOADING, false)); + final Uri notificationUri = cursor.getNotificationUri(); + + ArchivesProvider.releaseArchive(client, archiveUri); + final CountDownLatch latch = new CountDownLatch(1); + resolver.registerContentObserver(notificationUri, false, new ContentObserver(null) { + @Override + public void onChange(boolean selfChange, Uri uri) { + latch.countDown(); + } + }); + + // Assert that there is no notification if no one has acquired this archive and this wait + // times out. + assertFalse(latch.await(1, TimeUnit.SECONDS)); + + client.release(); + } + + private void getDocumentMetadata_byDocumentId_shouldMatchSize(String documentId) + throws Exception { + final Uri sourceUri = DocumentsContract.buildDocumentUri( + ResourcesProvider.AUTHORITY, documentId); + final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, + ParcelFileDescriptor.MODE_READ_ONLY); + + final ContentResolver resolver = mContext.getContentResolver(); + final ContentProviderClient client = + resolver.acquireUnstableContentProviderClient(archiveUri); + + ArchivesProvider.acquireArchive(client, archiveUri); + + Uri archivedImageUri = Uri.parse( + "content://com.android.documentsui.archives/document/content%3A%2F%2F" + + "com.android.documentsui.archives.resourcesprovider%2F" + + "document%2F" + documentId + "%23268435456%23%2Ffreddy.jpg"); + + Bundle metadata = DocumentsContract.getDocumentMetadata(wrap(client), archivedImageUri); + assertNotNull(metadata); + Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF); + assertNotNull(exif); + + assertThat(exif.getInt(ExifInterface.TAG_IMAGE_WIDTH)).isEqualTo(3036); + assertThat(exif.getInt(ExifInterface.TAG_IMAGE_LENGTH)).isEqualTo(4048); + assertThat(exif.getString(ExifInterface.TAG_MODEL)).isEqualTo("Pixel"); + + ArchivesProvider.releaseArchive(client, archiveUri); + client.close(); + } + + @Test + public void testGetDocumentMetadata() throws Exception { + getDocumentMetadata_byDocumentId_shouldMatchSize("images.zip"); + } + + @Test + public void getDocumentMetadata_sevenZFile_shouldMatchSize() throws Exception { + getDocumentMetadata_byDocumentId_shouldMatchSize("images.7z"); + } + + @Test + public void getDocumentMetadata_tgz_shouldMatchSize() throws Exception { + getDocumentMetadata_byDocumentId_shouldMatchSize("images.tgz"); + } + + @Test + public void getDocumentMetadata_tar_shouldMatchSize() throws Exception { + getDocumentMetadata_byDocumentId_shouldMatchSize("images.tar"); + } +} diff --git a/tests/functional/com/android/documentsui/PickActivityTest.java b/tests/functional/com/android/documentsui/PickActivityTest.java new file mode 100644 index 000000000..cd042b1f5 --- /dev/null +++ b/tests/functional/com/android/documentsui/PickActivityTest.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.documentsui; + +import static com.android.documentsui.base.Providers.AUTHORITY_STORAGE; + +import static com.google.common.truth.Truth.assertThat; + +import android.app.Activity; +import android.app.Instrumentation; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.SystemClock; +import android.provider.DocumentsContract; + +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.ActivityTestRule; + +import com.android.documentsui.base.DocumentInfo; +import com.android.documentsui.picker.PickActivity; +import com.android.documentsui.testing.TestProvidersAccess; +import com.android.documentsui.ui.TestDialogController; +import com.android.documentsui.util.VersionUtils; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +@SmallTest +public class PickActivityTest { + + private static final String RESULT_EXTRA = "test_result_extra"; + private static final String RESULT_DATA = "123321"; + + private Context mTargetContext; + private Intent mIntentGetContent; + private TestDialogController mTestDialogs; + + @Rule + public final ActivityTestRule mRule = + new ActivityTestRule<>(PickActivity.class, false, false); + + @Before + public void setUp() throws Exception { + mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + mIntentGetContent = new Intent(Intent.ACTION_GET_CONTENT); + mIntentGetContent.addCategory(Intent.CATEGORY_OPENABLE); + mIntentGetContent.setType("*/*"); + Uri hintUri = DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary"); + mIntentGetContent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, hintUri); + + mTestDialogs = new TestDialogController(); + } + + @Test + public void testOnDocumentPicked() { + DocumentInfo doc = new DocumentInfo(); + doc.userId = TestProvidersAccess.USER_ID; + doc.authority = "authority"; + doc.documentId = "documentId"; + + PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); + pickActivity.mState.canShareAcrossProfile = true; + pickActivity.onDocumentPicked(doc); + SystemClock.sleep(3000); + + Instrumentation.ActivityResult result = mRule.getActivityResult(); + assertThat(pickActivity.isFinishing()).isTrue(); + assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); + assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); + } + + @Test + public void testOnDocumentPicked_otherUser() { + if (VersionUtils.isAtLeastR()) { + DocumentInfo doc = new DocumentInfo(); + doc.userId = TestProvidersAccess.OtherUser.USER_ID; + doc.authority = "authority"; + doc.documentId = "documentId"; + + PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); + pickActivity.mState.canShareAcrossProfile = true; + pickActivity.onDocumentPicked(doc); + SystemClock.sleep(3000); + + Instrumentation.ActivityResult result = mRule.getActivityResult(); + assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); + assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); + } + } + + @Test + public void testOnDocumentPicked_otherUserDoesNotReturn() { + DocumentInfo doc = new DocumentInfo(); + doc.userId = TestProvidersAccess.OtherUser.USER_ID; + doc.authority = "authority"; + doc.documentId = "documentId"; + + PickActivity pickActivity = mRule.launchActivity(mIntentGetContent); + pickActivity.mState.canShareAcrossProfile = false; + pickActivity.getInjector().dialogs = mTestDialogs; + pickActivity.onDocumentPicked(doc); + SystemClock.sleep(3000); + + assertThat(pickActivity.isFinishing()).isFalse(); + mTestDialogs.assertActionNotAllowedShown(); + } +} diff --git a/tests/functional/com/android/documentsui/ThemeOverlayManagerTest.java b/tests/functional/com/android/documentsui/ThemeOverlayManagerTest.java new file mode 100644 index 000000000..93ff89f05 --- /dev/null +++ b/tests/functional/com/android/documentsui/ThemeOverlayManagerTest.java @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.documentsui; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.content.om.OverlayInfo; +import android.content.om.OverlayManager; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.os.UserHandle; + +import androidx.core.util.Consumer; +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.runner.AndroidJUnit4; + +import com.android.documentsui.theme.ThemeOverlayManager; + +import com.google.common.collect.Lists; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class ThemeOverlayManagerTest { + private static final String TEST_DISABLED_PREFIX = "com.example."; + private static final String TEST_ENABLED_PREFIX = "com.example.enabled."; + private static final String TEST_OVERLAY_PACKAGE = "test.overlay"; + private static final String TEST_TARGET_PACKAGE = "test.target"; + + @Mock + OverlayManager mOverlayManager; + + @Mock + PackageManager mPackageManager; + + Consumer mCallback; + Context mContext; + CountDownLatch mLatch; + ThemeOverlayManager mThemeOverlayManager; + UserHandle mUserHandle; + + @Before + public void setUp() throws Exception { + InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( + "android.permission.CHANGE_OVERLAY_PACKAGES"); + + MockitoAnnotations.initMocks(this); + mContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + mLatch = new CountDownLatch(1); + mUserHandle = UserHandle.of(UserHandle.myUserId()); + mCallback = result -> mLatch.countDown(); + + when(mOverlayManager.getOverlayInfosForTarget(getEnabledTargetPackageId(), + mUserHandle)).thenReturn(Lists.newArrayList( + createOverlayInfo(getOverlayPackageId(), getEnabledTargetPackageId(), true))); + + when(mOverlayManager.getOverlayInfosForTarget(getDisabledTargetPackageId(), + mUserHandle)).thenReturn(Lists.newArrayList( + createOverlayInfo(getOverlayPackageId(), getDisabledTargetPackageId(), false))); + } + + @Test + public void testApplyOverlays_shouldSetEnabled() throws Exception { + final boolean enabled = true; + + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getDisabledTargetPackageId()); + + mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); + mLatch.await(5, TimeUnit.SECONDS); + + verify(mOverlayManager, times(1)).setEnabled(getOverlayPackageId(), enabled, + mUserHandle); + } + + @Test + public void testApplyOverlays_shouldGetOverlayInfo() throws Exception { + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getEnabledTargetPackageId()); + + mThemeOverlayManager.applyOverlays(mContext, true /* enabled */, mCallback); + mLatch.await(5, TimeUnit.SECONDS); + + verify(mOverlayManager, times(1)).getOverlayInfosForTarget(getEnabledTargetPackageId(), + mUserHandle); + } + + @Test + public void testApplyOverlays_shouldCheckEnabled_beforeSetEnabled() { + final boolean enabled = true; + + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getEnabledTargetPackageId()); + + mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); + + verify(mOverlayManager, never()).setEnabled(getOverlayPackageId(), enabled, + mUserHandle); + } + + @Test + public void testDefaultDisabled_applyOverlays_shouldEnabled() throws Exception { + final boolean enabled = true; + + assertThat(mOverlayManager.getOverlayInfosForTarget(getDisabledTargetPackageId(), + mUserHandle).get(0).isEnabled()).isEqualTo(!enabled); + + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getDisabledTargetPackageId()); + + mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); + mLatch.await(5, TimeUnit.SECONDS); + + verify(mOverlayManager, times(1)).setEnabled(getOverlayPackageId(), enabled, + mUserHandle); + } + + @Test + public void testDefaultEnabled_applyOverlays_shouldDisabled() throws Exception { + final boolean enabled = false; + + assertThat(mOverlayManager.getOverlayInfosForTarget(getEnabledTargetPackageId(), + mUserHandle).get(0).isEnabled()).isEqualTo(!enabled); + + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getEnabledTargetPackageId()); + + mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); + mLatch.await(5, TimeUnit.SECONDS); + + verify(mOverlayManager, times(1)).setEnabled(getOverlayPackageId(), enabled, + mUserHandle); + } + + @Test + public void testDefaultEnabled_launchDocumentsUI_shouldSuccess() throws Exception { + final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + final Activity activity = + InstrumentationRegistry.getInstrumentation().startActivitySync(intent); + + assertThat(activity).isNotNull(); + + if (activity != null) { + activity.finish(); + } + } + + @Test + public void testGetValidOverlay_system_valid() throws Exception { + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getEnabledTargetPackageId()); + + ApplicationInfo ai = mock(ApplicationInfo.class); + ai.flags |= ApplicationInfo.FLAG_SYSTEM; + when(mPackageManager.getApplicationInfo(getOverlayPackageId(), 0)).thenReturn(ai); + + assertThat(mThemeOverlayManager.getValidOverlay(mPackageManager)).isEqualTo( + mOverlayManager.getOverlayInfosForTarget(getEnabledTargetPackageId(), + mUserHandle).iterator().next()); + } + + @Test + public void testGetValidOverlay_data_invalid() throws Exception { + mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, + getEnabledTargetPackageId()); + + ApplicationInfo ai = mock(ApplicationInfo.class); + String path = "/data/app/DocumentUIOverlay/DocumentUIOverlay.apk"; + ai.sourceDir = path; + when(mPackageManager.getApplicationInfo(getOverlayPackageId(), 0)).thenReturn(ai); + + assertThat(mThemeOverlayManager.getValidOverlay(mPackageManager)).isEqualTo(null); + } + + private static OverlayInfo createOverlayInfo(String packageName, String targetPackageName, + boolean enabled) { + return new OverlayInfo(packageName, targetPackageName, null, null, "", + enabled ? OverlayInfo.STATE_ENABLED : OverlayInfo.STATE_DISABLED, 0, 0, false); + } + + private static String getDisabledTargetPackageId() { + return TEST_DISABLED_PREFIX + TEST_TARGET_PACKAGE; + } + + private static String getEnabledTargetPackageId() { + return TEST_ENABLED_PREFIX + TEST_TARGET_PACKAGE; + } + + private static String getOverlayPackageId() { + return TEST_DISABLED_PREFIX + TEST_OVERLAY_PACKAGE; + } + +} diff --git a/tests/unit/com/android/documentsui/PickActivityTest.java b/tests/unit/com/android/documentsui/PickActivityTest.java deleted file mode 100644 index 1fbc20d9b..000000000 --- a/tests/unit/com/android/documentsui/PickActivityTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.documentsui; - -import static com.android.documentsui.base.Providers.AUTHORITY_STORAGE; - -import static com.google.common.truth.Truth.assertThat; - -import android.app.Activity; -import android.app.Instrumentation; -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.SystemClock; -import android.provider.DocumentsContract; - -import androidx.test.filters.SmallTest; -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.rule.ActivityTestRule; - -import com.android.documentsui.base.DocumentInfo; -import com.android.documentsui.picker.PickActivity; -import com.android.documentsui.testing.TestProvidersAccess; -import com.android.documentsui.ui.TestDialogController; -import com.android.documentsui.util.VersionUtils; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; - -@SmallTest -public class PickActivityTest { - - private static final String RESULT_EXTRA = "test_result_extra"; - private static final String RESULT_DATA = "123321"; - - private Context mTargetContext; - private Intent intentGetContent; - private TestDialogController testDialogs; - - @Rule - public final ActivityTestRule mRule = - new ActivityTestRule<>(PickActivity.class, false, false); - - @Before - public void setUp() throws Exception { - mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); - - intentGetContent = new Intent(Intent.ACTION_GET_CONTENT); - intentGetContent.addCategory(Intent.CATEGORY_OPENABLE); - intentGetContent.setType("*/*"); - Uri hintUri = DocumentsContract.buildRootUri(AUTHORITY_STORAGE, "primary"); - intentGetContent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, hintUri); - - testDialogs = new TestDialogController(); - } - - @Test - public void testOnDocumentPicked() { - DocumentInfo doc = new DocumentInfo(); - doc.userId = TestProvidersAccess.USER_ID; - doc.authority = "authority"; - doc.documentId = "documentId"; - - PickActivity pickActivity = mRule.launchActivity(intentGetContent); - pickActivity.mState.canShareAcrossProfile = true; - pickActivity.onDocumentPicked(doc); - SystemClock.sleep(3000); - - Instrumentation.ActivityResult result = mRule.getActivityResult(); - assertThat(pickActivity.isFinishing()).isTrue(); - assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); - assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); - } - - @Test - public void testOnDocumentPicked_otherUser() { - if (VersionUtils.isAtLeastR()) { - DocumentInfo doc = new DocumentInfo(); - doc.userId = TestProvidersAccess.OtherUser.USER_ID; - doc.authority = "authority"; - doc.documentId = "documentId"; - - PickActivity pickActivity = mRule.launchActivity(intentGetContent); - pickActivity.mState.canShareAcrossProfile = true; - pickActivity.onDocumentPicked(doc); - SystemClock.sleep(3000); - - Instrumentation.ActivityResult result = mRule.getActivityResult(); - assertThat(result.getResultCode()).isEqualTo(Activity.RESULT_OK); - assertThat(result.getResultData().getData()).isEqualTo(doc.getDocumentUri()); - } - } - - @Test - public void testOnDocumentPicked_otherUserDoesNotReturn() { - DocumentInfo doc = new DocumentInfo(); - doc.userId = TestProvidersAccess.OtherUser.USER_ID; - doc.authority = "authority"; - doc.documentId = "documentId"; - - PickActivity pickActivity = mRule.launchActivity(intentGetContent); - pickActivity.mState.canShareAcrossProfile = false; - pickActivity.getInjector().dialogs = testDialogs; - pickActivity.onDocumentPicked(doc); - SystemClock.sleep(3000); - - assertThat(pickActivity.isFinishing()).isFalse(); - testDialogs.assertActionNotAllowedShown(); - } -} diff --git a/tests/unit/com/android/documentsui/archives/ArchivesProviderTest.java b/tests/unit/com/android/documentsui/archives/ArchivesProviderTest.java deleted file mode 100644 index 484342525..000000000 --- a/tests/unit/com/android/documentsui/archives/ArchivesProviderTest.java +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.documentsui.archives; - -import static android.content.ContentResolver.wrap; - -import static com.google.common.truth.Truth.assertThat; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; - -import android.content.ContentProviderClient; -import android.content.ContentResolver; -import android.content.Context; -import android.database.ContentObserver; -import android.database.Cursor; -import android.media.ExifInterface; -import android.net.Uri; -import android.os.Bundle; -import android.os.FileUtils; -import android.os.ParcelFileDescriptor; -import android.os.RemoteException; -import android.provider.DocumentsContract; -import android.text.TextUtils; -import android.util.Log; - -import androidx.test.InstrumentationRegistry; -import androidx.test.filters.MediumTest; -import androidx.test.runner.AndroidJUnit4; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -@RunWith(AndroidJUnit4.class) -@MediumTest -public class ArchivesProviderTest { - - private Context mContext; - private ExecutorService mExecutor = null; - - @Before - public void setUp() throws Exception { - mContext = InstrumentationRegistry.getContext(); - mExecutor = Executors.newSingleThreadExecutor(); - } - - @After - public void tearDown() throws Exception { - mExecutor.shutdown(); - assertTrue(mExecutor.awaitTermination(3 /* timeout */, TimeUnit.SECONDS)); - } - - @Test - public void testQueryRoots() throws InterruptedException, RemoteException { - final ContentResolver resolver = mContext.getContentResolver(); - final Uri rootsUri = DocumentsContract.buildRootsUri(ArchivesProvider.AUTHORITY); - try (final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( - rootsUri)) { - final Cursor cursor = client.query(rootsUri, null, null, null, null, null); - assertNotNull("Cursor must not be null.", cursor); - assertEquals(0, cursor.getCount()); - } - } - - @Test - public void testOpen_Success() throws InterruptedException { - final Uri sourceUri = DocumentsContract.buildDocumentUri( - ResourcesProvider.AUTHORITY, "archive.zip"); - final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, - ParcelFileDescriptor.MODE_READ_ONLY); - - final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( - ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); - - final ContentResolver resolver = mContext.getContentResolver(); - final CountDownLatch latch = new CountDownLatch(1); - - final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( - archiveUri); - ArchivesProvider.acquireArchive(client, archiveUri); - - { - final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); - assertNotNull("Cursor must not be null. File not found?", cursor); - - assertEquals(0, cursor.getCount()); - final Bundle extras = cursor.getExtras(); - assertEquals(true, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); - assertNull(extras.getString(DocumentsContract.EXTRA_ERROR)); - - final Uri notificationUri = cursor.getNotificationUri(); - assertNotNull(notificationUri); - - resolver.registerContentObserver(notificationUri, false, new ContentObserver(null) { - @Override - public void onChange(boolean selfChange, Uri uri) { - latch.countDown(); - } - }); - } - - latch.await(3, TimeUnit.SECONDS); - { - final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); - assertNotNull("Cursor must not be null. File not found?", cursor); - - assertEquals(3, cursor.getCount()); - final Bundle extras = cursor.getExtras(); - assertEquals(false, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); - assertNull(extras.getString(DocumentsContract.EXTRA_ERROR)); - } - - ArchivesProvider.releaseArchive(client, archiveUri); - client.release(); - } - - @Test - public void testOpen_Failure() throws InterruptedException { - final Uri sourceUri = DocumentsContract.buildDocumentUri( - ResourcesProvider.AUTHORITY, "broken.zip"); - final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, - ParcelFileDescriptor.MODE_READ_ONLY); - - final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( - ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); - - final ContentResolver resolver = mContext.getContentResolver(); - final CountDownLatch latch = new CountDownLatch(1); - - final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( - archiveUri); - ArchivesProvider.acquireArchive(client, archiveUri); - - { - // TODO: Close this and any other cursor in this file. - final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); - assertNotNull("Cursor must not be null. File not found?", cursor); - - assertEquals(0, cursor.getCount()); - final Bundle extras = cursor.getExtras(); - assertEquals(true, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); - assertNull(extras.getString(DocumentsContract.EXTRA_ERROR)); - - final Uri notificationUri = cursor.getNotificationUri(); - assertNotNull(notificationUri); - - resolver.registerContentObserver(notificationUri, false, new ContentObserver(null) { - @Override - public void onChange(boolean selfChange, Uri uri) { - latch.countDown(); - } - }); - } - - latch.await(3, TimeUnit.SECONDS); - { - final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); - assertNotNull("Cursor must not be null. File not found?", cursor); - - assertEquals(0, cursor.getCount()); - final Bundle extras = cursor.getExtras(); - assertEquals(false, extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)); - assertFalse(TextUtils.isEmpty(extras.getString(DocumentsContract.EXTRA_ERROR))); - } - - ArchivesProvider.releaseArchive(client, archiveUri); - client.release(); - } - - @Test - public void testOpen_ClosesOnRelease() throws InterruptedException { - final Uri sourceUri = DocumentsContract.buildDocumentUri( - ResourcesProvider.AUTHORITY, "archive.zip"); - final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, - ParcelFileDescriptor.MODE_READ_ONLY); - - final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( - ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); - - final ContentResolver resolver = mContext.getContentResolver(); - - final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( - archiveUri); - - // Acquire twice to ensure that the refcount works correctly. - ArchivesProvider.acquireArchive(client, archiveUri); - ArchivesProvider.acquireArchive(client, archiveUri); - - { - final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); - assertNotNull("Cursor must not be null. File not found?", cursor); - } - - ArchivesProvider.releaseArchive(client, archiveUri); - - { - final Cursor cursor = resolver.query(childrenUri, null, null, null, null, null); - assertNotNull("Cursor must not be null. File not found?", cursor); - } - - ArchivesProvider.releaseArchive(client, archiveUri); - - try { - resolver.query(childrenUri, null, null, null, null, null); - fail("The archive was expected to be invalid on the last release call."); - } catch (IllegalStateException e) { - // Expected. - } - - client.release(); - } - - @Test - public void testNoNotificationAfterAllReleased() throws InterruptedException, RemoteException { - final Uri sourceUri = DocumentsContract.buildDocumentUri( - ResourcesProvider.AUTHORITY, "archive.zip"); - final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, - ParcelFileDescriptor.MODE_READ_ONLY); - - final Uri childrenUri = DocumentsContract.buildChildDocumentsUri( - ArchivesProvider.AUTHORITY, DocumentsContract.getDocumentId(archiveUri)); - - final ContentResolver resolver = mContext.getContentResolver(); - - final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( - archiveUri); - - ArchivesProvider.acquireArchive(client, archiveUri); - final Cursor cursor = client.query(childrenUri, null, null, null, null, null); - final Bundle extra = cursor.getExtras(); - assertTrue(extra.getBoolean(DocumentsContract.EXTRA_LOADING, false)); - final Uri notificationUri = cursor.getNotificationUri(); - - ArchivesProvider.releaseArchive(client, archiveUri); - final CountDownLatch latch = new CountDownLatch(1); - resolver.registerContentObserver(notificationUri, false, new ContentObserver(null) { - @Override - public void onChange(boolean selfChange, Uri uri) { - latch.countDown(); - } - }); - - // Assert that there is no notification if no one has acquired this archive and this wait - // times out. - assertFalse(latch.await(1, TimeUnit.SECONDS)); - - client.release(); - } - - private void getDocumentMetadata_byDocumentId_shouldMatchSize(String documentId) - throws Exception { - final Uri sourceUri = DocumentsContract.buildDocumentUri( - ResourcesProvider.AUTHORITY, documentId); - final Uri archiveUri = ArchivesProvider.buildUriForArchive(sourceUri, - ParcelFileDescriptor.MODE_READ_ONLY); - - final ContentResolver resolver = mContext.getContentResolver(); - final ContentProviderClient client = - resolver.acquireUnstableContentProviderClient(archiveUri); - - ArchivesProvider.acquireArchive(client, archiveUri); - - Uri archivedImageUri = Uri.parse( - "content://com.android.documentsui.archives/document/content%3A%2F%2F" - + "com.android.documentsui.archives.resourcesprovider%2F" - + "document%2F" + documentId + "%23268435456%23%2Ffreddy.jpg"); - - Bundle metadata = DocumentsContract.getDocumentMetadata(wrap(client), archivedImageUri); - assertNotNull(metadata); - Bundle exif = metadata.getBundle(DocumentsContract.METADATA_EXIF); - assertNotNull(exif); - - assertThat(exif.getInt(ExifInterface.TAG_IMAGE_WIDTH)).isEqualTo(3036); - assertThat(exif.getInt(ExifInterface.TAG_IMAGE_LENGTH)).isEqualTo(4048); - assertThat(exif.getString(ExifInterface.TAG_MODEL)).isEqualTo("Pixel"); - - ArchivesProvider.releaseArchive(client, archiveUri); - client.close(); - } - - @Test - public void testGetDocumentMetadata() throws Exception { - getDocumentMetadata_byDocumentId_shouldMatchSize("images.zip"); - } - - @Test - public void getDocumentMetadata_sevenZFile_shouldMatchSize() throws Exception { - getDocumentMetadata_byDocumentId_shouldMatchSize("images.7z"); - } - - @Test - public void getDocumentMetadata_tgz_shouldMatchSize() throws Exception { - getDocumentMetadata_byDocumentId_shouldMatchSize("images.tgz"); - } - - @Test - public void getDocumentMetadata_tar_shouldMatchSize() throws Exception { - getDocumentMetadata_byDocumentId_shouldMatchSize("images.tar"); - } -} diff --git a/tests/unit/com/android/documentsui/theme/ThemeOverlayManagerTest.java b/tests/unit/com/android/documentsui/theme/ThemeOverlayManagerTest.java deleted file mode 100644 index 2e5e1a700..000000000 --- a/tests/unit/com/android/documentsui/theme/ThemeOverlayManagerTest.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.documentsui.theme; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.content.om.OverlayInfo; -import android.content.om.OverlayManager; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.os.UserHandle; - -import androidx.core.util.Consumer; -import androidx.test.filters.SmallTest; -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.runner.AndroidJUnit4; - -import com.google.common.collect.Lists; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -@SmallTest -@RunWith(AndroidJUnit4.class) -public class ThemeOverlayManagerTest { - private static final String TEST_DISABLED_PREFIX = "com.example."; - private static final String TEST_ENABLED_PREFIX = "com.example.enabled."; - private static final String TEST_OVERLAY_PACKAGE = "test.overlay"; - private static final String TEST_TARGET_PACKAGE = "test.target"; - - @Mock - OverlayManager mOverlayManager; - - @Mock - PackageManager mPackageManager; - - Consumer mCallback; - Context mContext; - CountDownLatch mLatch; - ThemeOverlayManager mThemeOverlayManager; - UserHandle mUserHandle; - - @Before - public void setUp() throws Exception { - InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity( - "android.permission.CHANGE_OVERLAY_PACKAGES"); - - MockitoAnnotations.initMocks(this); - mContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); - mLatch = new CountDownLatch(1); - mUserHandle = UserHandle.of(UserHandle.myUserId()); - mCallback = result -> mLatch.countDown(); - - when(mOverlayManager.getOverlayInfosForTarget(getEnabledTargetPackageId(), - mUserHandle)).thenReturn(Lists.newArrayList( - createOverlayInfo(getOverlayPackageId(), getEnabledTargetPackageId(), true))); - - when(mOverlayManager.getOverlayInfosForTarget(getDisabledTargetPackageId(), - mUserHandle)).thenReturn(Lists.newArrayList( - createOverlayInfo(getOverlayPackageId(), getDisabledTargetPackageId(), false))); - } - - @Test - public void testApplyOverlays_shouldSetEnabled() throws Exception { - final boolean enabled = true; - - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getDisabledTargetPackageId()); - - mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); - mLatch.await(5, TimeUnit.SECONDS); - - verify(mOverlayManager, times(1)).setEnabled(getOverlayPackageId(), enabled, - mUserHandle); - } - - @Test - public void testApplyOverlays_shouldGetOverlayInfo() throws Exception { - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getEnabledTargetPackageId()); - - mThemeOverlayManager.applyOverlays(mContext, true /* enabled */, mCallback); - mLatch.await(5, TimeUnit.SECONDS); - - verify(mOverlayManager, times(1)).getOverlayInfosForTarget(getEnabledTargetPackageId(), - mUserHandle); - } - - @Test - public void testApplyOverlays_shouldCheckEnabled_beforeSetEnabled() { - final boolean enabled = true; - - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getEnabledTargetPackageId()); - - mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); - - verify(mOverlayManager, never()).setEnabled(getOverlayPackageId(), enabled, - mUserHandle); - } - - @Test - public void testDefaultDisabled_applyOverlays_shouldEnabled() throws Exception { - final boolean enabled = true; - - assertThat(mOverlayManager.getOverlayInfosForTarget(getDisabledTargetPackageId(), - mUserHandle).get(0).isEnabled()).isEqualTo(!enabled); - - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getDisabledTargetPackageId()); - - mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); - mLatch.await(5, TimeUnit.SECONDS); - - verify(mOverlayManager, times(1)).setEnabled(getOverlayPackageId(), enabled, - mUserHandle); - } - - @Test - public void testDefaultEnabled_applyOverlays_shouldDisabled() throws Exception { - final boolean enabled = false; - - assertThat(mOverlayManager.getOverlayInfosForTarget(getEnabledTargetPackageId(), - mUserHandle).get(0).isEnabled()).isEqualTo(!enabled); - - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getEnabledTargetPackageId()); - - mThemeOverlayManager.applyOverlays(mContext, enabled, mCallback); - mLatch.await(5, TimeUnit.SECONDS); - - verify(mOverlayManager, times(1)).setEnabled(getOverlayPackageId(), enabled, - mUserHandle); - } - - @Test - public void testDefaultEnabled_launchDocumentsUI_shouldSuccess() throws Exception { - final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - - final Activity activity = - InstrumentationRegistry.getInstrumentation().startActivitySync(intent); - - assertThat(activity).isNotNull(); - - if (activity != null) { - activity.finish(); - } - } - - @Test - public void testGetValidOverlay_system_valid() throws Exception { - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getEnabledTargetPackageId()); - - ApplicationInfo ai = mock(ApplicationInfo.class); - ai.flags |= ApplicationInfo.FLAG_SYSTEM; - when(mPackageManager.getApplicationInfo(getOverlayPackageId(), 0)).thenReturn(ai); - - assertThat(mThemeOverlayManager.getValidOverlay(mPackageManager)).isEqualTo( - mOverlayManager.getOverlayInfosForTarget(getEnabledTargetPackageId(), - mUserHandle).iterator().next()); - } - - @Test - public void testGetValidOverlay_data_invalid() throws Exception { - mThemeOverlayManager = new ThemeOverlayManager(mOverlayManager, - getEnabledTargetPackageId()); - - ApplicationInfo ai = mock(ApplicationInfo.class); - String path = "/data/app/DocumentUIOverlay/DocumentUIOverlay.apk"; - ai.sourceDir = path; - when(mPackageManager.getApplicationInfo(getOverlayPackageId(), 0)).thenReturn(ai); - - assertThat(mThemeOverlayManager.getValidOverlay(mPackageManager)).isEqualTo(null); - } - - private static OverlayInfo createOverlayInfo(String packageName, String targetPackageName, - boolean enabled) { - return new OverlayInfo(packageName, targetPackageName, null, null, "", - enabled ? OverlayInfo.STATE_ENABLED : OverlayInfo.STATE_DISABLED, 0, 0, false); - } - - private static String getDisabledTargetPackageId() { - return TEST_DISABLED_PREFIX + TEST_TARGET_PACKAGE; - } - - private static String getEnabledTargetPackageId() { - return TEST_ENABLED_PREFIX + TEST_TARGET_PACKAGE; - } - - private static String getOverlayPackageId() { - return TEST_DISABLED_PREFIX + TEST_OVERLAY_PACKAGE; - } - -} -- cgit v1.2.3-59-g8ed1b