/*
 * Copyright 2018 New Vector Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.matrix.androidsdk.util;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.text.SpannableStringBuilder;

import com.google.gson.JsonObject;

import org.junit.Assert;
import org.junit.Test;
import org.matrix.androidsdk.interfaces.HtmlToolbox;
import org.matrix.androidsdk.rest.model.Event;
import org.matrix.androidsdk.rest.model.message.Message;

public class EventDisplayTest {

    @Test
    public void EventDisplay_message_Simple_text() {
        Context context = InstrumentationRegistry.getContext();

        Event event = new Event();
        event.type = Event.EVENT_TYPE_MESSAGE;

        event.content = new JsonObject();
        ((JsonObject) event.content).addProperty("body", "message");

        EventDisplay eventDisplay = new EventDisplay(context, event, null);

        CharSequence textualDisplay = eventDisplay.getTextualDisplay();

        Assert.assertTrue(textualDisplay instanceof String);
        Assert.assertEquals("message", eventDisplay.getTextualDisplay());
    }

    @Test
    public void EventDisplay_formattedMessage_Simple_text() {
        EventDisplay eventDisplay = createEventDisplayWithFormattedBody("message");

        CharSequence textualDisplay = eventDisplay.getTextualDisplay();

        Assert.assertTrue(textualDisplay instanceof SpannableStringBuilder);
        Assert.assertEquals("message", textualDisplay.toString());
    }

    @Test
    public void EventDisplay_formattedMessage_italic_text() {
        EventDisplay eventDisplay = createEventDisplayWithFormattedBody("<i>italic</i>");

        CharSequence textualDisplay = eventDisplay.getTextualDisplay();

        Assert.assertTrue(textualDisplay instanceof SpannableStringBuilder);
        Assert.assertEquals("italic", textualDisplay.toString());
    }

    @Test
    public void EventDisplay_formattedMessage_bold_text() {
        EventDisplay eventDisplay = createEventDisplayWithFormattedBody("<strong>bold</strong>");

        CharSequence textualDisplay = eventDisplay.getTextualDisplay();

        Assert.assertTrue(textualDisplay instanceof SpannableStringBuilder);
        Assert.assertEquals("bold", textualDisplay.toString());
    }

    /**
     * This test check list item. It also check the trimming which has been added at the end of
     * {@link EventDisplay#getFormattedMessage(Context, JsonObject, HtmlToolbox)}
     */
    @Test
    public void EventDisplay_formattedMessage_li_text() {
        EventDisplay eventDisplay = createEventDisplayWithFormattedBody("<ol><li>list</li></ol>");

        CharSequence textualDisplay = eventDisplay.getTextualDisplay();

        Assert.assertTrue(textualDisplay instanceof SpannableStringBuilder);

        Assert.assertEquals("list", textualDisplay.toString());
    }

    /* ==========================================================================================
     * Private
     * ========================================================================================== */

    private EventDisplay createEventDisplayWithFormattedBody(String formattedBody) {
        Context context = InstrumentationRegistry.getContext();

        Event event = new Event();
        event.type = Event.EVENT_TYPE_MESSAGE;

        event.content = new JsonObject();
        ((JsonObject) event.content).addProperty("format", Message.FORMAT_MATRIX_HTML);
        ((JsonObject) event.content).addProperty("formatted_body", formattedBody);

        EventDisplay eventDisplay = new EventDisplay(context, event, null);

        return eventDisplay;
    }

}

@@ -3386,24 +3386,1154 @@
 ());%0A    %7D%0A%0A
+    /**%0A     * Test blockquote. It also check the trimming which has been added at the end of%0A     * %7B@link EventDisplay#getFormattedMessage(Context, JsonObject, HtmlToolbox)%7D%0A     */%0A    @Test%0A    public void EventDisplay_formattedMessage_blockquote_text() %7B%0A        EventDisplay eventDisplay = createEventDisplayWithFormattedBody(%22%3Cblockquote%3Eblockquote%3C/blockquote%3E%22);%0A%0A        CharSequence textualDisplay = eventDisplay.getTextualDisplay();%0A%0A        Assert.assertTrue(textualDisplay instanceof SpannableStringBuilder);%0A%0A        Assert.assertEquals(%22blockquote%22, textualDisplay.toString());%0A    %7D%0A%0A    /**%0A     * Test blockquote with text%0A     */%0A    @Test%0A    public void EventDisplay_formattedMessage_blockquoteWithText_text() %7B%0A        EventDisplay eventDisplay = createEventDisplayWithFormattedBody(%22%3Cblockquote%3Eblockquote%3C/blockquote%3Emessage%22);%0A%0A        CharSequence textualDisplay = eventDisplay.getTextualDisplay();%0A%0A        Assert.assertTrue(textualDisplay instanceof SpannableStringBuilder);%0A%0A        Assert.assertEquals(%22blockquote%5Cn%5Cnmessage%22, textualDisplay.toString());%0A    %7D%0A%0A    // TODO Test other message type%0A%0A
     /* =====
@@ -4618,16 +4618,16 @@
 =======%0A
-
      * P
@@ -5194,35 +5194,14 @@
     
-EventDisplay eventDisplay =
+return
  new
@@ -5240,38 +5240,8 @@
 ll);
-%0A%0A        return eventDisplay;
 %0A   

Add test for blockquotes