Skip to content

Commit 6babf71

Browse files
committed
Modernized Android unit tests.
Moved TestUtils into correct directory for its package. Removed redundant tests. Reformatted some code and renamed methods for clarity.
1 parent 7eac8a0 commit 6babf71

File tree

7 files changed

+64
-92
lines changed

7 files changed

+64
-92
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 {

database/src/androidTest/java/com/firebase/ui/database/utils/Bean.java renamed to database/src/androidTest/java/com/firebase/ui/database/Bean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.firebase.ui.database.utils;
1+
package com.firebase.ui.database;
22

33
public class Bean {
44
private int number;

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

Lines changed: 18 additions & 25 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;
@@ -33,34 +31,35 @@
3331
import static com.firebase.ui.database.TestUtils.getAppInstance;
3432
import static com.firebase.ui.database.TestUtils.getBean;
3533
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
34+
import static org.junit.Assert.assertEquals;
3635

3736
@RunWith(AndroidJUnit4.class)
38-
@SmallTest
39-
public class FirebaseArrayOfObjectsTest extends InstrumentationTestCase {
37+
public class FirebaseArrayOfObjectsTest {
38+
private static final int INITIAL_SIZE = 3;
39+
4040
private DatabaseReference mRef;
4141
private FirebaseArray mArray;
4242

4343
@Before
4444
public void setUp() throws Exception {
45-
FirebaseApp app = getAppInstance(getInstrumentation().getContext());
45+
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
4646
mRef = FirebaseDatabase.getInstance(app).getReference()
4747
.child("firebasearray").child("objects");
4848
mArray = new FirebaseArray(mRef);
4949
mRef.removeValue();
5050
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-
);
51+
@Override
52+
public void run() {
53+
for (int i = 1; i <= INITIAL_SIZE; 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() == INITIAL_SIZE;
61+
}
62+
});
6463
}
6564

6665
@After
@@ -74,14 +73,8 @@ public void tearDown() throws Exception {
7473
}
7574
}
7675

77-
@Test
78-
public void testSize() throws Exception {
79-
assertEquals(3, mArray.getCount());
80-
}
81-
8276
@Test
8377
public void testPushIncreasesSize() throws Exception {
84-
assertEquals(3, mArray.getCount());
8578
runAndWaitUntil(mArray, new Runnable() {
8679
public void run() {
8780
mRef.push().setValue(new Bean(4));

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

Lines changed: 7 additions & 13 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;
@@ -32,28 +31,29 @@
3231
import static com.firebase.ui.database.TestUtils.getAppInstance;
3332
import static com.firebase.ui.database.TestUtils.isValuesEqual;
3433
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
34+
import static org.junit.Assert.assertEquals;
3535

3636
@RunWith(AndroidJUnit4.class)
37-
@SmallTest
38-
public class FirebaseArrayTest extends InstrumentationTestCase {
37+
public class FirebaseArrayTest {
38+
private static final int INITIAL_SIZE = 3;
3939
private DatabaseReference mRef;
4040
private FirebaseArray mArray;
4141

4242
@Before
4343
public void setUp() throws Exception {
44-
FirebaseApp app = getAppInstance(getInstrumentation().getContext());
44+
FirebaseApp app = getAppInstance(InstrumentationRegistry.getContext());
4545
mRef = FirebaseDatabase.getInstance(app).getReference().child("firebasearray");
4646
mArray = new FirebaseArray(mRef);
4747
mRef.removeValue();
4848
runAndWaitUntil(mArray, new Runnable() {
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>() {
5555
public Boolean call() throws Exception {
56-
return mArray.getCount() == 3;
56+
return mArray.getCount() == INITIAL_SIZE;
5757
}
5858
});
5959
}
@@ -69,14 +69,8 @@ public void tearDown() throws Exception {
6969
}
7070
}
7171

72-
@Test
73-
public void testSize() throws Exception {
74-
assertEquals(3, mArray.getCount());
75-
}
76-
7772
@Test
7873
public void testPushIncreasesSize() throws Exception {
79-
assertEquals(3, mArray.getCount());
8074
runAndWaitUntil(mArray, new Runnable() {
8175
public void run() {
8276
mRef.push().setValue(4);

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

Lines changed: 22 additions & 29 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.database.DatabaseReference;
2321
import com.google.firebase.database.FirebaseDatabase;
2422

@@ -32,18 +30,20 @@
3230
import static com.firebase.ui.database.TestUtils.getAppInstance;
3331
import static com.firebase.ui.database.TestUtils.getBean;
3432
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
33+
import static org.junit.Assert.assertEquals;
3534

3635
@RunWith(AndroidJUnit4.class)
37-
@SmallTest
38-
public class FirebaseIndexArrayOfObjectsTest extends InstrumentationTestCase {
36+
public class FirebaseIndexArrayOfObjectsTest {
37+
private static final int INITIAL_SIZE = 3;
38+
3939
private DatabaseReference mRef;
4040
private DatabaseReference mKeyRef;
4141
private FirebaseArray mArray;
4242

4343
@Before
4444
public void setUp() throws Exception {
4545
FirebaseDatabase databaseInstance =
46-
FirebaseDatabase.getInstance(getAppInstance(getInstrumentation().getContext()));
46+
FirebaseDatabase.getInstance(getAppInstance(InstrumentationRegistry.getContext()));
4747
mRef = databaseInstance.getReference().child("firebasearray").child("objects");
4848
mKeyRef = databaseInstance.getReference().child("firebaseindexarray").child("objects");
4949

@@ -52,19 +52,18 @@ public void setUp() throws Exception {
5252
mKeyRef.removeValue();
5353

5454
runAndWaitUntil(mArray, new Runnable() {
55-
@Override
56-
public void run() {
57-
for (int i = 1; i <= 3; i++) {
58-
setValue(new Bean(i, "Text " + i, i % 2 == 0), i);
59-
}
60-
}
61-
}, new Callable<Boolean>() {
62-
@Override
63-
public Boolean call() throws Exception {
64-
return mArray.getCount() == 3;
65-
}
66-
}
67-
);
55+
@Override
56+
public void run() {
57+
for (int i = 1; i <= INITIAL_SIZE; i++) {
58+
pushValue(new Bean(i, "Text " + i, i % 2 == 0), i);
59+
}
60+
}
61+
}, new Callable<Boolean>() {
62+
@Override
63+
public Boolean call() throws Exception {
64+
return mArray.getCount() == INITIAL_SIZE;
65+
}
66+
});
6867
}
6968

7069
@After
@@ -78,17 +77,11 @@ public void tearDown() throws Exception {
7877
}
7978
}
8079

81-
@Test
82-
public void testSize() throws Exception {
83-
assertEquals(3, mArray.getCount());
84-
}
85-
8680
@Test
8781
public void testPushIncreasesSize() throws Exception {
88-
assertEquals(3, mArray.getCount());
8982
runAndWaitUntil(mArray, new Runnable() {
9083
public void run() {
91-
setValue(new Bean(4), null);
84+
pushValue(new Bean(4), null);
9285
}
9386
}, new Callable<Boolean>() {
9487
@Override
@@ -102,7 +95,7 @@ public Boolean call() throws Exception {
10295
public void testPushAppends() throws Exception {
10396
runAndWaitUntil(mArray, new Runnable() {
10497
public void run() {
105-
setValue(new Bean(4), 4);
98+
pushValue(new Bean(4), 4);
10699
}
107100
}, new Callable<Boolean>() {
108101
@Override
@@ -116,7 +109,7 @@ public Boolean call() throws Exception {
116109
public void testAddValueWithPriority() throws Exception {
117110
runAndWaitUntil(mArray, new Runnable() {
118111
public void run() {
119-
setValue(new Bean(4), 0.5);
112+
pushValue(new Bean(4), 0.5);
120113
}
121114
}, new Callable<Boolean>() {
122115
public Boolean call() throws Exception {
@@ -143,7 +136,7 @@ && getBean(mArray, 1).getNumber() == 1
143136
});
144137
}
145138

146-
private void setValue(Object value, Object priority) {
139+
private void pushValue(Object value, Object priority) {
147140
String key = mKeyRef.push().getKey();
148141

149142
if (priority != null) {

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

Lines changed: 12 additions & 18 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.database.DatabaseReference;
2221
import com.google.firebase.database.FirebaseDatabase;
@@ -33,16 +32,17 @@
3332
import static com.firebase.ui.database.TestUtils.runAndWaitUntil;
3433

3534
@RunWith(AndroidJUnit4.class)
36-
@SmallTest
37-
public class FirebaseIndexArrayTest extends InstrumentationTestCase {
35+
public class FirebaseIndexArrayTest {
36+
private static final int INITIAL_SIZE = 3;
37+
3838
private DatabaseReference mRef;
3939
private DatabaseReference mKeyRef;
4040
private FirebaseIndexArray mArray;
4141

4242
@Before
4343
public void setUp() throws Exception {
4444
FirebaseDatabase databaseInstance =
45-
FirebaseDatabase.getInstance(getAppInstance(getInstrumentation().getContext()));
45+
FirebaseDatabase.getInstance(getAppInstance(InstrumentationRegistry.getContext()));
4646
mRef = databaseInstance.getReference().child("firebasearray");
4747
mKeyRef = databaseInstance.getReference().child("firebaseindexarray");
4848

@@ -53,14 +53,14 @@ public void setUp() throws Exception {
5353
runAndWaitUntil(mArray, new Runnable() {
5454
@Override
5555
public void run() {
56-
for (int i = 1; i <= 3; i++) {
57-
setValue(i, i);
56+
for (int i = 1; i <= INITIAL_SIZE; i++) {
57+
pushValue(i, i);
5858
}
5959
}
6060
}, new Callable<Boolean>() {
6161
@Override
6262
public Boolean call() throws Exception {
63-
return mArray.getCount() == 3;
63+
return mArray.getCount() == INITIAL_SIZE;
6464
}
6565
});
6666
}
@@ -76,17 +76,11 @@ public void tearDown() throws Exception {
7676
}
7777
}
7878

79-
@Test
80-
public void testSize() throws Exception {
81-
assertEquals(3, mArray.getCount());
82-
}
83-
8479
@Test
8580
public void testPushIncreasesSize() throws Exception {
86-
assertEquals(3, mArray.getCount());
8781
runAndWaitUntil(mArray, new Runnable() {
8882
public void run() {
89-
setValue(4, null);
83+
pushValue(4, null);
9084
}
9185
}, new Callable<Boolean>() {
9286
@Override
@@ -100,7 +94,7 @@ public Boolean call() throws Exception {
10094
public void testPushAppends() throws Exception {
10195
runAndWaitUntil(mArray, new Runnable() {
10296
public void run() {
103-
setValue(4, 4);
97+
pushValue(4, 4);
10498
}
10599
}, new Callable<Boolean>() {
106100
@Override
@@ -114,7 +108,7 @@ public Boolean call() throws Exception {
114108
public void testAddValueWithPriority() throws Exception {
115109
runAndWaitUntil(mArray, new Runnable() {
116110
public void run() {
117-
setValue(4, 0.5);
111+
pushValue(4, 0.5);
118112
}
119113
}, new Callable<Boolean>() {
120114
public Boolean call() throws Exception {
@@ -137,7 +131,7 @@ public Boolean call() throws Exception {
137131
});
138132
}
139133

140-
private void setValue(Object value, Object priority) {
134+
private void pushValue(Object value, Object priority) {
141135
String key = mKeyRef.push().getKey();
142136

143137
if (priority != null) {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
import android.content.Context;
44

5-
import com.firebase.ui.database.utils.Bean;
65
import com.google.firebase.FirebaseApp;
76
import com.google.firebase.FirebaseOptions;
87
import com.google.firebase.database.DatabaseError;
98

10-
import junit.framework.AssertionFailedError;
11-
129
import java.util.concurrent.Callable;
1310
import java.util.concurrent.Semaphore;
1411
import java.util.concurrent.TimeUnit;
1512

13+
import static org.junit.Assert.assertTrue;
14+
1615
public class TestUtils {
1716
private static final String APP_NAME = "firebaseui-tests";
1817
private static final int TIMEOUT = 10000;
@@ -64,9 +63,7 @@ public void onCancelled(DatabaseError databaseError) {
6463
// and we're not done
6564
}
6665
}
67-
if (!isDone) {
68-
throw new AssertionFailedError();
69-
}
66+
assertTrue("Timed out waiting for expected results on FirebaseArray", isDone);
7067
array.setOnChangedListener(null);
7168
}
7269

0 commit comments

Comments
 (0)