package co.jasonwyatt.srml;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.text.Html;
import android.util.Log;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Locale;

import static org.junit.Assert.assertTrue;

/**
 * Created by jason on 11/11/16.
 *
 * Performance tests. They fail if {@link SRML#getString} is slower than {@link Html#fromHtml}.
 */
@RunWith(AndroidJUnit4.class)
public class PerformanceTest {
    private static final String TAG = PerformanceTest.class.getSimpleName();

    @Test
    public void testSRMLvsHTMLLegacy() {
        int iterations = 100000;
        Context context = InstrumentationRegistry.getContext();

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            SRML.getString(context, R.string.simple_srml_string);
        }
        long elapsedSRML = System.currentTimeMillis() - startTime;

        startTime = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            Html.fromHtml(context.getString(R.string.simple_html_string));
        }
        long elapsedHTML = System.currentTimeMillis() - startTime;

        double srmlTimePer = elapsedSRML / (double) iterations;
        double htmlTimePer = elapsedHTML / (double) iterations;
        Log.i(TAG, String.format(Locale.US, "SRML.getString averaged %.2fms", srmlTimePer));
        Log.i(TAG, String.format(Locale.US, "Html.fromHtml averaged %.2fms", htmlTimePer));
        assertTrue("SRML.getString should be as fast, or faster than, Html.fromHtml.", srmlTimePer <= htmlTimePer);
    }

    @Test
    public void testSRMLvsHTMLLegacyWithParam() {
        int iterations = 100000;
        Context context = InstrumentationRegistry.getContext();

        long startTime = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            SRML.getString(context, R.string.simple_srml_string_with_param, Integer.toString(i));
        }
        long elapsedSRML = System.currentTimeMillis() - startTime;

        startTime = System.currentTimeMillis();
        for (int i = 0; i < iterations; i++) {
            Html.fromHtml(context.getString(R.string.simple_html_string_with_param, Integer.toString(i)));
        }
        long elapsedHTML = System.currentTimeMillis() - startTime;

        double srmlTimePer = elapsedSRML / (double) iterations;
        double htmlTimePer = elapsedHTML / (double) iterations;
        Log.i(TAG, String.format(Locale.US, "SRML.getString averaged %.2fms", srmlTimePer));
        Log.i(TAG, String.format(Locale.US, "Html.fromHtml averaged %.2fms", htmlTimePer));
        assertTrue("SRML.getString should be as fast, or faster than, Html.fromHtml.", srmlTimePer <= htmlTimePer);
    }
}

@@ -54,16 +54,88 @@
 ontext;%0A
+import android.os.Build;%0Aimport android.support.annotation.RequiresApi;%0A
 import a
@@ -2470,32 +2470,1305 @@
 ) - startTime;%0A%0A
+        double srmlTimePer = elapsedSRML / (double) iterations;%0A        double htmlTimePer = elapsedHTML / (double) iterations;%0A        Log.i(TAG, String.format(Locale.US, %22SRML.getString averaged %25.2fms%22, srmlTimePer));%0A        Log.i(TAG, String.format(Locale.US, %22Html.fromHtml averaged %25.2fms%22, htmlTimePer));%0A        assertTrue(%22SRML.getString should be as fast, or faster than, Html.fromHtml.%22, srmlTimePer %3C= htmlTimePer);%0A    %7D%0A%0A    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)%0A    @Test%0A    public void testSRMLvsHTMLLegacyWithDirtyParam() %7B%0A        int iterations = 100000;%0A        Context context = InstrumentationRegistry.getContext();%0A%0A        long startTime = System.currentTimeMillis();%0A        for (int i = 0; i %3C iterations; i++) %7B%0A            SRML.getString(context, R.string.simple_srml_string_with_param, String.format(Locale.US, %22%7B%7Bb%7D%7D%25d%7B%7B/b%7D%7D%22, i));%0A        %7D%0A        long elapsedSRML = System.currentTimeMillis() - startTime;%0A%0A        startTime = System.currentTimeMillis();%0A        for (int i = 0; i %3C iterations; i++) %7B%0A            Html.fromHtml(context.getString(R.string.simple_html_string_with_param, Html.escapeHtml(String.format(Locale.US, %22%3Cb%3E%25d%3C/b%3E%22, i))));%0A        %7D%0A        long elapsedHTML = System.currentTimeMillis() - startTime;%0A%0A
         double s

Add a test case for dirty params. Use Html.escapeHtml for dirty params passed to getString, as you'd do.