Skip to content

Commit

Permalink
Initial commit, everything works
Browse files Browse the repository at this point in the history
  • Loading branch information
samtstern committed Jun 10, 2019
0 parents commit b5fc119
Show file tree
Hide file tree
Showing 67 changed files with 3,873 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "geofiretest-8d811"
}
}
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.gradle
*.iml
/local.properties
.idea/**
!.idea/codeStyleSettings.xml
.DS_Store
build
google-services.json
!/library/google-services.json

crashlytics-build.properties
auth/src/main/res/values/com_crashlytics_export_strings.xml

*debug.log
.attach*
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
40 changes: 40 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
applicationId "com.firebase.geofire.example"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

testOptions {
unitTests {
includeAndroidResources = true
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation project(':library')
implementation 'com.google.firebase:firebase-auth:17.0.0'

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:support-compat:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
195 changes: 195 additions & 0 deletions app/src/androidTest/java/com/firebase/geofire/GeoFireIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.firebase.geofire;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.firebase.geofire.example.MainActivity;
import com.firebase.geofire.testing.GeoFireTestingRule;
import com.firebase.geofire.testing.SimpleFuture;
import com.firebase.geofire.testing.TestCallback;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@RunWith(AndroidJUnit4.class)
public class GeoFireIT {

@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);

private final GeoFireTestingRule geoFireTestingRule = new GeoFireTestingRule();

@Before
public void before() throws Exception {
geoFireTestingRule.before(mActivityRule.getActivity());
}

@After
public void after() {
geoFireTestingRule.after();
}

@Test
public void geoFireSetsLocations() throws InterruptedException, TimeoutException {
GeoFire geoFire = geoFireTestingRule.newTestGeoFire();
geoFireTestingRule.setLocation(geoFire, "loc1", 0.1, 0.1);
geoFireTestingRule.setLocation(geoFire, "loc2", 50.1, 50.1);
geoFireTestingRule.setLocation(geoFire, "loc3", -89.1, -89.1, true);

final SimpleFuture<Object> future = new SimpleFuture<>();
geoFire.getDatabaseReference().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
future.put(dataSnapshot);
}

@Override
public void onCancelled(DatabaseError databaseError) {
future.put(databaseError);
}
});

Map<String, Object> expected = new HashMap<>();
expected.put("loc1", new HashMap<String, Object>() {{
put("l", Arrays.asList(0.1, 0.1));
put("g", "s000d60yd1");
}});
expected.put("loc2", new HashMap<String, Object>() {{
put("l", Arrays.asList(50.1, 50.1));
put("g", "v0gth03tws");
}});
expected.put("loc3", new HashMap<String, Object>() {{
put("l", Arrays.asList(-89.1, -89.1));
put("g", "400th7z6gs");
}});
Object result = future.get(geoFireTestingRule.timeout, TimeUnit.SECONDS);
Assert.assertEquals(expected, ((DataSnapshot)result).getValue());
}

@Test
public void getLocationReturnsCorrectLocation() throws InterruptedException, TimeoutException {
GeoFire geoFire = geoFireTestingRule.newTestGeoFire();

TestCallback testCallback1 = new TestCallback();
geoFire.getLocation("loc1", testCallback1);
Assert.assertEquals(TestCallback.noLocation("loc1"), testCallback1.getCallbackValue());

TestCallback testCallback2 = new TestCallback();
geoFireTestingRule.setLocation(geoFire, "loc1", 0, 0, true);
geoFire.getLocation("loc1", testCallback2);
Assert.assertEquals(TestCallback.location("loc1", 0, 0), testCallback2.getCallbackValue());

TestCallback testCallback3 = new TestCallback();
geoFireTestingRule.setLocation(geoFire, "loc2", 1, 1, true);
geoFire.getLocation("loc2", testCallback3);
Assert.assertEquals(TestCallback.location("loc2", 1, 1), testCallback3.getCallbackValue());

TestCallback testCallback4 = new TestCallback();
geoFireTestingRule.setLocation(geoFire, "loc1", 5, 5, true);
geoFire.getLocation("loc1", testCallback4);
Assert.assertEquals(TestCallback.location("loc1", 5, 5), testCallback4.getCallbackValue());

TestCallback testCallback5 = new TestCallback();
geoFireTestingRule.removeLocation(geoFire, "loc1");
geoFire.getLocation("loc1", testCallback5);
Assert.assertEquals(TestCallback.noLocation("loc1"), testCallback5.getCallbackValue());
}

@Test
public void getLocationOnWrongDataReturnsError() throws InterruptedException {
GeoFire geoFire = geoFireTestingRule.newTestGeoFire();
geoFireTestingRule.setValueAndWait(geoFire.getDatabaseRefForKey("loc1"), "NaN");

final Semaphore semaphore = new Semaphore(0);
geoFire.getLocation("loc1", new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
Assert.fail("This should not be a valid location!");
}

@Override
public void onCancelled(DatabaseError databaseError) {
semaphore.release();
}
});
semaphore.tryAcquire(geoFireTestingRule.timeout, TimeUnit.SECONDS);

geoFireTestingRule.setValueAndWait(geoFire.getDatabaseRefForKey("loc2"), new HashMap<String, Object>() {{
put("l", 10);
put("g", "abc");
}});

geoFire.getLocation("loc2", new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
Assert.fail("This should not be a valid location!");
}

@Override
public void onCancelled(DatabaseError databaseError) {
semaphore.release();
}
});
semaphore.tryAcquire(geoFireTestingRule.timeout, TimeUnit.SECONDS);
}

@Test
public void invalidCoordinatesThrowException() {
GeoFire geoFire = geoFireTestingRule.newTestGeoFire();
try {
geoFire.setLocation("test", new GeoLocation(-91, 90));
Assert.fail("Did not throw illegal argument exception!");
} catch (IllegalArgumentException expected) {
}

try {
geoFire.setLocation("test", new GeoLocation(0, -180.1));
Assert.fail("Did not throw illegal argument exception!");
} catch (IllegalArgumentException expected) {
}

try {
geoFire.setLocation("test", new GeoLocation(0, 181.1));
Assert.fail("Did not throw illegal argument exception!");
} catch (IllegalArgumentException expected) {
}
}

@Test
public void locationWorksWithLongs() throws InterruptedException, TimeoutException {
GeoFire geoFire = geoFireTestingRule.newTestGeoFire();
DatabaseReference databaseReference = geoFire.getDatabaseRefForKey("loc");

final Semaphore semaphore = new Semaphore(0);
databaseReference.setValue(new HashMap<String, Object>() {{
put("l", Arrays.asList(1L, 2L));
put("g", "7zzzzzzzzz"); // this is wrong but we don't care in this test
}}, "7zzzzzzzzz", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
semaphore.release();
}
});
semaphore.tryAcquire(geoFireTestingRule.timeout, TimeUnit.SECONDS);

TestCallback testCallback = new TestCallback();
geoFire.getLocation("loc", testCallback);
Assert.assertEquals(TestCallback.location("loc", 1, 2), testCallback.getCallbackValue());
}
}
Loading

0 comments on commit b5fc119

Please sign in to comment.