Skip to content

Perf and fixups #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
targetSdkVersion targetSdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.firebase.ui.database.utils;
package com.firebase.ui.database;

public class Bean {
private int number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,34 +31,35 @@
import static com.firebase.ui.database.TestUtils.getAppInstance;
import static com.firebase.ui.database.TestUtils.getBean;
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
import static org.junit.Assert.assertEquals;

@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());
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<Boolean>() {
@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<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == INITIAL_SIZE;
}
});
}

@After
Expand All @@ -74,14 +73,8 @@ public void tearDown() throws Exception {
}
}

@Test
public void testSize() throws Exception {
assertEquals(3, mArray.getCount());
}

@Test
public void testPushIncreasesSize() throws Exception {
assertEquals(3, mArray.getCount());
runAndWaitUntil(mArray, new Runnable() {
public void run() {
mRef.push().setValue(new Bean(4));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,28 +31,29 @@
import static com.firebase.ui.database.TestUtils.getAppInstance;
import static com.firebase.ui.database.TestUtils.isValuesEqual;
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
import static org.junit.Assert.assertEquals;

@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() {
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<Boolean>() {
public Boolean call() throws Exception {
return mArray.getCount() == 3;
return mArray.getCount() == INITIAL_SIZE;
}
});
}
Expand All @@ -69,14 +69,8 @@ public void tearDown() throws Exception {
}
}

@Test
public void testSize() throws Exception {
assertEquals(3, mArray.getCount());
}

@Test
public void testPushIncreasesSize() throws Exception {
assertEquals(3, mArray.getCount());
runAndWaitUntil(mArray, new Runnable() {
public void run() {
mRef.push().setValue(4);
Expand Down Expand Up @@ -118,7 +112,7 @@ public Boolean call() throws Exception {
}

@Test
public void testChangePriorities() throws Exception {
public void testChangePriorityBackToFront() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
public void run() {
mArray.getItem(2).getRef().setPriority(0.5);
Expand All @@ -129,4 +123,17 @@ public Boolean call() throws Exception {
}
});
}

@Test
public void testChangePriorityFrontToBack() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puf FYI

runAndWaitUntil(mArray, new Runnable() {
public void run() {
mArray.getItem(0).getRef().setPriority(4);
}
}, new Callable<Boolean>() {
public Boolean call() throws Exception {
return isValuesEqual(mArray, new int[]{2, 3, 1});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,18 +30,20 @@
import static com.firebase.ui.database.TestUtils.getAppInstance;
import static com.firebase.ui.database.TestUtils.getBean;
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
import static org.junit.Assert.assertEquals;

@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;

@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");

Expand All @@ -52,19 +52,18 @@ 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<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == 3;
}
}
);
@Override
public void run() {
for (int i = 1; i <= INITIAL_SIZE; i++) {
pushValue(new Bean(i, "Text " + i, i % 2 == 0), i);
}
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == INITIAL_SIZE;
}
});
}

@After
Expand All @@ -78,17 +77,11 @@ public void tearDown() throws Exception {
}
}

@Test
public void testSize() throws Exception {
assertEquals(3, mArray.getCount());
}

@Test
public void testPushIncreasesSize() throws Exception {
assertEquals(3, mArray.getCount());
runAndWaitUntil(mArray, new Runnable() {
public void run() {
setValue(new Bean(4), null);
pushValue(new Bean(4), null);
}
}, new Callable<Boolean>() {
@Override
Expand All @@ -102,7 +95,7 @@ public Boolean call() throws Exception {
public void testPushAppends() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
public void run() {
setValue(new Bean(4), 4);
pushValue(new Bean(4), 4);
}
}, new Callable<Boolean>() {
@Override
Expand All @@ -116,7 +109,7 @@ public Boolean call() throws Exception {
public void testAddValueWithPriority() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
public void run() {
setValue(new Bean(4), 0.5);
pushValue(new Bean(4), 0.5);
}
}, new Callable<Boolean>() {
public Boolean call() throws Exception {
Expand All @@ -143,7 +136,7 @@ && getBean(mArray, 1).getNumber() == 1
});
}

private void setValue(Object value, Object priority) {
private void pushValue(Object value, Object priority) {
String key = mKeyRef.push().getKey();

if (priority != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,16 +32,17 @@
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;

@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");

Expand All @@ -53,14 +53,14 @@ 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++) {
pushValue(i, i);
}
}
}, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return mArray.getCount() == 3;
return mArray.getCount() == INITIAL_SIZE;
}
});
}
Expand All @@ -76,17 +76,11 @@ public void tearDown() throws Exception {
}
}

@Test
public void testSize() throws Exception {
assertEquals(3, mArray.getCount());
}

@Test
public void testPushIncreasesSize() throws Exception {
assertEquals(3, mArray.getCount());
runAndWaitUntil(mArray, new Runnable() {
public void run() {
setValue(4, null);
pushValue(4, null);
}
}, new Callable<Boolean>() {
@Override
Expand All @@ -100,7 +94,7 @@ public Boolean call() throws Exception {
public void testPushAppends() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
public void run() {
setValue(4, 4);
pushValue(4, 4);
}
}, new Callable<Boolean>() {
@Override
Expand All @@ -114,7 +108,7 @@ public Boolean call() throws Exception {
public void testAddValueWithPriority() throws Exception {
runAndWaitUntil(mArray, new Runnable() {
public void run() {
setValue(4, 0.5);
pushValue(4, 0.5);
}
}, new Callable<Boolean>() {
public Boolean call() throws Exception {
Expand All @@ -137,7 +131,7 @@ public Boolean call() throws Exception {
});
}

private void setValue(Object value, Object priority) {
private void pushValue(Object value, Object priority) {
String key = mKeyRef.push().getKey();

if (priority != null) {
Expand Down
Loading