Back to project page android-memento.
The source code is released under:
Apache License
If you think the Android project android-memento listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * android-memento-lib https://github.com/twofortyfouram/android-memento * Copyright 2014 two forty four a.m. LLC *//w ww .j a v a 2 s . c o m * 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.twofortyfouram.memento.provider; import com.twofortyfouram.test.context.ReceiverContextWrapper; import com.twofortyfouram.test.context.ReceiverContextWrapper.SentIntent; import com.twofortyfouram.spackle.util.ThreadUtil; import com.twofortyfouram.spackle.util.ThreadUtil.ThreadPriority; import android.content.ContentResolver; import android.content.Intent; import android.database.ContentObserver; import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; import android.support.annotation.NonNull; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.SmallTest; import android.text.format.DateUtils; import java.util.Collection; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * Tests {@link ContentChangeNotificationQueue}. */ public final class ContentChangeNotificationQueueTest extends AndroidTestCase { @SmallTest public void testIsBatch_notify() { final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); assertFalse(queue.isBatch()); queue.startBatch(); assertTrue(queue.isBatch()); queue.endBatch(true); assertFalse(queue.isBatch()); } @SmallTest public void testIsBatch_dont_notify() { final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); assertFalse(queue.isBatch()); queue.startBatch(); assertTrue(queue.isBatch()); queue.endBatch(false); assertFalse(queue.isBatch()); } @SmallTest public void testStartBatch_state_checking() { final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); assertFalse(queue.isBatch()); queue.startBatch(); assertTrue(queue.isBatch()); try { queue.startBatch(); fail(); } catch (final IllegalStateException e) { // Expected exception } } @SmallTest public void testEndBatch_state_checking_notify() { final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); try { queue.endBatch(true); fail(); } catch (final IllegalStateException e) { // Expected exception } } @SmallTest public void testEndBatch_state_checking_dontnotify() { final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); try { queue.endBatch(false); fail(); } catch (final IllegalStateException e) { // Expected exception } } @SmallTest public void testOnContentChanged_no_batch() throws InterruptedException { final Uri uri = buildUri(); final HandlerThread thread = ThreadUtil.newHandlerThread(getName(), ThreadPriority.DEFAULT); try { final CountDownLatch latch = new CountDownLatch(1); final ContentObserver observer = new ContentObserver(new Handler(thread.getLooper())) { @Override public void onChange(final boolean selfChange) { super.onChange(selfChange); latch.countDown(); } }; getContext().getContentResolver().registerContentObserver(uri, true, observer); final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); queue.onContentChanged(uri); assertTrue(latch.await(1 * DateUtils.SECOND_IN_MILLIS, TimeUnit.MILLISECONDS)); } finally { thread.quit(); } } @MediumTest public void testOnContentChanged_batch_notify() throws InterruptedException { final Uri uri = buildUri(); final HandlerThread thread = ThreadUtil.newHandlerThread(getName(), ThreadPriority.DEFAULT); try { final CountDownLatch latch = new CountDownLatch(1); final ContentObserver observer = new ContentObserver(new Handler(thread.getLooper())) { @Override public void onChange(final boolean selfChange) { super.onChange(selfChange); latch.countDown(); } }; getContext().getContentResolver().registerContentObserver(uri, true, observer); final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); queue.startBatch(); queue.onContentChanged(uri); assertFalse(latch.await(500, TimeUnit.MILLISECONDS)); queue.endBatch(true); assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); } finally { thread.quit(); } } @MediumTest public void testOnContentChanged_batch_dont_notify() throws InterruptedException { final Uri uri = buildUri(); final HandlerThread thread = ThreadUtil.newHandlerThread(getName(), ThreadPriority.DEFAULT); try { final CountDownLatch latch = new CountDownLatch(1); final ContentObserver observer = new ContentObserver(new Handler(thread.getLooper())) { @Override public void onChange(final boolean selfChange) { super.onChange(selfChange); latch.countDown(); } }; getContext().getContentResolver().registerContentObserver(uri, true, observer); final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue( getContext(), false, null); queue.startBatch(); queue.onContentChanged(uri); queue.endBatch(false); assertFalse(latch.await(500, TimeUnit.MILLISECONDS)); } finally { thread.quit(); } } @SmallTest public void testIntent_no_batch_no_permission_exported() { assertIntentNotification(null, true, buildUri()); } @SmallTest public void testIntent_no_batch_permission_exported() { assertIntentNotification("com.twofortyfouram.bootstrap.permission.TEST_PERMISSION", true, //$NON-NLS-1$ buildUri()); } @SmallTest public void testIntent_no_batch_no_permission_not_exported() { assertIntentNotification(null, false, buildUri()); } @MediumTest public void testIntent_batch_notify() throws InterruptedException { assertIntentNotification(true, true, null, false, buildUri()); } @MediumTest public void testIntent_batch_dont_notify() throws InterruptedException { assertIntentNotification(true, false, null, false, buildUri()); } @NonNull private Uri buildUri() { final Uri.Builder builder = new Uri.Builder(); builder.scheme(ContentResolver.SCHEME_CONTENT); builder.authority(getClass().getName()); builder.path(Uri.encode(getName())); return builder.build(); } /** * @param providerReadPermission Read permission for the Content Provider. * @param isProviderExported True if the ContentProvider is exported. * @param changedUri Uri that changed. */ private void assertIntentNotification(@NonNull final String providerReadPermission, final boolean isProviderExported, @NonNull final Uri changedUri) { assertIntentNotification(false, false, providerReadPermission, isProviderExported, changedUri); } /** * @param isBatch True if this is a batch operation. * @param isBatchSuccessful True if the batch operation was successful. * @param providerReadPermission Read permission for the Content Provider. * @param isProviderExported True if the ContentProvider is exported. * @param changedUri Uri that changed. */ private void assertIntentNotification(final boolean isBatch, final boolean isBatchSuccessful, @NonNull final String providerReadPermission, final boolean isProviderExported, @NonNull final Uri changedUri) { final ReceiverContextWrapper context = new ReceiverContextWrapper(getContext()); final ContentChangeNotificationQueue queue = new ContentChangeNotificationQueue(context, isProviderExported, providerReadPermission); if (isBatch) { queue.startBatch(); } queue.onContentChanged(changedUri); if (isBatch) { queue.endBatch(isBatchSuccessful); } final Collection<SentIntent> intents = context.getAndClearSentIntents(); if (!isBatch || (isBatch && isBatchSuccessful)) { assertEquals(1, intents.size()); } else { assertEquals(0, intents.size()); } for (final SentIntent sentIntent : intents) { assertFalse(sentIntent.getIsOrdered()); assertFalse(sentIntent.getIsSticky()); assertEquals(providerReadPermission, sentIntent.getPermission()); final Intent intent = sentIntent.getIntent(); assertNotNull(intent); assertEquals(Intent.ACTION_PROVIDER_CHANGED, intent.getAction()); if (!isProviderExported) { assertEquals(context.getPackageName(), intent.getPackage()); } assertEquals(changedUri, intent.getData()); } } }