aboutsummaryrefslogtreecommitdiff
path: root/libs/editor/WordPressEditor/src/androidTest/java/org.wordpress.android.editor/ZssEditorTest.java
blob: 9989cbd65ac2ad7f70656ef8a72e0fd9ecacbbef (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.wordpress.android.editor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Instrumentation;
import android.os.Build;
import android.test.ActivityInstrumentationTestCase2;
import android.webkit.JavascriptInterface;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * Tests for the <code>ZSSEditor</code> inside an <code>EditorWebViewAbstract</code>, with no UI.
 */
public class ZssEditorTest extends ActivityInstrumentationTestCase2<MockActivity> {
    private static final String JS_CALLBACK_HANDLER = "nativeCallbackHandler";

    private Instrumentation mInstrumentation;
    private EditorWebViewAbstract mWebView;

    private CountDownLatch mSetUpLatch;

    private TestMethod mTestMethod;
    private CountDownLatch mCallbackLatch;
    private CountDownLatch mDomLoadedCallbackLatch;
    private Set<String> mCallbackSet;

    private enum TestMethod {
        INIT
    }

    public ZssEditorTest() {
        super(MockActivity.class);
    }

    @SuppressLint("AddJavascriptInterface")
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mInstrumentation = getInstrumentation();
        Activity activity = getActivity();
        mSetUpLatch = new CountDownLatch(1);
        mDomLoadedCallbackLatch = new CountDownLatch(1);

        mSetUpLatch.countDown();

        String htmlEditor = Utils.getHtmlFromFile(activity, "android-editor.html");

        if (htmlEditor != null) {
            htmlEditor = htmlEditor.replace("%%TITLE%%", getActivity().getString(R.string.visual_editor));
            htmlEditor = htmlEditor.replace("%%ANDROID_API_LEVEL%%", String.valueOf(Build.VERSION.SDK_INT));
            htmlEditor = htmlEditor.replace("%%LOCALIZED_STRING_INIT%%",
                    "nativeState.localizedStringEdit = '" + getActivity().getString(R.string.edit) + "';\n" +
                    "nativeState.localizedStringUploading = '" + getActivity().getString(R.string.uploading) + "';\n" +
                    "nativeState.localizedStringUploadingGallery = '" +
                            getActivity().getString(R.string.uploading_gallery_placeholder) + "';\n");
        }

        final String finalHtmlEditor = htmlEditor;

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mWebView = new EditorWebView(mInstrumentation.getContext(), null);
                if (Build.VERSION.SDK_INT < 17) {
                    mWebView.setJsCallbackReceiver(new MockJsCallbackReceiver(new EditorFragmentForTests()));
                } else {
                    mWebView.addJavascriptInterface(new MockJsCallbackReceiver(new EditorFragmentForTests()),
                            JS_CALLBACK_HANDLER);
                }
                mWebView.loadDataWithBaseURL("file:///android_asset/", finalHtmlEditor, "text/html", "utf-8", "");
                mSetUpLatch.countDown();
            }
        });
    }

    public void testInitialization() throws InterruptedException {
        // Wait for setUp() to finish initializing the WebView
        mSetUpLatch.await();

        // Identify this method to the MockJsCallbackReceiver
        mTestMethod = TestMethod.INIT;

        // Expecting three startup callbacks from the ZSS editor
        mCallbackLatch = new CountDownLatch(3);
        mCallbackSet = new HashSet<>();
        boolean callbacksReceived = mCallbackLatch.await(5, TimeUnit.SECONDS);
        assertTrue(callbacksReceived);

        Set<String> expectedSet = new HashSet<>();
        expectedSet.add("callback-new-field:id=zss_field_title");
        expectedSet.add("callback-new-field:id=zss_field_content");
        expectedSet.add("callback-dom-loaded:");

        assertEquals(expectedSet, mCallbackSet);
    }

    private class MockJsCallbackReceiver extends JsCallbackReceiver {
        public MockJsCallbackReceiver(EditorFragmentAbstract editorFragmentAbstract) {
            super(editorFragmentAbstract);
        }

        @JavascriptInterface
        public void executeCallback(String callbackId, String params) {
            if (callbackId.equals("callback-dom-loaded")) {
                // Notify test methods that the dom has loaded
                mDomLoadedCallbackLatch.countDown();
            }

            // Handle callbacks and count down latches according to the currently running test
            switch(mTestMethod) {
                case INIT:
                    if (callbackId.equals("callback-dom-loaded")) {
                        mCallbackSet.add(callbackId + ":");
                    } else if (callbackId.equals("callback-new-field")) {
                        mCallbackSet.add(callbackId + ":" + params);
                    }
                    mCallbackLatch.countDown();
                    break;
                default:
                    throw(new RuntimeException("Unknown calling method"));
            }
        }
    }
}