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 = "###package###";     //  飞凡商家
    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();

            /**
             * ======== 测试代码此处开始 ==========
             */
            ###code###

            /**
             * ======== 测试代码此处结束 ==========
             */
        }  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);
    }
}
