Skip to content

Commit

Permalink
Fix some codacy issues
Browse files Browse the repository at this point in the history
Also add the badge
  • Loading branch information
Protino committed Feb 28, 2017
1 parent 9fe5814 commit cddbca3
Show file tree
Hide file tree
Showing 19 changed files with 189 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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() {
private void deleteTheDatabase() {
mContext.deleteDatabase(WeatherDbHelper.DATABASE_NAME);
}

Expand Down
56 changes: 26 additions & 30 deletions app/src/androidTest/java/io/github/protino/data/TestProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@
public class TestProvider extends AndroidTestCase {

public static final String LOG_TAG = TestProvider.class.getSimpleName();
static private final int BULK_INSERT_RECORDS_TO_INSERT = 10;

public 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;
}

/*
This helper function deletes all records from both database tables using the ContentProvider.
Expand Down Expand Up @@ -131,7 +154,7 @@ public void testProviderRegistry() {

// 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,
" instead of authority: " + WeatherContract.CONTENT_AUTHORITY,
providerInfo.authority, WeatherContract.CONTENT_AUTHORITY);
} catch (PackageManager.NameNotFoundException e) {
// I guess the provider isn't registered correctly.
Expand Down Expand Up @@ -176,7 +199,6 @@ public void testGetType() {
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
Expand All @@ -187,7 +209,6 @@ public void testBasicWeatherQuery() {
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!
Expand Down Expand Up @@ -238,7 +259,7 @@ public void testBasicLocationQueries() {

// 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 ) {
if (Build.VERSION.SDK_INT >= 19) {
assertEquals("Error: Location Query did not properly set NotificationUri",
locationCursor.getNotificationUri(), LocationEntry.CONTENT_URI);
}
Expand Down Expand Up @@ -273,7 +294,7 @@ public void testUpdateLocation() {

int count = mContext.getContentResolver().update(
LocationEntry.CONTENT_URI, updatedValues, LocationEntry._ID + "= ?",
new String[] { Long.toString(locationRowId)});
new String[]{Long.toString(locationRowId)});
assertEquals(count, 1);

// Test to make sure our observer is called. If not, we throw an assertion.
Expand All @@ -300,7 +321,6 @@ public void testUpdateLocation() {
cursor.close();
}


// Make sure we can still delete after adding/updating stuff
//
// Student: Uncomment this test after you have completed writing the insert functionality
Expand Down Expand Up @@ -435,30 +455,6 @@ public void testDeleteRecords() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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;
Expand Down
42 changes: 21 additions & 21 deletions app/src/androidTest/java/io/github/protino/data/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@
import android.os.HandlerThread;
import android.test.AndroidTestCase;

import io.github.protino.utils.PollingCheck;

import java.util.Map;
import java.util.Set;

import io.github.protino.utils.PollingCheck;

/*
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
public static final String TEST_LOCATION = "99705";
public static final long TEST_DATE = 1419033600L; // December 20th, 2014

static void validateCursor(String error, Cursor valueCursor, ContentValues expectedValues) {
public 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) {
public 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();
Expand All @@ -62,7 +62,7 @@ static void validateCurrentRecord(String error, Cursor valueCursor, ContentValue
/*
Students: Use this to create some default weather values for your database tests.
*/
static ContentValues createWeatherValues(long locationRowId) {
public 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);
Expand All @@ -82,7 +82,7 @@ static ContentValues createWeatherValues(long locationRowId) {
Students: You can uncomment this helper function once you have finished creating the
LocationEntry part of the WeatherContract.
*/
static ContentValues createNorthPoleLocationValues() {
public 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);
Expand All @@ -97,7 +97,7 @@ static ContentValues createNorthPoleLocationValues() {
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) {
public static long insertNorthPoleLocationValues(Context context) {
// insert our test records into the database
WeatherDbHelper dbHelper = new WeatherDbHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Expand All @@ -112,6 +112,10 @@ static long insertNorthPoleLocationValues(Context context) {
return locationRowId;
}

public static TestContentObserver getTestContentObserver() {
return TestContentObserver.getTestContentObserver();
}

/*
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
Expand All @@ -120,21 +124,21 @@ static long insertNorthPoleLocationValues(Context context) {
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;
public static class TestContentObserver extends ContentObserver {
public final HandlerThread mHT;
public boolean mContentChanged;

private TestContentObserver(HandlerThread ht) {
super(new Handler(ht.getLooper()));
mHT = ht;
}

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) {
Expand All @@ -160,8 +164,4 @@ protected boolean check() {
mHT.quit();
}
}

static TestContentObserver getTestContentObserver() {
return TestContentObserver.getTestContentObserver();
}
}
28 changes: 14 additions & 14 deletions app/src/androidTest/java/io/github/protino/utils/PollingCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public PollingCheck(long timeout) {
mTimeout = 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());
}

protected abstract boolean check();

public void run() {
Expand All @@ -54,18 +68,4 @@ public void run() {

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());
}
}
42 changes: 23 additions & 19 deletions app/src/main/java/io/github/protino/ItemChoiceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public class ItemChoiceManager {
/**
* Running state of which positions are currently checked
*/
SparseBooleanArray mCheckStates = new SparseBooleanArray();
private SparseBooleanArray mCheckStates = new SparseBooleanArray();
/**
* Running state of which IDs are currently checked.
* If there is a value for a given key, the checked state for that ID is true
* and the value holds the last known position in the adapter for that id.
*/
LongSparseArray<Integer> mCheckedIdStates = new LongSparseArray<Integer>();
private LongSparseArray<Integer> mCheckedIdStates = new LongSparseArray<Integer>();
private int mChoiceMode;
private RecyclerView.Adapter mAdapter;
private RecyclerView.AdapterDataObserver mAdapterDataObserver = new RecyclerView.AdapterDataObserver() {
Expand All @@ -66,6 +66,25 @@ public ItemChoiceManager(RecyclerView.Adapter adapter) {
mAdapter = adapter;
}

//Lifecycle start
public void onRestoreInstanceState(Bundle savedInstanceState) {
byte[] states = savedInstanceState.getByteArray(SELECTED_ITEMS_KEY);
if (null != states) {
Parcel inParcel = Parcel.obtain();
inParcel.unmarshall(states, 0, states.length);
inParcel.setDataPosition(0);
mCheckStates = inParcel.readSparseBooleanArray();
final int numStates = inParcel.readInt();
mCheckedIdStates.clear();
for (int i = 0; i < numStates; i++) {
final long key = inParcel.readLong();
final int value = inParcel.readInt();
mCheckedIdStates.put(key, value);
}
}
}
//Lifecycle end

public void onClick(RecyclerView.ViewHolder vh) {
if (mChoiceMode == AbsListView.CHOICE_MODE_NONE)
return;
Expand Down Expand Up @@ -110,6 +129,8 @@ public void onClick(RecyclerView.ViewHolder vh) {
case AbsListView.CHOICE_MODE_MULTIPLE_MODAL: {
throw new RuntimeException("Multiple Modal not implemented in ItemChoiceManager.");
}
default://ignore
break;
}
}

Expand Down Expand Up @@ -188,23 +209,6 @@ public void onBindViewHolder(RecyclerView.ViewHolder vh, int position) {
ViewCompat.setActivated(vh.itemView, checked);
}

public void onRestoreInstanceState(Bundle savedInstanceState) {
byte[] states = savedInstanceState.getByteArray(SELECTED_ITEMS_KEY);
if (null != states) {
Parcel inParcel = Parcel.obtain();
inParcel.unmarshall(states, 0, states.length);
inParcel.setDataPosition(0);
mCheckStates = inParcel.readSparseBooleanArray();
final int numStates = inParcel.readInt();
mCheckedIdStates.clear();
for (int i = 0; i < numStates; i++) {
final long key = inParcel.readLong();
final int value = inParcel.readInt();
mCheckedIdStates.put(key, value);
}
}
}

public void onSaveInstanceState(Bundle outState) {
Parcel outParcel = Parcel.obtain();
outParcel.writeSparseBooleanArray(mCheckStates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class MainActivity extends AppCompatActivity implements ForecastFragment.
private static boolean mTwoPane;
private String mLocation;

//Lifecycle start
//Lifecycle start
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -130,6 +130,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_refresh:
SunshineSyncAdapter.syncImmediately(this);
return true;
default: //ignore
break;
}
return super.onOptionsItemSelected(item);
}
Expand Down
Loading

0 comments on commit cddbca3

Please sign in to comment.