Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basics for ContentProvider testing. #7

Merged
merged 11 commits into from
Aug 16, 2016
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
resValue("string", "content_provider_authority", "com.g11x.checklistapp.provider")
}
buildTypes {
release {
Expand All @@ -49,4 +50,5 @@ dependencies {
compile 'com.android.support:recyclerview-v7:24.1.0'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.g11x.checklistapp;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ProviderTestCase2;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Tests for the app's ContentProvider.
*/
@RunWith(AndroidJUnit4.class)
public class LocalRepositoryTest extends ProviderTestCase2<LocalRepository> {
public LocalRepositoryTest() {
super(LocalRepository.class, InstrumentationRegistry.getInstrumentation().getTargetContext().getString(R.string.content_provider_authority));
}

@Override
protected void setUp() throws Exception {
setContext(InstrumentationRegistry.getTargetContext());
super.setUp();
}

@Test
public void shouldFetchData() {
}
}
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
<activity android:name=".ChecklistItemActivity">
</activity>
<activity android:name=".AboutActivity"/>
<provider
android:authorities="@string/content_provider_authority"
android:name=".LocalRepository"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These names are like tables in a database, right? If so, how about something specific to the items in the table, like .ChecklistItems?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Authorities are like databases, not tables within the db. It would be rather rare for a single app to have more than one.

android:exported="false" />
</application>

</manifest>
9 changes: 9 additions & 0 deletions app/src/main/java/com/g11x/checklistapp/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.g11x.checklistapp;

/**
* Created by wcalabrese on 8/15/16.
*/

public class Constants {
public static final String LOCAL_REPOSITORY_AUTHORITY = "com.g11x.checklistapp.provider";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can declare this in build.gradle as a res property and then use it in the manifest as well as within the app getString(context, R.string.thing).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is src/main/res/values/strings.xml the correct place?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In build.gradle add resValue("string", "content_provider_authority", "packagename")
That auto-adds it to the strings file and also lets you vary it based on build type. So you can install prod and non-prod versions at the same time (Content Provider authorities must be unique on a device).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 mind blown 🔥

  1. Can this be added to the top level in build.gradle?
  2. Please disregard latest change that did it wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, android.defaultConfig element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. PTAL.

}
43 changes: 43 additions & 0 deletions app/src/main/java/com/g11x/checklistapp/LocalRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.g11x.checklistapp;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;

/** Application data repository. */
public class LocalRepository extends ContentProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a question that got buried in a previous commit. Just so it doesn't get lost in the shuffle: #7 (diff)

@Override
public boolean onCreate() {
return false;
}

@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
return null;
}

@Nullable
@Override
public String getType(Uri uri) {
return null;
}

@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
return null;
}

@Override
public int delete(Uri uri, String s, String[] strings) {
return 0;
}

@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
return 0;
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-alpha7'
classpath 'com.android.tools.build:gradle:2.2.0-beta1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down