aboutsummaryrefslogtreecommitdiff
path: root/libs/editor/example/src/test/java/org/wordpress/android/editor/JsCallbackReceiverTest.java
blob: 7584824f7f28e4a62a40b20a0d2f801b1a59dcc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.wordpress.android.editor;

import android.util.Log;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLog;
import org.wordpress.android.util.AppLog;

import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.robolectric.shadows.ShadowLog.LogItem;

@Config(sdk = 18)
@RunWith(RobolectricTestRunner.class)
public class JsCallbackReceiverTest {
    private final static String EDITOR_LOG_TAG = "WordPress-" + AppLog.T.EDITOR.toString();

    private JsCallbackReceiver mJsCallbackReceiver;

    @Before
    public void setUp() {
        EditorFragment editorFragment = mock(EditorFragment.class);
        mJsCallbackReceiver = new JsCallbackReceiver(editorFragment);
    }

    @Test
    public void testCallbacksRecognized() {
        mJsCallbackReceiver.executeCallback("callback-dom-loaded", "");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-new-field", "field-name");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-input", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-selection-changed", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-selection-style", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-focus-in", "");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-focus-out", "");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-image-replaced", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-image-tap", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-link-tap", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-log", "arguments");
        assertNotLogged("Unhandled callback");

        mJsCallbackReceiver.executeCallback("callback-response-string", "arguments");
        assertNotLogged("Unhandled callback");
    }

    @Test
    public void testUnknownCallbackShouldBeLogged() {
        mJsCallbackReceiver.executeCallback("callback-does-not-exist", "content");
        assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "Unhandled callback: callback-does-not-exist:content", null);
    }

    @Test
    public void testCallbackLog() {
        mJsCallbackReceiver.executeCallback("callback-log", "msg=test-message");
        assertLogged(Log.DEBUG, EDITOR_LOG_TAG, "callback-log: test-message", null);
    }

    private void assertLogged(int type, String tag, String msg, Throwable throwable) {
        LogItem lastLog = ShadowLog.getLogs().get(0);
        assertEquals(type, lastLog.type);
        assertEquals(msg, lastLog.msg);
        assertEquals(tag, lastLog.tag);
        assertEquals(throwable, lastLog.throwable);
    }

    private void assertNotLogged(String msg) {
        List<LogItem> logList = ShadowLog.getLogs();
        if (!logList.isEmpty()) {
            assertFalse(logList.get(0).msg.contains(msg));
            ShadowLog.reset();
        }
    }
}