|
| 1 | +package org.wordpress.android.editor; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.robolectric.RobolectricTestRunner; |
| 9 | +import org.robolectric.annotation.Config; |
| 10 | +import org.robolectric.shadows.ShadowLog; |
| 11 | +import org.wordpress.android.util.AppLog; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static junit.framework.Assert.assertEquals; |
| 16 | +import static junit.framework.Assert.assertFalse; |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.robolectric.shadows.ShadowLog.LogItem; |
| 19 | + |
| 20 | +@Config(emulateSdk = 18) |
| 21 | +@RunWith(RobolectricTestRunner.class) |
| 22 | +public class JsCallbackHandlerTest { |
| 23 | + private final static String EDITOR_LOG_TAG = "WordPress-" + AppLog.T.EDITOR.toString(); |
| 24 | + |
| 25 | + private JsCallbackReceiver mJsCallbackReceiver; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void setUp() { |
| 29 | + EditorFragment editorFragment = mock(EditorFragment.class); |
| 30 | + mJsCallbackReceiver = new JsCallbackReceiver(editorFragment); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCallbacksRecognized() { |
| 35 | + mJsCallbackReceiver.executeCallback("callback-dom-loaded", ""); |
| 36 | + assertNotLogged("Unhandled callback"); |
| 37 | + |
| 38 | + mJsCallbackReceiver.executeCallback("callback-new-field", "field-name"); |
| 39 | + assertNotLogged("Unhandled callback"); |
| 40 | + |
| 41 | + mJsCallbackReceiver.executeCallback("callback-input", "arguments"); |
| 42 | + assertNotLogged("Unhandled callback"); |
| 43 | + |
| 44 | + mJsCallbackReceiver.executeCallback("callback-selection-changed", "arguments"); |
| 45 | + assertNotLogged("Unhandled callback"); |
| 46 | + |
| 47 | + mJsCallbackReceiver.executeCallback("callback-selection-style", "arguments"); |
| 48 | + assertNotLogged("Unhandled callback"); |
| 49 | + |
| 50 | + mJsCallbackReceiver.executeCallback("callback-focus-in", ""); |
| 51 | + assertNotLogged("Unhandled callback"); |
| 52 | + |
| 53 | + mJsCallbackReceiver.executeCallback("callback-focus-out", ""); |
| 54 | + assertNotLogged("Unhandled callback"); |
| 55 | + |
| 56 | + mJsCallbackReceiver.executeCallback("callback-image-replaced", "arguments"); |
| 57 | + assertNotLogged("Unhandled callback"); |
| 58 | + |
| 59 | + mJsCallbackReceiver.executeCallback("callback-image-tap", "arguments"); |
| 60 | + assertNotLogged("Unhandled callback"); |
| 61 | + |
| 62 | + mJsCallbackReceiver.executeCallback("callback-link-tap", "arguments"); |
| 63 | + assertNotLogged("Unhandled callback"); |
| 64 | + |
| 65 | + mJsCallbackReceiver.executeCallback("callback-log", "arguments"); |
| 66 | + assertNotLogged("Unhandled callback"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testUnknownCallbackShouldBeLogged() { |
| 71 | + mJsCallbackReceiver.executeCallback("callback-does-not-exist", "content"); |
| 72 | + assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "Unhandled callback: callback-does-not-exist:content", null); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testCallbackLog() { |
| 77 | + mJsCallbackReceiver.executeCallback("callback-log", "msg=test-message"); |
| 78 | + assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "callback-log: test-message", null); |
| 79 | + } |
| 80 | + |
| 81 | + private void assertLogged(int type, String tag, String msg, Throwable throwable) { |
| 82 | + LogItem lastLog = ShadowLog.getLogs().get(0); |
| 83 | + assertEquals(type, lastLog.type); |
| 84 | + assertEquals(msg, lastLog.msg); |
| 85 | + assertEquals(tag, lastLog.tag); |
| 86 | + assertEquals(throwable, lastLog.throwable); |
| 87 | + } |
| 88 | + |
| 89 | + private void assertNotLogged(String msg) { |
| 90 | + List<LogItem> logList = ShadowLog.getLogs(); |
| 91 | + if (!logList.isEmpty()) { |
| 92 | + assertFalse(logList.get(0).msg.contains(msg)); |
| 93 | + ShadowLog.reset(); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments