package com.philisense.rdgo.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.philisense.rdgo.BuildConfig;
import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.LoginBean;
import com.philisense.rdgo.databinding.ActivityLoginBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.ui.MainActivity;
import com.philisense.rdgo.ui.user.LoginActivity;
import com.philisense.rdgo.viewModel.LoginViewModel;

import org.apache.tools.ant.Main;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowToast;

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


import io.reactivex.functions.Consumer;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.internal.RealmCore;
import io.realm.log.RealmLog;

import static junit.framework.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

/**
 * Created by shaop on 2017/12/17.
 */


@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@SuppressStaticInitializationFor("io.realm.internal.Util")
@PrepareForTest({Realm.class, RealmConfiguration.class, RealmQuery.class, RealmResults.class, RealmCore.class, RealmLog.class})
public class LoginActivityTest {

    private ActivityLoginBinding mBinding;
    private LoginActivity activity;
    private LoginViewModel viewModel;

    @Before
    public void setUp() {
        activity = Robolectric.setupActivity(LoginActivity.class);
        mBinding = DataBindingUtil.setContentView(activity, R.layout.activity_login);
        viewModel = new LoginViewModel(activity,
                RxTextView.textChanges(mBinding.loginUsernameEdit),
                RxTextView.textChanges(mBinding.loginPasswordEdit));
        // 登录验证
        viewModel.loginValidation.subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                mBinding.loginButton.setEnabled(aBoolean);
            }
        });
    }

    /**
     * 没有用户名
     */
    @Test
    public void loginWithOutUsername() {
        mBinding.loginPasswordEdit.setText("123456");

        assertEquals(mBinding.loginButton.isEnabled(),false);
    }

    /**
     * 没有用户名
     */
    @Test
    public void loginValidation() {
        mBinding.loginUsernameEdit.setText("2211");
        mBinding.loginPasswordEdit.setText("123456");

        assertEquals(mBinding.loginButton.isEnabled(),true);
    }

    /**
     * 没有密码
     */
    @Test
    public void loginWithOutPassword() {
        mBinding.loginUsernameEdit.setText("2211");

        assertEquals(mBinding.loginButton.isEnabled(),false);
    }

    /**
     * 显示错误密码错误
     */
    @Test
    public void showPasswordError() {
        OError error = new OError(1,"密码错误!");
        activity.showError(mBinding,error);

        assertEquals(mBinding.loginPasswordEdit.getError(),"密码错误!");
    }

    /**
     * 显示账号错误
     */
    @Test
    public void showUsernameError() {
        OError error = new OError(1,"账号错误!");
        activity.showError(mBinding,error);

        assertEquals(mBinding.loginUsernameEdit.getError(),"账号错误!");
    }

}
