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

Assert autocomplet suggestion #486

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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<View>, 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<View>, 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<View>, text: String, expectedSuggestions: List<String>) {
triggerAutocompleteSuggestion(matcher, text)
expectedSuggestions.forEach {
onView(withText(it))
.inRoot(RootMatchers.isPlatformPopup())
.check(ViewAssertions.matches(isDisplayed()))
}

}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -24,6 +26,14 @@ object BaristaClickInteractions {
withCompatText(text).performAction(click())
}

/**
* Click on the matched view
*/
@JvmStatic
fun clickOn(matcher: Matcher<View>) {
matcher.performAction(click())
}

@JvmStatic
fun longClickOn(resId: Int) {
resId.resourceMatcher().performAction(longClick())
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
17 changes: 12 additions & 5 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("com.android.application")
kotlin("android")
id("kotlin-android-extensions")
kotlin("android.extensions")
}

apply(from = "../config/android-quality.gradle")
Expand Down Expand Up @@ -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)
Expand All @@ -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")
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}