Skip to content

Commit

Permalink
Fixed and added a test shouldShowToastWithHello()
Browse files Browse the repository at this point in the history
  • Loading branch information
drakeet committed Dec 9, 2017
1 parent 1d7e9db commit 4e52caa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 65 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.drakeet.toast.sample;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);


@Test
public void shouldShowToastWithHello() {
onView(withText("hello"))
.inRoot(withDecorView(not(is(rule.getActivity().getWindow().getDecorView()))))
.check(matches(isDisplayed()));
}
}
14 changes: 1 addition & 13 deletions sample/src/main/java/me/drakeet/toast/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package me.drakeet.toast.sample;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import me.drakeet.support.toast.ToastCompat;

Expand All @@ -16,17 +14,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
ToastCompat.makeText(this, "hello", Toast.LENGTH_SHORT)
.setBadTokenListener(toast -> {
Log.e("failed toast", text(toast));
Log.e("failed toast", "hello");
}).show();
}


private @NonNull String text(@NonNull Toast toast) {
TextView textView = toast.getView().findViewById(com.android.internal.R.id.message);
if (textView != null) {
return textView.getText().toString();
} else {
return toast.toString();
}
}
}

0 comments on commit 4e52caa

Please sign in to comment.