Skip to content

Instantly share code, notes, and snippets.

package co.ortatech.showandhide.service;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import javax.inject.Inject;
import co.ortatech.showandhide.application.ShowAndHideApplication;
package co.ortatech.showandhide.ui.activity;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.net.Uri;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.intent.Intents;
import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4;
@Test
public void testTapInspireButton() {
onView(withId(R.id.home_inspire_button)).check(matches(withText("Inspire")));
onView(withId(R.id.home_inspire_button)).perform(click());
// Capture web browser intent.
intended(toPackage("com.android.browser"));
Intents.assertNoUnverifiedIntents();
}
@Test
public void testTapCameraButtonAndCancel() {
// Uri needed to launch the Camera intent.
Uri uri = Uri.parse("uri_string");
stub(fileUtilities.getOutputMediaFileUri()).toReturn(uri);
// Build a result to return when the activity is launched.
Intent resultData = new Intent();
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, resultData);
@Test
public void testTapCameraButtonAndReturnOK() {
// Uri needed to launch the Camera intent.
Uri uri = Uri.parse("uri_string");
stub(fileUtilities.getOutputMediaFileUri()).toReturn(uri);
// Build a result to return when the activity is launched.
Intent resultData = new Intent();
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
@Test
public void testTapGalleryButtonAndReturnCancel() {
// Build a result to return when the activity is launched.
Intent resultData = new Intent();
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, resultData);
// Set up result stubbing when an intent sent to "choose photo" is seen.
intending(toPackage("com.android.gallery")).respondWith(result);
private static class HomeActivityTestRule extends IntentsTestRule {
private HomeActivityTestRule() {
super(HomeActivity.class);
}
@Override public void beforeActivityLaunched() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
ShowAndHideApplication app =
(ShowAndHideApplication) instrumentation.getTargetContext().getApplicationContext();
ShowAndHideTestComponent component = DaggerShowAndHideTestComponent.builder()
@Test
public void testTapGalleryButtonAndReturnOK() {
// Stub the Uri returned by the gallery intent.
Uri uri = Uri.parse("uri_string");
// Build a result to return when the activity is launched.
Intent resultData = new Intent();
resultData.setData(uri);
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
@Test
public void testLaunchActivity() {
onView(withId(R.id.home_camera_button)).check(matches(withText("Camera")));
onView(withId(R.id.home_gallery_button)).check(matches(withText("Gallery")));
onView(withId(R.id.home_inspire_button)).check(matches(withText("Inspire")));
}
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
originalImageView.setVisibility(event.getAction() == MotionEvent.ACTION_DOWN ?
View.VISIBLE : View.GONE);
return true;
}
});