Пример #1
0
 /*
    Students: Uncomment this out to test your weather location function.
 */
 public void testBuildWeatherLocation() {
   Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION);
   assertNotNull(
       "Error: Null Uri returned.  You must fill-in buildWeatherLocation in " + "WeatherContract.",
       locationUri);
   assertEquals(
       "Error: Weather location not properly appended to the end of the Uri",
       TEST_WEATHER_LOCATION,
       locationUri.getLastPathSegment());
   assertEquals(
       "Error: Weather location Uri doesn't match our expected result",
       locationUri.toString(),
       "content://org.krackedeggs.sunshine/weather/%2FNorth%20Pole");
 }
Пример #2
0
  public void testGetType() {
    // content://com.example.android.sunshine.app/weather/
    String type = mContext.getContentResolver().getType(WeatherContract.WeatherEntry.CONTENT_URI);
    // vnd.android.cursor.dir/com.example.android.sunshine.app/weather
    assertEquals(
        "Error: the WeatherEntry CONTENT_URI should return WeatherEntry.CONTENT_TYPE",
        WeatherContract.WeatherEntry.CONTENT_TYPE,
        type);

    String testLocation = "94074";
    // content://com.example.android.sunshine.app/weather/94074
    type =
        mContext
            .getContentResolver()
            .getType(WeatherContract.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",
        WeatherContract.WeatherEntry.CONTENT_TYPE,
        type);

    long testDate = 1419120000L; // December 21st, 2014
    // content://com.example.android.sunshine.app/weather/94074/20140612
    type =
        mContext
            .getContentResolver()
            .getType(
                WeatherContract.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",
        WeatherContract.WeatherEntry.CONTENT_ITEM_TYPE,
        type);

    // content://com.example.android.sunshine.app/location/
    type = mContext.getContentResolver().getType(WeatherContract.LocationEntry.CONTENT_URI);
    // vnd.android.cursor.dir/com.example.android.sunshine.app/location
    assertEquals(
        "Error: the LocationEntry CONTENT_URI should return LocationEntry.CONTENT_TYPE",
        WeatherContract.LocationEntry.CONTENT_TYPE,
        type);
  }
Пример #3
0
/** Created by wyatt.barnes on 1/28/16. */
public class TestUriMatcher extends AndroidTestCase {
  private static final String LOCATION_QUERY = "London, UK";
  private static final long TEST_LOCATION_ID = 10L;

  // content://com.wyattbarnes.sunshine/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, TestUtilities.TEST_DATE);
  // content://com.wyattbarnes.sunshine/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.",
        WeatherProvider.WEATHER,
        testMatcher.match(TEST_WEATHER_DIR));
    assertEquals(
        "Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
        WeatherProvider.WEATHER_WITH_LOCATION,
        testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR));
    assertEquals(
        "Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
        WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE,
        testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR));
    assertEquals(
        "Error: The LOCATION URI was matched incorrectly.",
        WeatherProvider.LOCATION,
        testMatcher.match(TEST_LOCATION_DIR));
  }
}
Пример #4
0
  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(WeatherContract.LocationEntry.CONTENT_URI, true, tco);
    Uri locationUri =
        mContext.getContentResolver().insert(WeatherContract.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(
                WeatherContract.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(WeatherContract.WeatherEntry.CONTENT_URI, true, tco);

    Uri weatherInsertUri =
        mContext
            .getContentResolver()
            .insert(WeatherContract.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(
                WeatherContract.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(
                WeatherContract.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(
                WeatherContract.WeatherEntry.builWeatherLocationWithStartDate(
                    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(
                WeatherContract.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);
  }