Last active
February 12, 2021 17:13
-
-
Save up1/20725a7e236465b4509f21f025a4054a to your computer and use it in GitHub Desktop.
Android Testing :: Custom View 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
dependencies { | |
... | |
testCompile "org.robolectric:robolectric:3.1.1" | |
... | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<com.workshop.customviewdemo.view.LoadingView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content"> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/loading_view_spinner" | |
android:visibility="visible" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:gravity="center" | |
android:padding="10dip" | |
> | |
<ProgressBar | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Loading..." | |
android:paddingLeft="10dip" | |
/> | |
</LinearLayout> | |
<TextView | |
android:id="@+id/loading_text_view" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:visibility="invisible" | |
/> | |
</com.workshop.customviewdemo.view.LoadingView> |
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
public class LoadingView extends RelativeLayout { | |
public LoadingView(Context context) { | |
super(context); | |
} | |
public LoadingView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public LoadingView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
public void stopLoadingAndSetText(int resourceId) { | |
TextView textView = (TextView) findViewById(R.id.loading_text_view); | |
textView.setText(resourceId); | |
textView.setVisibility(View.VISIBLE); | |
findViewById(R.id.loading_view_spinner).setVisibility(View.GONE); | |
} | |
} |
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
@RunWith(RobolectricTestRunner.class) | |
@Config(constants = BuildConfig.class, sdk = 21) | |
public class LoadingViewTest { | |
private LoadingView loadingView; | |
private View loadingSpinner; | |
private TextView loadingTextView; | |
@Before | |
public void setUp() throws Exception { | |
ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class); | |
loadingView = (LoadingView) LayoutInflater.from(activityController.get()).inflate(R.layout.loading_view, null); | |
loadingSpinner = loadingView.findViewById(R.id.loading_view_spinner); | |
loadingTextView = (TextView) loadingView.findViewById(R.id.loading_text_view); | |
} | |
} |
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
@RunWith(RobolectricTestRunner.class) | |
@Config(constants = BuildConfig.class, sdk = 21) | |
public class LoadingViewTest { | |
... | |
@Test | |
public void testStopLoadingAndSetTextShouldHideTheSpinnerAndShowTheTextView() throws Exception { | |
assertThat(loadingSpinner.getVisibility(), equalTo(View.VISIBLE)); | |
assertThat(loadingTextView.getVisibility(), equalTo(View.INVISIBLE)); | |
loadingView.stopLoadingAndSetText(R.string.unit_tests_ftw); | |
assertThat(loadingSpinner.getVisibility(), equalTo(View.GONE)); | |
assertThat(loadingTextView.getVisibility(), equalTo(View.VISIBLE)); | |
} | |
} |
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
@RunWith(RobolectricTestRunner.class) | |
@Config(constants = BuildConfig.class, sdk = 21) | |
public class LoadingViewTest { | |
... | |
@Test | |
public void testStopLoadingAndSEtTextShouldSetTheTextOnTheTextView() { | |
loadingView.stopLoadingAndSetText(R.string.unit_tests_ftw); | |
assertThat((String) loadingTextView.getText(), equalTo("Unit Tests FTW!!!")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still in progress