diff --git a/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaAutoCompleteTextViewInteractions.kt b/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaAutoCompleteTextViewInteractions.kt index 496874d1..c4c082a4 100644 --- a/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaAutoCompleteTextViewInteractions.kt +++ b/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaAutoCompleteTextViewInteractions.kt @@ -1,12 +1,18 @@ package com.adevinta.android.barista.interaction +import android.view.View import android.widget.EditText import androidx.annotation.IdRes -import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom -import androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA -import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions +import androidx.test.espresso.action.ViewActions.replaceText +import androidx.test.espresso.assertion.ViewAssertions +import androidx.test.espresso.matcher.RootMatchers +import androidx.test.espresso.matcher.ViewMatchers.* +import com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn import com.adevinta.android.barista.internal.performAction import com.adevinta.android.barista.internal.viewaction.AutoCompleteViewActions.replaceAutoComplete +import org.hamcrest.Matcher import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.anyOf @@ -21,4 +27,43 @@ object BaristaAutoCompleteTextViewInteractions { val combinedMatcher = anyOf(simpleMatcher, wrapperMatcher) combinedMatcher.performAction(replaceAutoComplete(text)) } + + /** + * Trigger suggestion displayed on the matched view from the provided string + */ + @JvmStatic + fun triggerAutocompleteSuggestion(matcher: Matcher, text: String) { + clickOn(matcher) + onView( + matcher + ).perform( + replaceText(text), + ViewActions.closeSoftKeyboard() + ) + } + + /** + * Assert that the expectedSuggestion string is displayed when we fill the matched view with the text. + */ + @JvmStatic + fun assertAutocompleteSuggestion(matcher: Matcher, text: String, expectedSuggestion: String) { + triggerAutocompleteSuggestion(matcher, text) + onView(withText(expectedSuggestion)) + .inRoot(RootMatchers.isPlatformPopup()) + .check(ViewAssertions.matches(isDisplayed())) + } + + /** + * Assert that all the expectedSuggestions strings are displayed when we fill the matched view with the text. + */ + @JvmStatic + fun assertAutocompleteSuggestion(matcher: Matcher, text: String, expectedSuggestions: List) { + triggerAutocompleteSuggestion(matcher, text) + expectedSuggestions.forEach { + onView(withText(it)) + .inRoot(RootMatchers.isPlatformPopup()) + .check(ViewAssertions.matches(isDisplayed())) + } + + } } diff --git a/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaClickInteractions.kt b/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaClickInteractions.kt index 820a5e93..e42436fc 100644 --- a/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaClickInteractions.kt +++ b/barista/src/main/java/com/adevinta/android/barista/interaction/BaristaClickInteractions.kt @@ -1,11 +1,13 @@ package com.adevinta.android.barista.interaction +import android.view.View import androidx.test.espresso.Espresso.pressBack import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.longClick +import com.adevinta.android.barista.internal.matcher.withCompatText import com.adevinta.android.barista.internal.performAction import com.adevinta.android.barista.internal.util.resourceMatcher -import com.adevinta.android.barista.internal.matcher.withCompatText +import org.hamcrest.Matcher object BaristaClickInteractions { @@ -24,6 +26,14 @@ object BaristaClickInteractions { withCompatText(text).performAction(click()) } + /** + * Click on the matched view + */ + @JvmStatic + fun clickOn(matcher: Matcher) { + matcher.performAction(click()) + } + @JvmStatic fun longClickOn(resId: Int) { resId.resourceMatcher().performAction(longClick()) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8c035d09..75b99f0c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Sat Dec 03 13:54:10 CET 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/sample/build.gradle.kts b/sample/build.gradle.kts index d265d4f8..88ce6896 100644 --- a/sample/build.gradle.kts +++ b/sample/build.gradle.kts @@ -1,7 +1,7 @@ plugins { id("com.android.application") kotlin("android") - id("kotlin-android-extensions") + kotlin("android.extensions") } apply(from = "../config/android-quality.gradle") @@ -32,12 +32,12 @@ android { dependencies { implementation(libs.androidX.legacy.supportV4) - + implementation(libs.androidX.appCompat) implementation(libs.androidX.recyclerView) implementation(libs.androidX.material) implementation(libs.androidX.annotation) - + implementation(libs.glide) implementation(libs.androidX.material) implementation(libs.androidX.core) @@ -48,8 +48,15 @@ dependencies { androidTestImplementation(libs.testing.assertJ) androidTestImplementation(libs.testing.mockito.kotlin) androidTestImplementation(libs.testing.mockito.android) - androidTestImplementation("androidx.test.ext:junit:1.1.3") testImplementation(libs.testing.jUnit) - + // testImplementation("org.hamcrest:hamcrest:2.2") + // testImplementation("androidx.test.ext:junit-ktx:1.1.5") + androidTestImplementation("androidx.test.ext:junit-ktx:1.1.5") + //testImplementation("androidx.test:core:1.5.0") + androidTestImplementation("androidx.test:core:1.5.0") + //testImplementation("androidx.test:rules:1.5.0") + androidTestImplementation("androidx.test:rules:1.5.0") + //testImplementation("androidx.test.ext:truth:1.5.0") + //androidTestImplementation("androidx.test.ext:truth:1.5.0") } \ No newline at end of file diff --git a/sample/src/androidTest/java/com/adevinta/android/barista/sample/AutoCompleteTextViewTest.java b/sample/src/androidTest/java/com/adevinta/android/barista/sample/AutoCompleteTextViewTest.java deleted file mode 100644 index ae9c0274..00000000 --- a/sample/src/androidTest/java/com/adevinta/android/barista/sample/AutoCompleteTextViewTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.adevinta.android.barista.sample; - -import androidx.test.rule.ActivityTestRule; -import com.adevinta.android.barista.sample.util.FailureHandlerValidatorRule; -import org.junit.Rule; -import org.junit.Test; - -import static com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed; -import static com.adevinta.android.barista.interaction.BaristaAutoCompleteTextViewInteractions.writeToAutoComplete; - -public class AutoCompleteTextViewTest { - - @Rule - public ActivityTestRule activityRule = new ActivityTestRule<>(AutoCompleteTextViewActivity.class); - - @Rule - public FailureHandlerValidatorRule handlerValidator = new FailureHandlerValidatorRule(); - - @Test - public void checkWriteOnAutoComplete_whenIsVisible() { - writeToAutoComplete(R.id.autocomplete, "Apple"); - assertDisplayed("Apple"); - } - - @Test - public void checkWriteOnAutoComplete_whenScrollIsNeeded() { - writeToAutoComplete(R.id.autocomplete_very_far_away, "Apple"); - assertDisplayed("Apple"); - } - - @Test - public void checkWriteOnAutoComplete_whenParentIsNotAScrollView() { - writeToAutoComplete(R.id.autocomplete_centered, "Hello!"); - assertDisplayed("Hello!"); - } -} diff --git a/sample/src/androidTest/java/com/adevinta/android/barista/sample/AutoCompleteTextViewTest.kt b/sample/src/androidTest/java/com/adevinta/android/barista/sample/AutoCompleteTextViewTest.kt new file mode 100644 index 00000000..cb219cfa --- /dev/null +++ b/sample/src/androidTest/java/com/adevinta/android/barista/sample/AutoCompleteTextViewTest.kt @@ -0,0 +1,52 @@ +package com.adevinta.android.barista.sample + +import androidx.test.espresso.matcher.ViewMatchers.withId +import androidx.test.rule.ActivityTestRule +import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertDisplayed +import com.adevinta.android.barista.interaction.BaristaAutoCompleteTextViewInteractions.assertAutocompleteSuggestion +import com.adevinta.android.barista.interaction.BaristaAutoCompleteTextViewInteractions.writeToAutoComplete +import com.adevinta.android.barista.interaction.BaristaClickInteractions.clickOn +import com.adevinta.android.barista.sample.util.FailureHandlerValidatorRule +import org.junit.Rule +import org.junit.Test + + +class AutoCompleteTextViewTest { + @Rule + var activityRule = ActivityTestRule( + AutoCompleteTextViewActivity::class.java + ) + + @Rule + var handlerValidator = FailureHandlerValidatorRule() + + @Test + fun checkWriteOnAutoComplete_whenIsVisible() { + writeToAutoComplete(R.id.autocomplete, "Apple") + assertDisplayed("Apple") + } + + @Test + fun checkWriteOnAutoComplete_whenScrollIsNeeded() { + writeToAutoComplete(R.id.autocomplete_very_far_away, "Apple") + assertDisplayed("Apple") + } + + @Test + fun checkWriteOnAutoComplete_whenParentIsNotAScrollView() { + writeToAutoComplete(R.id.autocomplete_centered, "Hello!") + assertDisplayed("Hello!") + } + + @Test + fun checkWriteOnAutoComplete_proposeOneChoice() { + clickOn(R.id.autocomplete) + assertAutocompleteSuggestion(withId(R.id.autocomplete), "Ap", "Apple") + } + + @Test + fun checkWriteOnAutoComplete_proposeMultipleChoise() { + clickOn(R.id.autocomplete) + assertAutocompleteSuggestion(withId(R.id.autocomplete), "B", listOf("Blueberry", "Banana")) + } +} \ No newline at end of file diff --git a/sample/src/main/java/com/adevinta/android/barista/sample/AutoCompleteTextViewActivity.java b/sample/src/main/java/com/adevinta/android/barista/sample/AutoCompleteTextViewActivity.java index 07b11400..1c46f3f4 100644 --- a/sample/src/main/java/com/adevinta/android/barista/sample/AutoCompleteTextViewActivity.java +++ b/sample/src/main/java/com/adevinta/android/barista/sample/AutoCompleteTextViewActivity.java @@ -3,24 +3,25 @@ import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; + import androidx.appcompat.app.AppCompatActivity; public class AutoCompleteTextViewActivity extends AppCompatActivity { - private static final String[] FRUITS = { "Banana", "Apple", "Orange", "Raspberry" }; + private static final String[] FRUITS = {"Banana", "Apple", "Orange", "Raspberry", "Blueberry"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_autocompletetextview); - AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autocomplete); - AutoCompleteTextView veryFarAwayAutoComplete = (AutoCompleteTextView) findViewById(R.id.autocomplete_very_far_away); + AutoCompleteTextView autoComplete = findViewById(R.id.autocomplete); + AutoCompleteTextView veryFarAwayAutoComplete = findViewById(R.id.autocomplete_very_far_away); - autoComplete.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, FRUITS)); + autoComplete.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, FRUITS)); autoComplete.setThreshold(1); - veryFarAwayAutoComplete.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, FRUITS)); + veryFarAwayAutoComplete.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, FRUITS)); veryFarAwayAutoComplete.setThreshold(1); } }