diff --git a/database/build.gradle b/database/build.gradle index 9da173895..dafe721b5 100644 --- a/database/build.gradle +++ b/database/build.gradle @@ -10,6 +10,7 @@ android { targetSdkVersion targetSdk versionCode 1 versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/database/src/androidTest/java/com/firebase/ui/database/utils/Bean.java b/database/src/androidTest/java/com/firebase/ui/database/Bean.java similarity index 52% rename from database/src/androidTest/java/com/firebase/ui/database/utils/Bean.java rename to database/src/androidTest/java/com/firebase/ui/database/Bean.java index ef926a547..faf5ccbcb 100644 --- a/database/src/androidTest/java/com/firebase/ui/database/utils/Bean.java +++ b/database/src/androidTest/java/com/firebase/ui/database/Bean.java @@ -1,17 +1,18 @@ -package com.firebase.ui.database.utils; +package com.firebase.ui.database; public class Bean { - private int number; - private String text; - private boolean bool; + private int mNumber; + private String mText; + private boolean mBool; public Bean() { + // Needed for Firebase } public Bean(int number, String text, boolean bool) { - this.number = number; - this.text = text; - this.bool = bool; + mNumber = number; + mText = text; + mBool = bool; } public Bean(int index) { @@ -19,14 +20,14 @@ public Bean(int index) { } public int getNumber() { - return number; + return mNumber; } public String getText() { - return text; + return mText; } public boolean isBool() { - return bool; + return mBool; } } diff --git a/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java b/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java index f7420eee7..40c521986 100644 --- a/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java +++ b/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java @@ -14,11 +14,9 @@ package com.firebase.ui.database; +import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import android.test.InstrumentationTestCase; -import android.test.suitebuilder.annotation.SmallTest; -import com.firebase.ui.database.utils.Bean; import com.google.firebase.FirebaseApp; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; @@ -35,54 +33,46 @@ import static com.firebase.ui.database.TestUtils.runAndWaitUntil; @RunWith(AndroidJUnit4.class) -@SmallTest -public class FirebaseArrayOfObjectsTest extends InstrumentationTestCase { +public class FirebaseArrayOfObjectsTest { + private static final int INITIAL_SIZE = 3; + private DatabaseReference mRef; private FirebaseArray mArray; @Before public void setUp() throws Exception { - FirebaseApp app = getAppInstance(getInstrumentation().getContext()); - mRef = FirebaseDatabase.getInstance(app).getReference() - .child("firebasearray").child("objects"); + FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext()); + mRef = FirebaseDatabase.getInstance(app) + .getReference() + .child("firebasearray") + .child("objects"); mArray = new FirebaseArray(mRef); mRef.removeValue(); runAndWaitUntil(mArray, new Runnable() { - @Override - public void run() { - for (int i = 1; i <= 3; i++) { - mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i); - } - } - }, new Callable() { - @Override - public Boolean call() throws Exception { - return mArray.getCount() == 3; - } - } - ); + @Override + public void run() { + for (int i = 1; i <= INITIAL_SIZE; i++) { + mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i); + } + } + }, new Callable() { + @Override + public Boolean call() throws Exception { + return mArray.getCount() == INITIAL_SIZE; + } + }); } @After public void tearDown() throws Exception { - if (mRef != null) { - mRef.getRoot().removeValue(); - } - - if (mArray != null) { - mArray.cleanup(); - } - } - - @Test - public void testSize() throws Exception { - assertEquals(3, mArray.getCount()); + mArray.cleanup(); + mRef.getRoot().removeValue(); } @Test public void testPushIncreasesSize() throws Exception { - assertEquals(3, mArray.getCount()); runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mRef.push().setValue(new Bean(4)); } @@ -97,6 +87,7 @@ public Boolean call() throws Exception { @Test public void testPushAppends() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mRef.push().setValue(new Bean(4), 4); } @@ -111,10 +102,12 @@ public Boolean call() throws Exception { @Test public void testAddValueWithPriority() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mRef.push().setValue(new Bean(4), 0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return mArray.getItem(3).getValue(Bean.class).getNumber() == 3 && mArray.getItem(0) .getValue(Bean.class) @@ -126,10 +119,12 @@ public Boolean call() throws Exception { @Test public void testChangePriorities() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mArray.getItem(2).getRef().setPriority(0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return getBean(mArray, 0).getNumber() == 3 && getBean(mArray, 1).getNumber() == 1 diff --git a/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java b/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java index b79a4fcf3..2b7c4bf93 100644 --- a/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java +++ b/database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java @@ -14,9 +14,8 @@ package com.firebase.ui.database; +import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import android.test.InstrumentationTestCase; -import android.test.suitebuilder.annotation.SmallTest; import com.google.firebase.FirebaseApp; import com.google.firebase.database.DatabaseReference; @@ -34,50 +33,42 @@ import static com.firebase.ui.database.TestUtils.runAndWaitUntil; @RunWith(AndroidJUnit4.class) -@SmallTest -public class FirebaseArrayTest extends InstrumentationTestCase { +public class FirebaseArrayTest { + private static final int INITIAL_SIZE = 3; private DatabaseReference mRef; private FirebaseArray mArray; @Before public void setUp() throws Exception { - FirebaseApp app = getAppInstance(getInstrumentation().getContext()); + FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext()); mRef = FirebaseDatabase.getInstance(app).getReference().child("firebasearray"); mArray = new FirebaseArray(mRef); mRef.removeValue(); runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - for (int i = 1; i <= 3; i++) { + for (int i = 1; i <= INITIAL_SIZE; i++) { mRef.push().setValue(i, i); } } }, new Callable() { + @Override public Boolean call() throws Exception { - return mArray.getCount() == 3; + return mArray.getCount() == INITIAL_SIZE; } }); } @After public void tearDown() throws Exception { - if (mRef != null) { - mRef.getRoot().removeValue(); - } - - if (mArray != null) { - mArray.cleanup(); - } - } - - @Test - public void testSize() throws Exception { - assertEquals(3, mArray.getCount()); + mArray.cleanup(); + mRef.getRoot().removeValue(); } @Test public void testPushIncreasesSize() throws Exception { - assertEquals(3, mArray.getCount()); runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mRef.push().setValue(4); } @@ -92,6 +83,7 @@ public Boolean call() throws Exception { @Test public void testPushAppends() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mRef.push().setValue(4, 4); } @@ -106,10 +98,12 @@ public Boolean call() throws Exception { @Test public void testAddValueWithPriority() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mRef.push().setValue(4, 0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return mArray.getItem(3).getValue(Integer.class).equals(3) && mArray.getItem(0).getValue(Integer.class).equals(4); @@ -118,15 +112,32 @@ public Boolean call() throws Exception { } @Test - public void testChangePriorities() throws Exception { + public void testChangePriorityBackToFront() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mArray.getItem(2).getRef().setPriority(0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return isValuesEqual(mArray, new int[]{3, 1, 2}); } }); } + + @Test + public void testChangePriorityFrontToBack() throws Exception { + runAndWaitUntil(mArray, new Runnable() { + @Override + public void run() { + mArray.getItem(0).getRef().setPriority(4); + } + }, new Callable() { + @Override + public Boolean call() throws Exception { + return isValuesEqual(mArray, new int[]{2, 3, 1}); + } + }); + } } diff --git a/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayOfObjectsTest.java b/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayOfObjectsTest.java index ce833f74b..959582a74 100644 --- a/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayOfObjectsTest.java +++ b/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayOfObjectsTest.java @@ -14,11 +14,9 @@ package com.firebase.ui.database; +import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import android.test.InstrumentationTestCase; -import android.test.suitebuilder.annotation.SmallTest; -import com.firebase.ui.database.utils.Bean; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; @@ -34,8 +32,9 @@ import static com.firebase.ui.database.TestUtils.runAndWaitUntil; @RunWith(AndroidJUnit4.class) -@SmallTest -public class FirebaseIndexArrayOfObjectsTest extends InstrumentationTestCase { +public class FirebaseIndexArrayOfObjectsTest { + private static final int INITIAL_SIZE = 3; + private DatabaseReference mRef; private DatabaseReference mKeyRef; private FirebaseArray mArray; @@ -43,7 +42,7 @@ public class FirebaseIndexArrayOfObjectsTest extends InstrumentationTestCase { @Before public void setUp() throws Exception { FirebaseDatabase databaseInstance = - FirebaseDatabase.getInstance(getAppInstance(getInstrumentation().getContext())); + FirebaseDatabase.getInstance(getAppInstance(InstrumentationRegistry.getContext())); mRef = databaseInstance.getReference().child("firebasearray").child("objects"); mKeyRef = databaseInstance.getReference().child("firebaseindexarray").child("objects"); @@ -52,43 +51,32 @@ public void setUp() throws Exception { mKeyRef.removeValue(); runAndWaitUntil(mArray, new Runnable() { - @Override - public void run() { - for (int i = 1; i <= 3; i++) { - setValue(new Bean(i, "Text " + i, i % 2 == 0), i); - } - } - }, new Callable() { - @Override - public Boolean call() throws Exception { - return mArray.getCount() == 3; - } - } - ); + @Override + public void run() { + for (int i = 1; i <= INITIAL_SIZE; i++) { + TestUtils.pushValue(mKeyRef, mRef, new Bean(i, "Text " + i, i % 2 == 0), i); + } + } + }, new Callable() { + @Override + public Boolean call() throws Exception { + return mArray.getCount() == INITIAL_SIZE; + } + }); } @After public void tearDown() throws Exception { - if (mRef != null) { - mRef.getRoot().removeValue(); - } - - if (mArray != null) { - mArray.cleanup(); - } - } - - @Test - public void testSize() throws Exception { - assertEquals(3, mArray.getCount()); + mArray.cleanup(); + mRef.getRoot().removeValue(); } @Test public void testPushIncreasesSize() throws Exception { - assertEquals(3, mArray.getCount()); runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - setValue(new Bean(4), null); + TestUtils.pushValue(mKeyRef, mRef, new Bean(4), null); } }, new Callable() { @Override @@ -101,8 +89,9 @@ public Boolean call() throws Exception { @Test public void testPushAppends() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - setValue(new Bean(4), 4); + TestUtils.pushValue(mKeyRef, mRef, new Bean(4), 4); } }, new Callable() { @Override @@ -115,10 +104,12 @@ public Boolean call() throws Exception { @Test public void testAddValueWithPriority() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - setValue(new Bean(4), 0.5); + TestUtils.pushValue(mKeyRef, mRef, new Bean(4), 0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return mArray.getItem(3).getValue(Bean.class).getNumber() == 3 && mArray.getItem(0) .getValue(Bean.class) @@ -130,10 +121,12 @@ public Boolean call() throws Exception { @Test public void testChangePriorities() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mKeyRef.child(mArray.getItem(2).getKey()).setPriority(0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return getBean(mArray, 0).getNumber() == 3 && getBean(mArray, 1).getNumber() == 1 @@ -142,16 +135,4 @@ && getBean(mArray, 1).getNumber() == 1 } }); } - - private void setValue(Object value, Object priority) { - String key = mKeyRef.push().getKey(); - - if (priority != null) { - mKeyRef.child(key).setValue(true, priority); - mRef.child(key).setValue(value, priority); - } else { - mKeyRef.child(key).setValue(true); - mRef.child(key).setValue(value); - } - } } diff --git a/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayTest.java b/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayTest.java index 71d62f96e..e0b884b3a 100644 --- a/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayTest.java +++ b/database/src/androidTest/java/com/firebase/ui/database/FirebaseIndexArrayTest.java @@ -14,9 +14,8 @@ package com.firebase.ui.database; +import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; -import android.test.InstrumentationTestCase; -import android.test.suitebuilder.annotation.SmallTest; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; @@ -33,8 +32,9 @@ import static com.firebase.ui.database.TestUtils.runAndWaitUntil; @RunWith(AndroidJUnit4.class) -@SmallTest -public class FirebaseIndexArrayTest extends InstrumentationTestCase { +public class FirebaseIndexArrayTest { + private static final int INITIAL_SIZE = 3; + private DatabaseReference mRef; private DatabaseReference mKeyRef; private FirebaseIndexArray mArray; @@ -42,7 +42,7 @@ public class FirebaseIndexArrayTest extends InstrumentationTestCase { @Before public void setUp() throws Exception { FirebaseDatabase databaseInstance = - FirebaseDatabase.getInstance(getAppInstance(getInstrumentation().getContext())); + FirebaseDatabase.getInstance(getAppInstance(InstrumentationRegistry.getContext())); mRef = databaseInstance.getReference().child("firebasearray"); mKeyRef = databaseInstance.getReference().child("firebaseindexarray"); @@ -53,40 +53,30 @@ public void setUp() throws Exception { runAndWaitUntil(mArray, new Runnable() { @Override public void run() { - for (int i = 1; i <= 3; i++) { - setValue(i, i); + for (int i = 1; i <= INITIAL_SIZE; i++) { + TestUtils.pushValue(mKeyRef, mRef, i, i); } } }, new Callable() { @Override public Boolean call() throws Exception { - return mArray.getCount() == 3; + return mArray.getCount() == INITIAL_SIZE; } }); } @After public void tearDown() throws Exception { - if (mRef != null) { - mRef.getRoot().removeValue(); - } - - if (mArray != null) { - mArray.cleanup(); - } - } - - @Test - public void testSize() throws Exception { - assertEquals(3, mArray.getCount()); + mArray.cleanup(); + mRef.getRoot().removeValue(); } @Test public void testPushIncreasesSize() throws Exception { - assertEquals(3, mArray.getCount()); runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - setValue(4, null); + TestUtils.pushValue(mKeyRef, mRef, 4, null); } }, new Callable() { @Override @@ -99,8 +89,9 @@ public Boolean call() throws Exception { @Test public void testPushAppends() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - setValue(4, 4); + TestUtils.pushValue(mKeyRef, mRef, 4, 4); } }, new Callable() { @Override @@ -113,10 +104,12 @@ public Boolean call() throws Exception { @Test public void testAddValueWithPriority() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { - setValue(4, 0.5); + TestUtils.pushValue(mKeyRef, mRef, 4, 0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return mArray.getItem(3).getValue(Integer.class).equals(3) && mArray.getItem(0).getValue(Integer.class).equals(4); @@ -127,25 +120,15 @@ public Boolean call() throws Exception { @Test public void testChangePriorities() throws Exception { runAndWaitUntil(mArray, new Runnable() { + @Override public void run() { mKeyRef.child(mArray.getItem(2).getKey()).setPriority(0.5); } }, new Callable() { + @Override public Boolean call() throws Exception { return isValuesEqual(mArray, new int[]{3, 1, 2}); } }); } - - private void setValue(Object value, Object priority) { - String key = mKeyRef.push().getKey(); - - if (priority != null) { - mKeyRef.child(key).setValue(true, priority); - mRef.child(key).setValue(value, priority); - } else { - mKeyRef.child(key).setValue(true); - mRef.child(key).setValue(value); - } - } } diff --git a/database/src/androidTest/java/com/firebase/ui/database/TestUtils.java b/database/src/androidTest/java/com/firebase/ui/database/TestUtils.java index d36b177a5..aa9151fc3 100644 --- a/database/src/androidTest/java/com/firebase/ui/database/TestUtils.java +++ b/database/src/androidTest/java/com/firebase/ui/database/TestUtils.java @@ -2,17 +2,17 @@ import android.content.Context; -import com.firebase.ui.database.utils.Bean; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; import com.google.firebase.database.DatabaseError; - -import junit.framework.AssertionFailedError; +import com.google.firebase.database.DatabaseReference; import java.util.concurrent.Callable; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertTrue; + public class TestUtils { private static final String APP_NAME = "firebaseui-tests"; private static final int TIMEOUT = 10000; @@ -26,7 +26,7 @@ public static FirebaseApp getAppInstance(Context context) { } } - public static FirebaseApp initializeApp(Context context) { + private static FirebaseApp initializeApp(Context context) { return FirebaseApp.initializeApp(context, new FirebaseOptions.Builder() .setApplicationId("fir-ui-tests") .setDatabaseUrl("https://fir-ui-tests.firebaseio.com/") @@ -40,12 +40,15 @@ public static void runAndWaitUntil(FirebaseArray array, array.setOnChangedListener(new ChangeEventListener() { @Override - public void onChildChanged(ChangeEventListener.EventType type, int index, int oldIndex) { + public void onChildChanged(ChangeEventListener.EventType type, + int index, + int oldIndex) { semaphore.release(); } @Override - public void onDataChanged() {} + public void onDataChanged() { + } @Override public void onCancelled(DatabaseError error) { @@ -64,9 +67,7 @@ public void onCancelled(DatabaseError error) { // and we're not done } } - if (!isDone) { - throw new AssertionFailedError(); - } + assertTrue("Timed out waiting for expected results on FirebaseArray", isDone); array.setOnChangedListener(null); } @@ -83,4 +84,19 @@ public static boolean isValuesEqual(FirebaseArray array, int[] expected) { public static Bean getBean(FirebaseArray array, int index) { return array.getItem(index).getValue(Bean.class); } + + public static void pushValue(DatabaseReference keyRef, + DatabaseReference ref, + Object value, + Object priority) { + String key = keyRef.push().getKey(); + + if (priority != null) { + keyRef.child(key).setValue(true, priority); + ref.child(key).setValue(value, priority); + } else { + keyRef.child(key).setValue(true); + ref.child(key).setValue(value); + } + } } diff --git a/database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java b/database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java index 823629c30..37077dff5 100644 --- a/database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java +++ b/database/src/main/java/com/firebase/ui/database/FirebaseIndexArray.java @@ -74,7 +74,7 @@ private int getIndexForKey(String key) { return index; } - private boolean isMatch(int index, String key) { + private boolean isKeyAtIndex(String key, int index) { return index >= 0 && index < getCount() && mDataSnapshots.get(index).getKey().equals(key); } @@ -105,7 +105,7 @@ public void onChildRemoved(DataSnapshot keySnapshot) { super.onChildRemoved(keySnapshot); super.setOnChangedListener(mListener); - if (isMatch(index, key)) { + if (isKeyAtIndex(key, index)) { mDataSnapshots.remove(index); notifyChangedListeners(ChangeEventListener.EventType.REMOVED, index); } @@ -120,7 +120,7 @@ public void onChildMoved(DataSnapshot keySnapshot, String previousChildKey) { super.onChildMoved(keySnapshot, previousChildKey); super.setOnChangedListener(mListener); - if (isMatch(oldIndex, key)) { + if (isKeyAtIndex(key, oldIndex)) { DataSnapshot snapshot = mDataSnapshots.remove(oldIndex); int newIndex = getIndexForKey(key); mDataSnapshots.add(newIndex, snapshot); @@ -147,7 +147,7 @@ public void onDataChange(DataSnapshot snapshot) { int index = getIndexForKey(key); if (snapshot.getValue() != null) { - if (!isMatch(index, key)) { + if (!isKeyAtIndex(key, index)) { mDataSnapshots.add(index, snapshot); notifyChangedListeners(ChangeEventListener.EventType.ADDED, index); } else { @@ -155,7 +155,7 @@ public void onDataChange(DataSnapshot snapshot) { notifyChangedListeners(ChangeEventListener.EventType.CHANGED, index); } } else { - if (isMatch(index, key)) { + if (isKeyAtIndex(key, index)) { mDataSnapshots.remove(index); notifyChangedListeners(ChangeEventListener.EventType.REMOVED, index); } else {