-
-
Save rokuoku/b5c457073a78e78abbb59d43100459bd to your computer and use it in GitHub Desktop.
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
package com.example.myapplication; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.SystemClock; | |
import android.util.Log; | |
import android.view.accessibility.AccessibilityEvent; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import androidx.test.core.app.ApplicationProvider; | |
import androidx.test.ext.junit.runners.AndroidJUnit4; | |
import androidx.test.platform.app.InstrumentationRegistry; | |
import androidx.test.uiautomator.By; | |
import androidx.test.uiautomator.UiDevice; | |
import androidx.test.uiautomator.Until; | |
import static org.junit.Assert.assertTrue; | |
/** | |
* Instrumented test, which will execute on an Android device. | |
* | |
* @see <a href=http://d.android.com/tools/testing>Testing documentation</a> | |
*/ | |
@RunWith(AndroidJUnit4.class) | |
public class ToastTest { | |
private static final String TAG = "TestAutomation"; | |
private static final long LAUNCH_TIMEOUT = 5000; | |
private static final String PACKAGE_NAME = "com.example.myapplication"; | |
private final UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); | |
private boolean toastDisplayed; | |
@Test | |
public void testToast() { | |
launchApp(); | |
assertTrue("Toast was not displayed", isToastDisplayed("toast test", () -> { | |
device.findObject(By.res(PACKAGE_NAME + ":id/fab")).click(); | |
})); | |
} | |
private boolean isToastDisplayed(String text, Runnable action) { | |
toastDisplayed = false; | |
long startTimeMs = System.currentTimeMillis(); | |
Log.d(TAG, "isToastDisplayed: start waiting"); | |
InstrumentationRegistry.getInstrumentation().getUiAutomation() | |
.setOnAccessibilityEventListener((event) -> { | |
Log.d(TAG, "isToastDisplayed: event"); | |
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED | |
&& PACKAGE_NAME.equals(event.getPackageName().toString()) | |
&& event.getClassName().toString().contains(android.widget.Toast.class.getName()) | |
&& ("[" + text + "]").equals(event.getText().toString()) | |
) | |
{ | |
Log.d(TAG, "isToastDisplayed: TOAST"); | |
toastDisplayed = true; | |
} | |
event.recycle(); | |
}); | |
action.run(); | |
while (!toastDisplayed && System.currentTimeMillis() - startTimeMs < 10000) { | |
Log.d(TAG, "isToastDisplayed: sleep"); | |
SystemClock.sleep(500); } | |
InstrumentationRegistry.getInstrumentation().getUiAutomation().setOnAccessibilityEventListener(null); | |
return toastDisplayed; | |
} | |
private void launchApp() { | |
Context context = ApplicationProvider.getApplicationContext(); | |
final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
context.startActivity(intent); | |
device.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment