Added barebones MovieContract and MovieDbHelper, with tests

This commit is contained in:
Aargonian
2016-02-15 06:55:09 -05:00
parent 7857f24c03
commit 443340e8e1
4 changed files with 773 additions and 0 deletions

View File

@@ -0,0 +1,507 @@
package com.example.android.popularmovies.data;
import android.test.AndroidTestCase;
//
//import android.content.ContentUris;
//import android.content.ContentValues;
//import android.database.Cursor;
//import android.database.sqlite.SQLiteDatabase;
//import android.net.Uri;
//import android.os.Build;
//import android.test.AndroidTestCase;
//import android.util.Log;
//
//import android.content.ContentUris;
//import android.content.ContentValues;
//import android.database.Cursor;
//import android.database.sqlite.SQLiteDatabase;
//import android.net.Uri;
//import android.os.Build;
//import android.test.AndroidTestCase;
//import android.util.Log;
//
//import com.example.android.sunshine.app.data.WeatherContract.LocationEntry;
//import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry;
//
///*
// Note: This is not a complete set of tests of the Sunshine ContentProvider, but it does test
// that at least the basic functionality has been implemented correctly.
//
// Students: Uncomment the tests in this class as you implement the functionality in your
// ContentProvider to make sure that you've implemented things reasonably correctly.
// */
public class TestProvider extends AndroidTestCase {
//
// public static final String LOG_TAG = TestProvider.class.getSimpleName();
//
// /*
// This helper function deletes all records from both database tables using the ContentProvider.
// It also queries the ContentProvider to make sure that the database has been successfully
// deleted, so it cannot be used until the Query and Delete functions have been written
// in the ContentProvider.
//
// Students: Replace the calls to deleteAllRecordsFromDB with this one after you have written
// the delete functionality in the ContentProvider.
// */
// public void deleteAllRecordsFromProvider() {
// mContext.getContentResolver().delete(
// WeatherEntry.CONTENT_URI,
// null,
// null
// );
// mContext.getContentResolver().delete(
// LocationEntry.CONTENT_URI,
// null,
// null
// );
//
// Cursor cursor = mContext.getContentResolver().query(
// WeatherEntry.CONTENT_URI,
// null,
// null,
// null,
// null
// );
// assertEquals("Error: Records not deleted from Weather table during delete", 0, cursor.getCount());
// cursor.close();
//
// cursor = mContext.getContentResolver().query(
// LocationEntry.CONTENT_URI,
// null,
// null,
// null,
// null
// );
// assertEquals("Error: Records not deleted from Location table during delete", 0, cursor.getCount());
// cursor.close();
// }
//
// /*
// Student: Refactor this function to use the deleteAllRecordsFromProvider functionality once
// you have implemented delete functionality there.
// */
// public void deleteAllRecords() {
// deleteAllRecordsFromProvider();
// }
//
// // Since we want each test to start with a clean slate, run deleteAllRecords
// // in setUp (called by the test runner before each test).
// @Override
// protected void setUp() throws Exception {
// super.setUp();
// deleteAllRecords();
// }
//
// /*
// This test checks to make sure that the content provider is registered correctly.
// Students: Uncomment this test to make sure you've correctly registered the WeatherProvider.
// */
//// public void testProviderRegistry() {
//// PackageManager pm = mContext.getPackageManager();
////
//// // We define the component name based on the package name from the context and the
//// // WeatherProvider class.
//// ComponentName componentName = new ComponentName(mContext.getPackageName(),
//// WeatherProvider.class.getName());
//// try {
//// // Fetch the provider info using the component name from the PackageManager
//// // This throws an exception if the provider isn't registered.
//// ProviderInfo providerInfo = pm.getProviderInfo(componentName, 0);
////
//// // Make sure that the registered authority matches the authority from the Contract.
//// assertEquals("Error: WeatherProvider registered with authority: " + providerInfo.authority +
//// " instead of authority: " + WeatherContract.CONTENT_AUTHORITY,
//// providerInfo.authority, WeatherContract.CONTENT_AUTHORITY);
//// } catch (PackageManager.NameNotFoundException e) {
//// // I guess the provider isn't registered correctly.
//// assertTrue("Error: WeatherProvider not registered at " + mContext.getPackageName(),
//// false);
//// }
//// }
//
// /*
// This test doesn't touch the database. It verifies that the ContentProvider returns
// the correct type for each type of URI that it can handle.
// Students: Uncomment this test to verify that your implementation of GetType is
// functioning correctly.
// */
// public void testGetType() {
// // content://com.example.android.sunshine.app/weather/
// String type = mContext.getContentResolver().getType(WeatherEntry.CONTENT_URI);
// // vnd.android.cursor.dir/com.example.android.sunshine.app/weather
// assertEquals("Error: the WeatherEntry CONTENT_URI should return WeatherEntry.CONTENT_TYPE",
// WeatherEntry.CONTENT_TYPE, type);
//
// String testLocation = "94074";
// // content://com.example.android.sunshine.app/weather/94074
// type = mContext.getContentResolver().getType(
// WeatherEntry.buildWeatherLocation(testLocation));
// // vnd.android.cursor.dir/com.example.android.sunshine.app/weather
// assertEquals("Error: the WeatherEntry CONTENT_URI with location should return WeatherEntry.CONTENT_TYPE",
// WeatherEntry.CONTENT_TYPE, type);
//
// long testDate = 1419120000L; // December 21st, 2014
// // content://com.example.android.sunshine.app/weather/94074/20140612
// type = mContext.getContentResolver().getType(
// WeatherEntry.buildWeatherLocationWithDate(testLocation, testDate));
// // vnd.android.cursor.item/com.example.android.sunshine.app/weather/1419120000
// assertEquals("Error: the WeatherEntry CONTENT_URI with location and date should return WeatherEntry.CONTENT_ITEM_TYPE",
// WeatherEntry.CONTENT_ITEM_TYPE, type);
//
// // content://com.example.android.sunshine.app/location/
// type = mContext.getContentResolver().getType(LocationEntry.CONTENT_URI);
// // vnd.android.cursor.dir/com.example.android.sunshine.app/location
// assertEquals("Error: the LocationEntry CONTENT_URI should return LocationEntry.CONTENT_TYPE",
// LocationEntry.CONTENT_TYPE, type);
// }
//
//
// /*
// This test uses the database directly to insert and then uses the ContentProvider to
// read out the data. Uncomment this test to see if the basic weather query functionality
// given in the ContentProvider is working correctly.
// */
// public void testBasicWeatherQuery() {
// // insert our test records into the database
// WeatherDbHelper dbHelper = new WeatherDbHelper(mContext);
// SQLiteDatabase db = dbHelper.getWritableDatabase();
//
// ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
// long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext);
//
// // Fantastic. Now that we have a location, add some weather!
// ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId);
//
// long weatherRowId = db.insert(WeatherEntry.TABLE_NAME, null, weatherValues);
// assertTrue("Unable to Insert WeatherEntry into the Database", weatherRowId != -1);
//
// db.close();
//
// // Test the basic content provider query
// Cursor weatherCursor = mContext.getContentResolver().query(
// WeatherEntry.CONTENT_URI,
// null,
// null,
// null,
// null
// );
//
// // Make sure we get the correct cursor out of the database
// TestUtilities.validateCursor("testBasicWeatherQuery", weatherCursor, weatherValues);
// }
//
// /*
// This test uses the database directly to insert and then uses the ContentProvider to
// read out the data. Uncomment this test to see if your location queries are
// performing correctly.
// */
// public void testBasicLocationQueries() {
// // insert our test records into the database
// WeatherDbHelper dbHelper = new WeatherDbHelper(mContext);
// SQLiteDatabase db = dbHelper.getWritableDatabase();
//
// ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
// long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext);
//
// // Test the basic content provider query
// Cursor locationCursor = mContext.getContentResolver().query(
// LocationEntry.CONTENT_URI,
// null,
// null,
// null,
// null
// );
//
// // Make sure we get the correct cursor out of the database
// TestUtilities.validateCursor("testBasicLocationQueries, location query", locationCursor, testValues);
//
// // Has the NotificationUri been set correctly? --- we can only test this easily against API
// // level 19 or greater because getNotificationUri was added in API level 19.
// if ( Build.VERSION.SDK_INT >= 19 ) {
// assertEquals("Error: Location Query did not properly set NotificationUri",
// locationCursor.getNotificationUri(), LocationEntry.CONTENT_URI);
// }
// }
//
// /*
// This test uses the provider to insert and then update the data. Uncomment this test to
// see if your update location is functioning correctly.
// */
// public void testUpdateLocation() {
// // Create a new map of values, where column names are the keys
// ContentValues values = TestUtilities.createNorthPoleLocationValues();
//
// Uri locationUri = mContext.getContentResolver().
// insert(LocationEntry.CONTENT_URI, values);
// long locationRowId = ContentUris.parseId(locationUri);
//
// // Verify we got a row back.
// assertTrue(locationRowId != -1);
// Log.d(LOG_TAG, "New row id: " + locationRowId);
//
// ContentValues updatedValues = new ContentValues(values);
// updatedValues.put(LocationEntry._ID, locationRowId);
// updatedValues.put(LocationEntry.COLUMN_CITY_NAME, "Santa's Village");
//
// // Create a cursor with observer to make sure that the content provider is notifying
// // the observers as expected
// Cursor locationCursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI, null, null, null, null);
//
// TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver();
// locationCursor.registerContentObserver(tco);
//
// int count = mContext.getContentResolver().update(
// LocationEntry.CONTENT_URI, updatedValues, LocationEntry._ID + "= ?",
// new String[] { Long.toString(locationRowId)});
// assertEquals(count, 1);
//
// // Test to make sure our observer is called. If not, we throw an assertion.
// //
// // Students: If your code is failing here, it means that your content provider
// // isn't calling getContext().getContentResolver().notifyChange(uri, null);
// tco.waitForNotificationOrFail();
//
// locationCursor.unregisterContentObserver(tco);
// locationCursor.close();
//
// // A cursor is your primary interface to the query results.
// Cursor cursor = mContext.getContentResolver().query(
// LocationEntry.CONTENT_URI,
// null, // projection
// LocationEntry._ID + " = " + locationRowId,
// null, // Values for the "where" clause
// null // sort order
// );
//
// TestUtilities.validateCursor("testUpdateLocation. Error validating location entry update.",
// cursor, updatedValues);
//
// cursor.close();
// }
//
//
// // Make sure we can still delete after adding/updating stuff
// //
// // Student: Uncomment this test after you have completed writing the insert functionality
// // in your provider. It relies on insertions with testInsertReadProvider, so insert and
// // query functionality must also be complete before this test can be used.
// public void testInsertReadProvider() {
// ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
//
// // Register a content observer for our insert. This time, directly with the content resolver
// TestUtilities.TestContentObserver tco = TestUtilities.getTestContentObserver();
// mContext.getContentResolver().registerContentObserver(LocationEntry.CONTENT_URI, true, tco);
// Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues);
//
// // Did our content observer get called? Students: If this fails, your insert location
// // isn't calling getContext().getContentResolver().notifyChange(uri, null);
// tco.waitForNotificationOrFail();
// mContext.getContentResolver().unregisterContentObserver(tco);
//
// long locationRowId = ContentUris.parseId(locationUri);
//
// // Verify we got a row back.
// assertTrue(locationRowId != -1);
//
// // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
// // the round trip.
//
// // A cursor is your primary interface to the query results.
// Cursor cursor = mContext.getContentResolver().query(
// LocationEntry.CONTENT_URI,
// null, // leaving "columns" null just returns all the columns.
// null, // cols for "where" clause
// null, // values for "where" clause
// null // sort order
// );
//
// TestUtilities.validateCursor("testInsertReadProvider. Error validating LocationEntry.",
// cursor, testValues);
//
// // Fantastic. Now that we have a location, add some weather!
// ContentValues weatherValues = TestUtilities.createWeatherValues(locationRowId);
// // The TestContentObserver is a one-shot class
// tco = TestUtilities.getTestContentObserver();
//
// mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, tco);
//
// Uri weatherInsertUri = mContext.getContentResolver()
// .insert(WeatherEntry.CONTENT_URI, weatherValues);
// assertTrue(weatherInsertUri != null);
//
// // Did our content observer get called? Students: If this fails, your insert weather
// // in your ContentProvider isn't calling
// // getContext().getContentResolver().notifyChange(uri, null);
// tco.waitForNotificationOrFail();
// mContext.getContentResolver().unregisterContentObserver(tco);
//
// // A cursor is your primary interface to the query results.
// Cursor weatherCursor = mContext.getContentResolver().query(
// WeatherEntry.CONTENT_URI, // Table to Query
// null, // leaving "columns" null just returns all the columns.
// null, // cols for "where" clause
// null, // values for "where" clause
// null // columns to group by
// );
//
// TestUtilities.validateCursor("testInsertReadProvider. Error validating WeatherEntry insert.",
// weatherCursor, weatherValues);
//
// // Add the location values in with the weather data so that we can make
// // sure that the join worked and we actually get all the values back
// weatherValues.putAll(testValues);
//
// // Get the joined Weather and Location data
// weatherCursor = mContext.getContentResolver().query(
// WeatherEntry.buildWeatherLocation(TestUtilities.TEST_LOCATION),
// null, // leaving "columns" null just returns all the columns.
// null, // cols for "where" clause
// null, // values for "where" clause
// null // sort order
// );
// TestUtilities.validateCursor("testInsertReadProvider. Error validating joined Weather and Location Data.",
// weatherCursor, weatherValues);
//
// // Get the joined Weather and Location data with a start date
// weatherCursor = mContext.getContentResolver().query(
// WeatherEntry.buildWeatherLocationWithStartDate(
// TestUtilities.TEST_LOCATION, TestUtilities.TEST_DATE),
// null, // leaving "columns" null just returns all the columns.
// null, // cols for "where" clause
// null, // values for "where" clause
// null // sort order
// );
// TestUtilities.validateCursor("testInsertReadProvider. Error validating joined Weather and Location Data with start date.",
// weatherCursor, weatherValues);
//
// // Get the joined Weather data for a specific date
// weatherCursor = mContext.getContentResolver().query(
// WeatherEntry.buildWeatherLocationWithDate(TestUtilities.TEST_LOCATION, TestUtilities.TEST_DATE),
// null,
// null,
// null,
// null
// );
// TestUtilities.validateCursor("testInsertReadProvider. Error validating joined Weather and Location data for a specific date.",
// weatherCursor, weatherValues);
// }
//
// // Make sure we can still delete after adding/updating stuff
// //
// // Student: Uncomment this test after you have completed writing the delete functionality
// // in your provider. It relies on insertions with testInsertReadProvider, so insert and
// // query functionality must also be complete before this test can be used.
// public void testDeleteRecords() {
// testInsertReadProvider();
//
// // Register a content observer for our location delete.
// TestUtilities.TestContentObserver locationObserver = TestUtilities.getTestContentObserver();
// mContext.getContentResolver().registerContentObserver(LocationEntry.CONTENT_URI, true, locationObserver);
//
// // Register a content observer for our weather delete.
// TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
// mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver);
//
// deleteAllRecordsFromProvider();
//
// // Students: If either of these fail, you most-likely are not calling the
// // getContext().getContentResolver().notifyChange(uri, null); in the ContentProvider
// // delete. (only if the insertReadProvider is succeeding)
// locationObserver.waitForNotificationOrFail();
// weatherObserver.waitForNotificationOrFail();
//
// mContext.getContentResolver().unregisterContentObserver(locationObserver);
// mContext.getContentResolver().unregisterContentObserver(weatherObserver);
// }
//
//
// static private final int BULK_INSERT_RECORDS_TO_INSERT = 10;
// static ContentValues[] createBulkInsertWeatherValues(long locationRowId) {
// long currentTestDate = TestUtilities.TEST_DATE;
// long millisecondsInADay = 1000*60*60*24;
// ContentValues[] returnContentValues = new ContentValues[BULK_INSERT_RECORDS_TO_INSERT];
//
// for ( int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, currentTestDate+= millisecondsInADay ) {
// ContentValues weatherValues = new ContentValues();
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_LOC_KEY, locationRowId);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DATE, currentTestDate);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DEGREES, 1.1);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_HUMIDITY, 1.2 + 0.01 * (float) i);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_PRESSURE, 1.3 - 0.01 * (float) i);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, 75 + i);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, 65 - i);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, "Asteroids");
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, 5.5 + 0.2 * (float) i);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, 321);
// returnContentValues[i] = weatherValues;
// }
// return returnContentValues;
// }
//
// // Student: Uncomment this test after you have completed writing the BulkInsert functionality
// // in your provider. Note that this test will work with the built-in (default) provider
// // implementation, which just inserts records one-at-a-time, so really do implement the
// // BulkInsert ContentProvider function.
//// public void testBulkInsert() {
//// // first, let's create a location value
//// ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
//// Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues);
//// long locationRowId = ContentUris.parseId(locationUri);
////
//// // Verify we got a row back.
//// assertTrue(locationRowId != -1);
////
//// // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
//// // the round trip.
////
//// // A cursor is your primary interface to the query results.
//// Cursor cursor = mContext.getContentResolver().query(
//// LocationEntry.CONTENT_URI,
//// null, // leaving "columns" null just returns all the columns.
//// null, // cols for "where" clause
//// null, // values for "where" clause
//// null // sort order
//// );
////
//// TestUtilities.validateCursor("testBulkInsert. Error validating LocationEntry.",
//// cursor, testValues);
////
//// // Now we can bulkInsert some weather. In fact, we only implement BulkInsert for weather
//// // entries. With ContentProviders, you really only have to implement the features you
//// // use, after all.
//// ContentValues[] bulkInsertContentValues = createBulkInsertWeatherValues(locationRowId);
////
//// // Register a content observer for our bulk insert.
//// TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
//// mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver);
////
//// int insertCount = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, bulkInsertContentValues);
////
//// // Students: If this fails, it means that you most-likely are not calling the
//// // getContext().getContentResolver().notifyChange(uri, null); in your BulkInsert
//// // ContentProvider method.
//// weatherObserver.waitForNotificationOrFail();
//// mContext.getContentResolver().unregisterContentObserver(weatherObserver);
////
//// assertEquals(insertCount, BULK_INSERT_RECORDS_TO_INSERT);
////
//// // A cursor is your primary interface to the query results.
//// cursor = mContext.getContentResolver().query(
//// WeatherEntry.CONTENT_URI,
//// null, // leaving "columns" null just returns all the columns.
//// null, // cols for "where" clause
//// null, // values for "where" clause
//// WeatherEntry.COLUMN_DATE + " ASC" // sort order == by DATE ASCENDING
//// );
////
//// // we should have as many records in the database as we've inserted
//// assertEquals(cursor.getCount(), BULK_INSERT_RECORDS_TO_INSERT);
////
//// // and let's make sure they match the ones we created
//// cursor.moveToFirst();
//// for ( int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, cursor.moveToNext() ) {
//// TestUtilities.validateCurrentRecord("testBulkInsert. Error validating WeatherEntry " + i,
//// cursor, bulkInsertContentValues[i]);
//// }
//// cursor.close();
//// }
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (C) 2014 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.popularmovies.data;
import android.test.AndroidTestCase;
/*
Uncomment this class when you are ready to test your UriMatcher. Note that this class utilizes
constants that are declared with package protection inside of the UriMatcher, which is why
the test must be in the same data package as the Android app code. Doing the test this way is
a nice compromise between data hiding and testability.
*/
public class TestUriMatcher extends AndroidTestCase {
// private static final String LOCATION_QUERY = "London, UK";
// private static final long TEST_DATE = 1419033600L; // December 20th, 2014
// private static final long TEST_LOCATION_ID = 10L;
//
// // content://com.example.android.sunshine.app/weather"
// private static final Uri TEST_WEATHER_DIR = WeatherContract.WeatherEntry.CONTENT_URI;
// private static final Uri TEST_WEATHER_WITH_LOCATION_DIR = WeatherContract.WeatherEntry.buildWeatherLocation(LOCATION_QUERY);
// private static final Uri TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(LOCATION_QUERY, TEST_DATE);
// // content://com.example.android.sunshine.app/location"
// private static final Uri TEST_LOCATION_DIR = WeatherContract.LocationEntry.CONTENT_URI;
//
// /*
// Students: This function tests that your UriMatcher returns the correct integer value
// for each of the Uri types that our ContentProvider can handle. Uncomment this when you are
// ready to test your UriMatcher.
// */
// public void testUriMatcher() {
// UriMatcher testMatcher = WeatherProvider.buildUriMatcher();
//
// assertEquals("Error: The WEATHER URI was matched incorrectly.",
// testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER);
// assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
// testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION);
// assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
// testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE);
// assertEquals("Error: The LOCATION URI was matched incorrectly.",
// testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION);
// }
}

View File

@@ -0,0 +1,138 @@
package com.example.android.popularmovies.data;
import android.test.AndroidTestCase;
/*
Students: These are functions and some test data to make it easier to test your database and
Content Provider. Note that you'll want your WeatherContract class to exactly match the one
in our solution to use these as-given.
*/
public class TestUtilities extends AndroidTestCase {
// static final String TEST_LOCATION = "99705";
// static final long TEST_DATE = 1419033600L; // December 20th, 2014
//
// static void validateCursor(String error, Cursor valueCursor, ContentValues expectedValues) {
// assertTrue("Empty cursor returned. " + error, valueCursor.moveToFirst());
// validateCurrentRecord(error, valueCursor, expectedValues);
// valueCursor.close();
// }
//
// static void validateCurrentRecord(String error, Cursor valueCursor, ContentValues expectedValues) {
// Set<Map.Entry<String, Object>> valueSet = expectedValues.valueSet();
// for (Map.Entry<String, Object> entry : valueSet) {
// String columnName = entry.getKey();
// int idx = valueCursor.getColumnIndex(columnName);
// assertFalse("Column '" + columnName + "' not found. " + error, idx == -1);
// String expectedValue = entry.getValue().toString();
// assertEquals("Value '" + entry.getValue().toString() +
// "' did not match the expected value '" +
// expectedValue + "'. " + error, expectedValue, valueCursor.getString(idx));
// }
// }
//
// /*
// Students: Use this to create some default weather values for your database tests.
// */
// static ContentValues createWeatherValues(long locationRowId) {
// ContentValues weatherValues = new ContentValues();
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_LOC_KEY, locationRowId);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DATE, TEST_DATE);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_DEGREES, 1.1);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_HUMIDITY, 1.2);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_PRESSURE, 1.3);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, 75);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, 65);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, "Asteroids");
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WIND_SPEED, 5.5);
// weatherValues.put(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID, 321);
//
// return weatherValues;
// }
//
// /*
// Students: You can uncomment this helper function once you have finished creating the
// LocationEntry part of the WeatherContract.
// */
// static ContentValues createNorthPoleLocationValues() {
// // Create a new map of values, where column names are the keys
// ContentValues testValues = new ContentValues();
// testValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, TEST_LOCATION);
// testValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, "North Pole");
// testValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, 64.7488);
// testValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, -147.353);
//
// return testValues;
// }
//
// /*
// Students: You can uncomment this function once you have finished creating the
// LocationEntry part of the WeatherContract as well as the WeatherDbHelper.
// */
// static long insertNorthPoleLocationValues(Context context) {
// // insert our test records into the database
// WeatherDbHelper dbHelper = new WeatherDbHelper(context);
// SQLiteDatabase db = dbHelper.getWritableDatabase();
// ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
//
// long locationRowId;
// locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);
//
// // Verify we got a row back.
// assertTrue("Error: Failure to insert North Pole Location Values", locationRowId != -1);
//
// return locationRowId;
// }
//
// /*
// Students: The functions we provide inside of TestProvider use this utility class to test
// the ContentObserver callbacks using the PollingCheck class that we grabbed from the Android
// CTS tests.
//
// Note that this only tests that the onChange function is called; it does not test that the
// correct Uri is returned.
// */
// static class TestContentObserver extends ContentObserver {
// final HandlerThread mHT;
// boolean mContentChanged;
//
// static TestContentObserver getTestContentObserver() {
// HandlerThread ht = new HandlerThread("ContentObserverThread");
// ht.start();
// return new TestContentObserver(ht);
// }
//
// private TestContentObserver(HandlerThread ht) {
// super(new Handler(ht.getLooper()));
// mHT = ht;
// }
//
// // On earlier versions of Android, this onChange method is called
// @Override
// public void onChange(boolean selfChange) {
// onChange(selfChange, null);
// }
//
// @Override
// public void onChange(boolean selfChange, Uri uri) {
// mContentChanged = true;
// }
//
// public void waitForNotificationOrFail() {
// // Note: The PollingCheck class is taken from the Android CTS (Compatibility Test Suite).
// // It's useful to look at the Android CTS source for ideas on how to test your Android
// // applications. The reason that PollingCheck works is that, by default, the JUnit
// // testing framework is not running on the main Android application thread.
// new PollingCheck(5000) {
// @Override
// protected boolean check() {
// return mContentChanged;
// }
// }.run();
// mHT.quit();
// }
// }
//
// static TestContentObserver getTestContentObserver() {
// return TestContentObserver.getTestContentObserver();
// }
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2012 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.
*
* Note: This file copied from the Android CTS Tests
*/
package com.example.android.popularmovies.utils;
import junit.framework.Assert;
import java.util.concurrent.Callable;
public abstract class PollingCheck {
private static final long TIME_SLICE = 50;
private long mTimeout = 3000;
public PollingCheck() {
}
public PollingCheck(long timeout) {
mTimeout = timeout;
}
protected abstract boolean check();
public void run() {
if (check()) {
return;
}
long timeout = mTimeout;
while (timeout > 0) {
try {
Thread.sleep(TIME_SLICE);
} catch (InterruptedException e) {
Assert.fail("unexpected InterruptedException");
}
if (check()) {
return;
}
timeout -= TIME_SLICE;
}
Assert.fail("unexpected timeout");
}
public static void check(CharSequence message, long timeout, Callable<Boolean> condition)
throws Exception {
while (timeout > 0) {
if (condition.call()) {
return;
}
Thread.sleep(TIME_SLICE);
timeout -= TIME_SLICE;
}
Assert.fail(message.toString());
}
}