robolectric java.lang.AssertionError while unit testing - android

I have a login form which contains two edit texts, one to enter an emailId and another to enter a password. I have written a simple method to validate that these fields are not empty. This is my method
public boolean validate() {
if(edtEmail.getText().toString().trim().equalsIgnoreCase("")){
showErrorMessage(true);
return false;
}
if(edtPassword.getText().toString().trim().equalsIgnoreCase("")){
showErrorMessage(false);
return false;
}
return true;
}
I have written a unit test for this method
#Test
public void check_validate() {
EditText edtEmail = loginActivity.findViewById(R.id.edtEmail);
EditText edtPassword=loginActivity.findViewById(R.id.edtPassword);
assertTrue(loginActivity.validate());
}
I am getting the error as java.lang.AssertionError. All i want to check is whether the function is working correctly.

Related

How to prevent navigation until all validation are fulfill?

I want to add validation to my form. But there is one problem I press submit button it shows the validation for a quick time, and blank form submitted to recyclerview. please give me the solution if you have.
/**
* Performs action to submit the form if all the validations are fulfilled
*/
public void submitForm() {
if (validateFields()) {
//Todo add your form submission code here
}
}
/**
* Validate all the fields present in the form according to the requirements
* Returns true if there is no validation error, false otherwise.
*/
public boolean validateFields() {
if (editTextEmail.getText().toString().isEmpty()) {
//Show toast or snackbar for validation failed
return false;
} else if (//todo another validation code)
{
//Show toast or snackbar for validation failed
return false;
}
return true;
}
Performs action to submit the form if all the validations are fulfilled
public void submitForm() {
if (validateInputFields()) {
//Todo add your form submission code here
}
}
Validate all the fields present in the form according to the requirements
Returns true if there is no validation error, false otherwise.
public boolean validateInputFields() {
if (TextUtils.isEmpty(email)) {
//Show toast or snackbar for validation failed
return false;
}
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
//Show toast or snackbar for validation failed
return false;
}
else if (//todo another validation code)
{
//Show toast or snackbar for validation failed
return false;
}
return true;
}
You can go like this:
public void clickAction(){
if(validateFields()){
//Todo add your form submission code here
}
}
public boolean validateFields(){
if(editTextEmail.getText().toString().isEmpty()){
//Show toast validation failed
return false;
}else if(//todo another validation code){
return false;
}
return true;
}
Try the below validation method, if it's not working, please share your code here to look further more into your problem.
if (editTextName.getText().toString().trim().length() <= 0 ||
editTextAge.getText().toString().trim().length() <= 0) {
Toast.makeText(LoginActivity.this, "Fields should not be blank",
Toast.LENGTH_LONG).show();
} else {
callSubmitFormApi();
}

onQueryTextChanged Method in the searchView method goes backwards without filtering/publishing search results

I have a strange issue with onQueryTextChanged method with SearchView in android. My problem is that when specific text e.g: "Al a" has been entered (i.e. right after entering in letter 'a' in the search Field) the searchView closes by itself. But if I enter in "Al A" i.e. capitalizing 'a' there is no issue. Similarly the same issue persists with "Al b", "Al c",.....etc etc. At first I thought it had something to do with escape sequences but that's not true. I am clueless, please help.
private void search(SearchView searchView) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
System.out.println("text entered="+newText);
if (condition!= null) {
if (newText.length()!=0) { //if newText is not empty
} else {
isFiltersActive = false;
}
ListAdapter.getFilter().filter(newText);
} else
isFiltersActive = false;
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
}
});
}
onQueryTextSubmit:
Called when the user submits the query. This could be due to a key
press on the keyboard or due to pressing a submit button. The listener
can override the standard behavior by returning true to indicate that
it has handled the submit request. Otherwise return false to let the
SearchView handle the submission by launching any associated intent.
Return true from onQueryTextSubmit function.
To Check whether String is empty or not, Use
TextUtils.isEmpty() Returns true if the string is null or 0-length.

How to validate signature pad empty or not in android?

How to validate signature pad empty or not in android?
How can I check if a signature is completely blank, i.e. all its pixels are transparent?
I must avoid an x-y loop on every pixel.
//globally
private boolean isSignatured = false;
//onCreate
mSignaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {
#Override
public void onSigned() {
//Event triggered when the pad is signed
isSignatured = true;
}
#Override
public void onClear() {
//Event triggered when the pad is cleared
}
});
//in your on click
if(isSignatured){
//do next process
}else{
//intimate to sign
}
try this code,it's working
if(!signature_view.isBitmapEmpty()) {
saveNotes(signature_view);
}

Error with checking for empty email string in Android

I have an activity that has two EditText boxes, one email and one plain text. I want to check that the email box is not empty and also that is matches an email pattern before allowing the user to move on. The pattern matcher part works, but the empty string part doesn't.
I validate as the user enters data, and then set the two buttons to the be clickable or not according to whether the email is correct. However, if the box is empty, it allows the user to press the button when it shouldn't. When there is text in the box, it does the correct thing of only letting the user press the button when the email pattern has been matched.
My after text changed method:
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String enteredEmail = email.getText().toString();
if (validateEmail(enteredEmail) == true) {
image1.setImageResource(R.drawable.greentick);
play.setClickable(true);
play.setFocusable(true);
existingUser.setClickable(true);
existingUser.setFocusable(true);
}
else {
System.out.println("Invalid");
image1.setImageResource(R.drawable.redcross);
play.setClickable(false);
play.setFocusable(false);
existingUser.setClickable(false);
existingUser.setFocusable(false);
}
}
And my validateEmail() method:
public boolean validateEmail(String email) {
boolean validated = false;
if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() && (! email.equals("")) ) {
validated = true;
}
else {
validated = false;
}
return validated;
}
TextChanged won't fire if the user hasn't entered anything, as the text hasn't changed... You should disable the button by default.
Have you tried debugging, as this should have shown you the event isn't firing.
this is how i validate email fields (regular expressions):
private Boolean validateEmail(String email)
{
return email.matches("^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*#[a-zA-Z](-?[a-zA-Z0-9])*(\\.[a-zA-Z](-?[a-zA-Z0-9])*)+$");
}
FYI: i dont remember offhand, you may need org.apache.commons.lang3.stringutils

How to use Console in android

Please how can i apply this code to my android application. I am making an application that gives access to the User by entering the correct password.
I seem to be getting an error with the Console
public static void main (String args[]) throws IOException {
Console c=System.console();
String login = c.readLine("Enter your login: ");
char [] oldPassword = c.readPassword("Enter your old password: ");
if (verify(login, oldPassword)) {
boolean noMatch;
do {
char [] newPassword1 =
c.readPassword("Enter your new password: ");
char [] newPassword2 =
c.readPassword("Enter new password again: ");
noMatch = ! Arrays.equals(newPassword1, newPassword2);
if (noMatch) {
c.format("Passwords don't match. Try again.%n");
} else {
change(login, newPassword1);
c.format("Password for %s changed.%n", login);
}
Arrays.fill(newPassword1, ' ');
Arrays.fill(newPassword2, ' ');
} while (noMatch);
}
Arrays.fill(oldPassword, ' ');
}
//Dummy change method.
static boolean verify(String login, char[] password) {
// this method always returns true in this example.
// modify this method to verify password according to your rules.
return true;
}
//Dummy change method.
static void change(String login, char[] password) {
// modify this method to change password according to your rules.
}
}
Yes you can re-use your verify and change apis for android but you have discard whatever in the main method.
On Android, you will be creating an Activity having three EditText (for hide user text use android:inputType="textPassword"), one for each.
Old Password
New Password
New Password Again
Then you ll have one button call it Change Password. To this Change Password button you can add onClickListenr. When user presses this Change Password button, you will fetch the text values from EditText and then user your verify and change apis to do the actual work.
You can optionally choose a Cancel button too.
Below is how screen will look:
Snippet of handling the Change button:
// Declared in your Activity class.
EditText editTextOldPass;
EditText editTextNewPass;
EditText editTextNewPassAgain;
String login = "";
public void onCreate(Bundle savedInstanceState) {
editTextOldPass = (EditText) findViewById(R.id.editTextOldPass);
editTextNewPass = (EditText) findViewById(R.id.editTextNewPass);
editTextNewPassAgain = (EditText) findViewById(R.id.editTextNewPassAgain);
Button buttonChange = (Button) findViewById(R.id.buttonChange);
buttonChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editTextNewPass.getText().equals(editTextNewPassAgain)) {
if (verify(login, editTextOldPass.getText().toString().toCharArray()))
change(login, editTextNewPass.getText().toString().toCharArray());
} else {
Log.i("PasswordActivity", "Passwords don't match. Try again.");
}
}
});
}

Categories

Resources