mardi 2 mai 2017

how do i create a unit test to show a function works correctly

i want to be able to test the following code to ensure a button click works and that the password is successfully stored as well as testing to prove that intent allows user to open new activity . what method would i implement in the unit test file as i have not done this before?

public class CreatePassword extends AppCompatActivity {

EditText editText, editText3;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_password);

    editText = (EditText) findViewById(R.id.editText);
    editText3 = (EditText) findViewById(R.id.editText3);
    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = editText.getText().toString();
            String text3 = editText3.getText().toString();

            if (text.equals("") || text3.equals("")) {
                //no password
                Toast.makeText(CreatePassword.this, "No Password Entered",
                        Toast.LENGTH_SHORT).show();

            } else {
                if (text.equals(text3)){
                    //save password
                    SharedPreferences settings = getSharedPreferences("PREFS", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("password", text);
                    editor.apply();

                    //enter the app
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    //incorrect password
                    Toast.makeText(CreatePassword.this, "Password Does Not Match",
                            Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}

Aucun commentaire:

Enregistrer un commentaire