-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
skygear/src/androidTest/java/io/skygear/skygear/UnregisterDeviceRequestUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 UnregisterDeviceRequestUnitTest { | ||
@Test | ||
public void testUnregisterDeviceRequestCreateFlow() throws Exception { | ||
UnregisterDeviceRequest request = new UnregisterDeviceRequest("device_1"); | ||
|
||
assertEquals("device:unregister", request.action); | ||
|
||
Map<String, Object> data = request.data; | ||
assertEquals("device_1", data.get("id")); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
skygear/src/androidTest/java/io/skygear/skygear/UnregisterDeviceResponseHandlerUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 UnregisterDeviceResponseHandlerUnitTest { | ||
@Test | ||
public void testUnregisterDeviceResponseSuccessFlow() throws Exception { | ||
JSONObject result = new JSONObject(); | ||
result.put("id", "device_1"); | ||
|
||
final boolean[] checkpoints = new boolean[] { false }; | ||
UnregisterDeviceResponseHandler handler = new UnregisterDeviceResponseHandler() { | ||
@Override | ||
public void onUnregisterSuccess(String deviceId) { | ||
assertEquals("device_1", deviceId); | ||
checkpoints[0] = true; | ||
} | ||
|
||
@Override | ||
public void onUnregisterError(Error error) { | ||
fail("Should not get fail callback"); | ||
} | ||
}; | ||
|
||
handler.onSuccess(result); | ||
assertTrue(checkpoints[0]); | ||
} | ||
|
||
@Test | ||
public void testUnregisterDeviceResponseFailFlow() throws Exception { | ||
final boolean[] checkpoints = new boolean[] { false }; | ||
UnregisterDeviceResponseHandler handler = new UnregisterDeviceResponseHandler() { | ||
@Override | ||
public void onUnregisterSuccess(String deviceId) { | ||
fail("Should not get success callback"); | ||
} | ||
|
||
@Override | ||
public void onUnregisterError(Error error) { | ||
assertEquals("Test error", error.getMessage()); | ||
checkpoints[0] = true; | ||
} | ||
}; | ||
|
||
handler.onFail(new Error("Test error")); | ||
assertTrue(checkpoints[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
skygear/src/main/java/io/skygear/skygear/UnregisterDeviceRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.skygear.skygear; | ||
|
||
import java.util.HashMap; | ||
|
||
public class UnregisterDeviceRequest extends Request { | ||
public UnregisterDeviceRequest() { | ||
super("device:unregister"); | ||
|
||
this.data = new HashMap<>(); | ||
} | ||
|
||
public UnregisterDeviceRequest(String deviceId) { | ||
this(); | ||
|
||
if (deviceId != null) { | ||
this.data.put("id", deviceId); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
skygear/src/main/java/io/skygear/skygear/UnregisterDeviceResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.skygear.skygear; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
/** | ||
* The Skygear Unregister Device Response Handler. | ||
*/ | ||
public abstract class UnregisterDeviceResponseHandler implements ResponseHandler { | ||
|
||
/** | ||
* Unregister success callback. | ||
* | ||
* @param deviceId the device id | ||
*/ | ||
public abstract void onUnregisterSuccess(String deviceId); | ||
|
||
/** | ||
* Unregister error callback. | ||
* | ||
* @param error the error | ||
*/ | ||
public abstract void onUnregisterError(Error error); | ||
|
||
@Override | ||
public void onSuccess(JSONObject result) { | ||
try { | ||
String deviceId = result.getString("id"); | ||
this.onUnregisterSuccess(deviceId); | ||
} catch (JSONException e) { | ||
this.onUnregisterError(new Error("Malformed server response")); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFail(Error error) { | ||
this.onUnregisterError(error); | ||
} | ||
} |