|
| 1 | +package com.algolia.instantsearch.voice.demo; |
| 2 | + |
| 3 | + |
| 4 | +import android.Manifest; |
| 5 | +import android.support.test.espresso.EspressoException; |
| 6 | +import android.support.test.espresso.matcher.ViewMatchers; |
| 7 | +import android.support.test.filters.LargeTest; |
| 8 | +import android.support.test.rule.ActivityTestRule; |
| 9 | +import android.support.test.rule.GrantPermissionRule; |
| 10 | +import android.support.test.runner.AndroidJUnit4; |
| 11 | + |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | + |
| 16 | +import static android.support.test.espresso.Espresso.onView; |
| 17 | +import static android.support.test.espresso.action.ViewActions.click; |
| 18 | +import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; |
| 19 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 20 | +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 21 | +import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; |
| 22 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 23 | +import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 24 | +import static junit.framework.TestCase.fail; |
| 25 | + |
| 26 | +@RunWith(AndroidJUnit4.class) |
| 27 | +@LargeTest |
| 28 | +public class MainActivityWithPermissionTest { |
| 29 | + |
| 30 | + @Rule |
| 31 | + public ActivityTestRule testRule = new ActivityTestRule<>(MainActivity.class); |
| 32 | + |
| 33 | + @Rule |
| 34 | + public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.RECORD_AUDIO); |
| 35 | + |
| 36 | + @Test |
| 37 | + public void clickPermissionButton_displaysPermissionOverlay() { |
| 38 | + when_clickButtonPermission(); |
| 39 | + |
| 40 | + // the permission overlay should display |
| 41 | + onView(withId(R.id.voicePermission)) |
| 42 | + .check(matches(isDisplayed())); |
| 43 | + // the input overlay should not |
| 44 | + onView(withId(R.id.voiceInput)) |
| 45 | + .check(doesNotExist()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void clickInputButton_displaysInputOverlay() { |
| 50 | + when_clickButtonVoice(); |
| 51 | + |
| 52 | + check_displaysInputOverlay(); |
| 53 | + check_displaysListeningOrError(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void clickInput_thenCancel_displaysError() { |
| 58 | + when_clickButtonVoice(); |
| 59 | + |
| 60 | + // Then clicking on the mic button |
| 61 | + onView(withId(R.id.microphone)) |
| 62 | + .perform(click()); |
| 63 | + |
| 64 | + check_displaysError(); |
| 65 | + } |
| 66 | + |
| 67 | + //region Helpers |
| 68 | + private void when_clickButtonPermission() { |
| 69 | + // When clicking on the buttonPermission |
| 70 | + onView(withId(R.id.buttonPermission)) |
| 71 | + .perform(click()); |
| 72 | + } |
| 73 | + |
| 74 | + private void when_clickButtonVoice() { |
| 75 | + // When clicking on the buttonVoice |
| 76 | + onView(withId(R.id.buttonVoice)) |
| 77 | + .perform(click()); |
| 78 | + } |
| 79 | + |
| 80 | + private void check_displaysInputOverlay() { |
| 81 | + // the input overlay should display |
| 82 | + onView(withId(R.id.voiceInput)) |
| 83 | + .check(matches(isDisplayed())); |
| 84 | + // the permission overlay should not |
| 85 | + onView(withId(R.id.voicePermission)) |
| 86 | + .check(doesNotExist()); |
| 87 | + } |
| 88 | + |
| 89 | + private void check_displaysListening() { |
| 90 | + // listening should be displayed |
| 91 | + onView(withId(R.id.title)) |
| 92 | + .check(matches(withText(R.string.input_title_listening))); |
| 93 | + onView(withId(R.id.subtitle)) |
| 94 | + .check(matches(withText(R.string.input_subtitle_listening))); |
| 95 | + // No hint should be visible |
| 96 | + onView(withId(R.id.hint)) |
| 97 | + .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE))); |
| 98 | + } |
| 99 | + |
| 100 | + private void check_displaysError() { |
| 101 | + // the error should be displayed |
| 102 | + onView(withId(R.id.title)) |
| 103 | + .check(matches(withText(R.string.input_title_error))); |
| 104 | + onView(withId(R.id.subtitle)) |
| 105 | + .check(matches(withText(R.string.input_subtitle_error))); |
| 106 | + |
| 107 | + // And the button to be labeled try again |
| 108 | + onView(withId(R.id.hint)) |
| 109 | + .check(matches(isDisplayed())) |
| 110 | + .check(matches(withText(R.string.input_hint_error))); |
| 111 | + } |
| 112 | + |
| 113 | + private void check_displaysListeningOrError() { |
| 114 | + try { |
| 115 | + check_displaysListening(); |
| 116 | + } catch (Exception e) { |
| 117 | + if (e instanceof EspressoException) { |
| 118 | + // Maybe we are on emulator -> no SpeechRecognizer? |
| 119 | + // in this case, we should see error displayed |
| 120 | + check_displaysError(); |
| 121 | + } else fail(e.getMessage()); |
| 122 | + } |
| 123 | + } |
| 124 | + //endregion |
| 125 | +} |
0 commit comments