/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * 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 com.android.example.text.styling.renderer;

import android.content.Context;
import android.graphics.Typeface;
import android.support.test.InstrumentationRegistry;
import android.support.v4.content.res.ResourcesCompat;
import android.text.SpannedString;
import android.text.style.LeadingMarginSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import com.android.example.text.styling.R;
import com.android.example.text.styling.parser.Parser;
import com.android.example.text.styling.renderer.spans.BulletPointSpan;
import com.android.example.text.styling.renderer.spans.CodeBlockSpan;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MarkdownBuilderTest {

    private MarkdownBuilder builder;

    public MarkdownBuilderTest() {
        Context context = InstrumentationRegistry.getTargetContext();
        int bulletPointColor = context.getColor(R.color.colorAccent);
        int codeBackgroundColor = context.getColor(R.color.code_background);
        Typeface codeBlockTypeface = ResourcesCompat.getFont(context, R.font.inconsolata);
        builder = new MarkdownBuilder(bulletPointColor, codeBackgroundColor, codeBlockTypeface,
                new Parser());
    }

    @Test
    public void builder() {
        SpannedString result = builder.markdownToSpans("Hello, world!");
        assertEquals("Hello, world!", result.toString());
    }

    @Test
    public void text() {
        SpannedString result = builder.markdownToSpans("Text");

        Object[] spans = result.getSpans(0, result.length(), Object.class);
        assertEquals(0, spans.length);
    }

    @Test
    public void textWithQuote() {
        SpannedString result = builder.markdownToSpans("Text\n> Quote");

        assertEquals("Text\nQuote", result.toString());
        Object[] spans = result.getSpans(0, result.length(), Object.class);
        assertEquals(3, spans.length);

        StyleSpan styleSpan = (StyleSpan) spans[0];
        assertEquals(Typeface.ITALIC, styleSpan.getStyle());
        assertEquals(5, result.getSpanStart(styleSpan));
        assertEquals(10, result.getSpanEnd(styleSpan));
        LeadingMarginSpan leadingMarginSpan = (LeadingMarginSpan) spans[1];
        assertEquals(5, result.getSpanStart(leadingMarginSpan));
        assertEquals(10, result.getSpanEnd(leadingMarginSpan));
        RelativeSizeSpan relativeSizeSpan = (RelativeSizeSpan) spans[2];
        assertEquals(5, result.getSpanStart(relativeSizeSpan));
        assertEquals(10, result.getSpanEnd(relativeSizeSpan));
    }

    @Test
    public void textWithBulletPoints() {
        SpannedString result = builder.markdownToSpans("Points\n* one\n+ two");

        assertEquals("Points\none\ntwo", result.toString());
        Object[] spans = result.getSpans(0, result.length(), Object.class);
        assertEquals(2, spans.length);

        BulletPointSpan bulletSpan = (BulletPointSpan) spans[0];
        assertEquals(7, result.getSpanStart(bulletSpan));
        assertEquals(11, result.getSpanEnd(bulletSpan));
        BulletPointSpan bulletSpan2 = (BulletPointSpan) spans[1];
        assertEquals(11, result.getSpanStart(bulletSpan2));
        assertEquals(14, result.getSpanEnd(bulletSpan2));
    }

    @Test
    public void textWithCode() {
        SpannedString result = builder.markdownToSpans("Text `code`");

        assertEquals("Text code", result.toString());
        Object[] spans = result.getSpans(0, result.length(), Object.class);
        assertEquals(1, spans.length);

        CodeBlockSpan codeSpan = (CodeBlockSpan) spans[0];
        assertEquals(5, result.getSpanStart(codeSpan));
        assertEquals(9, result.getSpanEnd(codeSpan));
    }
}
@@ -784,16 +784,65 @@
 gistry;%0A
+import android.support.v4.content.ContextCompat;%0A
 import a
@@ -1563,39 +1563,45 @@
 letPointColor = 
-c
+C
 ontext
+Compat
 .getColor(R.colo
@@ -1586,32 +1586,41 @@
 Compat.getColor(
+context, 
 R.color.colorAcc
@@ -1659,23 +1659,29 @@
 Color = 
-c
+C
 ontext
+Compat
 .getColo
@@ -1682,16 +1682,25 @@
 etColor(
+context, 
 R.color.

Use ContextCompat.getColor(context