Skip to content

Commit

Permalink
Support Push Notification
Browse files Browse the repository at this point in the history
refs #67
  • Loading branch information
rickmak committed Nov 22, 2016
2 parents 429e11c + d46e329 commit 099fae9
Show file tree
Hide file tree
Showing 26 changed files with 787 additions and 118 deletions.
4 changes: 4 additions & 0 deletions skygear/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ android {
}

dependencies {
compile 'com.google.android.gms:play-services-base:9.8.0'
compile 'com.google.android.gms:play-services-gcm:9.8.0'

compile 'dev.dworks.libs:volleyplus:0.1.4'
compile 'joda-time:joda-time:2.9.4'
compile 'org.java-websocket:Java-WebSocket:1.3.0'

compile fileTree(dir: 'libs', include: ['*.jar'])

testCompile 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.security.InvalidParameterException;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class ConfigurationUnitTest {
Expand All @@ -17,17 +20,23 @@ public void testDefaultConfigurationNormalFlow() throws Exception {

assertEquals("http://skygear.dev/", defaultConfig.endpoint);
assertEquals("changeme", defaultConfig.apiKey);
assertNull(defaultConfig.gcmSenderId);
assertFalse(defaultConfig.pubsubHandlerExecutionInBackground);
}

@Test
public void testConfigurationBuilderNormalFlow() throws Exception {
Configuration config = new Configuration.Builder()
.endPoint("http://my-endpoint.skygeario.com/")
.apiKey("my-api-key")
.gcmSenderId("my-sender-id")
.pubsubHandlerExecutionInBackground(true)
.build();

assertEquals("http://my-endpoint.skygeario.com/", config.endpoint);
assertEquals("my-api-key", config.apiKey);
assertEquals("my-sender-id", config.gcmSenderId);
assertTrue(config.pubsubHandlerExecutionInBackground);
}

@Test(expected = InvalidParameterException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,104 @@ public void testPersistentStoreSaveNullDefaultAccessControl() throws Exception {
);
assertEquals("[]", pref.getString(PersistentStore.DEFAULT_ACCESS_CONTROL_KEY, "[]"));
}

@Test
public void testPersistentStoreRestoreDeviceId() throws Exception {
SharedPreferences pref = instrumentationContext.getSharedPreferences(
PersistentStore.SKYGEAR_PREF_SPACE,
Context.MODE_PRIVATE
);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PersistentStore.DEVICE_ID_KEY, "testing-device-id");
editor.commit();

PersistentStore persistentStore = new PersistentStore(instrumentationContext);
assertEquals("testing-device-id", persistentStore.deviceId);
}

@Test
public void testPersistentStoreRestoreDeviceIdFromEmptyState() throws Exception {
PersistentStore persistentStore = new PersistentStore(instrumentationContext);
assertNull(persistentStore.deviceId);
}

@Test
public void testPersistentStoreSaveDeviceId() throws Exception {
PersistentStore persistentStore = new PersistentStore(instrumentationContext);
persistentStore.deviceId = "testing-device-id";
persistentStore.save();

SharedPreferences pref = instrumentationContext.getSharedPreferences(
PersistentStore.SKYGEAR_PREF_SPACE,
Context.MODE_PRIVATE
);
assertEquals("testing-device-id", pref.getString(PersistentStore.DEVICE_ID_KEY, null));
}

@Test
public void testPersistentStoreSaveNullDeviceId() throws Exception {
SharedPreferences pref = instrumentationContext.getSharedPreferences(
PersistentStore.SKYGEAR_PREF_SPACE,
Context.MODE_PRIVATE
);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PersistentStore.DEVICE_ID_KEY, "testing-device-id");
editor.commit();

PersistentStore persistentStore = new PersistentStore(instrumentationContext);
persistentStore.deviceId = null;
persistentStore.save();

assertNull(pref.getString(PersistentStore.DEVICE_ID_KEY, null));
}

@Test
public void testPersistentStoreRestoreDeviceToken() throws Exception {
SharedPreferences pref = instrumentationContext.getSharedPreferences(
PersistentStore.SKYGEAR_PREF_SPACE,
Context.MODE_PRIVATE
);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PersistentStore.DEVICE_TOKEN_KEY, "testing-device-token");
editor.commit();

PersistentStore persistentStore = new PersistentStore(instrumentationContext);
assertEquals("testing-device-token", persistentStore.deviceToken);
}

@Test
public void testPersistentStoreRestoreDeviceTokenFromEmptyState() throws Exception {
PersistentStore persistentStore = new PersistentStore(instrumentationContext);
assertNull(persistentStore.deviceToken);
}

@Test
public void testPersistentStoreSaveDeviceToken() throws Exception {
PersistentStore persistentStore = new PersistentStore(instrumentationContext);
persistentStore.deviceToken = "testing-device-token";
persistentStore.save();

SharedPreferences pref = instrumentationContext.getSharedPreferences(
PersistentStore.SKYGEAR_PREF_SPACE,
Context.MODE_PRIVATE
);
assertEquals("testing-device-token", pref.getString(PersistentStore.DEVICE_TOKEN_KEY, null));
}

@Test
public void testPersistentStoreSaveNullDeviceToken() throws Exception {
SharedPreferences pref = instrumentationContext.getSharedPreferences(
PersistentStore.SKYGEAR_PREF_SPACE,
Context.MODE_PRIVATE
);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PersistentStore.DEVICE_TOKEN_KEY, "testing-device-token");
editor.commit();

PersistentStore persistentStore = new PersistentStore(instrumentationContext);
persistentStore.deviceToken = null;
persistentStore.save();

assertNull(pref.getString(PersistentStore.DEVICE_TOKEN_KEY, null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.skygear.skygear;

import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Map;

import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public class RegisterDeviceRequestUnitTest {
@Test
public void testRegisterDeviceRequestCreateFlow1() throws Exception {
RegisterDeviceRequest request = new RegisterDeviceRequest();

assertEquals("device:register", request.action);

Map<String, Object> data = request.data;
assertEquals("android", data.get("type"));
assertNull(data.get("id"));
assertNull(data.get("device_token"));
}

@Test
public void testRegisterDeviceRequestCreateFlow2() throws Exception {
RegisterDeviceRequest request = new RegisterDeviceRequest("testing-device", "testing-token");

assertEquals("device:register", request.action);

Map<String, Object> data = request.data;
assertEquals("android", data.get("type"));
assertEquals("testing-device", data.get("id"));
assertEquals("testing-token", data.get("device_token"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.skygear.skygear;

import android.support.test.runner.AndroidJUnit4;

import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public class RegisterDeviceResponseHandlerUnitTest {
@Test
public void testRegisterDeviceResponseHandlerSuccessFlow() throws Exception {
JSONObject result = new JSONObject();
result.put("id", "test-device-id");

final boolean[] checkpoints = new boolean[] { false };
RegisterDeviceResponseHandler handler = new RegisterDeviceResponseHandler() {
@Override
public void onRegisterSuccess(String deviceId) {
assertEquals("test-device-id", deviceId);
checkpoints[0] = true;
}

@Override
public void onRegisterError(Error error) {
fail("Should not get fail callback");
}
};

handler.onSuccess(result);
assertTrue(checkpoints[0]);
}

@Test
public void testRegisterDeviceResponseHandlerFailFlow() throws Exception {
final boolean[] checkpoints = new boolean[] { false };
RegisterDeviceResponseHandler handler = new RegisterDeviceResponseHandler() {
@Override
public void onRegisterSuccess(String deviceId) {
fail("Should not get success callback");
}

@Override
public void onRegisterError(Error error) {
assertEquals("Test error", error.getMessage());
checkpoints[0] = true;
}
};

handler.onFail(new Error("Test error"));
assertTrue(checkpoints[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* The Skygear Access Control Serializer.
*/
public class AccessControlSerializer {
private static final String TAG = "Skygear SDK";

/**
* Serializes a Skygear Access Control
*
Expand Down Expand Up @@ -119,7 +121,7 @@ static JSONObject serialize(Entry entry) {
return jsonObject;
}
} catch (JSONException e) {
Log.w("Skygear SDK", "Fail to serialize AccessControl Entry", e);
Log.w(TAG, "Fail to serialize AccessControl Entry", e);
}
}

Expand Down
54 changes: 46 additions & 8 deletions skygear/src/main/java/io/skygear/skygear/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ public final class Configuration {
*/
final String apiKey;

/**
* GCM Sender ID
*/
final String gcmSenderId;

/**
* Boolean indicating whether Pubsub Handler Execution is in Background.
*/
final boolean pubsubHandlerExecutionInBackground;

private Configuration(String endpoint, String apiKey, boolean pubsubHandlerExecutionInBackground) {
private Configuration(
String endpoint,
String apiKey,
String gcmSenderId,
boolean pubsubHandlerExecutionInBackground
) {
this.endpoint = endpoint;
this.apiKey = apiKey;
this.gcmSenderId = gcmSenderId;
this.pubsubHandlerExecutionInBackground = pubsubHandlerExecutionInBackground;
}

Expand All @@ -36,7 +47,7 @@ private Configuration(String endpoint, String apiKey, boolean pubsubHandlerExecu
* @return the endpoint
*/
public String getEndpoint() {
return new String(endpoint);
return endpoint;
}

/**
Expand All @@ -45,7 +56,16 @@ public String getEndpoint() {
* @return the api key
*/
public String getApiKey() {
return new String(apiKey);
return apiKey;
}

/**
* Gets GCM Sender ID.
*
* @return the sender id
*/
public String getGcmSenderId() {
return gcmSenderId;
}

/**
Expand All @@ -63,11 +83,10 @@ public boolean isPubsubHandlerExecutionInBackground() {
* @return a default configuration
*/
static Configuration defaultConfiguration() {
return new Configuration(
DEFAULT_BASE_URL,
DEFAULT_API_KEY,
false
);
return new Builder()
.endPoint(DEFAULT_BASE_URL)
.apiKey(DEFAULT_API_KEY)
.build();
}

/**
Expand All @@ -76,6 +95,7 @@ static Configuration defaultConfiguration() {
public static final class Builder {
private String endpoint;
private String apiKey;
private String gcmSenderId;
private boolean pubsubHandlerExecutionInBackground;

/**
Expand All @@ -100,6 +120,23 @@ public Builder apiKey(String apiKey) {
return this;
}

/**
* Sets the GCM Sender ID.
*
* @param senderId the sender id
* @return the builder
*/
public Builder gcmSenderId(String senderId) {
this.gcmSenderId = senderId;
return this;
}

/**
* Sets whether Pubsub Handlers Execution Should be in Background.
*
* @param isInBackground the boolean indicating whether the execution is in background
* @return the builder
*/
public Builder pubsubHandlerExecutionInBackground(boolean isInBackground) {
this.pubsubHandlerExecutionInBackground = isInBackground;
return this;
Expand All @@ -122,6 +159,7 @@ public Configuration build() {
return new Configuration(
this.endpoint,
this.apiKey,
this.gcmSenderId,
this.pubsubHandlerExecutionInBackground
);
}
Expand Down
Loading

0 comments on commit 099fae9

Please sign in to comment.