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
Merged

Conversation

hiwaylon
Copy link
Contributor

Writing tests is good.

  • Added an empty ContentProvider called LocalRepository. This will serve as the data access point ("repository") for all app data that will be stored locally.
  • Added basic testing skeleton for LocalRepository.
  • Realized that running all of Android to run a simple test isn't great. Roboelectric will be helpful for that.

See #5 for more details.

@hiwaylon
Copy link
Contributor Author

@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 ContentProvider action.

*/

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.

@PaulRashidi
Copy link
Contributor

The AndroidManifest isn't included so this doesn't yet make the CP available. Just making sure you knew that.

@hiwaylon
Copy link
Contributor Author

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);
Copy link
Contributor Author

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.

Copy link
Contributor

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);

Copy link
Contributor

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 {
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)

@hiwaylon
Copy link
Contributor Author

@PaulRashidi I think I've addressed your feedback. PTAL
@aaronj1335 I think Paul answered your question, correct?

@PaulRashidi
Copy link
Contributor

lgtm

@aaronj1335
Copy link
Contributor

@hiwaylon yup, my Q was A'ed, thnx @PaulRashidi :shipit:

@hiwaylon hiwaylon merged commit 8e3b5a6 into g11x:master Aug 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants