I am building an android app which uses Firebase as the back-end and an model, view, presenter architecture. However, the fact that Firebase is a cloud service complicates the automated testing in my android app. So far I have built most of the authentication system, but am unable to see how to implement unit tests for the Firebase code in my app. In terms of end to end testing I am also stuck.
Since testing is fundamental to any android app and without it application developers can't be sure what they have implemented is functioning as expected, I can't really progress any further without automated tests.
In conclusion, my question is:
Generally, how do you implement Firebase automated testing in an android app?
EDIT:
As an example could someone unit test the following method?
public void addUser(final String name, final String birthday,
final String email, final String password) {
Firebase mUsersNode = Constants.mRef.child("users");
final Firebase mSingleUser = mUsersNode.child(name);
mSingleUser.runTransaction(new Transaction.Handler() {
#Override
public Transaction.Result doTransaction(MutableData mutableData) {
mSingleUser.child("birthday").setValue(birthday);
mSingleUser.child("email").setValue(email);
mSingleUser.child("password").setValue(password);
return Transaction.success(mutableData);
}
#Override
public void onComplete(FirebaseError firebaseError, boolean b, DataSnapshot dataSnapshot) {
if(firebaseError != null) {
mSignUpPresenter.addUserFail(firebaseError);
} else {
mSignUpPresenter.addUserComplete();
}
}
});
}
UPDATE 2020:
"So far this year (2020), this problem seems to have been solved using a (beta, at the date of this comment) Firebase emulator: Build unit tests using Fb Emulators and Unit testing security rules with the Firebase Emulator Suite, at YouTube This is done locally in the developer's computer. – carloswm85"
I found this https://www.firebase.com/blog/2015-04-24-end-to-end-testing-firebase-server.html but the article is over a year old. I only scanned it, I'll give it a more thorough read in a bit.
Either way, we really need the equivalent of the local Google AppEngine Backend that you can run in Intellij (Android Studio). Testing cannot be an afterthought in 2016. Really hoping one of the awesome Firebase devs notices this thread and comments. Testing should be part of their official guides.
Here is my solution - hope this helps:
[Update]
I've removed my prev. sample in favor of this one. It's simpler and shows the main essence
public class TestFirebase extends AndroidTestCase {
private static Logger logger = LoggerFactory.getLogger(TestFirebase.class);
private CountDownLatch authSignal = null;
private FirebaseAuth auth;
#Override
public void setUp() throws InterruptedException {
authSignal = new CountDownLatch(1);
Firebase.setAndroidContext(mContext); //initializeFireBase(context);
auth = FirebaseAuth.getInstance();
if(auth.getCurrentUser() == null) {
auth.signInWithEmailAndPassword("urbi#orbi.it", "12345678").addOnCompleteListener(
new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull final Task<AuthResult> task) {
final AuthResult result = task.getResult();
final FirebaseUser user = result.getUser();
authSignal.countDown();
}
});
} else {
authSignal.countDown();
}
authSignal.await(10, TimeUnit.SECONDS);
}
#Override
public void tearDown() throws Exception {
super.tearDown();
if(auth != null) {
auth.signOut();
auth = null;
}
}
#Test
public void testWrite() throws InterruptedException {
final CountDownLatch writeSignal = new CountDownLatch(1);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Do you have data? You'll love Firebase. - 3")
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull final Task<Void> task) {
writeSignal.countDown();
}
});
writeSignal.await(10, TimeUnit.SECONDS);
}
}
Related
I have the following code for an activity of my app. It works by redirecting from another activity. But even though it's run once, it does hundreds of readings. Where is the problem?
public class CalculatedResults extends AppCompatActivity {
private Intent intent;
private float clearTurkıshCorrect, clearSocialStudiesCorrect, clearBasicMathCorrect, clearScienceCorrect;
private TextView txtClearTurkishCorrect,txtClearSocialStudiesCorrect, txtClearBasicMathCorrect,txtClearScienceCorrect, txtRawScore, txtPlacementScore, txtRawRanking, txtPlacementRanking;
FirebaseFirestore db = FirebaseFirestore.getInstance();
private static final String TAG = "DocSnippets";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculated_results);
setDataFromFirebase();
}
private void setDataFromFirebase() {
DocumentReference docRef = db.collection("rawRankings").document(String.valueOf((int)intent.getDoubleExtra("rawScore",0)));
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
txtRawRanking.setText(String.valueOf(document.getData().get("tyt")));
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
}
}
Posting the comments of #VenkateshTalacheeru, #FrankvanPuffelen as a Community wiki answer.
Firebase Firestore read counts are calculated even you load data in your Firebase console. It will read all the collections and documents you have every time you refresh or real-time syncs. It's expected behavior.
As was mentioned in another thread,
use of the Firebase console will incur reads. If you leave the console
open on a collection/document with busy write activity, the console
will automatically read the changes that update the console's display.
This is very often the source of unexpected reads.
I am trying to download some Quiz objects from my database.
The following function is called from onCreate of a certain activity.
private void downloadQuizzesFromCloud(){
String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");
Query userQuizzes = db.collection(user_quizzes_path);
userQuizzes.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
quizzes.clear();
for (QueryDocumentSnapshot document : task.getResult()) {
Quiz quizDownloaded = getQuizFromCloud(document.getId());
quizzes.add(quizDownloaded);
}
Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
//TODO put in recycle adapter
} else { }
}
});
}
(user_quizzes_path contains the correct path to a collection of Quiz objects stored on the cloud)
I debugged this functions and found out that after the command:
userQuizzes.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
The function finishes execution, that is the onComplete cases aren't checked and executed and all this code is just skipped.
I tried to find this on the documentation of firebase but didn't find anything.
Why is this happening and how can I fix this?
Would appreciate some help here, thanks!
The onComplete is called when the read operation has completed from the Firestore servers. If it's not getting called, I can see two possible reasons:
You're not connected to the server. Unless you've read the data before (and it's in the local database that the Firestore client maintains), this means the read never completes locally.
You're not thinking asynchronously. Note that data is read from the server asynchronously, and there may be some time between when you call get() and when onComplete fires. To test if this is the case, put a breakpoint on if (task.isSuccessful()) { and run the app in the debugger. The breakpoint will hit when the data is read from the server.
Use a callback interface. Just like this below.
private void downloadQuizzesFromCloud(Consumer listener) {
String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
String user_quizzes_path = "users/".concat(user_id).concat("/quizzes");
Query userQuizzes = db.collection(user_quizzes_path);
userQuizzes.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Quiz> quizzes = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
Quiz quizDownloaded = getQuizFromCloud(document.getId());
quizzes.add(quizDownloaded);
}
listener.onGet(quizzes);
Toast.makeText(QuizzesActivity.this,"downloaded to list ".concat(String.valueOf(quizzes.size()).concat(" quizzes")), Toast.LENGTH_SHORT).show();
//TODO put in recycle adapter
} else { }
}
});
}
interface Consumer {
void onGet(List<Quiz> quizzes);
}
I want to store locally the data I am reading from the cloud.
To achieve this I am using a global variable(quizzes) to hold all the data.
For this, when I am building my Quiz objects, I need to make sure that before I am creating them, the relevant data has been already downloaded from the cloud. Since when reading data from firestore, it happens asynchronously.
I didn't enforced this (waiting for the read to finish) before -I just used onSuccess listeners, and I encountered synchronization problem because the reading tasks weren't finished before I created my Quiz objects with the data from the cloud.
I fixed this with a very primitive way of "busy waiting" until the read from the cloud is complete. I know this is very stupid, a very bad practice, and making the application to be super slow, and I am sure there is a better way to fix this.
private void downloadQuizzesFromCloud(){
String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference quizzesRefrence = db.collection("users").document(user_id).collection("quizzes");
Task<QuerySnapshot> task = quizzesRefrence.get();
while(task.isComplete() == false){
System.out.println("busy wait");
}
for (QueryDocumentSnapshot document : task.getResult()) {
Quiz quizDownloaded = getQuizFromCloud(document.getId());
quizzes.add(quizDownloaded);
}
}
I looked online in the documentation of firestore and firebase and didn't find anything that I could use. (tried for example to use the "wait" method) but that didn't help.
What else can I do to solve this synchronization problem?
I didn't understand if you tried this solution, but I think this is the better and the easier: add an onCompleteListener to the Task object returned from the get() method, the if the task is succesfull, you can do all your stuff, like this:
private void downloadQuizzesFromCloud(){
String user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference quizzesRefrence = db.collection("users").document(user_id).collection("quizzes");
quizzesRefrence.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccesful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Quiz quizDownloaded = getQuizFromCloud(document.getId());
quizzes.add(quizDownloaded);
}
}
});
}
}
In this way, you'll do all you have to do (here the for loop) as soon as the data is downloaded
You can make your own callback. For this, make an interface
public interface FireStoreResults {
public void onResultGet();
}
now send this call back when you get results
public void readData(final FireStoreResults){
db.collection("users").document(user_id).collection("quizzes")
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot document : task.getResult()) {
Quiz quizDownloaded = getQuizFromCloud(document.getId());
quizzes.add(quizDownloaded);
}
results.onResultGet();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
results.onResultGet();
}
});
}
Now in your activity or fragment
new YourResultGetClass().readData(new FireStoreResults(){
#Override
public void onResultGet() {
new YourResultGetClass().getQuizzes(); //this is your list of quizzes
//do whatever you want with it
}
Hope this makes sense!
Within my app I often have the need to read data once. I originally started by using the addListenerForSingleValueEvent() method for this, however I ran into problems using this method as it does not work as I wanted when offline capabilities are enabled (see here the issue: Firebase Offline Capabilities and addListenerForSingleValueEvent)
In the question above it is mentioned that a workaround is to use the addValueEventListener() method, however I do not fully understand how to do this (particularly how to remove the ValueEventListener as soon I am finished grabbing the data I need).
Take this method which I created in a standalone class to query the Users node on Firebase where I store the users FCM Token. It seems to have an issue of not returning the latest token from the server everytime.
public class SendFCMMessage {
String userToken;
String currentUser;
String userName;
ValueEventListener userListener;
public void sendMessage(final String contactNumber) {
final DatabaseReference ref = FirebaseDatabase.getInstance().getReferenceFromUrl(link).child("Users").child(contactNumber);
userListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
userToken = user.getToken();
// Send FCM Message after getting user token and then remove event listener
ref.removeEventListener(userListener);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("TAG", "Something terrible went wrong: " + databaseError);
}
};
ref.addValueEventListener(userListener);
}
}
If I remove the line
ref.removeEventListener(userListener);
Then this code works fine, however I would like to know how I could remove the ValueEventListener as soon as I receive the data I need?
Thanks,
R
ValueEventListener vel; //Declared Global
Listen your DatabaseReference like this;
vel = yourDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChanged(DataSnapshot dataSnapShot) {
//Do your stuff here. I suggest you create another method for this if you don't want a problem with inner class.
//For example, workDataSnapshot(DataSnapshot dataSnapShot) <-- Work here
yourDatabaseReference.removeEventListener(vel);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Hope it helps you.
I am pretty new to android development and Firebase(the new google one). I want to use the email and password feature, but sign specific users in(the ones in my firebase console). I know this is a pretty broad question, but any help would be much appreciated.
First of all, you should check a documentation for Firebase. You can find on official Firebase website:
1. https://firebase.google.com/docs/auth/android/manage-users
2. https://firebase.google.com/docs/auth/android/password-auth
Then you should check Firebase Authentication samples and look for Email/Password Setup.
After you have created your project and added your app to it, you have to go to the console/auth/sign_in method and enable the email and password authentication. Then you have to create a activity/fragment to represent the login, sign up etc.
On the launcher activity you should have this method to check the state of current user and do the necessary action of user != null or if user == null based on your preferences.
FirebaseAuth.AuthStateListener authStateListener;
authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null){
startActivity(new Intent(LoginPage.this,MainActivity.class));
}
}
};
auth.addAuthStateListener(authStateListener);//this one in onStart
then you can use these 2 methods for signin and create new user respectively
FirebaseAuth auth;
auth = FirebaseAuth.getInstance();
auth.signInWithEmailAndPassword(email , password)
.addOnCompleteListener(LoginPage.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
}else {
}
}
});
auth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(SignUpPage.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
}else {
}
}
});
go to all the links provided by mmBs and also see this link for a simple and working example