/*
* Copyright (C) 2010 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.example.android.notepad;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.test.ProviderTestCase2;
import android.test.mock.MockContentResolver;
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
/*
*/
/**
* This class tests the content provider for the Note Pad sample application.
*
* To learn how to run an entire test package or one of its classes, please see
* "Testing in Eclipse, with ADT" or "Testing in Other IDEs" in the Developer Guide.
*/
public class NotePadProviderTest extends ProviderTestCase2<NotePadProvider> {
// A URI that the provider does not offer, for testing error handling.
private static final Uri INVALID_URI =
Uri.withAppendedPath(NotePad.Notes.CONTENT_URI, "invalid");
// Contains a reference to the mocked content resolver for the provider under test.
private MockContentResolver mMockResolver;
// Contains an SQLite database, used as test data
private SQLiteDatabase mDb;
// Contains the test data, as an array of NoteInfo instances.
private final NoteInfo[] TEST_NOTES = {
new NoteInfo("Note0", "This is note 0"),
new NoteInfo("Note1", "This is note 1"),
new NoteInfo("Note2", "This is note 2"),
new NoteInfo("Note3", "This is note 3"),
new NoteInfo("Note4", "This is note 4"),
new NoteInfo("Note5", "This is note 5"),
new NoteInfo("Note6", "This is note 6"),
new NoteInfo("Note7", "This is note 7"),
new NoteInfo("Note8", "This is note 8"),
new NoteInfo("Note9", "This is note 9") };
// Number of milliseconds in one day (milliseconds * seconds * minutes * hours)
private static final long ONE_DAY_MILLIS = 1000 * 60 * 60 * 24;
// Number of milliseconds in one week
private static final long ONE_WEEK_MILLIS = ONE_DAY_MILLIS * 7;
// Creates a calendar object equal to January 1, 2010 at 12 midnight
private static final GregorianCalendar TEST_CALENDAR =
new GregorianCalendar(2010, Calendar.JANUARY, 1, 0, 0, 0);
// Stores a timestamp value, set to an arbitrary starting point
private final static long START_DATE = TEST_CALENDAR.getTimeInMillis();
// Sets a MIME type filter, used to test provider methods that return more than one MIME type
// for a particular note. The filter will retrieve any MIME types supported for the content URI.
private final static String MIME_TYPES_ALL = "*/*";
// Sets a MIME type filter, used to test provider methods that return more than one MIME type
// for a particular note. The filter is nonsense, so it will not retrieve any MIME types.
private final static String MIME_TYPES_NONE = "qwer/qwer";
// Sets a MIME type filter for plain text, used to the provider's methods that only handle
// plain text
private final static String MIME_TYPE_TEXT = "text/plain";
/*
* Constructor for the test case class.
* Calls the super constructor with the class name of the provider under test and the
* authority name of the provider.
*/
public NotePadProviderTest() {
super(NotePadProvider.class, NotePad.AUTHORITY);
}
/*
* Sets up the test environment before each test method. Creates a mock content resolver,
* gets the provider under test, and creates a new database for the provider.
*/
@Override
protected void setUp() throws Exception {
// Calls the base class implementation of this method.
super.setUp();
// Gets the resolver for this test.
mMockResolver = getMockContentResolver();
/*
* Gets a handle to the database underlying the provider. Gets the provider instance
* created in super.setUp(), gets the DatabaseOpenHelper for the provider, and gets
* a database object from the helper.
*/
mDb = getProvider().getOpenHelperForTest().getWritableDatabase();
}
/*
* This method is called after each test method, to clean up the current fixture. Since
* this sample test case runs in an isolated context, no cleanup is necessary.
*/
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
/*
* Sets up test data.
* The test data is in an SQL database. It is created in setUp() without any data,
* and populated in insertData if necessary.
*/
private void insertData() {
// Creates an instance of the ContentValues map type expected by database insertions
ContentValues values = new ContentValues();
// Sets up test data
for (int index = 0; index < TEST_NOTES.length; index++) {
// Set the creation and modification date for the note
TEST_NOTES[index].setCreationDate(START_DATE + (index * ONE_DAY_MILLIS));
TEST_NOTES[index].setModificationDate(START_DATE + (index * ONE_WEEK_MILLIS));
// Adds a record to the database.
mDb.insertOrThrow(
NotePad.Notes.TABLE_NAME, // the table name for the insert
NotePad.Notes.COLUMN_NAME_TITLE, // column set to null if empty values map
TEST_NOTES[index].getContentValues() // the values map to insert
);
}
}
/*
* Tests the provider's publicly available URIs. If the URI is not one that the provider
* understands, the provider should throw an exception. It also tests the provider's getType()
* method for each URI, which should return the MIME type associated with the URI.
*/
public void testUriAndGetType() {
// Tests the MIME type for the notes table URI.
String mimeType = mMockResolver.getType(NotePad.Notes.CONTENT_URI);
assertEquals(NotePad.Notes.CONTENT_TYPE, mimeType);
// Tests the MIME type for the live folder URI.
mimeType = mMockResolver.getType(NotePad.Notes.LIVE_FOLDER_URI);
assertEquals(NotePad.Notes.CONTENT_TYPE, mimeType);
// Creates a URI with a pattern for note ids. The id doesn't have to exist.
Uri noteIdUri = ContentUris.withAppendedId(NotePad.Notes.CONTENT_ID_URI_BASE, 1);
// Gets the note ID URI MIME type.
mimeType = mMockResolver.getType(noteIdUri);
assertEquals(NotePad.Notes.CONTENT_ITEM_TYPE, mimeType);
// Tests an invalid URI. This should throw an IllegalArgumentException.
mimeType = mMockResolver.getType(INVALID_URI);
}
/*
* Tests the provider's stream MIME types returned by getStreamTypes(). If the provider supports
* stream data for the URI, the MIME type is returned. Otherwise, the provider returns null.
*/
public void testGetStreamTypes() {
// Tests the notes table URI. This should return null, since the content provider does
// not provide a stream MIME type for multiple notes.
assertNull(mMockResolver.getStreamTypes(NotePad.Notes.CONTENT_URI, MIME_TYPES_ALL));
//