コード例 #1
0
  /*
     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);
  }
コード例 #2
0
  // 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);
  }