Skip to content

Commit b11cceb

Browse files
authored
Merge pull request #579 from SUPERCILEX/db-tests
Modernize database tests and cleanup
2 parents 6b0c1a6 + b342a73 commit b11cceb

File tree

8 files changed

+150
-162
lines changed

8 files changed

+150
-162
lines changed

database/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ android {
1010
targetSdkVersion targetSdk
1111
versionCode 1
1212
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1314
}
1415

1516
buildTypes {
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
package com.firebase.ui.database.utils;
1+
package com.firebase.ui.database;
22

33
public class Bean {
4-
private int number;
5-
private String text;
6-
private boolean bool;
4+
private int mNumber;
5+
private String mText;
6+
private boolean mBool;
77

88
public Bean() {
9+
// Needed for Firebase
910
}
1011

1112
public Bean(int number, String text, boolean bool) {
12-
this.number = number;
13-
this.text = text;
14-
this.bool = bool;
13+
mNumber = number;
14+
mText = text;
15+
mBool = bool;
1516
}
1617

1718
public Bean(int index) {
1819
this(index, "Text " + index, index % 2 == 0);
1920
}
2021

2122
public int getNumber() {
22-
return number;
23+
return mNumber;
2324
}
2425

2526
public String getText() {
26-
return text;
27+
return mText;
2728
}
2829

2930
public boolean isBool() {
30-
return bool;
31+
return mBool;
3132
}
3233
}

database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayOfObjectsTest.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414

1515
package com.firebase.ui.database;
1616

17+
import android.support.test.InstrumentationRegistry;
1718
import android.support.test.runner.AndroidJUnit4;
18-
import android.test.InstrumentationTestCase;
19-
import android.test.suitebuilder.annotation.SmallTest;
2019

21-
import com.firebase.ui.database.utils.Bean;
2220
import com.google.firebase.FirebaseApp;
2321
import com.google.firebase.database.DatabaseReference;
2422
import com.google.firebase.database.FirebaseDatabase;
@@ -35,54 +33,46 @@
3533
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
3634

3735
@RunWith(AndroidJUnit4.class)
38-
@SmallTest
39-
public class FirebaseArrayOfObjectsTest extends InstrumentationTestCase {
36+
public class FirebaseArrayOfObjectsTest {
37+
private static final int INITIAL_SIZE = 3;
38+
4039
private DatabaseReference mRef;
4140
private FirebaseArray mArray;
4241

4342
@Before
4443
public void setUp() throws Exception {
45-
FirebaseApp app = getAppInstance(getInstrumentation().getContext());
46-
mRef = FirebaseDatabase.getInstance(app).getReference()
47-
.child("firebasearray").child("objects");
44+
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
45+
mRef = FirebaseDatabase.getInstance(app)
46+
.getReference()
47+
.child("firebasearray")
48+
.child("objects");
4849
mArray = new FirebaseArray(mRef);
4950
mRef.removeValue();
5051
runAndWaitUntil(mArray, new Runnable() {
51-
@Override
52-
public void run() {
53-
for (int i = 1; i <= 3; i++) {
54-
mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i);
55-
}
56-
}
57-
}, new Callable<Boolean>() {
58-
@Override
59-
public Boolean call() throws Exception {
60-
return mArray.getCount() == 3;
61-
}
62-
}
63-
);
52+
@Override
53+
public void run() {
54+
for (int i = 1; i <= INITIAL_SIZE; i++) {
55+
mRef.push().setValue(new Bean(i, "Text " + i, i % 2 == 0), i);
56+
}
57+
}
58+
}, new Callable<Boolean>() {
59+
@Override
60+
public Boolean call() throws Exception {
61+
return mArray.getCount() == INITIAL_SIZE;
62+
}
63+
});
6464
}
6565

6666
@After
6767
public void tearDown() throws Exception {
68-
if (mRef != null) {
69-
mRef.getRoot().removeValue();
70-
}
71-
72-
if (mArray != null) {
73-
mArray.cleanup();
74-
}
75-
}
76-
77-
@Test
78-
public void testSize() throws Exception {
79-
assertEquals(3, mArray.getCount());
68+
mArray.cleanup();
69+
mRef.getRoot().removeValue();
8070
}
8171

8272
@Test
8373
public void testPushIncreasesSize() throws Exception {
84-
assertEquals(3, mArray.getCount());
8574
runAndWaitUntil(mArray, new Runnable() {
75+
@Override
8676
public void run() {
8777
mRef.push().setValue(new Bean(4));
8878
}
@@ -97,6 +87,7 @@ public Boolean call() throws Exception {
9787
@Test
9888
public void testPushAppends() throws Exception {
9989
runAndWaitUntil(mArray, new Runnable() {
90+
@Override
10091
public void run() {
10192
mRef.push().setValue(new Bean(4), 4);
10293
}
@@ -111,10 +102,12 @@ public Boolean call() throws Exception {
111102
@Test
112103
public void testAddValueWithPriority() throws Exception {
113104
runAndWaitUntil(mArray, new Runnable() {
105+
@Override
114106
public void run() {
115107
mRef.push().setValue(new Bean(4), 0.5);
116108
}
117109
}, new Callable<Boolean>() {
110+
@Override
118111
public Boolean call() throws Exception {
119112
return mArray.getItem(3).getValue(Bean.class).getNumber() == 3 && mArray.getItem(0)
120113
.getValue(Bean.class)
@@ -126,10 +119,12 @@ public Boolean call() throws Exception {
126119
@Test
127120
public void testChangePriorities() throws Exception {
128121
runAndWaitUntil(mArray, new Runnable() {
122+
@Override
129123
public void run() {
130124
mArray.getItem(2).getRef().setPriority(0.5);
131125
}
132126
}, new Callable<Boolean>() {
127+
@Override
133128
public Boolean call() throws Exception {
134129
return getBean(mArray, 0).getNumber() == 3
135130
&& getBean(mArray, 1).getNumber() == 1

database/src/androidTest/java/com/firebase/ui/database/FirebaseArrayTest.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
package com.firebase.ui.database;
1616

17+
import android.support.test.InstrumentationRegistry;
1718
import android.support.test.runner.AndroidJUnit4;
18-
import android.test.InstrumentationTestCase;
19-
import android.test.suitebuilder.annotation.SmallTest;
2019

2120
import com.google.firebase.FirebaseApp;
2221
import com.google.firebase.database.DatabaseReference;
@@ -34,50 +33,42 @@
3433
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
3534

3635
@RunWith(AndroidJUnit4.class)
37-
@SmallTest
38-
public class FirebaseArrayTest extends InstrumentationTestCase {
36+
public class FirebaseArrayTest {
37+
private static final int INITIAL_SIZE = 3;
3938
private DatabaseReference mRef;
4039
private FirebaseArray mArray;
4140

4241
@Before
4342
public void setUp() throws Exception {
44-
FirebaseApp app = getAppInstance(getInstrumentation().getContext());
43+
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
4544
mRef = FirebaseDatabase.getInstance(app).getReference().child("firebasearray");
4645
mArray = new FirebaseArray(mRef);
4746
mRef.removeValue();
4847
runAndWaitUntil(mArray, new Runnable() {
48+
@Override
4949
public void run() {
50-
for (int i = 1; i <= 3; i++) {
50+
for (int i = 1; i <= INITIAL_SIZE; i++) {
5151
mRef.push().setValue(i, i);
5252
}
5353
}
5454
}, new Callable<Boolean>() {
55+
@Override
5556
public Boolean call() throws Exception {
56-
return mArray.getCount() == 3;
57+
return mArray.getCount() == INITIAL_SIZE;
5758
}
5859
});
5960
}
6061

6162
@After
6263
public void tearDown() throws Exception {
63-
if (mRef != null) {
64-
mRef.getRoot().removeValue();
65-
}
66-
67-
if (mArray != null) {
68-
mArray.cleanup();
69-
}
70-
}
71-
72-
@Test
73-
public void testSize() throws Exception {
74-
assertEquals(3, mArray.getCount());
64+
mArray.cleanup();
65+
mRef.getRoot().removeValue();
7566
}
7667

7768
@Test
7869
public void testPushIncreasesSize() throws Exception {
79-
assertEquals(3, mArray.getCount());
8070
runAndWaitUntil(mArray, new Runnable() {
71+
@Override
8172
public void run() {
8273
mRef.push().setValue(4);
8374
}
@@ -92,6 +83,7 @@ public Boolean call() throws Exception {
9283
@Test
9384
public void testPushAppends() throws Exception {
9485
runAndWaitUntil(mArray, new Runnable() {
86+
@Override
9587
public void run() {
9688
mRef.push().setValue(4, 4);
9789
}
@@ -106,10 +98,12 @@ public Boolean call() throws Exception {
10698
@Test
10799
public void testAddValueWithPriority() throws Exception {
108100
runAndWaitUntil(mArray, new Runnable() {
101+
@Override
109102
public void run() {
110103
mRef.push().setValue(4, 0.5);
111104
}
112105
}, new Callable<Boolean>() {
106+
@Override
113107
public Boolean call() throws Exception {
114108
return mArray.getItem(3).getValue(Integer.class).equals(3)
115109
&& mArray.getItem(0).getValue(Integer.class).equals(4);
@@ -118,15 +112,32 @@ public Boolean call() throws Exception {
118112
}
119113

120114
@Test
121-
public void testChangePriorities() throws Exception {
115+
public void testChangePriorityBackToFront() throws Exception {
122116
runAndWaitUntil(mArray, new Runnable() {
117+
@Override
123118
public void run() {
124119
mArray.getItem(2).getRef().setPriority(0.5);
125120
}
126121
}, new Callable<Boolean>() {
122+
@Override
127123
public Boolean call() throws Exception {
128124
return isValuesEqual(mArray, new int[]{3, 1, 2});
129125
}
130126
});
131127
}
128+
129+
@Test
130+
public void testChangePriorityFrontToBack() throws Exception {
131+
runAndWaitUntil(mArray, new Runnable() {
132+
@Override
133+
public void run() {
134+
mArray.getItem(0).getRef().setPriority(4);
135+
}
136+
}, new Callable<Boolean>() {
137+
@Override
138+
public Boolean call() throws Exception {
139+
return isValuesEqual(mArray, new int[]{2, 3, 1});
140+
}
141+
});
142+
}
132143
}

0 commit comments

Comments
 (0)