-
Notifications
You must be signed in to change notification settings - Fork 6
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
Conversation
Add content provider testing
@PaulRashidi Can you take a look at this? It's hardly anything, but it is a start, and gets some unpleasant stuff out of the way for those ready for |
*/ | ||
|
||
public class Constants { | ||
public static final String LOCAL_REPOSITORY_AUTHORITY = "com.g11x.checklistapp.provider"; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 mind blown 🔥
- Can this be added to the top level in build.gradle?
- Please disregard latest change that did it wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, android.defaultConfig element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. PTAL.
The AndroidManifest isn't included so this doesn't yet make the CP available. Just making sure you knew that. |
I realized that, and I just added it. PTAL. |
@RunWith(AndroidJUnit4.class) | ||
public class LocalRepositoryTest extends ProviderTestCase2<LocalRepository> { | ||
public LocalRepositoryTest() { | ||
super(LocalRepository.class, Constants.LOCAL_REPOSITORY_AUTHORITY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PaulRashidi Is there a way to access the resources strings from tests? Trying to figure that out so I can use R.strings.content_provider_authority
, but so far it isn't clear to me how to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String text = InstrumentationRegistry.getInstrumentation().getTargetContext().getString(R.string.about_text);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this context Target = App under test.
Allows sharing same string in both code and manifest.
import android.support.annotation.Nullable; | ||
|
||
/** Application data repository. */ | ||
public class LocalRepository extends ContentProvider { |
There was a problem hiding this comment.
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)
@PaulRashidi I think I've addressed your feedback. PTAL |
lgtm |
@hiwaylon yup, my Q was A'ed, thnx @PaulRashidi |
Writing tests is good.
ContentProvider
calledLocalRepository
. This will serve as the data access point ("repository") for all app data that will be stored locally.LocalRepository
.See #5 for more details.