package org.wikipedia.test.perf;

import org.wikipedia.page.PageActivity;
import org.wikipedia.page.PageFragment;
import org.wikipedia.page.PageLoadCallbacks;
import org.wikipedia.page.PageLoadTests;
import org.wikipedia.testlib.TestLatch;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;

/**
 * Test performance of page loading. Update the NUM_RUNS for better statistical significance.
 */
@LargeTest
public class PageLoadPerformanceTests extends ActivityInstrumentationTestCase2<PageActivity> {
    private static final int NUM_RUNS = 1; //50;
    private final MeasurementController measurement = new MeasurementController();

    public PageLoadPerformanceTests() {
        super(PageActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Launch Activity.
        getActivity();
    }

    public void testLoadPages() throws Throwable {
        loadPageMultipleTimes("Test_page_for_app_testing/Section1");
        loadPageMultipleTimes("A_long_page");
        loadPageMultipleTimes("Barack_Obama"); // much longer than previous pages, has a lead image

        measurement.analyzeAll();
    }

    private void loadPageMultipleTimes(String title) throws Throwable {
        for (int i = 0; i < NUM_RUNS; i++) {
            loadPageUi(title);
        }
    }

    private void loadPageUi(final String title) throws Throwable {
        final TestLatch latch = new TestLatch();
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                assertThat(getFragment(), notNullValue());
                getFragment().setPageLoadCallbacks(newCallbacks(title, latch));

                measurement.start(title);
                PageLoadTests.loadPage(getFragment(), title);
            }
        });
        latch.await();
    }

    private PageLoadCallbacks newCallbacks(final String title, final TestLatch latch) {
        return new PageLoadCallbacks() {
            @Override
            public void onLoadComplete() {
                measurement.stop(title);
                latch.countDown();
            }
        };
    }

    private PageFragment getFragment() {
        return (PageFragment) getActivity().getTopFragment();
    }
}

@@ -27,16 +27,62 @@
 .perf;%0A%0A
+import org.junit.Rule;%0Aimport org.junit.Test;%0A
 import o
@@ -299,102 +299,221 @@
 oid.
-test.ActivityInstrumentationTestCase2;%0Aimport android.test.suitebuilder.annotation.LargeTest;%0A
+support.annotation.NonNull;%0Aimport android.support.test.rule.ActivityTestRule;%0Aimport android.test.suitebuilder.annotation.LargeTest;%0A%0Aimport static android.support.test.InstrumentationRegistry.getInstrumentation;
 %0Aimp
@@ -771,49 +771,115 @@
 sts 
-extends ActivityInstrumentationTestCase2%3C
+%7B%0A    @Rule @NonNull%0A    public final ActivityTestRule%3CPageActivity%3E activityRule = new ActivityTestRule%3C%3E(
 Page
@@ -890,11 +890,17 @@
 vity
-%3E %7B
+.class);%0A
 %0A   
@@ -1037,227 +1037,13 @@
     
-public PageLoadPerformanceTests() %7B%0A        super(PageActivity.class);%0A    %7D%0A%0A    @Override%0A    protected void setUp() throws Exception %7B%0A        super.setUp();%0A%0A        // Launch Activity.%0A        getActivity();%0A    %7D%0A
+@Test
 %0A   
@@ -1624,24 +1624,25 @@
 estLatch();%0A
+%0A
         getI
@@ -2453,11 +2453,102 @@
 );%0A    %7D
+%0A%0A    private PageActivity getActivity() %7B%0A        return activityRule.getActivity();%0A    %7D
 %0A%7D%0A

move PageLoadPerformanceTests to JUnit 4