Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 1916675

Browse files
authored
GeoData Listener Tests (#114)
1 parent 75445a8 commit 1916675

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

src/main/java/com/firebase/geofire/GeoQueryDataEventListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ public interface GeoQueryDataEventListener {
6767

6868
/**
6969
* Called if a dataSnapshot changed within the search area.
70-
* An onDataMoved() event would always be preceded by onDataChanged() but it would be possible to see onDataChanged() without an antecedent onDataMoved().
7170
*
72-
* This method can be called multiple times.
71+
* An onDataMoved() is always followed by onDataChanged() but it is be possible to see
72+
* onDataChanged() without an preceding onDataMoved().
73+
*
74+
* This method can be called multiple times for a single location change, due to the way
75+
* the Realtime Database handles floating point numbers.
76+
*
77+
* Note: this method is not related to ValueEventListener#onDataChange(DataSnapshot).
7378
*
7479
* @param dataSnapshot The associated dataSnapshot that moved within the search area
7580
* @param location The location for this dataSnapshot as a GeoLocation object
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.firebase.geofire;
2+
3+
import static java.util.Locale.US;
4+
5+
import com.google.firebase.database.DatabaseError;
6+
import com.google.firebase.database.DataSnapshot;
7+
8+
public class GeoQueryDataEventTestListener extends TestListener implements GeoQueryDataEventListener {
9+
10+
private boolean recordEntered;
11+
private boolean recordMoved;
12+
private boolean recordChanged;
13+
private boolean recordExited;
14+
15+
public static String dataEntered(String key, double latitude, double longitude) {
16+
return String.format(US, "DATA_ENTERED(%s,%f,%f)", key, latitude, longitude);
17+
}
18+
19+
public static String dataExited(String key) {
20+
return String.format("DATA_EXITED(%s)", key);
21+
}
22+
23+
public static String dataMoved(String key, double latitude, double longitude) {
24+
return String.format(US, "DATA_MOVED(%s,%f,%f)", key, latitude, longitude);
25+
}
26+
27+
public static String dataChanged(String key, double latitude, double longitude) {
28+
return String.format(US, "DATA_CHANGED(%s,%f,%f)", key, latitude, longitude);
29+
}
30+
31+
public GeoQueryDataEventTestListener(boolean recordEntered,
32+
boolean recordMoved,
33+
boolean recordChanged,
34+
boolean recordExited) {
35+
this.recordEntered = recordEntered;
36+
this.recordMoved = recordMoved;
37+
this.recordChanged = recordChanged;
38+
this.recordExited = recordExited;
39+
40+
}
41+
42+
@Override
43+
public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
44+
if (recordEntered) {
45+
this.addEvent(dataEntered(dataSnapshot.getKey(), location.latitude, location.longitude));
46+
}
47+
}
48+
49+
@Override
50+
public void onDataExited(DataSnapshot dataSnapshot) {
51+
if (recordExited) {
52+
this.addEvent(dataExited(dataSnapshot.getKey()));
53+
}
54+
}
55+
56+
@Override
57+
public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
58+
if (recordMoved) {
59+
this.addEvent(dataMoved(dataSnapshot.getKey(), location.latitude, location.longitude));
60+
}
61+
}
62+
63+
@Override
64+
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
65+
if (recordChanged) {
66+
this.addEvent(dataChanged(dataSnapshot.getKey(), location.latitude, location.longitude));
67+
}
68+
}
69+
70+
@Override
71+
public void onGeoQueryReady() {
72+
73+
}
74+
75+
@Override
76+
public void onGeoQueryError(DatabaseError error) {
77+
78+
}
79+
80+
}

src/test/java/com/firebase/geofire/GeoQueryTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.firebase.geofire;
22

33
import com.google.firebase.database.DatabaseError;
4+
import com.google.firebase.database.DatabaseReference;
45
import junit.framework.Assert;
56
import org.junit.Test;
67
import org.junit.runner.RunWith;
@@ -109,6 +110,43 @@ public void keyMoved() throws InterruptedException {
109110
testListener.expectEvents(events);
110111
}
111112

113+
@Test
114+
public void dataChanged() throws InterruptedException {
115+
GeoFire geoFire = newTestGeoFire();
116+
setLoc(geoFire, "0", 0, 0);
117+
setLoc(geoFire, "1", 37.0000, -122.0001);
118+
setLoc(geoFire, "2", 37.0001, -122.0001);
119+
setLoc(geoFire, "3", 37.1000, -122.0000);
120+
setLoc(geoFire, "4", 37.0002, -121.9998, true);
121+
122+
GeoQuery query = geoFire.queryAtLocation(new GeoLocation(37, -122), 0.5);
123+
124+
GeoQueryDataEventTestListener testListener = new GeoQueryDataEventTestListener(
125+
false, true, true, false);
126+
query.addGeoQueryDataEventListener(testListener);
127+
128+
waitForGeoFireReady(geoFire);
129+
130+
setLoc(geoFire, "0", 1, 1, true); // outside of query
131+
setLoc(geoFire, "1", 37.0001, -122.0001, true); // moved
132+
setLoc(geoFire, "2", 37.0001, -122.0001, true); // location stayed the same
133+
setLoc(geoFire, "4", 37.0002, -121.9999, true); // moved
134+
135+
DatabaseReference childRef = geoFire.getDatabaseRefForKey("2").child("some_child");
136+
setValueAndWait(childRef, "some_value"); // data changed
137+
138+
List<String> events = new LinkedList<>();
139+
events.add(GeoQueryDataEventTestListener.dataMoved("1", 37.0001, -122.0001));
140+
events.add(GeoQueryDataEventTestListener.dataChanged("1", 37.0001, -122.0001));
141+
142+
events.add(GeoQueryDataEventTestListener.dataMoved("4", 37.0002, -121.9999));
143+
events.add(GeoQueryDataEventTestListener.dataChanged("4", 37.0002, -121.9999));
144+
145+
events.add(GeoQueryDataEventTestListener.dataChanged("2", 37.0001, -122.0001));
146+
147+
testListener.expectEvents(events);
148+
}
149+
112150
@Test
113151
public void subQueryTriggersKeyMoved() throws InterruptedException {
114152
GeoFire geoFire = newTestGeoFire();

0 commit comments

Comments
 (0)