Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Robolectric 3.0 for Beginners

Robolectric 3.0 for Beginners

本当に初めての人向け

Fumihiko Shiroyama

June 09, 2016
Tweet

More Decks by Fumihiko Shiroyama

Other Decks in Programming

Transcript

  1. @Test
 public void test_label_text_change() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 TextView

    textViewLabel = ButterKnife.findById(activity, R.id.label);
 assertThat(textViewLabel.getVisibility(), is(View.VISIBLE));
 assertThat(textViewLabel.getText().toString(), equalTo("LABEL BEFORE"));
 
 Button buttonCenter = ButterKnife.findById(activity, R.id.center_button);
 buttonCenter.performClick();
 assertThat(textViewLabel.getVisibility(), is(View.VISIBLE));
 assertThat(textViewLabel.getText().toString(), equalTo("LABEL AFTER"));
 }
  2. @Test
 public void test_label_text_change() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 TextView

    textViewLabel = ButterKnife.findById(activity, R.id.label);
 assertThat(textViewLabel.getVisibility(), is(View.VISIBLE));
 assertThat(textViewLabel.getText().toString(), equalTo("LABEL BEFORE"));
 
 Button buttonCenter = ButterKnife.findById(activity, R.id.center_button);
 buttonCenter.performClick();
 assertThat(textViewLabel.getVisibility(), is(View.VISIBLE));
 assertThat(textViewLabel.getText().toString(), equalTo("LABEL AFTER"));
 } "DUJWJUZΛ४උ
  3. @Test
 public void test_label_text_change() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 TextView

    textViewLabel = ButterKnife.findById(activity, R.id.label);
 assertThat(textViewLabel.getVisibility(), is(View.VISIBLE));
 assertThat(textViewLabel.getText().toString(), equalTo("LABEL BEFORE"));
 
 Button buttonCenter = ButterKnife.findById(activity, R.id.center_button);
 buttonCenter.performClick();
 assertThat(textViewLabel.getVisibility(), is(View.VISIBLE));
 assertThat(textViewLabel.getText().toString(), equalTo("LABEL AFTER"));
 } ΫϦοΫ
  4. @Test
 public void test_next_activity() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 Button

    buttonUpper = ButterKnife.findById(activity, R.id.upper_button);
 buttonUpper.performClick();
 
 Intent expectedIntent = HogeActivity.newIntent(activity);
 assertThat(expectedIntent, is(Shadows.shadowOf(activity).getNextStartedActivity()));
 }
  5. @Test
 public void test_next_activity() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 Button

    buttonUpper = ButterKnife.findById(activity, R.id.upper_button);
 buttonUpper.performClick();
 
 Intent expectedIntent = HogeActivity.newIntent(activity);
 assertThat(expectedIntent, is(Shadows.shadowOf(activity).getNextStartedActivity()));
 } ։࢝͞Εͨ*OUFOU
  6. @Test
 public void test_lifecycle() {
 ActivityController<MainActivity> activityController = Robolectric.buildActivity(MainActivity.class);
 MainActivity

    mainActivity = activityController.create().visible().get();
 TextView lifecycle = ButterKnife.findById(mainActivity, R.id.lifecycle);
 assertThat(lifecycle.getText().toString(), equalTo("onCreate"));
 
 activityController.start().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onStart"));
 
 activityController.resume().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onResume"));
 
 activityController.pause().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onPause"));
 
 activityController.destroy().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onDestroy"));
 }
  7. @Test
 public void test_lifecycle() {
 ActivityController<MainActivity> activityController = Robolectric.buildActivity(MainActivity.class);
 MainActivity

    mainActivity = activityController.create().visible().get();
 TextView lifecycle = ButterKnife.findById(mainActivity, R.id.lifecycle);
 assertThat(lifecycle.getText().toString(), equalTo("onCreate"));
 
 activityController.start().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onStart"));
 
 activityController.resume().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onResume"));
 
 activityController.pause().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onPause"));
 
 activityController.destroy().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onDestroy"));
 } ४උ
  8. @Test
 public void test_lifecycle() {
 ActivityController<MainActivity> activityController = Robolectric.buildActivity(MainActivity.class);
 MainActivity

    mainActivity = activityController.create().visible().get();
 TextView lifecycle = ButterKnife.findById(mainActivity, R.id.lifecycle);
 assertThat(lifecycle.getText().toString(), equalTo("onCreate"));
 
 activityController.start().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onStart"));
 
 activityController.resume().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onResume"));
 
 activityController.pause().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onPause"));
 
 activityController.destroy().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onDestroy"));
 } PO$SFBUF PO4UBSU PO3FTVNF PO1BVTF PO%FTUSPZ
  9. @Test
 public void test_lifecycle() {
 ActivityController<MainActivity> activityController = Robolectric.buildActivity(MainActivity.class);
 MainActivity

    mainActivity = activityController.create().visible().get();
 TextView lifecycle = ButterKnife.findById(mainActivity, R.id.lifecycle);
 assertThat(lifecycle.getText().toString(), equalTo("onCreate"));
 
 activityController.start().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onStart"));
 
 activityController.resume().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onResume"));
 
 activityController.pause().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onPause"));
 
 activityController.destroy().visible().get();
 assertThat(lifecycle.getText().toString(), equalTo("onDestroy"));
 } 7JFXͷඳըޙΛอূ
  10. @Test
 @Config(qualifiers = "en-port")
 public void test_multilingual_en() {
 MainActivity activity

    = Robolectric.setupActivity(MainActivity.class);
 TextView multilingual = ButterKnife.findById(activity, R.id.multilingual);
 assertThat(multilingual.getText().toString(), equalTo("English"));
 }
 
 @Test
 @Config(qualifiers = "ja-port")
 public void test_multilingual_ja() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 TextView multilingual = ButterKnife.findById(activity, R.id.multilingual);
 assertThat(multilingual.getText().toString(), equalTo("೔ຊޠ"));
 }
  11. @Test
 @Config(qualifiers = "en-port")
 public void test_multilingual_en() {
 MainActivity activity

    = Robolectric.setupActivity(MainActivity.class);
 TextView multilingual = ButterKnife.findById(activity, R.id.multilingual);
 assertThat(multilingual.getText().toString(), equalTo("English"));
 }
 
 @Test
 @Config(qualifiers = "ja-port")
 public void test_multilingual_ja() {
 MainActivity activity = Robolectric.setupActivity(MainActivity.class);
 TextView multilingual = ButterKnife.findById(activity, R.id.multilingual);
 assertThat(multilingual.getText().toString(), equalTo("೔ຊޠ"));
 }