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 android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

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

/**
 * Test performance of page loading. Update the NUM_RUNS for better statistical significance.
 */
@LargeTest
public class PageLoadPerformanceTests extends ActivityInstrumentationTestCase2<PageActivity> {
    private static final int TASK_COMPLETION_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(30);
    private static final int NUM_RUNS = 1; //50;
    private PageActivity activity;
    private CountDownLatch completionLatch;
    private PageFragment fragment;
    private String title;
    private MeasurementController measurement = new MeasurementController();

    private PageLoadCallbacks callback = new PageLoadCallbacks() {
        @Override
        public void onLoadComplete() {
            measurement.stop(title);
            completionLatch.countDown();
        }
    };

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

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        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 {
        this.title = title;
        for (int i = 0; i < NUM_RUNS; i++) {
            loadPageUi();
        }
    }

    private void loadPageUi() throws Throwable {
        completionLatch = new CountDownLatch(1);
        getInstrumentation().runOnMainSync(new Runnable() {
            @Override
            public void run() {
                fragment = (PageFragment) activity.getTopFragment();
                fragment.setPageLoadCallbacks(callback);

                measurement.start(title);
                PageLoadTests.loadPage(fragment, title);
            }
        });
        assertTrue(completionLatch.await(TASK_COMPLETION_TIMEOUT, TimeUnit.MILLISECONDS));
    }
}

@@ -686,9 +686,9 @@
 lis(
-3
+6
 0);%0A
@@ -752,431 +752,77 @@
 ate 
-PageActivity activity;%0A    private CountDownLatch completionLatch;%0A    private PageFragment fragment;%0A    private String title;%0A    private MeasurementController measurement = new MeasurementController();%0A%0A    private PageLoadCallbacks callback = new PageLoadCallbacks() %7B%0A        @Override%0A        public void onLoadComplete() %7B%0A            measurement.stop(title);%0A            completionLatch.countDown();%0A        %7D%0A    %7D
+final MeasurementController measurement = new MeasurementController()
 ;%0A%0A 
@@ -985,24 +985,25 @@
 etUp();%0A
+%0A
         
 activity
@@ -998,18 +998,35 @@
     
-activity =
+// Launch Activity.%0A       
  get
@@ -1428,36 +1428,8 @@
 e %7B%0A
-        this.title = title;%0A
     
@@ -1492,16 +1492,21 @@
 dPageUi(
+title
 );%0A     
@@ -1545,16 +1545,34 @@
 dPageUi(
+final String title
 ) throws
@@ -1592,27 +1592,38 @@
         
-completionL
+final CountDownLatch l
 atch = n
@@ -1778,85 +1778,21 @@
     
-fragment = (PageFragment) activity.getTopFragment();%0A                fragment
+getFragment()
 .set
@@ -1813,16 +1813,34 @@
 cks(
-c
+newC
 allback
+s(title, latch)
 );%0A%0A
@@ -1924,16 +1924,21 @@
 age(
-f
+getF
 ragment
+()
 , ti
@@ -1992,19 +1992,9 @@
 rue(
-completionL
+l
 atch
@@ -2055,11 +2055,428 @@
 );%0A    %7D
+%0A%0A    private PageLoadCallbacks newCallbacks(final String title, final CountDownLatch latch) %7B%0A        return new PageLoadCallbacks() %7B%0A            @Override%0A            public void onLoadComplete() %7B%0A                measurement.stop(title);%0A                latch.countDown();%0A            %7D%0A        %7D;%0A    %7D%0A%0A    private PageFragment getFragment() %7B%0A        return (PageFragment) getActivity().getTopFragment();%0A    %7D
 %0A%7D%0A

Fix NPE in PageLoadPerformanceTests