package com.baidu.mtc.baidu_test_host;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.SystemClock;
import android.support.test.uiautomator.IAutomationSupport;
import android.support.test.uiautomator.UiAutomatorTestCase;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import com.baidu.mtc.mtcuiautomatorsdk.MTCUiAutomatorHelper;

import java.io.IOException;

/**
 * 万达商家测试脚本
 * Created by hui on 29/03/2017.
 */
public class WandabpTest extends UiAutomatorTestCase {
    private UiDevice device;
    private Instrumentation instrumentation;
    private IAutomationSupport mAutomationSupport;
    private MTCUiAutomatorHelper helper;
    // TODO 修改目标APP的package名
    private String targetPackage = "com.ihandy.xgx.browsersit";     //  飞凡商家
    int index = 0;

    private Boolean needLeona = false;

    /**
     *
     * @throws Throwable
     */
    public void testApp() throws Throwable {
        try {
            instrumentation = getInstrumentation();
            device = getUiDevice();
            int height = device.getDisplayHeight();
            int width = device.getDisplayWidth();
            mAutomationSupport = getAutomationSupport();
            Context context = getInstrumentation().getTargetContext();
            helper = new MTCUiAutomatorHelper(device, instrumentation, mAutomationSupport, targetPackage);
            final Intent intent = context.getPackageManager().getLaunchIntentForPackage(targetPackage);
            intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);    // Clear out any previous instances
            context.startActivity(intent);
            helper.sweep();
            helper.waitForPackage(targetPackage, 5000);
            SystemClock.sleep(10000);
            // Step-1, 打开应用完成，打log，截图
            index++;
            helper.logAndScreenShot(index, "#启动飞凡APP");
            helper.sweep();

            /**
             * ======== 测试代码此处开始 ==========
             */
            helper.find(new UiSelector().resourceId("android:id/buttonPanel").instance(0).childSelector(new UiSelector().className("android.widget.LinearLayout").instance(0))).click();index++;SystemClock.sleep(120000);helper.logAndScreenShot(index, "");
helper.find(new UiSelector().resourceId("com.ihandy.xgx.browsersit:id/name").instance(0)).setText("BEJD1081");
index++;
SystemClock.sleep(5000);
helper.logAndScreenShot(index, "");

helper.find(new UiSelector().resourceId("com.ihandy.xgx.browsersit:id/password").instance(0)).setText("Qweasd321");
index++;
SystemClock.sleep(5000);
helper.logAndScreenShot(index, "");

helper.swipe(0.53, 0.65, 0.53, 0.64, 5);
index++;
SystemClock.sleep(5000);
helper.logAndScreenShot(index, "");

Point[] points4 = {new Point(Math.round(0.26f*width),Math.round(0.36f*height)),new Point(Math.round(0.25f*width),Math.round(0.51f*height)),new Point(Math.round(0.25f*width),Math.round(0.65f*height)),new Point(Math.round(0.50f*width),Math.round(0.64f*height)),new Point(Math.round(0.74f*width),Math.round(0.63f*height))};
device.swipe(points4, 40);
SystemClock.sleep(5000);
Point[] points9 = {new Point(Math.round(0.26f*width),Math.round(0.37f*height)),new Point(Math.round(0.25f*width),Math.round(0.50f*height)),new Point(Math.round(0.25f*width),Math.round(0.64f*height)),new Point(Math.round(0.51f*width),Math.round(0.64f*height)),new Point(Math.round(0.77f*width),Math.round(0.64f*height))};device.swipe(points9, 40);SystemClock.sleep(10000);helper.find(new UiSelector().resourceId("com.ihandy.xgx.browsersit:id/dialog_sure").instance(0)).click();index++;SystemClock.sleep(10000);helper.logAndScreenShot(index, "");
helper.find(new UiSelector().resourceId("com.ihandy.xgx.browsersit:id/img_im_customer").instance(0)).click();
index++;
SystemClock.sleep(5000);
helper.logAndScreenShot(index, "");
helper.find(new UiSelector().resourceId("com.ihandy.xgx.browsersit:id/imgdetail2").instance(2)).click();index++;SystemClock.sleep(50000);helper.logAndScreenShot(index, "");
helper.find(new UiSelector().resourceId("input_test").instance(0)).setText("123456");
index++;
SystemClock.sleep(5000);
helper.logAndScreenShot(index, "");

helper.find(new UiSelector().resourceId("main").instance(0).childSelector(new UiSelector().className("android.widget.Image").instance(1))).click();
index++;
SystemClock.sleep(5000);
helper.logAndScreenShot(index, "");


            /**
             * ======== 测试代码此处结束 ==========
             */
        }  catch (UiObjectNotFoundException e) {
            // 测试出错了，记录错误，截图
            helper.errorAndScreenShot(e);
            throw e;
        }
    }

    /**
     * 按屏幕百分比进行滑动操作
     * @param device
     * @param startX
     * @param startY
     * @param endX
     * @param endY
     * @param step
     */
    private void swipe(UiDevice device, double startX, double startY, double endX, double endY, int step) {
        int width = device.getDisplayWidth();
        int height = device.getDisplayHeight();
        device.swipe((int)(width * startX), (int)(height * startY), (int)(width * endX), (int)(height * endY), step);
    }

    private void clickRatio(UiDevice device, double xr, double yr) {
        int width = device.getDisplayWidth();
        int height = device.getDisplayHeight();
        device.click((int)(width * xr), (int)(height * yr));
    }

    private void swipeLeft(UiObject obj) throws UiObjectNotFoundException {
        Rect rec = obj.getVisibleBounds();
        device.swipe(rec.centerX() + rec.width() / 4, rec.centerY(), rec.centerX() - rec.width() / 4, rec.centerY(), 8);
        SystemClock.sleep(1000);
    }

    private void swipeUp(UiObject obj) throws UiObjectNotFoundException {
        Rect rec = obj.getVisibleBounds();
        device.swipe(rec.centerX(), rec.bottom - rec.height() / 4, rec.centerX(), rec.top + rec.height() / 4, 8);
        SystemClock.sleep(1000);
    }

    private void swipeDown(UiObject obj) throws UiObjectNotFoundException {
        Rect rec = obj.getVisibleBounds();
        device.swipe(rec.centerX(), rec.top + rec.height() / 4, rec.centerX(), rec.bottom - rec.height() / 4, 8);
        SystemClock.sleep(1000);
    }
}
