Search in sources :

Example 11 with MockContext

use of android.test.mock.MockContext in project android_frameworks_base by DirtyUnicorns.

the class ProviderTestCase2 method newResolverWithContentProviderFromSql.

/**
     * <p>
     *      Creates a new content provider of the same type as that passed to the test case class,
     *      with an authority name set to the authority parameter, and using an SQLite database as
     *      the underlying data source. The SQL statement parameter is used to create the database.
     *      This method also creates a new {@link MockContentResolver} and adds the provider to it.
     * </p>
     * <p>
     *      Both the new provider and the new resolver are put into an {@link IsolatedContext}
     *      that uses the targetContext parameter for file operations and a {@link MockContext}
     *      for everything else. The IsolatedContext prepends the filenamePrefix parameter to
     *      file, database, and directory names.
     * </p>
     * <p>
     *      This is a convenience method for creating a "mock" provider that can contain test data.
     * </p>
     *
     * @param targetContext The context to use as the basis of the IsolatedContext
     * @param filenamePrefix A string that is prepended to file, database, and directory names
     * @param providerClass The type of the provider being tested
     * @param authority The authority string to associated with the test provider
     * @param databaseName The name assigned to the database
     * @param databaseVersion The version assigned to the database
     * @param sql A string containing the SQL statements that are needed to create the desired
     * database and its tables. The format is the same as that generated by the
     * <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> tool's <code>.dump</code> command.
     * @return ContentResolver A new {@link MockContentResolver} linked to the provider
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, String databaseName, int databaseVersion, String sql) throws IllegalAccessException, InstantiationException {
    MockContentResolver resolver = new MockContentResolver();
    RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(// The context that most methods are delegated to
    new MockContext(), // The context that file methods are delegated to
    targetContext, filenamePrefix);
    Context context = new IsolatedContext(resolver, targetContextWrapper);
    DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
    T provider = createProviderForTest(context, providerClass, authority);
    resolver.addProvider(authority, provider);
    return resolver;
}
Also used : Context(android.content.Context) MockContext(android.test.mock.MockContext) MockContext(android.test.mock.MockContext) MockContentResolver(android.test.mock.MockContentResolver)

Example 12 with MockContext

use of android.test.mock.MockContext in project android_frameworks_base by DirtyUnicorns.

the class AccountManagerServiceTest method setUp.

@Override
protected void setUp() throws Exception {
    Context realTestContext = getContext();
    Context mockContext = new MyMockContext(realTestContext);
    setContext(mockContext);
    mAms = createAccountManagerService(mockContext, realTestContext);
}
Also used : Context(android.content.Context) MockContext(android.test.mock.MockContext)

Example 13 with MockContext

use of android.test.mock.MockContext in project Etar-Calendar by Etar-Group.

the class AsyncQueryServiceTest method buildTestContext.

private Context buildTestContext(final OperationInfo[] work) {
    MockContext context = new MockContext() {

        MockContentResolver mResolver;

        @Override
        public ContentResolver getContentResolver() {
            if (mResolver == null) {
                mResolver = new MockContentResolver();
                final String filenamePrefix = "test.";
                RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(new MockContext2(), getContext(), filenamePrefix);
                IsolatedContext providerContext = new IsolatedContext(mResolver, targetContextWrapper);
                ContentProvider provider = new TestProvider(work);
                provider.attachInfo(providerContext, null);
                mResolver.addProvider(AUTHORITY, provider);
            }
            return mResolver;
        }

        @Override
        public String getPackageName() {
            return AsyncQueryServiceTest.class.getPackage().getName();
        }

        @Override
        public ComponentName startService(Intent service) {
            AsyncQueryServiceTest.this.startService(service);
            return service.getComponent();
        }
    };
    return context;
}
Also used : RenamingDelegatingContext(android.test.RenamingDelegatingContext) MockContext(android.test.mock.MockContext) ContentProvider(android.content.ContentProvider) Intent(android.content.Intent) MockContentResolver(android.test.mock.MockContentResolver) IsolatedContext(android.test.IsolatedContext)

Example 14 with MockContext

use of android.test.mock.MockContext in project android_frameworks_base by AOSPA.

the class ListViewTest method testRequestLayout.

/**
     * If a view in a ListView requests a layout it should be remeasured.
     */
@MediumTest
public void testRequestLayout() throws Exception {
    MockContext context = new MockContext2();
    ListView listView = new ListView(context);
    List<String> items = Lists.newArrayList("hello");
    Adapter<String> adapter = new Adapter<String>(context, 0, items);
    listView.setAdapter(adapter);
    int measureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
    adapter.notifyDataSetChanged();
    listView.measure(measureSpec, measureSpec);
    listView.layout(0, 0, 100, 100);
    MockView childView = (MockView) listView.getChildAt(0);
    childView.requestLayout();
    childView.onMeasureCalled = false;
    listView.measure(measureSpec, measureSpec);
    listView.layout(0, 0, 100, 100);
    Assert.assertTrue(childView.onMeasureCalled);
}
Also used : MockContext(android.test.mock.MockContext) ListView(android.widget.ListView) ArrayAdapter(android.widget.ArrayAdapter) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 15 with MockContext

use of android.test.mock.MockContext in project android_frameworks_base by AOSPA.

the class ListViewTest method testNoSelectableItems.

/**
     * The list view should handle the disappearance of the only selected item, even when that item
     * was selected before its disappearance.
     *
     */
@MediumTest
public void testNoSelectableItems() throws Exception {
    MockContext context = new MockContext2();
    ListView listView = new ListView(context);
    // We use a header as the unselectable item to remain after the selectable one is removed.
    listView.addHeaderView(new View(context), null, false);
    List<String> items = Lists.newArrayList("hello");
    Adapter<String> adapter = new Adapter<String>(context, 0, items);
    listView.setAdapter(adapter);
    listView.setSelection(1);
    int measureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
    adapter.notifyDataSetChanged();
    listView.measure(measureSpec, measureSpec);
    listView.layout(0, 0, 100, 100);
    items.remove(0);
    adapter.notifyDataSetChanged();
    listView.measure(measureSpec, measureSpec);
    listView.layout(0, 0, 100, 100);
}
Also used : MockContext(android.test.mock.MockContext) ListView(android.widget.ListView) ArrayAdapter(android.widget.ArrayAdapter) View(android.view.View) ListView(android.widget.ListView) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Aggregations

MockContext (android.test.mock.MockContext)32 MockContentResolver (android.test.mock.MockContentResolver)16 Context (android.content.Context)14 MediumTest (android.test.suitebuilder.annotation.MediumTest)12 ArrayAdapter (android.widget.ArrayAdapter)12 ListView (android.widget.ListView)12 View (android.view.View)6 ContentProvider (android.content.ContentProvider)1 Intent (android.content.Intent)1 IsolatedContext (android.test.IsolatedContext)1 RenamingDelegatingContext (android.test.RenamingDelegatingContext)1