Last active
March 25, 2018 20:02
-
-
Save gugacavalieri/16b53d7e1e89e80f10e959c13c1d6f52 to your computer and use it in GitHub Desktop.
Android M.V.P Tests With Robolectric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.support.v7.widget.Toolbar; | |
/** | |
* Created by gustavo on 11/28/17. | |
* | |
* Interface for defining a Promotions View | |
* | |
* Insipired by Android Model-View-Presenter Pattern | |
* (https://antonioleiva.com/mvp-android/) | |
* | |
*/ | |
public interface IPromotionsView { | |
/** | |
* method to be called after view was created | |
*/ | |
void afterViews(); | |
/** | |
* method to be called by presenter after view was taken | |
* @param presenter IPromotionsPresenter that is linked to this view | |
*/ | |
void afterViewTaken(IPromotionsPresenter presenter); | |
/** | |
* method to show the share link menu | |
* @param mInvitationUrl Link to be shared as Uri | |
* @return Android share intent | |
*/ | |
Intent showShareMenu(Uri mInvitationUrl); | |
/** | |
* method for showing user referral link to be shared | |
* @param link Url to be shared | |
* @param show Boolean flag indicating if element should be shown or hidden | |
* @return boolean flag indicating success | |
*/ | |
boolean showReferralLink(String link, boolean show); | |
/** | |
* method for showing text view indicating how many referrals a user has | |
* @param referrals Number of referrals as String | |
* @param show Boolean flag indicating if element should be shown or hidden | |
* @return boolean flag indicating success | |
*/ | |
boolean showNumberReferrals(String referrals, boolean show); | |
/** | |
* method for showing / hidding loading screen | |
* @param show Boolean flag indicating if element should be shown or hidden | |
* @return boolean flag indicating success | |
*/ | |
boolean showLoading(boolean show); | |
/** | |
* method for configuring view's toolbar | |
* @param toolbar Android Toolbar present in view | |
* @return boolean flag indicating success | |
*/ | |
boolean configureToolbar(Toolbar toolbar); | |
/** | |
* method for displaying error message | |
* @param e Error as Java exception | |
* @return boolean flag indicating success | |
*/ | |
boolean showError(Exception e); | |
/** | |
* method for returning Android Context | |
* @return Android context | |
*/ | |
Context getContext(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.support.design.widget.CoordinatorLayout; | |
import android.support.design.widget.Snackbar; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import org.androidannotations.annotations.AfterViews; | |
import org.androidannotations.annotations.Click; | |
import org.androidannotations.annotations.EActivity; | |
import org.androidannotations.annotations.ViewById; | |
import org.androidannotations.annotations.sharedpreferences.Pref; | |
/** | |
* Created by gustavo on 11/28/17. | |
* | |
* Implementation of a Promotions View | |
*/ | |
@EActivity(R.layout.activity_promotions) | |
public class PromotionsView extends AppCompatActivity implements IPromotionsView { | |
/* start of view binding */ | |
/* we are using android annotations */ | |
/* (http://androidannotations.org/) */ | |
@ViewById(R.id.toolbar) | |
Toolbar toolbar; | |
@ViewById(R.id.loadingContainer) | |
View loadingContainer; | |
@ViewById(R.id.shareLinkBtn) | |
Button shareLinkBtn; | |
@ViewById(R.id.referralsCounterTextView) | |
TextView referralsCounterTextView; | |
@ViewById(R.id.coordinatorLayout) | |
CoordinatorLayout coordinatorLayout; | |
@Pref | |
Prefs_ userPrefs; | |
/* presenter for controlling view */ | |
IPromotionsPresenter presenter; | |
/* other variables */ | |
Snackbar mySnackbar; | |
/** | |
* public constructor | |
* @return A new Android Intent to this activity | |
*/ | |
public static Intent getIntent(Context context) { | |
return new Intent(context, PromotionsView_.class); | |
} | |
/** | |
* method to be called after view was created | |
*/ | |
@AfterViews | |
@Override | |
public void afterViews() { | |
/* create presenter for controlling view */ | |
presenter = new PromotionsPresenter(userPrefs); | |
presenter.takeView(this); | |
presenter.getNumberOfReferrals(); | |
presenter.getReferralLink(); | |
configureToolbar(toolbar); | |
} | |
/** | |
* method to be called by presenter after view was taken | |
* @param presenter IPromotionsPresenter that is linked to this view | |
*/ | |
@Override | |
public void afterViewTaken(IPromotionsPresenter presenter) { | |
/* do nothing for now */ | |
this.presenter = presenter; | |
} | |
/** | |
* method to show the share link menu | |
* @param mInvitationUrl Link to be shared as Uri | |
* @return Android share intent | |
*/ | |
@Override | |
public Intent showShareMenu(Uri mInvitationUrl) { | |
try { | |
/* create android intent to share */ | |
String textToShare = getString(R.string.share_html_message, mInvitationUrl.toString()); | |
Intent intent = new Intent(android.content.Intent.ACTION_SEND); | |
intent.setType("text/html"); | |
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.download_our_app)); | |
intent.putExtra(Intent.EXTRA_TEXT, textToShare); | |
startActivity(Intent.createChooser(intent, getString(R.string.choose_way_to_share))); | |
return intent; | |
} catch (Exception e) { | |
showError(e); | |
return null; | |
} | |
} | |
/** | |
* method for showing user referral link to be shared | |
* @param link Url to be shared | |
* @param show Boolean flag indicating if element should be shown or hidden | |
* @return boolean flag indicating success | |
*/ | |
@Override | |
public boolean showReferralLink(String link, boolean show) { | |
if(show) { | |
shareLinkBtn.setVisibility(View.VISIBLE); | |
} else { | |
shareLinkBtn.setVisibility(View.INVISIBLE); | |
} | |
return show; | |
} | |
/** | |
* method for showing text view indicating how many referrals a user has | |
* @param referrals Number of referrals as String | |
* @param show Boolean flag indicating if element should be shown or hidden | |
* @return boolean flag indicating success | |
*/ | |
@Override | |
public boolean showNumberReferrals(String referrals, boolean show) { | |
/* update text view with number of referrals */ | |
referralsCounterTextView.setText(getString(R.string.you_have_referred, referrals)); | |
/* check if we should show or hide our text view */ | |
if(show) { | |
AnimationsUtils.fadeIn(referralsCounterTextView); | |
} else { | |
AnimationsUtils.fadeOut(referralsCounterTextView); | |
} | |
return show; | |
} | |
/** | |
* method for showing / hidding loading screen | |
* @param show Boolean flag indicating if element should be shown or hidden | |
* @return boolean flag indicating success | |
*/ | |
@Override | |
public boolean showLoading(boolean show) { | |
/* check if we should show or hide our loading widget */ | |
if(show) { | |
loadingContainer.setVisibility(View.VISIBLE); | |
} else { | |
loadingContainer.setVisibility(View.GONE); | |
} | |
return show; | |
} | |
/** | |
* method for configuring view's toolbar | |
* @param toolbar Android Toolbar present in view | |
* @return boolean flag indicating success | |
*/ | |
@Override | |
public boolean configureToolbar(Toolbar toolbar) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
Window window = this.getWindow(); | |
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); | |
window.setStatusBarColor(ContextCompat.getColor(this, R.color.status_bar_green)); | |
} | |
/* configure toolbar colors */ | |
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.snotGreen)); | |
setSupportActionBar(toolbar); | |
assert getSupportActionBar() != null; | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
getSupportActionBar().setTitle(getString(R.string.promotions)); | |
return false; | |
} | |
/** | |
* method for displaying error message | |
* @param e Error as Java exception | |
* @return boolean flag indicating success | |
*/ | |
@Override | |
public boolean showError(Exception e) { | |
/* show error with Android Snackbar */ | |
mySnackbar = Snackbar.make(coordinatorLayout, | |
e.getMessage(), Snackbar.LENGTH_LONG); | |
mySnackbar.show(); | |
return true; | |
} | |
/** | |
* method for returning Android Context | |
* @return Android context | |
*/ | |
@Override | |
public Context getContext() { | |
return this; | |
} | |
/* lifecycle methods */ | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
presenter.onViewStart(); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
presenter.onViewStop(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
presenter.onViewStop(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
presenter.onViewStart(); | |
} | |
/** | |
* method called when user clicks on shareLinkBtn | |
*/ | |
@Click(R.id.shareLinkBtn) | |
void onBtnShareClick() { | |
/* call presenter to fetch data */ | |
presenter.onBtnShareClick(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.drawable.ColorDrawable; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v4.content.ContextCompat; | |
import android.view.View; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Mock; | |
import org.mockito.Mockito; | |
import org.mockito.MockitoAnnotations; | |
import org.robolectric.Robolectric; | |
import org.robolectric.RobolectricTestRunner; | |
import org.robolectric.RuntimeEnvironment; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertTrue; | |
/** | |
* Created by gustavo on 11/30/17. | |
* | |
* Test class for Promotions View | |
* @see PromotionsView | |
*/ | |
@RunWith(RobolectricTestRunner.class) | |
public class PromotionsViewTest { | |
private PromotionsView activity; | |
private Context context; | |
/* mock our presenter */ | |
@Mock | |
private | |
IPromotionsPresenter presenter; | |
private String shareUrl; | |
/** | |
* set up test environment | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Before | |
public void setUp() throws Exception { | |
/* init our mocks ! */ | |
MockitoAnnotations.initMocks(this); | |
/* get context from robolectric */ | |
context = RuntimeEnvironment.application; | |
/* build our activity and bind presenter */ | |
activity = Robolectric.buildActivity(PromotionsView_.class).create().get(); | |
activity.presenter = presenter; | |
shareUrl = "https://google.com"; | |
} | |
/** | |
* assert that test variables were created correctly | |
*/ | |
@Test | |
public void init() { | |
assertNotNull(activity); | |
assertNotNull(presenter); | |
assertNotNull(context); | |
/* assert that presenter is linked with activity */ | |
assertEquals(presenter, activity.presenter); | |
} | |
/** | |
* test getIntent method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void getIntent() throws Exception { | |
/* call method */ | |
Intent intent = PromotionsView.getIntent(context); | |
/* assert that intent was created correctly */ | |
assertNotNull(intent); | |
assertNotNull(intent.getComponent()); | |
assertEquals(PromotionsView_.class.getCanonicalName(), intent.getComponent().getClassName()); | |
} | |
/** | |
* test afterViewTaken method | |
* assert that presenter matches view | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void afterViewTaken() throws Exception { | |
assertEquals(presenter, activity.presenter); | |
} | |
/** | |
* test showShareLinkModal | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showShareLinkModal() throws Exception { | |
Uri uriToShare = Uri.parse(shareUrl); | |
/* call method */ | |
Intent intent = activity.showShareMenu(uriToShare); | |
Bundle intentExtras = intent.getExtras(); | |
/* assert that share intent was created with correct parameters */ | |
String textToShare = context.getString(R.string.share_html_message, uriToShare.toString()); | |
assertNotNull(intentExtras); | |
assertEquals(textToShare, intentExtras.getString(Intent.EXTRA_TEXT)); | |
assertEquals(context.getString(R.string.download_our_app), intentExtras.getString(Intent.EXTRA_SUBJECT)); | |
} | |
/** | |
* test showReferralLink when true parameter is passed | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showReferralLinkTrue() throws Exception { | |
/* call method */ | |
assertTrue(activity.showReferralLink(shareUrl, true)); | |
/* assert button is visible */ | |
assertEquals(View.VISIBLE, activity.shareLinkBtn.getVisibility()); | |
} | |
/** | |
* test showReferralLink when false parameter is passed | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showReferralLinkFalse() throws Exception { | |
/* call method */ | |
assertFalse(activity.showReferralLink(shareUrl, false)); | |
/* assert that button is invisble */ | |
assertEquals(View.INVISIBLE, activity.shareLinkBtn.getVisibility()); | |
} | |
/** | |
* test showNumberReferrals with true parameter | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showNumberReferralsTrue() throws Exception { | |
String numberOfReferrals = "5"; | |
/* call method and make sure referralsCounterTextView is visible with right text */ | |
assertTrue(activity.showNumberReferrals(numberOfReferrals, true)); | |
assertEquals(View.VISIBLE, activity.referralsCounterTextView.getVisibility()); | |
assertEquals(context.getString(R.string.you_have_referred, numberOfReferrals), activity.referralsCounterTextView.getText().toString()); | |
} | |
/** | |
* test showNumberReferrals with false parameter | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showNumberReferralsFalse() throws Exception { | |
String numberOfReferrals = "5"; | |
/* call method and make sure referralsCounterTextView is invisible and has right text */ | |
assertFalse(activity.showNumberReferrals(numberOfReferrals, false)); | |
assertEquals(View.INVISIBLE, activity.referralsCounterTextView.getVisibility()); | |
assertEquals(context.getString(R.string.you_have_referred, numberOfReferrals), activity.referralsCounterTextView.getText().toString()); | |
} | |
/** | |
* test showLoading with true parameter | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showLoadingTrue() throws Exception { | |
/* call method and assert that loadingContainer is visible */ | |
assertTrue(activity.showLoading(true)); | |
assertEquals(View.VISIBLE, activity.loadingContainer.getVisibility()); | |
} | |
/** | |
* test showLoading with false parameter | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showLoadingFalse() throws Exception { | |
/* call method and assert that loadingContainer is GONE */ | |
assertFalse(activity.showLoading(false)); | |
assertEquals(View.GONE, activity.loadingContainer.getVisibility()); | |
} | |
/** | |
* test configureToolbar method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void configureToolbar() throws Exception { | |
/* call method */ | |
activity.configureToolbar(activity.toolbar); | |
/* assert that toolbar was set with right color */ | |
int toolbarColor = ((ColorDrawable) activity.toolbar.getBackground()).getColor(); | |
assertEquals(ContextCompat.getColor(context, R.color.snotGreen), toolbarColor); | |
} | |
/** | |
* test showError() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void showError() throws Exception { | |
/* create exception and call method */ | |
Exception exception = new Exception("Opss! This is an exception"); | |
activity.showError(exception); | |
/* assert snackbar is showing error */ | |
assertTrue(activity.mySnackbar.isShownOrQueued()); | |
} | |
/** | |
* test getContext() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void getContext() throws Exception { | |
assertNotNull(activity.getContext()); | |
} | |
/** | |
* test onStart() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void onStart() throws Exception { | |
/* call method and assert that presenter is notified */ | |
activity.onStart(); | |
Mockito.verify(presenter, Mockito.atLeastOnce()).onViewStart(); | |
} | |
/** | |
* test onStop() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void onStop() throws Exception { | |
/* call method and assert that presenter is notified */ | |
activity.onStop(); | |
Mockito.verify(presenter, Mockito.atLeastOnce()).onViewStop(); | |
} | |
/** | |
* test onPause() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void onPause() throws Exception { | |
/* call method and assert that presenter is notified */ | |
activity.onPause(); | |
Mockito.verify(presenter, Mockito.atLeastOnce()).onViewStop(); | |
} | |
/** | |
* test onResume() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void onResume() throws Exception { | |
/* call method and assert that presenter is notified */ | |
activity.onResume(); | |
Mockito.verify(presenter, Mockito.atLeastOnce()).onViewStart(); | |
} | |
/** | |
* test onBtnShareClick() method | |
* @throws Exception Any Exception that might occur | |
*/ | |
@Test | |
public void onBtnShareClick() throws Exception { | |
/* call method and assert that presenter is notified */ | |
activity.onBtnShareClick(); | |
Mockito.verify(presenter, Mockito.atLeastOnce()).onBtnShareClick(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment