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

Fix getTextOfLatestSnackbar #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
A shadow class for snackbars for use in Robolectric tests

Usage:

```
@RunWith(RobolectricTestRunner::class)

@Config(shadows = [ShadowSnackbar::class])

class MyTest {

@Test
Expand All @@ -17,4 +15,15 @@ class MyTest {

assertThat(ShadowSnackbar.getTextOfLatestSnackbar(), 'is'(activity.getString(R.string.ok))
}

@Test
fun snackBarSetTextTest() {

val snackbar = Snackbar.make(rootView, "text of make", Snackbar.LENGTH_SHORT)
snackbar.setText("new text")
snackbar.show()

assertEquals("new text", ShadowSnackbar.getTextOfLatestSnackbar())
}
}
```
10 changes: 6 additions & 4 deletions ShadowSnackbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ShadowSnackbar {

@RealObject
Snackbar snackbar;
String text;
SnackbarContentLayout snackbarContentLayout;
private int duration;
private int gravity;
private int xOffset;
Expand All @@ -42,6 +42,7 @@ public class ShadowSnackbar {
@Implementation
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int duration) {
Snackbar snackbar = null;
SnackbarContentLayout content = null;

try {
Constructor<Snackbar> constructor = Snackbar.class.getDeclaredConstructor(ViewGroup.class, View.class, ContentViewCallback.class);
Expand All @@ -55,7 +56,7 @@ public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int
}

ViewGroup parent = findSuitableParent(view);
final SnackbarContentLayout content =
content =
(SnackbarContentLayout)
LayoutInflater.from(parent.getContext()).inflate(android.support.design.R.layout.design_layout_snackbar_include, parent, false);

Expand All @@ -66,7 +67,7 @@ public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int
e.printStackTrace();
}

shadowOf(snackbar).text = text.toString();
shadowOf(snackbar).snackbarContentLayout = content;

shadowSnackbars.add(shadowOf(snackbar));

Expand Down Expand Up @@ -121,7 +122,8 @@ public static int shownSnackbarCount() {

public static String getTextOfLatestSnackbar() {
if (!shadowSnackbars.isEmpty())
return shadowSnackbars.get(shadowSnackbars.size() - 1).text;
return shadowSnackbars.get(shadowSnackbars.size() - 1)
.snackbarContentLayout.getMessageView().getText().toString();

return null;
}
Expand Down