package com.genymotion.binocle.test;


import android.content.Intent;
import android.os.SystemClock;
import android.support.v4.app.FragmentManager;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.TextView;

import com.genymotion.api.GenymotionManager;
import com.genymotion.binocle.DiskIOSampleFragment;
import com.genymotion.binocle.R;
import com.genymotion.binocle.SampleActivity;

import junit.framework.Assert;

public class TestDiskIO  extends ActivityInstrumentationTestCase2<SampleActivity> {

    private DiskIOSampleFragment fragmentDiskIO;

    public TestDiskIO() {
        super(SampleActivity.class);
    }

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

        Intent diskIOIntent;
        diskIOIntent = new Intent(getInstrumentation().getTargetContext(), SampleActivity.class);
        diskIOIntent.putExtra(SampleActivity.ARG_ITEM_ID, DiskIOSampleFragment.TAG);
        setActivityIntent(diskIOIntent);

        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        fragmentDiskIO = (DiskIOSampleFragment) fragmentManager.findFragmentByTag(DiskIOSampleFragment.TAG);
    }

    public void testDiskIOLowEnd() {
        diskIOTest(50);
    }

    public void testDiskIOMiddleRange() {
        diskIOTest(100);
    }

    public void testDiskIOHighEnd() {
        diskIOTest(200);
    }

    public void testDisabled() {
        if (!GenymotionManager.isGenymotionDevice()) {
            // Avoid test on non Genymotion devices.
            return;
        }

        GenymotionManager genymotion = GenymotionManager.getGenymotionManager(getActivity());
        genymotion.getDiskIO().setByteRate(0);
        float activityByteRate = getActivityByteRate();

        // XXX: this might fail on a slow test machine
        Assert.assertTrue(300 < activityByteRate);
        Assert.assertEquals(-1, genymotion.getDiskIO().getByteRate());
    }

    public void testTooFast() {
        if (!GenymotionManager.isGenymotionDevice()) {
            // Avoid test on non Genymotion devices.
            return;
        }

        GenymotionManager genymotion = GenymotionManager.getGenymotionManager(getActivity());
        genymotion.getDiskIO().setByteRate(1000 * 1024 * 1024);
        float activityByteRate = getActivityByteRate();

        // XXX: this might fail on a too fast or too slow test machine
        Assert.assertTrue(300 < activityByteRate && activityByteRate < 900);
        Assert.assertEquals(1000 * 1024 * 1024, genymotion.getDiskIO().getByteRate());
    }

    private void diskIOTest(int byteRateMb) {
        if (!GenymotionManager.isGenymotionDevice()) {
            // Avoid test on non Genymotion devices.
            return;
        }

        GenymotionManager genymotion = GenymotionManager.getGenymotionManager(getActivity());
        genymotion.getDiskIO().setByteRate(byteRateMb * 1024 * 1024);
        float activityByteRate = getActivityByteRate();

        Assert.assertTrue(.85 * activityByteRate < byteRateMb && byteRateMb < 1.15 * activityByteRate);
        Assert.assertEquals(byteRateMb * 1024 * 1024, genymotion.getDiskIO().getByteRate());
    }

    private float getActivityByteRate() {
        TextView tvResult = (TextView) fragmentDiskIO.getView().findViewById(R.id.result);
        final  Button bench = (Button) fragmentDiskIO.getView().findViewById(R.id.bench);


        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bench.performClick();
            }
        });

        String currentResult = waitForText(tvResult); // eg: 10 Mb/s
        float byteRate = Float.parseFloat(currentResult.split(" ")[0]);

        return byteRate;
    }


    private String waitForText(TextView tv) {
        int max = 30;

        while (max > 0) {
            CharSequence txt = tv.getText();
            if (txt.length() > 0) {
                return txt.toString();
            }
            SystemClock.sleep(1000);
            max --;
        }
        return "";
    }
}

@@ -1427,1190 +1427,8 @@
  %7D%0A%0A
-    public void testDisabled() %7B%0A        if (!GenymotionManager.isGenymotionDevice()) %7B%0A            // Avoid test on non Genymotion devices.%0A            return;%0A        %7D%0A%0A        GenymotionManager genymotion = GenymotionManager.getGenymotionManager(getActivity());%0A        genymotion.getDiskIO().setByteRate(0);%0A        float activityByteRate = getActivityByteRate();%0A%0A        // XXX: this might fail on a slow test machine%0A        Assert.assertTrue(300 %3C activityByteRate);%0A        Assert.assertEquals(-1, genymotion.getDiskIO().getByteRate());%0A    %7D%0A%0A    public void testTooFast() %7B%0A        if (!GenymotionManager.isGenymotionDevice()) %7B%0A            // Avoid test on non Genymotion devices.%0A            return;%0A        %7D%0A%0A        GenymotionManager genymotion = GenymotionManager.getGenymotionManager(getActivity());%0A        genymotion.getDiskIO().setByteRate(1000 * 1024 * 1024);%0A        float activityByteRate = getActivityByteRate();%0A%0A        // XXX: this might fail on a too fast or too slow test machine%0A        Assert.assertTrue(300 %3C activityByteRate && activityByteRate %3C 900);%0A        Assert.assertEquals(1000 * 1024 * 1024, genymotion.getDiskIO().getByteRate());%0A    %7D%0A%0A
     

remove complex test