Android Open Source - android-memento Content Provider Operation Service Test






From Project

Back to project page android-memento.

License

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.

Java Source Code

/*
 * android-memento-lib https://github.com/twofortyfouram/android-memento
 * Copyright 2014 two forty four a.m. LLC
 */*from w  w  w .j a  va2  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.memento.debug.provider.SqliteContentProviderImpl;
import com.twofortyfouram.memento.debug.provider.TableOneContract;

import android.content.ContentProviderOperation;
import android.os.SystemClock;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.ArrayList;

/**
 * Tests {@link com.twofortyfouram.memento.provider.ContentProviderOperationService}.
 */
public final class ContentProviderOperationServiceTest extends AndroidTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        getContext().getContentResolver().delete(TableOneContract.getContentUri(getContext()),
                null, null);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();

        getContext().getContentResolver().delete(TableOneContract.getContentUri(getContext()),
                null, null);
    }

    @SmallTest
    public void testApplyBatchSynchronous_one_operation() {
        final ArrayList<ContentProviderOperation> primaryOps
                = new ArrayList<ContentProviderOperation>();

        primaryOps.add(ContentProviderOperation
                .newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues(getName())).build());

        ContentProviderOperationService.applyBatchWithAlternativeSynchronous(getContext(),
                SqliteContentProviderImpl.getContentAuthority(getContext()), primaryOps, null);

        assertCount(1);
    }

    @SmallTest
    public void testApplyBatchSynchronous_unused_backup_operation() {
        final ArrayList<ContentProviderOperation> primaryOps
                = new ArrayList<ContentProviderOperation>();
        primaryOps.add(ContentProviderOperation
                .newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues(getName())).build());

        final ArrayList<ContentProviderOperation> secondaryOps
                = new ArrayList<ContentProviderOperation>();
        secondaryOps.add(ContentProviderOperation.newDelete(
                TableOneContract.getContentUri(getContext())).build());

        ContentProviderOperationService.applyBatchWithAlternativeSynchronous(getContext(),
                SqliteContentProviderImpl.getContentAuthority(getContext()), primaryOps,
                secondaryOps);

        assertCount(1);
    }

    @SmallTest
    public void testApplyBatchSynchronous_used_backup_operation() {
        final ArrayList<ContentProviderOperation> primaryOps
                = new ArrayList<ContentProviderOperation>();
        primaryOps.add(ContentProviderOperation
                .newAssertQuery(TableOneContract.getContentUri(getContext())).withExpectedCount(1)
                .build());

        final ArrayList<ContentProviderOperation> secondaryOps
                = new ArrayList<ContentProviderOperation>();
        secondaryOps.add(ContentProviderOperation
                .newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues(getName())).build());

        ContentProviderOperationService.applyBatchWithAlternativeSynchronous(getContext(),
                SqliteContentProviderImpl.getContentAuthority(getContext()), primaryOps,
                secondaryOps);

        assertCount(1);
    }

    @MediumTest
    public void testApplyBatchAsynchronous_one_operation() {
        final ArrayList<ContentProviderOperation> primaryOps
                = new ArrayList<ContentProviderOperation>();

        primaryOps.add(ContentProviderOperation
                .newInsert(TableOneContract.getContentUri(getContext()))
                .withValues(TableOneContract.getContentValues(getName())).build());

        ContentProviderOperationService.applyBatchWithAlternativeAsynchronous(getContext(),
                SqliteContentProviderImpl.getContentAuthority(getContext()), primaryOps, null);

        SystemClock.sleep(500);

        assertCount(1);
    }

    /**
     * Asserts that {@link TableOneContract} has {@code count} rows.
     *
     * @param count Number of rows to assert exist in the table.
     */
    private void assertCount(final int count) {
        ContentProviderUtil.getCountForUri(getContext(),
                TableOneContract.getContentUri(getContext()));
    }
}




Java Source Code List

com.twofortyfouram.memento.debug.provider.SqliteContentProviderImpl.java
com.twofortyfouram.memento.debug.provider.SqliteOpenHelperImpl.java
com.twofortyfouram.memento.debug.provider.SqliteUriMatcherImpl.java
com.twofortyfouram.memento.debug.provider.TableOneContract.java
com.twofortyfouram.memento.provider.ContentChangeNotificationQueueTest.java
com.twofortyfouram.memento.provider.ContentChangeNotificationQueue.java
com.twofortyfouram.memento.provider.ContentProviderOperationServiceTest.java
com.twofortyfouram.memento.provider.ContentProviderOperationService.java
com.twofortyfouram.memento.provider.ContentProviderUtilTest.java
com.twofortyfouram.memento.provider.ContentProviderUtil.java
com.twofortyfouram.memento.provider.ImmutableUriMatcherTest.java
com.twofortyfouram.memento.provider.ImmutableUriMatcher.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProviderIntegrationTest.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProviderTest.java
com.twofortyfouram.memento.provider.sqlite.AbstractSqliteContentProvider.java
com.twofortyfouram.memento.provider.sqlite.SqliteColumnBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteColumnBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteIndexBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteIndexBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteOpenHelperCompat.java
com.twofortyfouram.memento.provider.sqlite.SqliteStorageClass.java
com.twofortyfouram.memento.provider.sqlite.SqliteTableBuilderTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteTableBuilder.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatchTest.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatch.java
com.twofortyfouram.memento.provider.sqlite.SqliteUriMatcher.java