|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.junit.Assert.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * Unit tests for EntryResultCallBack. |
| 9 | + */ |
| 10 | +public class TestEntryResultCallBack { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testOnRequestFinishCallsOnCompletionWithNullError() { |
| 14 | + class TestCallback extends EntryResultCallBack { |
| 15 | + boolean finished = false; |
| 16 | + ResponseType lastResponse; |
| 17 | + Error lastError; |
| 18 | + |
| 19 | + @Override |
| 20 | + public void onCompletion(ResponseType responseType, Error error) { |
| 21 | + finished = true; |
| 22 | + lastResponse = responseType; |
| 23 | + lastError = error; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + TestCallback callback = new TestCallback(); |
| 28 | + |
| 29 | + ResponseType responseType = ResponseType.NETWORK; // use SDK's ResponseType |
| 30 | + callback.onRequestFinish(responseType); |
| 31 | + |
| 32 | + assertTrue(callback.finished); |
| 33 | + assertEquals(responseType, callback.lastResponse); |
| 34 | + assertNull(callback.lastError); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testOnRequestFailCallsOnCompletionWithError() { |
| 39 | + class TestCallback extends EntryResultCallBack { |
| 40 | + boolean finished = false; |
| 41 | + ResponseType lastResponse; |
| 42 | + Error lastError; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onCompletion(ResponseType responseType, Error error) { |
| 46 | + finished = true; |
| 47 | + lastResponse = responseType; |
| 48 | + lastError = error; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + TestCallback callback = new TestCallback(); |
| 53 | + |
| 54 | + ResponseType responseType = ResponseType.NETWORK; |
| 55 | + Error error = new Error(); |
| 56 | + callback.onRequestFail(responseType, error); |
| 57 | + |
| 58 | + assertTrue(callback.finished); |
| 59 | + assertEquals(responseType, callback.lastResponse); |
| 60 | + assertEquals(error, callback.lastError); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testAlwaysCallable() { |
| 65 | + class TestCallback extends EntryResultCallBack { |
| 66 | + boolean alwaysCalled = false; |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onCompletion(ResponseType responseType, Error error) { |
| 70 | + // do nothing |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + void always() { |
| 75 | + alwaysCalled = true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + TestCallback callback = new TestCallback(); |
| 80 | + callback.always(); |
| 81 | + assertTrue(callback.alwaysCalled); |
| 82 | + } |
| 83 | +} |
0 commit comments