vendredi 31 mars 2017

Junit Testing for an Android Activity

Does any one have any experience with creating unit-test for the On Create and OnClick method in java, using Android Studio? I use data from my Firebasedatabase. I think I have to use Mock-object, but I don't knot where to start.

Under is the code from my class LoginActivity

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mfireBaseAuth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance().getReference();
        mProgressDialog = new ProgressDialog(this);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        buttonSignIn = (Button) findViewById(R.id.buttonSignIn);
        textViewSignUp = (TextView) findViewById(R.id.textViewSignUp);

        buttonSignIn.setOnClickListener(this);
        textViewSignUp.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(v == buttonSignIn){
            usersignin();

        }
        if(v==textViewSignUp){
            startActivity(new Intent(this, RegisterActivity.class));
        }
       }

    private void usersignin() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        if(TextUtils.isEmpty(email)){
            Toast.makeText(this, "Please enter Email", Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(password)){
            Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
            return;
        }

        mProgressDialog.setMessage("Logging in. Please wait...");
        mProgressDialog.show();
        mfireBaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                mProgressDialog.dismiss();
                if(task.isSuccessful()){
                    getSignedInUserProfile();
                }
            }
        });
    }

    private void getSignedInUserProfile() {

        DatabaseReference reference = mDatabase;//.child("eduback-2feef");
        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        userID = firebaseUser.getUid();
        reference.child("Users").child(userID).child("User info").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
                if(user != null) {
                    // Save if the user is student or prof in shared prefs.
                    PreferenceHelper helper = new PreferenceHelper(getBaseContext());
                    helper.setIsStudent(user.isStudent);
                    checkStudentOrProfessor(user);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Ups vis error
            }
        });



    }

    private void checkStudentOrProfessor(User user) {

        Intent i;
        if (user.isStudent ) {
            i = new Intent(this, MainActivityStudent.class);
        } else {
            i = new Intent(this, MainActivityProfessor.class);
        }
        startActivity(i);


    }



}

Aucun commentaire:

Enregistrer un commentaire