
com.jayway.android.robotium.solo.Checker Maven / Gradle / Ivy
package com.jayway.android.robotium.solo;
import java.util.ArrayList;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;
/**
* Contains various check methods. Examples are: isButtonChecked(),
* isSpinnerTextSelected.
*
* @author Renas Reda, [email protected]
*
*/
class Checker {
private final ViewFetcher viewFetcher;
private final Waiter waiter;
/**
* Constructs this object.
*
* @param viewFetcher the {@code ViewFetcher} instance.
* @param waiter the {@code Waiter} instance
*/
public Checker(ViewFetcher viewFetcher, Waiter waiter){
this.viewFetcher = viewFetcher;
this.waiter = waiter;
}
/**
* Checks if a {@link CompoundButton} with a given index is checked.
*
* @param expectedClass the expected class, e.g. {@code CheckBox.class} or {@code RadioButton.class}
* @param index of the {@code CompoundButton} to check. {@code 0} if only one is available
* @return {@code true} if {@code CompoundButton} is checked and {@code false} if it is not checked
*/
public boolean isButtonChecked(Class expectedClass, int index)
{
return (waiter.waitForAndGetView(index, expectedClass).isChecked());
}
/**
* Checks if a {@link CompoundButton} with a given text is checked.
*
* @param expectedClass the expected class, e.g. {@code CheckBox.class} or {@code RadioButton.class}
* @param text the text that is expected to be checked
* @return {@code true} if {@code CompoundButton} is checked and {@code false} if it is not checked
*/
public boolean isButtonChecked(Class expectedClass, String text)
{
waiter.waitForText(text, 0, 10000);
ArrayList list = viewFetcher.getCurrentViews(expectedClass);
for(T button : list){
if(button.getText().equals(text) && button.isChecked())
return true;
}
return false;
}
/**
* Checks if a {@link CheckedTextView} with a given text is checked.
*
* @param checkedTextView the {@code CheckedTextView} object
* @param text the text that is expected to be checked
* @return {@code true} if {@code CheckedTextView} is checked and {@code false} if it is not checked
*/
public boolean isCheckedTextChecked(String text)
{
waiter.waitForText(text, 0, 10000);
ArrayList list = viewFetcher.getCurrentViews(CheckedTextView.class);
for(CheckedTextView checkedText : list){
if(checkedText.getText().equals(text) && checkedText.isChecked())
return true;
}
return false;
}
/**
* Checks if a given text is selected in any {@link Spinner} located on the current screen.
* @param text the text that is expected to be selected
* @return {@code true} if the given text is selected in any {@code Spinner} and false if it is not
*
*/
public boolean isSpinnerTextSelected(String text)
{
waiter.waitForView(Spinner.class, 0);
ArrayList spinnerList = viewFetcher.getCurrentViews(Spinner.class);
for(int i = 0; i < spinnerList.size(); i++){
if(isSpinnerTextSelected(i, text))
return true;
}
return false;
}
/**
* Checks if a given text is selected in a given {@link Spinner}
* @param spinnerIndex the index of the spinner to check. 0 if only one spinner is available
* @param text the text that is expected to be selected
* @return true if the given text is selected in the given {@code Spinner} and false if it is not
*/
public boolean isSpinnerTextSelected(int spinnerIndex, String text)
{
Spinner spinner = waiter.waitForAndGetView(spinnerIndex, Spinner.class);
TextView textView = (TextView) spinner.getChildAt(0);
if(textView.getText().equals(text))
return true;
else
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy