Skip to content

Commit

Permalink
Add content provider
Browse files Browse the repository at this point in the history
This whole content provider thingy is actually
huge bunch of crap wrapped in some more crap mixed with
some other kind of crap. Exactly.
  • Loading branch information
Protino committed Aug 4, 2016
1 parent 9cfd91b commit b1b46be
Show file tree
Hide file tree
Showing 29 changed files with 2,300 additions and 246 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.calgen.prodek.sunshine_v2;

import android.test.suitebuilder.TestSuiteBuilder;

import junit.framework.Test;
import junit.framework.TestSuite;

public class FullTestSuite extends TestSuite {
public static Test suite() {
return new TestSuiteBuilder(FullTestSuite.class)
.includeAllPackagesUnderHere().build();
}

public FullTestSuite() {
super();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.calgen.prodek.sunshine_v2;

import android.annotation.TargetApi;
import android.database.Cursor;
import android.test.AndroidTestCase;

import com.calgen.prodek.sunshine_v2.data.FetchWeatherTask;
import com.calgen.prodek.sunshine_v2.data.WeatherContract;

public class TestFetchWeatherTask extends AndroidTestCase{
static final String ADD_LOCATION_SETTING = "Sunnydale, CA";
static final String ADD_LOCATION_CITY = "Sunnydale";
static final double ADD_LOCATION_LAT = 34.425833;
static final double ADD_LOCATION_LON = -119.714167;

/*
Students: uncomment testAddLocation after you have written the AddLocation function.
This test will only run on API level 11 and higher because of a requirement in the
content provider.
*/
@TargetApi(11)
public void testAddLocation() {
// start from a clean state
getContext().getContentResolver().delete(WeatherContract.LocationEntry.CONTENT_URI,
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
new String[]{ADD_LOCATION_SETTING});

FetchWeatherTask fwt = new FetchWeatherTask(getContext(), null);
long locationId = fwt.addLocation(ADD_LOCATION_SETTING, ADD_LOCATION_CITY,
ADD_LOCATION_LAT, ADD_LOCATION_LON);

// does addLocation return a valid record ID?
assertFalse("Error: addLocation returned an invalid ID on insert",
locationId == -1);

// test all this twice
for ( int i = 0; i < 2; i++ ) {

// does the ID point to our location?
Cursor locationCursor = getContext().getContentResolver().query(
WeatherContract.LocationEntry.CONTENT_URI,
new String[]{
WeatherContract.LocationEntry._ID,
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING,
WeatherContract.LocationEntry.COLUMN_CITY_NAME,
WeatherContract.LocationEntry.COLUMN_COORD_LAT,
WeatherContract.LocationEntry.COLUMN_COORD_LONG
},
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
new String[]{ADD_LOCATION_SETTING},
null);

// these match the indices of the projection
if (locationCursor.moveToFirst()) {
assertEquals("Error: the queried value of locationId does not match the returned value" +
"from addLocation", locationCursor.getLong(0), locationId);
assertEquals("Error: the queried value of location setting is incorrect",
locationCursor.getString(1), ADD_LOCATION_SETTING);
assertEquals("Error: the queried value of location city is incorrect",
locationCursor.getString(2), ADD_LOCATION_CITY);
assertEquals("Error: the queried value of latitude is incorrect",
locationCursor.getDouble(3), ADD_LOCATION_LAT);
assertEquals("Error: the queried value of longitude is incorrect",
locationCursor.getDouble(4), ADD_LOCATION_LON);
} else {
fail("Error: the id you used to query returned an empty cursor");
}

// there should be no more records
assertFalse("Error: there should be only one record returned from a location query",
locationCursor.moveToNext());

// add the location again
long newLocationId = fwt.addLocation(ADD_LOCATION_SETTING, ADD_LOCATION_CITY,
ADD_LOCATION_LAT, ADD_LOCATION_LON);

assertEquals("Error: inserting a location again should return the same ID",
locationId, newLocationId);
}
// reset our state back to normal
getContext().getContentResolver().delete(WeatherContract.LocationEntry.CONTENT_URI,
WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
new String[]{ADD_LOCATION_SETTING});

// clean up the test so that other tests can use the content provider
getContext().getContentResolver().
acquireContentProviderClient(WeatherContract.LocationEntry.CONTENT_URI).
getLocalContentProvider().shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* 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.calgen.prodek.sunshine_v2.data;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

import java.util.HashSet;

public class TestDb extends AndroidTestCase {

public static final String LOG_TAG = TestDb.class.getSimpleName();

// Since we want each test to start with a clean slate
void deleteTheDatabase() {
mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME);
}

/*
This function gets called before each test is executed to delete the database. This makes
sure that we always have a clean test.
*/
public void setUp() {
deleteTheDatabase();
}

/*
Students: Uncomment this test once you've written the code to create the Location
table. Note that you will have to have chosen the same column names that I did in
my solution for this test to compile, so if you haven't yet done that, this is
a good time to change your column names to match mine.
Note that this only tests that the Location table has the correct columns, since we
give you the code for the weather table. This test does not look at the
*/
public void testCreateDb() throws Throwable {
// build a HashSet of all of the table names we wish to look for
// Note that there will be another table in the DB that stores the
// Android metadata (db version information)
final HashSet<String> tableNameHashSet = new HashSet<>();
tableNameHashSet.add(WeatherContract.LocationEntry.TABLE_NAME);
tableNameHashSet.add(WeatherContract.WeatherEntry.TABLE_NAME);

mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME);
SQLiteDatabase db = new WeatherDbHelper(
this.mContext).getWritableDatabase();
assertEquals(true, db.isOpen());

// have we created the tables we want?
Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

assertTrue("Error: This means that the database has not been created correctly",
c.moveToFirst());

// verify that the tables have been created
do {
tableNameHashSet.remove(c.getString(0));
} while (c.moveToNext());

// if this fails, it means that your database doesn't contain both the location entry
// and weather entry tables
assertTrue("Error: Your database was created without both the location entry and weather entry tables",
tableNameHashSet.isEmpty());

// now, do our tables contain the correct columns?
c = db.rawQuery("PRAGMA table_info(" + WeatherContract.LocationEntry.TABLE_NAME + ")",
null);

assertTrue("Error: This means that we were unable to query the database for table information.",
c.moveToFirst());

// Build a HashSet of all of the column names we want to look for
final HashSet<String> locationColumnHashSet = new HashSet<>();
locationColumnHashSet.add(WeatherContract.LocationEntry._ID);
locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_CITY_NAME);
locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LAT);
locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_COORD_LONG);
locationColumnHashSet.add(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING);

int columnNameIndex = c.getColumnIndex("name");
do {
String columnName = c.getString(columnNameIndex);
locationColumnHashSet.remove(columnName);
} while (c.moveToNext());

// if this fails, it means that your database doesn't contain all of the required location
// entry columns
assertTrue("Error: The database doesn't contain all of the required location entry columns",
locationColumnHashSet.isEmpty());
c.close();
db.close();
}

/*
Students: Here is where you will build code to test that we can insert and query the
location database. We've done a lot of work for you. You'll want to look in TestUtilities
where you can uncomment out the "createNorthPoleLocationValues" function. You can
also make use of the ValidateCurrentRecord function from within TestUtilities.
*/
public void testLocationTable() {
// First step: Get reference to writable database
SQLiteDatabase db = new WeatherDbHelper(this.mContext).getWritableDatabase();

// Create ContentValues of what you want to insert
// (you can use the createNorthPoleLocationValues if you wish)
ContentValues values = TestUtilities.createNorthPoleLocationValues();

// Insert ContentValues into database and get a row ID back
long rowID = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, values);

assertTrue(rowID != -1);

// Query the database and receive a Cursor back
Cursor cursor = db.query(WeatherContract.LocationEntry.TABLE_NAME,
null,
null,
null,
null,
null,
null);

// Move the cursor to a valid database row
assertTrue("Empty cursor returned", cursor.moveToFirst());

// Validate data in resulting Cursor with the original ContentValues
// (you can use the validateCurrentRecord function in TestUtilities to validate the
// query if you like)
TestUtilities.validateCurrentRecord("Invalid data returned", cursor, values);

assertFalse("Database returned more than one row", cursor.moveToNext());

// Finally, close the cursor and database
db.close();
db.close();
}

public void testWeatherTable() {
// First insert the location, and then use the locationRowId to insert
// the weather. Make sure to cover as many failure cases as you can.
long locationKey = TestUtilities.insertNorthPoleLocationValues(mContext);
assertTrue(locationKey!=-1);

// Instead of rewriting all of the code we've already written in testLocationTable
// we can move this code to insertLocation and then call insertLocation from both
// tests. Why move it? We need the code to return the ID of the inserted location
// and our testLocationTable can only return void because it's a test.

// First step: Get reference to writable database
WeatherDbHelper weatherDbHelper = new WeatherDbHelper(mContext);
SQLiteDatabase db = weatherDbHelper.getWritableDatabase();

// Create ContentValues of what you want to insert
// (you can use the createWeatherValues TestUtilities function if you wish)
ContentValues values = TestUtilities.createWeatherValues(locationKey);

// Insert ContentValues into database and get a row ID back
long weatherRowId = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, values);

assertTrue(weatherRowId != -1);

// Query the database and receive a Cursor back
Cursor cursor = db.query(WeatherContract.WeatherEntry.TABLE_NAME,
null,
null,
null,
null,
null,
null);
// Move the cursor to a valid database row
TestUtilities.validateCursor("Empty Cursor", cursor, values);

// Finally, close the cursor and database
cursor.close();
db.close();
}


/*
Students: This is a helper method for the testWeatherTable quiz. You can move your
code from testLocationTable to here so that you can call this code from both
testWeatherTable and testLocationTable.
*/
public long insertLocation() {
return -1L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.calgen.prodek.sunshine_v2.data;

import android.test.AndroidTestCase;

public class TestPractice extends AndroidTestCase {
/*
This gets run before every test.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
}

public void testThatDemonstratesAssertions() throws Throwable {
int a = 5;
int b = 3;
int c = 5;
int d = 10;

assertEquals("X should be equal", a, c);
assertTrue("Y should be true", d > a);
assertFalse("Z should be false", a == b);

if (b > d) {
fail("XX should never happen");
}
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
}
}
Loading

0 comments on commit b1b46be

Please sign in to comment.